Files
chandra/lib/icondesk/icondesk_test.mbt
T
Claude 0f71779819
CI / test (push) Failing after 30s
Desktop modules: a wasm module can fill the desktop, with Icon Desktop and Live Starfield
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
2026-11-26 00:30:32 +00:00

88 lines
2.8 KiB
MoonBit

///|
fn apps(n : Int) -> Array[@types.AppEntry] {
Array::makei(n, i => {
id: "app" + i.to_string(),
name: ["Files", "Firefox", "Konsole", "Kate", "Gimp", "Foot"][i % 6] + i.to_string(),
comment: "",
category: "",
glyph: "A",
})
}
///|
fn desk() -> @icondesk.Desk {
let d = @icondesk.Desk::new()
// 1000 x 600 with a 40 px panel: medium cells (96) -> 10 columns, 5 rows.
d.w = 1000
d.h = 600
d.bottom = 40
d
}
///|
test "icons fill the grid a column at a time" {
let d = desk()
assert_eq(d.grid(), (10, 5))
let a = apps(20)
let cells = d.layout(a)
assert_eq(cells.length(), 12) // "12" apps shown by default
assert_eq(cells[0], (0, 0))
assert_eq(cells[4], (0, 4))
assert_eq(cells[5], (1, 0))
ignore(d.set_config("count", "All"))
assert_eq(d.layout(a).length(), 20)
}
///|
test "click launches, drag moves and swaps, positions round-trip through the setting" {
let d = desk()
let a = apps(12)
// Cell (0,0) holds app0 at x 20..116, y 20..116.
assert_eq(d.app_at(a, 60, 60), "app0")
assert_eq(d.app_at(a, 5, 5), "")
ignore(d.handle(a, PointerDown({ x: 60, y: 60 })))
assert_eq(d.handle(a, PointerUp({ x: 61, y: 61 })), (true, Launch("app0")))
// Drag app0 onto app5's cell (1,0): they swap.
ignore(d.handle(a, PointerDown({ x: 60, y: 60 })))
ignore(d.handle(a, PointerMove({ x: 120, y: 62 })))
assert_true(d.dragging)
assert_eq(d.handle(a, PointerUp({ x: 160, y: 62 })), (true, NoAction))
let cells = d.layout(a)
assert_eq(cells[0], (1, 0))
assert_eq(cells[5], (0, 0))
// Drag app1 to an empty cell far away.
ignore(d.handle(a, PointerDown({ x: 60, y: 150 })))
ignore(d.handle(a, PointerMove({ x: 900, y: 400 })))
ignore(d.handle(a, PointerUp({ x: 900, y: 400 })))
assert_eq(d.layout(a)[1], (9, 3))
// The hidden setting keeps it.
let saved = d.config_items().filter(c => c.key == "places")[0]
assert_eq(saved.label, "")
let d2 = desk()
ignore(d2.set_config("places", saved.value))
assert_eq(d2.layout(a), d.layout(a))
// A place off the grid (a smaller screen) falls back to a free cell.
d2.w = 400
assert_true(d2.layout(a)[1].0 < 3)
}
///|
test "keyboard moves the selection and launches" {
let d = desk()
let a = apps(12)
ignore(d.handle(a, KeyDown("ArrowDown")))
assert_eq(d.selected, "app0")
ignore(d.handle(a, KeyDown("ArrowDown")))
assert_eq(d.selected, "app1")
ignore(d.handle(a, KeyDown("ArrowRight")))
assert_eq(d.selected, "app6")
assert_eq(d.handle(a, KeyDown("Enter")), (false, Launch("app6")))
// A letter jumps to the next app with that initial.
ignore(d.handle(a, KeyDown("k")))
assert_eq(d.selected, "app8") // Konsole8 (Kate9 comes after it)
ignore(d.handle(a, KeyDown("Escape")))
assert_eq(d.selected, "")
ignore(d.handle(a, FocusLost))
assert_false(d.focused)
}