Files
chandra/lib/shell/shell.mbt
VandClaude Opus 5.5 af774d76fe
CI / test (push) Failing after 16s
Rename the project to Chandra
moon-plasma / moonplasma / MoonPlasma / Moon Plasma become chandra / Chandra
everywhere: the WIT package (chandra:desktop, chandra:host, chandra:wm), the
MoonBit module (chandra/desktop) and generated bindings, the Rust host,
identifiers (ChandraCompositor), the session script (chandra-session) and
desktop entry (chandra.desktop), config and data paths
($XDG_CONFIG_HOME/chandra, share/chandra/modules), env vars (CHANDRA_*),
UI strings, docs and tests. The test fixture components are rebuilt against
the renamed WIT.

Saved panel.edn and state.edn are still read from the old
$XDG_CONFIG_HOME/moon-plasma until the first save writes the new copy.

The remote repository keeps its experiments/moon-plasma name for now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hi3L9oVBoRd373Qr4n
2026-11-25 17:12:57 +00:00

259 lines
8.6 KiB
MoonBit

///| Desktop background and panel chrome (pure render logic).
///| The wallpapers the shell can draw, in choice order (what `wallpaper.wallpapers` lists).
pub let wallpaper_names : Array[String] = ["Plasma glow", "Aurora", "Starfield", "Dusk", "Solid"]
///| The default desktop background: wallpaper 0, "Plasma glow".
pub fn render_background(
screen : @types.Size,
t : @theme.Tokens,
) -> Array[@types.DrawOp] {
render_wallpaper(screen, t, 0)
}
///| Draw wallpaper `index` (see `wallpaper_names`) to fill `screen`; an unknown index draws wallpaper 0.
/// Everything is draw ops, coloured from the theme (Dusk keeps its own warm colours), so a
/// theme swap recolours the wallpaper too.
pub fn render_wallpaper(
screen : @types.Size,
t : @theme.Tokens,
index : Int,
) -> Array[@types.DrawOp] {
match index {
1 => aurora(screen, t)
2 => starfield(screen, t)
3 => dusk(screen)
4 => [@ui.fill_rect(@ui.rect(0, 0, screen.w.reinterpret_as_int(), screen.h.reinterpret_as_int()), t.palette.desktop_bottom)]
_ => plasma_glow(screen, t)
}
}
///| `bands` horizontal strips blending `top` into `bottom`.
fn gradient(w : Int, h : Int, top : @types.Color, bottom : @types.Color, bands : Int, ops : Array[@types.DrawOp]) -> Unit {
for i = 0; i < bands; i = i + 1 {
let y0 = h * i / bands
let y1 = h * (i + 1) / bands
ops.push(@ui.fill_rect(@ui.rect(0, y0, w, y1 - y0 + 1), @ui.lerp_color(top, bottom, i, bands - 1)))
}
}
///|
fn plasma_glow(screen : @types.Size, t : @theme.Tokens) -> Array[@types.DrawOp] {
let w = screen.w.reinterpret_as_int()
let h = screen.h.reinterpret_as_int()
let p = t.palette
let ops = []
gradient(w, h, p.desktop_top, p.desktop_bottom, 48, ops)
// Plasma glow: concentric translucent discs.
let gx = w * 3 / 4
let gy = h * 3 / 10
let rings = 8
for i = 0; i < rings; i = i + 1 {
let r = 40 + (rings - i) * 22
ops.push(@ui.circle(gx, gy, r, @ui.with_alpha(p.accent, 14 + i * 4)))
}
ops.push(@ui.circle(gx, gy, 38, @ui.with_alpha(p.accent, 200)))
ops.push(@ui.circle(gx + 14, gy - 10, 34, p.desktop_top))
ops.push(
@ui.text(w / 2, h / 2 - 20, "chandra", 54, @ui.with_alpha(p.foreground, 235), align=CENTER, bold=true),
)
ops.push(
@ui.text(
w / 2,
h / 2 + 24,
"every part is a WebAssembly component",
t.font_size.reinterpret_as_int() + 2,
p.foreground_muted,
align=CENTER,
),
)
ops
}
///| sin(2*pi*num/den) * 1000, by Bhaskara I's approximation (good to about 0.2%).
fn sin_milli(num : Int, den : Int) -> Int {
let d = if den > 0 { den } else { 1 }
let mut t = num % d
if t < 0 {
t = t + d
}
// Angle in degrees, 0..359, then fold the second half onto the first with a sign flip.
let deg = t * 360 / d
let (x, sign) = if deg < 180 { (deg, 1) } else { (deg - 180, -1) }
sign * 4000 * x * (180 - x) / (40500 - x * (180 - x))
}
///| Curtains of light across the upper sky, like northern lights: each ribbon is a wave of thin
/// vertical strokes whose colour fades out above and below the crest.
fn aurora(screen : @types.Size, t : @theme.Tokens) -> Array[@types.DrawOp] {
let w = screen.w.reinterpret_as_int()
let h = screen.h.reinterpret_as_int()
let p = t.palette
let ops = []
gradient(w, h, p.desktop_top, p.desktop_bottom, 48, ops)
let col = 4
let layers = 5
let ribbons = 3
for k = 0; k < ribbons; k = k + 1 {
let base = h * (22 + k * 11) / 100
let amp = h * (5 + k * 2) / 100
let period = w * (3 + k) / 4 + 1
let thick = h * (14 - k * 3) / 100
let tint = @ui.lerp_color(p.accent, p.foreground, k, ribbons)
let mut x = 0
while x < w {
let crest = base + sin_milli(x + k * period / 3, period) * amp / 1000
// The curtain hangs from the crest; its height breathes along the ribbon.
let len = thick * (1000 + sin_milli(x * 2 + k * 97, period) / 2) / 1000
for j = 0; j < layers; j = j + 1 {
let y0 = crest + len * j / layers
let y1 = crest + len * (j + 1) / layers
let alpha = (56 - k * 12) * (layers - j) / layers
ops.push(@ui.fill_rect(@ui.rect(x, y0, col, y1 - y0), @ui.with_alpha(tint, alpha)))
}
x = x + col
}
}
ops
}
///| A small deterministic pseudo-random generator (the same stars every frame).
fn next_rand(seed : Int) -> Int {
(seed * 1103515245 + 12345) & 0x7fffffff
}
///| A night sky: scattered stars and a crescent moon.
fn starfield(screen : @types.Size, t : @theme.Tokens) -> Array[@types.DrawOp] {
let w = screen.w.reinterpret_as_int()
let h = screen.h.reinterpret_as_int()
let p = t.palette
let ops = []
let night = @ui.lerp_color(p.desktop_top, @ui.color(0, 0, 0, 255), 1, 2)
gradient(w, h, night, p.desktop_top, 32, ops)
let n = (w / 16) * (h / 16) / 12 + 20
let mut seed = 20261125
for i = 0; i < n; i = i + 1 {
seed = next_rand(seed)
let x = seed % (if w > 0 { w } else { 1 })
seed = next_rand(seed)
let y = seed % (if h > 0 { h } else { 1 })
seed = next_rand(seed)
let bright = 90 + seed % 166
let r = if i % 17 == 0 { 2 } else { 1 }
ops.push(@ui.circle(x, y, r, @ui.with_alpha(p.foreground, bright)))
}
// Crescent: a bright disc with a sky-coloured disc over it.
let mx = w / 5
let my = h / 4
ops.push(@ui.circle(mx, my, 64, @ui.with_alpha(p.foreground, 18)))
ops.push(@ui.circle(mx, my, 40, @ui.with_alpha(p.foreground, 230)))
ops.push(@ui.circle(mx + 16, my - 10, 36, @ui.lerp_color(night, p.desktop_top, my, h)))
ops
}
///| A warm sunset: violet sky over orange, a low sun and hills, independent of the theme.
fn dusk(screen : @types.Size) -> Array[@types.DrawOp] {
let w = screen.w.reinterpret_as_int()
let h = screen.h.reinterpret_as_int()
let ops = []
let sky = @ui.color(46, 32, 84, 255)
let glow = @ui.color(240, 128, 84, 255)
gradient(w, h * 7 / 10, sky, glow, 40, ops)
// The sun, half set, with a halo.
let sx = w * 2 / 3
let sy = h * 7 / 10
for i = 0; i < 5; i = i + 1 {
ops.push(@ui.circle(sx, sy, 70 + (5 - i) * 24, @ui.color(255, 196, 120, 12 + i * 5)))
}
ops.push(@ui.circle(sx, sy, 64, @ui.color(255, 214, 140, 255)))
// Ground, then two hills on top of it.
ops.push(@ui.fill_rect(@ui.rect(0, h * 7 / 10, w, h - h * 7 / 10), @ui.color(44, 24, 52, 255)))
ops.push(@ui.circle(w / 4, h * 7 / 10 + h / 3, h / 2, @ui.color(60, 32, 68, 255)))
ops.push(@ui.circle(w * 9 / 10, h * 7 / 10 + h / 2, h * 3 / 5, @ui.color(36, 20, 44, 255)))
ops
}
///|
pub fn render_panel(
panel : @types.Rect,
t : @theme.Tokens,
) -> Array[@types.DrawOp] {
let p = t.palette
[
@ui.fill_rect(panel, p.surface),
@ui.line(panel.x, panel.y, panel.x + @ui.rect_w(panel), panel.y, p.border),
]
}
///|
fn test_tokens() -> @theme.Tokens {
let c = @ui.color(10, 20, 30, 255)
let d = @ui.color(110, 120, 130, 255)
{
name: "t",
palette: {
desktop_top: c,
desktop_bottom: d,
surface: c,
surface_hover: c,
surface_popup: c,
foreground: c,
foreground_muted: c,
accent: c,
accent_foreground: c,
border: c,
},
font_size: 14,
radius: 6,
spacing: 6,
panel_height: 44,
}
}
///|
test "background gradient covers the screen top to bottom" {
let ops = render_background(@ui.size(800, 600), test_tokens())
match ops[0] {
FillRect(f) => {
assert_eq(f.rect.y, 0)
assert_eq(f.color.r.to_int(), 10)
}
_ => fail("expected fill")
}
match ops[47] {
FillRect(f) => {
assert_eq(f.color.r.to_int(), 110)
assert_true(f.rect.y + f.rect.h.reinterpret_as_int() >= 600)
}
_ => fail("expected fill")
}
}
///|
test "every wallpaper fills the screen and an unknown index falls back to the first" {
let t = test_tokens()
let screen = @ui.size(640, 400)
for i = 0; i < wallpaper_names.length(); i = i + 1 {
let ops = render_wallpaper(screen, t, i)
assert_true(ops.length() > 0)
// The first op paints from the top-left corner across the full width.
match ops[0] {
FillRect(f) => {
assert_eq(f.rect.x, 0)
assert_eq(f.rect.y, 0)
assert_eq(f.rect.w.reinterpret_as_int(), 640)
}
_ => fail("wallpaper \{i} should start with a fill")
}
}
// (DrawOp has no Eq: compare op counts.)
let glow = render_wallpaper(screen, t, 0).length()
assert_eq(render_wallpaper(screen, t, 99).length(), glow)
assert_eq(render_wallpaper(screen, t, -1).length(), glow)
assert_eq(render_background(screen, t).length(), glow)
// Different choices draw different things.
assert_true(render_wallpaper(screen, t, 1).length() != glow)
assert_true(render_wallpaper(screen, t, 2).length() != glow)
assert_eq(render_wallpaper(screen, t, 4).length(), 1)
}