Desktop modules: a wasm module can fill the desktop, with Icon Desktop and Live Starfield
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:
Claude committed 2026-11-26 00:30:32 +00:00
1 parent 30f5e88639
commit 0f71779819
79 files changed
+4591 -113

No files matched your search

+113
View File
@@ -0,0 +1,113 @@
# Desktop modules
A desktop module is a wasm module that fills the desktop: the whole output behind the windows,
instead of a box in the panel. Use it for interactive desktops (an icon launcher, a live
wallpaper, a game, a dashboard). It runs in the same sandbox as a panel module, draws with the
same draw ops and asks for the same capabilities; only the way the shell drives it differs.
Two samples ship with Chandra:
* **Icon Desktop** (`components/icon-desktop`, logic in `lib/icondesk`): a grid of app icons on
the wallpaper. Click to launch, drag to move (dropping on another icon swaps them), arrow keys
and Enter after clicking the desktop, typing a letter jumps to the next app starting with it.
Positions are kept across logins. Module settings: how many apps (8, 12, 24, all) and icon size.
* **Live Starfield** (`components/starfield`, logic in `lib/starfield`): an animated starfield
that replaces the wallpaper. The pointer steers it, a click or Space sends a warp pulse, the
wheel changes speed. Module settings: speed, density, and Moving or Still (Still draws one
frame and then costs nothing).
![Icon Desktop](screenshots/desktop-icons.png)
![Live Starfield](screenshots/desktop-starfield.png)
## Choosing what fills the desktop
Settings > Desktop starts with **On the desktop**: *Wallpaper only*, *Clock card* (the default,
as before) and every installed desktop module. The choice takes effect at once, is saved in
`~/.config/chandra/state.edn` as `:desktop` (`"wallpaper"`, `"clock-card"` or the module's id)
and is loaded again at login. A module that is missing or fails to load leaves the clock card,
and the saved choice is kept for when it comes back.
Right-click the empty desktop for **Module settings...** (the module's own options) and
**System settings...** (Settings on the Desktop tab). A desktop module can't be added to the
panel: Settings > Panel doesn't list it, and adding one by path is refused with a notice.
## Writing one
A desktop module is a plasmoid (`wit/plasmoid.wit`) that also exports `desktop-module`
(`wit/desktop.wit`):
```wit
interface desktop-module {
enum wallpaper-mode { over-wallpaper, replace-wallpaper }
backdrop: func() -> wallpaper-mode;
on-scroll: func(at: point, dx: s32, dy: s32) -> bool;
tick: func(now-ms: u32) -> u32;
}
```
The smallest world is `desktop-widget` in `wit/worlds.wit` (`include widget; export
desktop-module;`). Add imports and `module-config` as a panel module would; the samples use
`icon-desktop-widget` (app catalog, app launch, icons, module config) and `starfield-widget`.
Add a line to `scripts/components.txt` and `scripts/build-modules.sh` builds it into
`build/components/NAME.wasm`; `just install` copies it to `/usr/local/share/chandra/modules/`,
the same place as panel modules. The shell tells desktop modules apart by the export.
How the shell drives it:
* **Drawing.** `plasmoid.render` gets the output's full size, in desktop coordinates ((0, 0) is
the output's top-left corner), and is called again with the new size when the output is
resized. The wallpaper is drawn first unless `backdrop` returns `replace-wallpaper`; windows
and the panel are drawn on top. The ops are cached: the shell calls `render` again only when
the module asked for a redraw, the size changed or the theme changed. `plasmoid.info`'s size
fields and `plasmoid.popup` are not used.
* **Pointer.** `plasmoid.on-event` gets move, press, release and leave only where nothing else
is: no window, panel, popup, menu or switcher covers that point. Moving onto a window or the
panel sends a leave. A press on the desktop keeps the pointer (a drag stays with the module
until release, even over a window). Right-click never reaches the module: it opens the desktop
menu. `on-scroll` gets the wheel over the empty desktop (one notch is about 10 pixels).
* **Keys.** Only while the desktop has the keyboard: after a click on the empty desktop, until a
window is focused (the module then gets `focus-lost`). Shortcuts are handled first, and a
focused window always gets its own keys.
* **Animation.** `tick(now-ms)` is the clock (`now-ms` counts from when the module was loaded).
It is called once after loading, after every event, and again when the delay it last returned
has passed. Return the ms until the next frame (16 for about 60 fps, 33 for 30) or 0 to sleep
until the next event. Each timed tick is followed by a redraw. The compositor's main loop
sleeps until the next frame is due, so a module that returns 0 costs no CPU while idle, and
frames are paused while a window covers the whole desktop. Requests are capped at 10 s.
* **Saved state.** `module-config` items with an empty label are hidden settings: the shell
doesn't show them, but saves them in state.edn (`:module-config`, under the module's id) when
their value changes after a release or a key, and hands them back with `set-config` at the
next load. Icon Desktop keeps its icon positions this way (`places`, `id=col,row;...`).
Logic is easiest to test outside the component: the samples keep it in `lib/` and test it with
`moon test --target wasm -p chandra/desktop/lib/starfield` (or `lib/icondesk`).
## Reloading
`chandra-ctl reload` (or `just reload`, Applications > Reload, SIGUSR1) reloads the desktop
module along with the panel modules; `chandra-ctl reload desktop` or `chandra-ctl reload
starfield` reloads only it. Settings and hidden state are handed back as at login. A broken file
is reported and the running instance stays (see [reloading.md](reloading.md)).
## Hosts
All three module hosts serve desktop modules: the native compositor and the MoonBit
`build/wasmhost` (both through `compositor/wasm/wasm_stub.c`) and the Rust `host-wasm`
(`src/plasmoid.rs`, bound with the `desktop-export` world). The compositor-as-wasm reaches them
through `desktop-backdrop`, `desktop-scroll`, `desktop-tick` and `publish-desktop` in
`wit/host/plasmoids.wit`. The settings module reads the list through the `wallpaper`
capability's `desktops`, `current-desktop` and `set-desktop` (`wit/capabilities.wit`).
## Tests
`compositor/host/desktop_test.mbt`: choosing and listing, input routing (windows and the panel
win, drags stay with the module), the right-click menu, resize, idle and animation pacing,
reload, and screenshots (`_build/desktop_*.png`). Run with:
```
XDG_CONFIG_HOME=$(mktemp -d) CHANDRA_TEST_CONFIG=1 \
moon test --target native --warn-list -a -p chandra/desktop/compositor/host --filter "*desktop*"
```
Also checked headless on `build/wasmhost` and `host-wasm` (`--gfx-out`) with `:desktop` set in
state.edn.