Desktop modules: a wasm module can fill the desktop, with Icon Desktop and Live Starfield
CI / test (push) Failing after 30s
CI / test (push) Failing after 30s
A module that exports chandra:desktop/desktop-module (wit/desktop.wit) owns the desktop behind the windows instead of a box in the panel: same sandbox, draw ops and capabilities, drawn at the output's full size and re-rendered on resize. It gets pointer and scroll events only on the empty desktop (windows, panel, popups and menus always win; a drag stays with the module until release), keys only while the desktop has focus, and can draw over the wallpaper or replace it. Its tick() asks for animation frames only while something moves; the main loop sleeps until the next one (wake_timeout), and frames pause while a window covers the desktop, so an idle desktop module costs nothing. Settings > Desktop gets "On the desktop": Wallpaper only, Clock card (default) or any installed desktop module, saved in state.edn as :desktop, loaded at login and reloaded by chandra-ctl reload. Right-click the empty desktop for Module settings and System settings. Hidden settings (empty label) are persisted by the shell, which is how Icon Desktop keeps icon positions. Samples: Icon Desktop (click to launch, drag to arrange, keyboard navigation, count and size settings) and Live Starfield (pointer steers, click or Space warps, wheel sets speed). Served by all hosts: compositor/wasm (native + MoonBit wasmhost) and Rust host-wasm. Docs in docs/desktop-modules.md; tests in compositor/host/desktop_test.mbt. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DPL5hgf1SwPMHVfLUbJKsB
This commit is contained in:
79 files changed
+4591
-113
No files matched your search
@@ -0,0 +1,395 @@
|
||||
///| Starfield: the animated sample desktop module (components/starfield). Stars fly towards the
|
||||
/// viewer out of a vanishing point that follows the pointer; the wheel changes speed, a click (or
|
||||
/// Space) sends a warp pulse that streaks the stars for a moment. It replaces the wallpaper.
|
||||
///
|
||||
/// Pacing: `tick` asks for a frame every 33 ms (30 a second) while anything moves and returns 0
|
||||
/// once nothing does, so with Motion set to Still the desktop costs nothing between events (a
|
||||
/// pulse or a steer then animates until it settles, and stops again).
|
||||
///
|
||||
/// Pure: the component hands in the theme, the size and the events; everything is in `Field`.
|
||||
|
||||
///| Frame interval while animating, in ms.
|
||||
pub let frame_ms : UInt = 33
|
||||
|
||||
///| Speed choices and their depth units per second.
|
||||
pub let speed_choices : Array[String] = ["Slow", "Normal", "Fast"]
|
||||
|
||||
///|
|
||||
pub let density_choices : Array[String] = ["Sparse", "Normal", "Dense"]
|
||||
|
||||
///|
|
||||
pub let motion_choices : Array[String] = ["Moving", "Still"]
|
||||
|
||||
///| One item of the module's right-click settings (the `module-config` record).
|
||||
#derive(Eq, Debug)
|
||||
pub(all) struct ConfigEntry {
|
||||
key : String
|
||||
label : String
|
||||
choices : Array[String]
|
||||
value : String
|
||||
}
|
||||
|
||||
///| A star in normalised space: x, y in [-1, 1], depth z in (0, 1] (1: far).
|
||||
struct Star {
|
||||
mut x : Double
|
||||
mut y : Double
|
||||
mut z : Double
|
||||
/// Depth one frame ago, for the warp streaks.
|
||||
mut pz : Double
|
||||
}
|
||||
|
||||
///| The starfield's state.
|
||||
pub(all) struct Field {
|
||||
stars : Array[Star]
|
||||
mut seed : UInt
|
||||
mut speed : String
|
||||
mut density : String
|
||||
mut motion : String
|
||||
/// Wheel multiplier on the speed, 0.25 .. 4.
|
||||
mut boost : Double
|
||||
/// Warp pulse, 1 right after a click, decaying to 0.
|
||||
mut pulse : Double
|
||||
/// Vanishing point offset from the centre (pixels), and where the pointer pulls it.
|
||||
mut cx : Double
|
||||
mut cy : Double
|
||||
mut tx : Double
|
||||
mut ty : Double
|
||||
mut w : Int
|
||||
mut h : Int
|
||||
/// The clock at the last tick (ms since load), -1 before the first.
|
||||
mut last : Int
|
||||
/// Frames drawn, for tests.
|
||||
mut frames : Int
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Field::new() -> Field {
|
||||
let f : Field = {
|
||||
stars: [],
|
||||
seed: 0x2545F491U,
|
||||
speed: "Normal",
|
||||
density: "Normal",
|
||||
motion: "Moving",
|
||||
boost: 1.0,
|
||||
pulse: 0.0,
|
||||
cx: 0.0,
|
||||
cy: 0.0,
|
||||
tx: 0.0,
|
||||
ty: 0.0,
|
||||
w: 0,
|
||||
h: 0,
|
||||
last: -1,
|
||||
frames: 0,
|
||||
}
|
||||
f.populate()
|
||||
f
|
||||
}
|
||||
|
||||
///| Uniform in [0, 1).
|
||||
fn Field::rand(self : Field) -> Double {
|
||||
// xorshift32
|
||||
let mut s = self.seed
|
||||
s = s ^ (s << 13)
|
||||
s = s ^ (s >> 17)
|
||||
s = s ^ (s << 5)
|
||||
self.seed = s
|
||||
(s >> 8).to_double() / 16777216.0
|
||||
}
|
||||
|
||||
///|
|
||||
fn Field::star_count(self : Field) -> Int {
|
||||
match self.density {
|
||||
"Sparse" => 180
|
||||
"Dense" => 640
|
||||
_ => 360
|
||||
}
|
||||
}
|
||||
|
||||
///| (Re)create the stars for the density, spread through the whole depth.
|
||||
fn Field::populate(self : Field) -> Unit {
|
||||
self.stars.clear()
|
||||
for i = 0; i < self.star_count(); i = i + 1 {
|
||||
// Spread over what is on screen at that depth (|x| < z), as if they had flown in from afar.
|
||||
let z = 0.05 + 0.95 * self.rand()
|
||||
self.stars.push({ x: (self.rand() * 2.0 - 1.0) * z, y: (self.rand() * 2.0 - 1.0) * z, z, pz: z })
|
||||
}
|
||||
}
|
||||
|
||||
///| Depth units per second.
|
||||
fn Field::velocity(self : Field) -> Double {
|
||||
let base = match self.speed {
|
||||
"Slow" => 0.05
|
||||
"Fast" => 0.32
|
||||
_ => 0.13
|
||||
}
|
||||
let still = self.motion == "Still"
|
||||
(if still { 0.0 } else { base * self.boost }) + self.pulse * 1.6
|
||||
}
|
||||
|
||||
///| True while something on screen still moves (a frame is wanted).
|
||||
pub fn Field::animating(self : Field) -> Bool {
|
||||
self.velocity() > 0.0 ||
|
||||
self.pulse > 0.0 ||
|
||||
(self.tx - self.cx).abs() > 0.5 ||
|
||||
(self.ty - self.cy).abs() > 0.5
|
||||
}
|
||||
|
||||
///| The animation clock (`desktop-module.tick`): advance to `now` (ms since load) and return the
|
||||
/// ms until the next frame, 0 when nothing moves.
|
||||
pub fn Field::tick(self : Field, now : UInt) -> UInt {
|
||||
let now = now.reinterpret_as_int()
|
||||
let dt = if self.last < 0 {
|
||||
0.0
|
||||
} else {
|
||||
let d = now - self.last
|
||||
// A long gap (paused while covered, or idle) restarts smoothly instead of jumping.
|
||||
(if d < 0 { 0 } else if d > 100 { 33 } else { d }).to_double() / 1000.0
|
||||
}
|
||||
self.last = now
|
||||
self.step(dt)
|
||||
if self.animating() {
|
||||
frame_ms
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
///| Advance the simulation by `dt` seconds.
|
||||
pub fn Field::step(self : Field, dt : Double) -> Unit {
|
||||
let v = self.velocity()
|
||||
for s in self.stars {
|
||||
s.pz = s.z
|
||||
s.z = s.z - v * dt
|
||||
// Flown past the viewer, or out of the side of the view: start again far away.
|
||||
if s.z <= 0.03 || s.x.abs() > s.z * 1.2 || s.y.abs() > s.z * 1.2 {
|
||||
s.x = self.rand() * 2.0 - 1.0
|
||||
s.y = self.rand() * 2.0 - 1.0
|
||||
s.z = 1.0
|
||||
s.pz = 1.0
|
||||
}
|
||||
}
|
||||
// The pulse fades over about a second; the vanishing point eases towards the pointer.
|
||||
self.pulse = if self.pulse < 0.02 { 0.0 } else { self.pulse * (1.0 - (dt * 2.5).min(1.0)) }
|
||||
let k = (dt * 4.0).min(1.0)
|
||||
self.cx = self.cx + (self.tx - self.cx) * k
|
||||
self.cy = self.cy + (self.ty - self.cy) * k
|
||||
if (self.tx - self.cx).abs() <= 0.5 {
|
||||
self.cx = self.tx
|
||||
}
|
||||
if (self.ty - self.cy).abs() <= 0.5 {
|
||||
self.cy = self.ty
|
||||
}
|
||||
}
|
||||
|
||||
///| An input event (`plasmoid.on-event`). True when a redraw is wanted now (the tick that follows
|
||||
/// every event takes care of the animation).
|
||||
pub fn Field::handle(self : Field, ev : @types.InputEvent) -> Bool {
|
||||
match ev {
|
||||
PointerMove(p) => {
|
||||
// The pointer pulls the vanishing point a fifth of the way towards it.
|
||||
self.tx = (p.x - self.w / 2).to_double() * 0.2
|
||||
self.ty = (p.y - self.h / 2).to_double() * 0.2
|
||||
false
|
||||
}
|
||||
PointerLeave => {
|
||||
self.tx = 0.0
|
||||
self.ty = 0.0
|
||||
false
|
||||
}
|
||||
PointerDown(_) => {
|
||||
self.pulse = 1.0
|
||||
true
|
||||
}
|
||||
KeyDown(" ") => {
|
||||
self.pulse = 1.0
|
||||
true
|
||||
}
|
||||
KeyDown("+") | KeyDown("=") => {
|
||||
ignore(self.scroll(-10))
|
||||
true
|
||||
}
|
||||
KeyDown("-") => {
|
||||
ignore(self.scroll(10))
|
||||
true
|
||||
}
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
///| Wheel (`desktop-module.on-scroll`): up is faster, down slower. True: redraw.
|
||||
pub fn Field::scroll(self : Field, dy : Int) -> Bool {
|
||||
if dy == 0 {
|
||||
return false
|
||||
}
|
||||
let f = if dy < 0 { 1.25 } else { 0.8 }
|
||||
self.boost = (self.boost * f).max(0.25).min(4.0)
|
||||
true
|
||||
}
|
||||
|
||||
///|
|
||||
fn shade(c : @types.Color, num : Int, den : Int) -> @types.Color {
|
||||
@ui.color(
|
||||
c.r.to_int() * num / den,
|
||||
c.g.to_int() * num / den,
|
||||
c.b.to_int() * num / den,
|
||||
255,
|
||||
)
|
||||
}
|
||||
|
||||
///| Draw the whole desktop, `size` being the output.
|
||||
pub fn Field::render(self : Field, size : @types.Size, tokens : @theme.Tokens) -> Array[@types.DrawOp] {
|
||||
let w = size.w.reinterpret_as_int()
|
||||
let h = size.h.reinterpret_as_int()
|
||||
self.w = w
|
||||
self.h = h
|
||||
self.frames = self.frames + 1
|
||||
let ops : Array[@types.DrawOp] = []
|
||||
// Deep space: the theme's desktop colours, much darker, in bands from top to bottom.
|
||||
let top = shade(tokens.palette.desktop_top, 1, 4)
|
||||
let bottom = shade(tokens.palette.desktop_bottom, 1, 3)
|
||||
let bands = 16
|
||||
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), @ui.lerp_color(top, bottom, i, bands - 1)))
|
||||
}
|
||||
// Two faint nebulae in the accent colour, drifting a little with the vanishing point.
|
||||
let neb = tokens.palette.accent
|
||||
let big = if w > h { w } else { h }
|
||||
for k = 0; k < 6; k = k + 1 {
|
||||
let r = big * (12 - k) / 40
|
||||
ops.push(@ui.circle(w * 3 / 10 - self.cx.to_int() / 3, h * 2 / 5 - self.cy.to_int() / 3, r, @ui.with_alpha(neb, 3 + k)))
|
||||
ops.push(@ui.circle(w * 3 / 4 - self.cx.to_int() / 4, h * 3 / 4 - self.cy.to_int() / 4, r * 2 / 3, @ui.with_alpha(@ui.color(180, 90, 200, 255), 2 + k)))
|
||||
}
|
||||
let ox = w / 2 + self.cx.to_int()
|
||||
let oy = h / 2 + self.cy.to_int()
|
||||
let scale = (if w > h { w } else { h }).to_double() * 0.5
|
||||
let tint = tokens.palette.accent
|
||||
let streak = self.pulse > 0.15
|
||||
for s in self.stars {
|
||||
let sx = ox + (s.x / s.z * scale).to_int()
|
||||
let sy = oy + (s.y / s.z * scale).to_int()
|
||||
if sx < -4 || sy < -4 || sx > w + 4 || sy > h + 4 {
|
||||
continue
|
||||
}
|
||||
let near = 1.0 - s.z // 0 far .. 1 near
|
||||
let lum = 120 + (near * 135.0).to_int()
|
||||
// Near stars pick up a little of the accent colour.
|
||||
let c = @ui.lerp_color(@ui.color(lum, lum, lum, 255), tint, (near * near * 40.0).to_int(), 100)
|
||||
let r = 1 + (near * 3.4).to_int()
|
||||
if streak && s.pz > s.z {
|
||||
let px = ox + (s.x / s.pz * scale).to_int()
|
||||
let py = oy + (s.y / s.pz * scale).to_int()
|
||||
ops.push(@ui.line(px, py, sx, sy, c, width=r))
|
||||
} else if r <= 1 {
|
||||
ops.push(@ui.fill_rect(@ui.rect(sx, sy, 1, 1), c))
|
||||
} else {
|
||||
ops.push(@ui.fill_rect(@ui.rect(sx - r / 2, sy - r / 2, r, r), c, radius=r / 2))
|
||||
}
|
||||
}
|
||||
ops
|
||||
}
|
||||
|
||||
///| The right-click settings.
|
||||
pub fn Field::config_items(self : Field) -> Array[ConfigEntry] {
|
||||
[
|
||||
{ key: "speed", label: "Speed", choices: speed_choices, value: self.speed },
|
||||
{ key: "density", label: "Stars", choices: density_choices, value: self.density },
|
||||
{ key: "motion", label: "Motion", choices: motion_choices, value: self.motion },
|
||||
]
|
||||
}
|
||||
|
||||
///| Set one; true when it changed something on screen.
|
||||
pub fn Field::set_config(self : Field, key : String, value : String) -> Bool {
|
||||
match key {
|
||||
"speed" if speed_choices.contains(value) => {
|
||||
self.speed = value
|
||||
self.boost = 1.0
|
||||
true
|
||||
}
|
||||
"density" if density_choices.contains(value) => {
|
||||
if self.density != value {
|
||||
self.density = value
|
||||
self.populate()
|
||||
}
|
||||
true
|
||||
}
|
||||
"motion" if motion_choices.contains(value) => {
|
||||
self.motion = value
|
||||
true
|
||||
}
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
test "moving stars keep asking for frames; still ones stop once settled" {
|
||||
let f = Field::new()
|
||||
assert_eq(f.tick(0), frame_ms)
|
||||
assert_eq(f.tick(33), frame_ms)
|
||||
ignore(f.set_config("motion", "Still"))
|
||||
assert_eq(f.tick(66), 0)
|
||||
// A click warps: frames until the pulse has faded, then sleep again.
|
||||
ignore(f.handle(PointerDown({ x: 10, y: 10 })))
|
||||
assert_eq(f.tick(99), frame_ms)
|
||||
let mut t = 99
|
||||
let mut n = 0
|
||||
while f.tick(t.reinterpret_as_uint()) != 0 && n < 200 {
|
||||
t = t + 33
|
||||
n = n + 1
|
||||
}
|
||||
assert_true(n > 5 && n < 200)
|
||||
// Steering eases towards the pointer, then stops.
|
||||
f.w = 1000
|
||||
f.h = 800
|
||||
ignore(f.handle(PointerMove({ x: 900, y: 400 })))
|
||||
assert_eq(f.tick((t + 33).reinterpret_as_uint()), frame_ms)
|
||||
n = 0
|
||||
while f.tick((t + 66 + n * 33).reinterpret_as_uint()) != 0 && n < 200 {
|
||||
n = n + 1
|
||||
}
|
||||
assert_true(n < 200)
|
||||
assert_eq(f.cx, 80.0)
|
||||
}
|
||||
|
||||
///|
|
||||
test "render fills the output and draws the stars" {
|
||||
let f = Field::new()
|
||||
let tokens : @theme.Tokens = {
|
||||
name: "t",
|
||||
palette: {
|
||||
desktop_top: @ui.color(20, 30, 60, 255),
|
||||
desktop_bottom: @ui.color(10, 10, 30, 255),
|
||||
surface: @ui.color(0, 0, 0, 255),
|
||||
surface_hover: @ui.color(0, 0, 0, 255),
|
||||
surface_popup: @ui.color(0, 0, 0, 255),
|
||||
foreground: @ui.color(255, 255, 255, 255),
|
||||
foreground_muted: @ui.color(200, 200, 200, 255),
|
||||
accent: @ui.color(100, 150, 255, 255),
|
||||
accent_foreground: @ui.color(0, 0, 0, 255),
|
||||
border: @ui.color(50, 50, 50, 255),
|
||||
},
|
||||
font_size: 14,
|
||||
radius: 6,
|
||||
spacing: 6,
|
||||
panel_height: 40,
|
||||
}
|
||||
let ops = f.render(@ui.size(1280, 720), tokens)
|
||||
match ops[0] {
|
||||
FillRect(r) => assert_eq((r.rect.x, r.rect.y, r.rect.w), (0, 0, 1280))
|
||||
_ => fail("background first")
|
||||
}
|
||||
assert_true(ops.length() > 100)
|
||||
// Wheel speeds up and slows down within bounds.
|
||||
for _ in 0..<20 {
|
||||
ignore(f.scroll(-10))
|
||||
}
|
||||
assert_eq(f.boost, 4.0)
|
||||
ignore(f.set_config("density", "Dense"))
|
||||
assert_eq(f.stars.length(), 640)
|
||||
assert_false(f.set_config("density", "Huge"))
|
||||
}
|
||||
|
||||
///| The one instance the component drives (its plasmoid, desktop-module and module-config
|
||||
/// exports share it).
|
||||
pub let state : Field = Field::new()
|
||||
Reference in new issue
Block a user