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

+2 -2
View File
@@ -276,7 +276,7 @@ pub fn run(args : Array[String]) -> Int {
fds.push(pfd)
}
fds.append(comp.auth_fds()) // polkit asking for a password, and its helper
ignore(@sys.wait_readable(fds, idle.timeout()))
ignore(@sys.wait_readable(fds, comp.wake_timeout(idle.timeout())))
} else {
// Sleep out what is left of the frame budget (8 ms), not a fixed 8 ms on top of the work.
let spent = (@sys.monotonic_ms() - f0).to_int()
@@ -379,7 +379,7 @@ pub fn run(args : Array[String]) -> Int {
let spent = (@sys.monotonic_ms() - f0).to_int()
@sys.sleep_ms(if spent >= 16 { 1 } else { 16 - spent })
} else {
ignore(@sys.wait_readable(wait_fds, idle.timeout()))
ignore(@sys.wait_readable(wait_fds, comp.wake_timeout(idle.timeout())))
}
}
comp.save_session() // what is open now is reopened at the next start
+96 -3
View File
@@ -644,6 +644,37 @@ pub fn ChandraCompositor::has_timed_work(self : ChandraCompositor) -> Bool {
self.shell.animating()
}
///| How long an idle main loop may sleep: `idle_ms` (until the wall clock's next second), or
/// less when a desktop module's next animation frame is due sooner.
pub fn ChandraCompositor::wake_timeout(self : ChandraCompositor, idle_ms : Int) -> Int {
let d = self.shell.desktop_wake_ms(@sys.monotonic_ms())
if d >= 0 && d < idle_ms {
d
} else {
idle_ms
}
}
///| True when a window covers the whole work area (or the output), give or take the tiling gaps:
/// the desktop can't be seen.
fn ChandraCompositor::desktop_covered(self : ChandraCompositor) -> Bool {
let m = 16
let wa = self.work_area
let wa : @wm.Rect = { x: wa.x + m, y: wa.y + m, w: wa.w - 2 * m, h: wa.h - 2 * m }
for wid in self.wm.stacking_order() {
match self.windows.get(wid) {
Some(win) => {
let g = win.geometry
if g.x <= wa.x && g.y <= wa.y && g.x + g.w >= wa.x + wa.w && g.y + g.h >= wa.y + wa.h {
return true
}
}
None => ()
}
}
false
}
///|
pub fn ChandraCompositor::set_layout_mode(
self : ChandraCompositor,
@@ -769,6 +800,9 @@ fn ChandraCompositor::build_scene(
self.shell.win_list = entries
self.shell.screenshot_state = (if self.rec is Some(_) { 1 } else { 0 }) |
(if self.rec_pointer { 2 } else { 0 })
// A window over the whole desktop (maximized, fullscreen, monocle) pauses a desktop module's
// animation: nobody can see it.
self.shell.set_desktop_covered(self.desktop_covered())
let t0 = @sys.monotonic_ms()
let (bg_ops, shell_ops) = self.shell.render(self.output_w, self.output_h, self.theme)
// The overview goes under the panel and the launcher.
@@ -1030,6 +1064,7 @@ fn ChandraCompositor::set_kbd_focus(self : ChandraCompositor, wid : UInt64?) ->
self.kbd_win = wid
match wid {
Some(w) => {
self.shell.desktop_unfocus() // a window has the keyboard now, not the desktop
self.server.keyboard_enter(w)
self.server.keyboard_modifiers(w, self.mods, 0U, 0U, 0U)
}
@@ -1330,6 +1365,12 @@ fn key_name(code : UInt, shift : Bool) -> String {
28U => "Enter"
57U => " "
12U => "-"
13U => if shift { "+" } else { "=" }
15U => "Tab"
103U => "ArrowUp"
108U => "ArrowDown"
105U => "ArrowLeft"
106U => "ArrowRight"
52U => "."
53U => "/"
_ => ""
@@ -1411,8 +1452,16 @@ pub fn ChandraCompositor::handle_input(
}
return
}
if self.shell.desktop_grabbing() {
// A press on the desktop is held (an icon being dragged): the desktop module follows the
// pointer wherever it goes until the release.
self.leave_pointer()
ignore(self.shell.desktop_pointer_move(xi, yi))
return
}
if self.shell.pointer_move(xi, yi) || self.shell.hit(xi, yi) {
self.leave_pointer()
self.shell.desktop_leave()
return
}
match self.split_drag {
@@ -1424,10 +1473,12 @@ pub fn ChandraCompositor::handle_input(
}
if self.handle_at(xi, yi) is Some(_) || self.resize_at(xi, yi) is Some(_) {
self.leave_pointer()
self.shell.desktop_leave()
return
}
match self.window_at(xi, yi) {
Some((wid, true)) =>
Some((wid, true)) => {
self.shell.desktop_leave()
match self.windows.get(wid) {
Some(win) => {
let lx = x - win.geometry.x.to_double()
@@ -1441,7 +1492,16 @@ pub fn ChandraCompositor::handle_input(
}
None => ()
}
_ => self.leave_pointer()
}
Some((_, false)) => {
self.leave_pointer()
self.shell.desktop_leave()
}
None => {
// The empty desktop: a desktop module sees the pointer here.
self.leave_pointer()
ignore(self.shell.desktop_pointer_move(xi, yi))
}
}
}
Scroll(axis, v, discrete) => {
@@ -1488,7 +1548,15 @@ pub fn ChandraCompositor::handle_input(
}
None => ()
}
_ => ()
Some((_, false)) => ()
None =>
if !self.shell.hit(xi, yi) {
// The empty desktop: a desktop module gets the wheel (in pixels; a notch is ~10).
let d = if v != 0.0 { v.to_int() } else { discrete * 10 }
ignore(
self.shell.desktop_scroll(xi, yi, if axis == 1U { d } else { 0 }, if axis == 0U { d } else { 0 }),
)
}
}
}
PointerButton(button, pressed) => {
@@ -1527,6 +1595,10 @@ pub fn ChandraCompositor::handle_input(
self.run_shell_actions()
return
}
if !pressed && self.shell.desktop_release(xi, yi) {
self.run_shell_actions()
return
}
if !pressed && self.split_drag is Some(_) {
self.split_drag = None
return
@@ -1549,6 +1621,11 @@ pub fn ChandraCompositor::handle_input(
if button == 273U && pressed && self.shell.right_click(xi, yi) {
return
}
// Right button on the empty desktop: the desktop menu (module settings, system settings).
if button == 273U && pressed && self.window_at(xi, yi) is None && !self.shell.hit(xi, yi) &&
self.shell.desktop_menu(xi, yi) {
return
}
if pressed {
self.super_solo = false
if button != 273U && self.shell.pointer_down(xi, yi) {
@@ -1593,6 +1670,13 @@ pub fn ChandraCompositor::handle_input(
self.leave_pointer()
return
}
// A left press on the empty desktop: a desktop module gets it, and the keyboard.
if button == 272U && self.window_at(xi, yi) is None && self.shell.desktop_press(xi, yi) {
self.leave_pointer()
self.set_kbd_focus(None)
self.run_shell_actions()
return
}
match self.window_at(xi, yi) {
Some((wid, _)) => {
self.activate_window(wid)
@@ -1642,6 +1726,15 @@ pub fn ChandraCompositor::handle_input(
self.shell.key_down(name)
}
}
} else if self.kbd_win is None && self.shell.desktop_has_focus() {
// The desktop has the keyboard (it was clicked): keys go to the desktop module.
self.super_solo = false
if pressed {
let name = key_name(code, (self.mods & 1U) != 0U)
if name.length() > 0 && self.shell.desktop_key(name) {
self.run_shell_actions()
}
}
} else {
self.super_solo = false
match self.kbd_win {
+300
View File
@@ -0,0 +1,300 @@
///| Desktop modules (wit/desktop.wit, compositor/shell/desktop_module.mbt): a module that fills the
/// desktop behind the windows. Needs the sample modules built: scripts/build-modules.sh.
///| A compositor with a first frame drawn and six made-up apps (so icon positions don't depend on
/// the machine's .desktop files).
fn desk_comp(tag : String, fake_apps? : Bool = true) -> @host.ChandraCompositor {
let socket_path = "/tmp/wayland-moon-desk-" + tag + "-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap()
if fake_apps {
comp.shell.apps.clear()
for i = 0; i < 6; i = i + 1 {
comp.shell.apps.push({
id: "app" + i.to_string(),
name: ["Files", "Browser", "Terminal", "Editor", "Mail", "Music"][i],
comment: "",
category: "",
glyph: "",
})
}
comp.shell.apps_dirty = true
}
ignore(comp.compose_frame())
comp
}
///|
fn desk_socket(tag : String) -> String {
"/tmp/wayland-moon-desk-" + tag + "-" + @sys.getpid().to_string() + ".sock"
}
///| Press at (x0, y0), move to (x1, y1) in steps, release there.
fn desk_drag(comp : @host.ChandraCompositor, x0 : Int, y0 : Int, x1 : Int, y1 : Int, t : UInt) -> Unit {
comp.handle_input(@nested.InputEvent::PointerMove(x0.to_double(), y0.to_double()), t)
comp.handle_input(@nested.InputEvent::PointerButton(272U, true), t + 1U)
for k = 1; k <= 4; k = k + 1 {
let x = x0 + (x1 - x0) * k / 4
let y = y0 + (y1 - y0) * k / 4
comp.handle_input(@nested.InputEvent::PointerMove(x.to_double(), y.to_double()), t + 2U + k.reinterpret_as_uint())
}
comp.handle_input(@nested.InputEvent::PointerButton(272U, false), t + 10U)
ignore(comp.compose_frame())
}
///| The value of the desktop module's setting `key` ("" when none).
fn desk_setting(comp : @host.ChandraCompositor, key : String) -> String {
for it in comp.shell.slot_config("desktop") {
if it.key == key {
return it.value
}
}
""
}
///| Whether the desktop module currently draws a focused selection (an outlined icon cell).
fn desk_selection_outlined(comp : @host.ChandraCompositor) -> Bool {
comp.shell.slot_ops("desktop").iter().any(op => op is StrokeRect(_))
}
///|
test "Settings > Desktop lists the desktop modules, and only the desktop takes them" {
let comp = desk_comp("choose")
let mods = comp.shell.desktop_modules()
assert_true(mods.contains(("icon-desktop", "Icon Desktop")))
assert_true(mods.contains(("starfield", "Live Starfield")))
// A desktop module is not a panel module, and the other way round.
assert_true(comp.shell.load_path("build/components/starfield.wasm") is None)
assert_false(comp.shell.set_desktop("launcher"))
assert_eq(comp.shell.desktop_choice(), "clock-card")
assert_true(comp.shell.desktop_module() is None)
// Choose one: it is loaded under the "desktop" slot (chandra-ctl list shows it last).
assert_true(comp.shell.set_desktop("icon-desktop"))
assert_eq(comp.shell.desktop_choice(), "icon-desktop")
let last = comp.shell.loaded_modules().last().unwrap()
assert_eq((last.0, last.1), ("desktop", "icon-desktop"))
// Another replaces it; the wallpaper alone unloads it.
assert_true(comp.shell.set_desktop("starfield"))
assert_eq(comp.shell.desktop_module().unwrap().module_id, "starfield")
assert_true(comp.shell.set_desktop("wallpaper"))
assert_true(comp.shell.desktop_module() is None)
// The built-in clock card can only be dragged while it is on the desktop.
assert_false(comp.shell.desk_hit(1280 - 100, 60))
assert_true(comp.shell.set_desktop("clock-card"))
assert_true(comp.shell.desk_hit(1280 - 100, 60))
}
///|
test "the empty desktop's pointer and keys reach the desktop module" {
let comp = desk_comp("input")
assert_true(comp.shell.set_desktop("icon-desktop"))
ignore(comp.compose_frame())
// Grid cells are 96 px from (20, 20): app0 at (0,0), app1 below it.
click_at(comp, 900, 300, 10U) // empty desktop, no icon there
assert_true(comp.shell.desktop_has_focus())
assert_false(desk_selection_outlined(comp))
// Arrow keys move the selection (drawn outlined while the desktop has the keyboard).
comp.handle_input(@nested.InputEvent::Key(108U, true), 20U) // Down
comp.handle_input(@nested.InputEvent::Key(108U, false), 21U)
ignore(comp.compose_frame())
assert_true(desk_selection_outlined(comp))
// Drag app0 to the cell at column 5, row 2: the arrangement is saved as the hidden setting.
desk_drag(comp, 68, 68, 20 + 5 * 96 + 40, 20 + 2 * 96 + 40, 30U)
assert_true(desk_setting(comp, "places").contains("app0=5,2"))
assert_eq(comp.shell.module_setting("icon-desktop", "places"), Some(desk_setting(comp, "places")))
// The wheel over the empty desktop goes to the module too (the icon desktop ignores it).
comp.handle_input(@nested.InputEvent::PointerMove(900.0, 300.0), 50U)
comp.handle_input(@nested.InputEvent::Scroll(0U, 10.0, 1), 51U)
assert_true(comp.shell.desktop_has_focus())
}
///|
test "windows and the panel win over the desktop module" {
let comp = desk_comp("hit")
assert_true(comp.shell.set_desktop("icon-desktop"))
ignore(comp.compose_frame())
// The panel: a click on a panel module doesn't reach the desktop.
let p = comp.shell.panel_rect
click_at(comp, p.x + p.w / 2, p.y + p.h / 2, 10U)
assert_false(comp.shell.desktop_has_focus())
assert_false(comp.shell.desktop_grabbing())
// A right-click on the panel opens the panel's menu, not the desktop's.
click_at(comp, p.x + p.w - 3, p.y + p.h / 2, 20U, btn=273U)
assert_true(comp.shell.menu_title != "Icon Desktop")
comp.shell.key_down("Escape")
// A window over the desktop (tiled: it fills the work area) takes clicks and keys.
let (_c, _shm, _surf, _xdg, _tl, _deco) = open_and_configure_client(comp, desk_socket("hit"), "Win", "test.win")
ignore(comp.compose_frame())
click_at(comp, 68, 68, 30U) // where app0's icon is, under the window
assert_false(comp.shell.desktop_has_focus())
assert_false(comp.shell.desktop_grabbing())
assert_eq(desk_setting(comp, "places"), "")
// No desktop menu over the window either.
click_at(comp, 68, 68, 40U, btn=273U)
assert_false(comp.shell.menu_open && comp.shell.menu_title == "Icon Desktop")
}
///|
test "a press on the desktop keeps the pointer until the release, even over a window" {
let comp = desk_comp("grab")
assert_true(comp.shell.set_desktop("icon-desktop"))
ignore(comp.compose_frame())
comp.handle_input(@nested.InputEvent::PointerMove(68.0, 68.0), 10U)
comp.handle_input(@nested.InputEvent::PointerButton(272U, true), 11U)
assert_true(comp.shell.desktop_grabbing())
comp.handle_input(@nested.InputEvent::PointerMove(1200.0, 600.0), 12U)
comp.handle_input(@nested.InputEvent::PointerButton(272U, false), 13U)
assert_false(comp.shell.desktop_grabbing())
}
///|
test "right-click on the empty desktop opens the desktop menu" {
let comp = desk_comp("menu")
// No module: just System settings (on the Desktop tab).
click_at(comp, 600, 300, 10U, btn=273U)
assert_true(comp.shell.menu_open)
assert_eq(comp.shell.menu_title, "Desktop")
assert_eq(comp.shell.menu_rows.map(r => r.label), ["System settings..."])
comp.shell.key_down("Escape")
assert_true(comp.shell.set_desktop("starfield"))
click_at(comp, 600, 300, 20U, btn=273U)
assert_eq(comp.shell.menu_title, "Live Starfield")
assert_eq(comp.shell.menu_rows.map(r => r.label), ["Module settings...", "System settings..."])
// It opens at the pointer, not above the panel.
let m = comp.shell.menu_rect
assert_eq((m.x, m.y), (600, 300))
// Module settings: the module's own choices; picking one applies it.
click_at(comp, m.x + 20, m.y + 26 + 14, 30U)
assert_eq(comp.shell.menu_title, "Live Starfield settings")
assert_true(comp.shell.menu_rows.map(r => r.label).contains("Still"))
comp.shell.set_module_setting("desktop", "speed", "Fast")
assert_eq(desk_setting(comp, "speed"), "Fast")
ignore(@sys.write_file_bytes("_build/desktop_menu.png", @renderer.encode_png(comp.compose_frame())))
comp.shell.key_down("Escape")
// System settings opens the settings module on its Desktop tab, listing the choices.
click_at(comp, 600, 300, 40U, btn=273U)
let m = comp.shell.menu_rect
click_at(comp, m.x + 20, m.y + 26 + 28 + 14, 50U)
let slot = comp.shell.slot_of("session-settings")
let texts = comp.shell.slot_popup_texts(slot).map(t => t.0)
assert_true(texts.contains("On the desktop"))
assert_true(texts.contains("Live Starfield"))
assert_true(texts.contains("Icon Desktop"))
assert_true(texts.contains("Clock card"))
}
///|
test "the desktop module is drawn at the output's size, and again after a resize" {
let comp = desk_comp("resize")
assert_true(comp.shell.set_desktop("starfield"))
let f = comp.compose_frame()
assert_eq(comp.shell.desktop_module().unwrap().rect.w, 1280)
// It replaces the wallpaper: deep space in the corner, darker than any wallpaper's.
let dark = fn(px : @scene.Rgba?) {
match px {
Some(c) => c.r.to_int() + c.g.to_int() + c.b.to_int() < 120
None => false
}
}
assert_true(dark(f.pixel(3, 3)))
comp.resize(1600, 900)
let f = comp.compose_frame()
assert_eq(f.width, 1600)
let d = comp.shell.desktop_module().unwrap()
assert_eq((d.rect.w, d.rect.h), (1600, 900))
assert_true(dark(f.pixel(1595, 5)))
match comp.shell.slot_ops("desktop")[0] {
FillRect(r) => assert_eq(r.rect.w, 1600U)
_ => fail("the starfield starts with its background")
}
}
///|
test "a still desktop module lets the main loop sleep; an animated one sets its pace" {
let comp = desk_comp("idle")
assert_true(comp.shell.set_desktop("icon-desktop"))
ignore(comp.compose_frame())
ignore(comp.compose_frame())
// The icon desktop never animates: nothing keeps the loop awake, frames are unchanged.
assert_false(comp.shell.desktop_animating())
assert_false(comp.has_timed_work())
assert_eq(comp.wake_timeout(980), 980)
ignore(comp.compose_frame())
assert_false(comp.frame_changed)
// The starfield asks for a frame every 33 ms: the idle sleep is cut to that, and each due
// frame is a changed one.
assert_true(comp.shell.set_desktop("starfield"))
ignore(comp.compose_frame())
assert_true(comp.shell.desktop_animating())
let t = comp.wake_timeout(980)
assert_true(t >= 0 && t <= 33)
@sys.sleep_ms(40)
ignore(comp.compose_frame())
assert_true(comp.frame_changed)
// Still: it stops asking, and the loop sleeps until the next second again.
comp.shell.set_module_setting("desktop", "motion", "Still")
@sys.sleep_ms(40)
ignore(comp.compose_frame())
ignore(comp.compose_frame())
assert_false(comp.shell.desktop_animating())
assert_eq(comp.wake_timeout(980), 980)
ignore(comp.compose_frame())
assert_false(comp.frame_changed)
// Moving again, but a window covering the desktop pauses it.
comp.shell.set_module_setting("desktop", "motion", "Moving")
assert_true(comp.shell.desktop_animating())
let (_c, _shm, _surf, _xdg, _tl, _deco) = open_and_configure_client(comp, desk_socket("idle"), "Cover", "test.cover")
ignore(comp.compose_frame())
assert_eq(comp.wake_timeout(980), 980)
}
///|
test "reload brings the desktop module back from disk with its settings" {
let comp = desk_comp("reload")
assert_true(comp.shell.set_desktop("starfield"))
comp.shell.set_module_setting("desktop", "speed", "Fast")
let (lines, ok) = comp.shell.reload_modules("starfield")
assert_true(ok)
assert_eq(lines, ["reloaded desktop starfield"])
assert_eq(desk_setting(comp, "speed"), "Fast")
// `reload` with no name reloads it along with the panel; `reload desktop` names it.
let (all, ok) = comp.shell.reload_modules("")
assert_true(ok)
assert_true(all.contains("reloaded desktop starfield"))
assert_eq(comp.shell.reload_modules("desktop").0, ["reloaded desktop starfield"])
assert_eq(comp.shell.desktop_module().unwrap().module_id, "starfield")
}
///|
test "desktop screenshots: icons, starfield, the Desktop tab" {
let comp = desk_comp("shots", fake_apps=false)
assert_true(comp.shell.set_desktop("icon-desktop"))
comp.shell.set_module_setting("desktop", "count", "All")
ignore(comp.compose_frame())
// Hover one icon and select another with the keyboard.
click_at(comp, 900, 300, 10U)
comp.handle_input(@nested.InputEvent::Key(106U, true), 20U) // Right: selects the first
comp.handle_input(@nested.InputEvent::Key(108U, true), 21U) // Down
comp.handle_input(@nested.InputEvent::PointerMove(68.0, 68.0), 22U)
ignore(@sys.write_file_bytes("_build/desktop_icons.png", @renderer.encode_png(comp.compose_frame())))
assert_true(comp.shell.set_desktop("starfield"))
comp.shell.set_module_setting("desktop", "speed", "Normal")
comp.handle_input(@nested.InputEvent::PointerMove(900.0, 250.0), 30U)
for _ in 0..<6 {
@sys.sleep_ms(34)
ignore(comp.compose_frame())
}
ignore(@sys.write_file_bytes("_build/desktop_starfield.png", @renderer.encode_png(comp.compose_frame())))
// Warp pulse: a click streaks the stars.
click_at(comp, 900, 250, 40U)
for _ in 0..<3 {
@sys.sleep_ms(34)
ignore(comp.compose_frame())
}
ignore(@sys.write_file_bytes("_build/desktop_warp.png", @renderer.encode_png(comp.compose_frame())))
assert_true(comp.shell.open_settings(Desktop))
for _ in 0..<10 {
@sys.sleep_ms(40) // the popup slides in
ignore(comp.compose_frame())
}
ignore(@sys.write_file_bytes("_build/desktop_settings.png", @renderer.encode_png(comp.compose_frame())))
}
+12 -3
View File
@@ -1472,13 +1472,20 @@ test "the settings module lists the wallpapers and switches the desktop to the o
let pr = comp.shell.slot_popup_rect(slot)
assert_true(pr.h >= 40 + 6 * 28) // heading and one row per wallpaper below the tab bar
assert_eq(comp.shell.wallpaper_sel, 0)
// The wallpaper rows follow what fills the desktop (wallpaper only, clock card, modules).
let row_y = fn(name : String) {
match comp.shell.slot_popup_texts(slot).iter().find_first(t => t.0 == name) {
Some(t) => pr.y + t.2
None => -1
}
}
// Third wallpaper: Starfield. The list stays open and the backdrop is drawn anew.
let star = click_wallpaper_ops(comp, pr.x + 40, pr.y + 40 + 3 * 28 + 13, 5U)
let star = click_wallpaper_ops(comp, pr.x + 40, row_y("Starfield"), 5U)
assert_eq(comp.shell.wallpaper_sel, 2)
assert_true(star > 0)
assert_true(comp.shell.slot_popup_rect(slot).w > 0)
// Last row: Solid, a single fill. Escape then closes the list.
assert_eq(click_wallpaper_ops(comp, pr.x + 40, pr.y + 40 + 5 * 28 + 13, 10U), 1)
assert_eq(click_wallpaper_ops(comp, pr.x + 40, row_y("Solid"), 10U), 1)
assert_eq(comp.shell.wallpaper_sel, 4)
comp.shell.key_down("Escape")
ignore(comp.compose_frame())
@@ -1682,7 +1689,9 @@ test "open_settings opens the settings module on the tab asked for" {
assert_true(comp.shell.open_settings(Desktop))
ignore(comp.compose_frame())
let pr = comp.shell.slot_popup_rect(slot)
assert_eq(pr.h, 40 + 6 * 28 + 6) // the Wallpaper heading and the five wallpapers
// "On the desktop" and its choices (wallpaper only, clock card, the two desktop modules), then
// the Wallpaper heading and the five wallpapers.
assert_eq(pr.h, 40 + (1 + 4 + 6) * 28 + 6)
assert_true(comp.shell.open_settings(Session))
ignore(comp.compose_frame())
assert_eq(comp.shell.slot_popup_rect(slot).h, 40 + 4 * 28 + 6) // the four settings
+511
View File
@@ -0,0 +1,511 @@
///| Desktop modules (wit/desktop.wit, docs/desktop-modules.md): a wasm module that fills the
/// desktop, the whole output behind the windows, instead of a box in the panel.
///
/// What fills the desktop is one of: the wallpaper alone, the built-in clock card on it (the
/// default), or one installed desktop module, chosen in Settings > Desktop (the `wallpaper`
/// capability's `desktops` / `set-desktop`) and saved in state.edn as `:desktop`. The module is a
/// plasmoid like any panel module (same sandbox, draw ops, capabilities and right-click settings),
/// kept apart from the panel's `widgets` in `desk_mod` under the slot name "desktop".
///
/// The compositor routes input here only where nothing else is: no window, panel, popup or menu
/// (desktop_pointer_move / desktop_press / desktop_release / desktop_scroll), keys only while the
/// desktop has the keyboard (desktop_key), and a right-click on the empty desktop opens the
/// desktop menu (desktop_menu) instead of reaching the module. The module's drawing is cached and
/// redrawn only when it asks (an event returning true, an animation frame, a resize or theme
/// change); animation frames come from its `tick`, and the main loop sleeps until the next one is
/// due (desktop_wake_ms), so a still desktop module costs nothing while idle.
///| The slot name of the desktop module (menus, reload, settings).
pub let desktop_slot : String = "desktop"
///| The `:desktop` choices that aren't modules.
pub let wallpaper_desktop : String = "wallpaper"
///|
pub let clock_card_desktop : String = "clock-card"
///| What fills a fresh session's desktop: the clock card, as before desktop modules.
let default_desktop : String = "clock-card"
///| A module's frame request is capped at this (ms): a module can't put itself to sleep for
/// hours by mistake and miss its own animation.
let max_frame_delay : Int = 10000
///| Is any loaded module (panel or desktop) granted `cap`?
fn DesktopShell::any_cap(self : DesktopShell, cap : String) -> Bool {
self.widgets.iter().any(w => w.has_cap(cap)) ||
(match self.desk_mod {
Some(d) => d.has_cap(cap)
None => false
})
}
///| The desktop module, if one is loaded.
pub fn DesktopShell::desktop_module(self : DesktopShell) -> Widget? {
self.desk_mod
}
///| What fills the desktop now: "wallpaper", "clock-card" or the module's id.
pub fn DesktopShell::desktop_choice(self : DesktopShell) -> String {
self.read_desktop_choice()
self.desk_choice
}
// ---------------------------------------------------------------- choosing and loading
///| Read `:desktop` from state.edn once (with `persist`; else the default stays).
fn DesktopShell::read_desktop_choice(self : DesktopShell) -> Unit {
if self.desk_choice_read {
return
}
self.desk_choice_read = true
if !self.persist {
return
}
match lookup_key(read_state_file(), "desktop") {
Some(Text(s)) if s.length() > 0 => self.desk_choice = s
Some(Keyword(s)) if s.length() > 0 => self.desk_choice = s
_ => ()
}
}
///| Write `:desktop` (keeping every other key); nothing without `persist`. The default is left out.
fn DesktopShell::write_desktop_choice(self : DesktopShell) -> Unit {
if !self.persist {
return
}
let entries : Array[(@edn.Value, @edn.Value)] = []
for e in read_state_file() {
match e.0 {
Keyword("desktop") => ()
_ => entries.push(e)
}
}
if self.desk_choice != default_desktop {
entries.push((@edn.Value::Keyword("desktop"), @edn.Value::Text(self.desk_choice)))
}
write_state_file(entries)
}
///| At boot: load the saved desktop module, if that is the choice. A module that is gone or
/// fails leaves the clock card (the saved choice is kept for when it is back).
fn DesktopShell::boot_desktop(self : DesktopShell) -> Unit {
self.read_desktop_choice()
let c = self.desk_choice
if c == wallpaper_desktop || c == clock_card_desktop || self.desk_mod is Some(_) {
return
}
match self.load_desktop(c) {
Ok(_) => ()
Err(e) => self.say(e)
}
}
///| The desktop modules on the module path: (id, name), in catalog order.
pub fn DesktopShell::desktop_modules(self : DesktopShell) -> Array[(String, String)] {
self.name_catalog()
let out : Array[(String, String)] = []
for e in self.catalog {
if e.desktop {
out.push((e.id, e.name))
}
}
out
}
///| The Desktops table the settings module lists: wallpaper only, the clock card, each module;
/// and which of them is current.
fn DesktopShell::desktop_rows(self : DesktopShell) -> (Array[String], Int) {
let names = ["Wallpaper only", "Clock card"]
let mods = self.desktop_modules()
let choice = self.desktop_choice()
let mut cur = if choice == wallpaper_desktop { 0 } else { 1 }
for i, m in mods {
names.push(m.1)
match self.desk_mod {
Some(d) if d.module_id == m.0 => cur = i + 2
_ => ()
}
}
(names, cur)
}
///|
fn DesktopShell::publish_desktops(self : DesktopShell) -> Unit {
let (names, cur) = self.desktop_rows()
let t = @wasm.Table::new()
for n in names {
t.str(n)
t.end_row()
}
@wasm.publish(Desktops, t)
@wasm.publish_desktop(cur)
}
///| The settings module's `set-desktop`: index into the Desktops table.
fn DesktopShell::set_desktop_index(self : DesktopShell, i : Int) -> Unit {
let mods = self.desktop_modules()
let id = match i {
0 => wallpaper_desktop
1 => clock_card_desktop
_ if i - 2 < mods.length() && i >= 2 => mods[i - 2].0
_ => return
}
ignore(self.set_desktop(id))
}
///| Put `id` on the desktop ("wallpaper", "clock-card", or a desktop module's id or path) and
/// remember it. False, with a notice, when the module can't be loaded (what was there stays).
pub fn DesktopShell::set_desktop(self : DesktopShell, id : String) -> Bool {
self.ensure_booted()
self.read_desktop_choice()
if id == wallpaper_desktop || id == clock_card_desktop {
self.unload_desktop()
} else {
match self.load_desktop(id) {
Ok(w) => self.say("Desktop: " + w.pl.name)
Err(e) => {
self.say(e)
return false
}
}
}
self.desk_choice = id
self.write_desktop_choice()
self.bg_key = None
self.publish_desktops()
true
}
///| Load desktop module `id` (catalog id or path) in place of the current one.
fn DesktopShell::load_desktop(self : DesktopShell, id : String) -> Result[Widget, String] {
let path = match self.find_module(id) {
Some(c) => c.path
None => if id.contains("/") { id } else { return Err("Desktop module '" + id + "' not found") }
}
match @wasm.load(path, self.tokens) {
Err(e) => Err("Can't load desktop module: " + e)
Ok(pl) => {
let backdrop = pl.desktop_backdrop()
if backdrop < 0 {
pl.unload()
return Err(pl.name + " is a panel module, not a desktop module")
}
let known = match self.find_module(stem(path)) {
Some(c) => c.path == path
None => false
}
let w : Widget = {
slot: desktop_slot,
module_id: if known { stem(path) } else { path },
path,
pl,
caps: pl.imports(),
rect: { x: 0, y: 0, w: self.screen_w, h: self.screen_h },
popup_rect: { x: 0, y: 0, w: 0, h: 0 },
popup_ops: [],
popup_hot: false,
slide_t0: 0,
slide_in: false,
ghost_rect: { x: 0, y: 0, w: 0, h: 0 },
ghost_ops: [],
}
self.unload_desktop()
self.desk_mod = Some(w)
self.desk_backdrop = backdrop
self.apply_module_cfg(w)
self.desk_saved.clear()
for it in w.pl.config_items() {
if it.label.length() == 0 {
self.desk_saved.set(it.key, it.value)
}
}
self.desk_t0 = @sys.monotonic_ms()
self.desk_next = 0
self.desk_dirty = true
self.desk_drawn = None
self.bg_key = None
// Its clock starts: the first frame request.
self.desktop_tick(self.desk_t0)
Ok(w)
}
}
}
///| Unload the desktop module (the wallpaper / card shows again).
fn DesktopShell::unload_desktop(self : DesktopShell) -> Unit {
match self.desk_mod {
Some(d) => {
d.pl.unload()
self.desk_mod = None
self.desk_ops = []
self.desk_next = 0
self.desk_focus = false
self.desk_hover = false
self.desk_grab = false
self.desk_drawn = None
self.bg_key = None
if self.menu_slot == desktop_slot {
self.menu_open = false
}
}
None => ()
}
}
///| Reload the desktop module from its file (chandra-ctl reload): settings and hidden state are
/// handed back as at login. On failure the old instance stays.
fn DesktopShell::reload_desktop(self : DesktopShell) -> Result[Widget, String] {
match self.desk_mod {
None => Err("")
Some(old) => {
// Load the new one before dropping the old, so a broken build leaves the desktop as it was.
match @wasm.load(old.path, self.tokens) {
Err(e) => Err("Can't load module: " + e)
Ok(probe) => {
let ok = probe.desktop_backdrop() >= 0
probe.unload()
if !ok {
return Err("not a desktop module any more")
}
let id = if old.module_id.contains("/") { old.path } else { old.module_id }
let path = old.path
self.unload_desktop()
match self.load_desktop(if self.find_module(id) is Some(_) { id } else { path }) {
Ok(w) => Ok(w)
Err(e) => Err(e)
}
}
}
}
}
}
// ---------------------------------------------------------------- drawing and frames
///| Redraw the module's ops when needed: asked for, or the output size or theme changed (the
/// module is told the new size through `render`).
fn DesktopShell::desktop_render(
self : DesktopShell,
w : Int,
h : Int,
theme : @scene.ThemeTokens,
tokens : @theme.Tokens,
) -> Unit {
match self.desk_mod {
None => ()
Some(d) => {
self.desktop_pump(@sys.monotonic_ms())
let same = match self.desk_drawn {
Some((dw, dh, t)) => dw == w && dh == h && t == theme
None => false
}
if same && !self.desk_dirty {
return
}
let out : Array[@scene.DrawOp] = []
translate(d.pl.render(w, h, tokens), 0, 0, out)
self.process_requests()
d.rect = { x: 0, y: 0, w, h }
self.desk_ops = out
self.desk_gen = self.desk_gen + 1
self.desk_dirty = false
self.desk_drawn = Some((w, h, theme))
}
}
}
///| Run the module's clock: ask it when the next frame is due, and whether to redraw.
fn DesktopShell::desktop_tick(self : DesktopShell, now : UInt64) -> Unit {
match self.desk_mod {
None => ()
Some(d) => {
let ms = (now - self.desk_t0).to_int()
let r = d.pl.desktop_tick(ms)
let r = if r > max_frame_delay { max_frame_delay } else { r }
self.desk_next = if r > 0 { now + r.to_uint64() } else { 0 }
}
}
}
///| A due animation frame: tick, then redraw. Frames pause while a window covers the desktop.
fn DesktopShell::desktop_pump(self : DesktopShell, now : UInt64) -> Unit {
if self.desk_next != 0 && now >= self.desk_next && !self.desk_covered {
self.desktop_tick(now)
self.desk_dirty = true
}
}
///| Milliseconds until the desktop module's next animation frame, -1 when none is due (no
/// module, it asked for none, or a window covers the desktop). The main loop's idle sleep is
/// capped by this.
pub fn DesktopShell::desktop_wake_ms(self : DesktopShell, now : UInt64) -> Int {
if self.desk_mod is None || self.desk_next == 0 || self.desk_covered {
-1
} else if now >= self.desk_next {
0
} else {
(self.desk_next - now).to_int()
}
}
///| True while the desktop module is animating (it asked for a frame).
pub fn DesktopShell::desktop_animating(self : DesktopShell) -> Bool {
self.desk_mod is Some(_) && self.desk_next != 0
}
///| Tell the shell whether a window covers the whole desktop (the compositor, every frame).
pub fn DesktopShell::set_desktop_covered(self : DesktopShell, covered : Bool) -> Unit {
self.desk_covered = covered
}
///| After the module handled something: redraw if it asked, restart its clock, and save hidden
/// settings that changed (`save`: on releases and keys, not on every motion).
fn DesktopShell::desktop_changed(self : DesktopShell, redraw : Bool, save? : Bool = false) -> Unit {
if redraw {
self.desk_dirty = true
}
self.desktop_tick(@sys.monotonic_ms())
if save {
self.save_desktop_hidden()
}
}
///| Remember the module's hidden settings (empty label: e.g. where icons were put) that changed.
fn DesktopShell::save_desktop_hidden(self : DesktopShell) -> Unit {
match self.desk_mod {
None => ()
Some(d) =>
for it in d.pl.config_items() {
if it.label.length() == 0 && self.desk_saved.get(it.key) != Some(it.value) {
self.desk_saved.set(it.key, it.value)
self.remember_module_setting(d.module_id, it.key, it.value)
}
}
}
}
///| Deliver one event to the desktop module and carry out its requests.
fn DesktopShell::desktop_event(self : DesktopShell, ev : @wasm.PlasmoidEvent, save? : Bool = false) -> Bool {
match self.desk_mod {
None => false
Some(d) => {
self.publish_host_data()
let redraw = d.pl.on_event(false, ev)
self.process_requests()
self.desktop_changed(redraw, save~)
true
}
}
}
// ---------------------------------------------------------------- input
///| The pointer moved over the empty desktop (no window, panel, popup or menu there). True when
/// a desktop module took it.
pub fn DesktopShell::desktop_pointer_move(self : DesktopShell, x : Int, y : Int) -> Bool {
if self.desk_mod is None {
return false
}
self.desk_hover = true
self.desktop_event(PointerMove(x, y))
}
///| The pointer left the empty desktop (onto a window, the panel ...). While a press on the
/// desktop is held it keeps getting the motion instead (see `desktop_grabbing`).
pub fn DesktopShell::desktop_leave(self : DesktopShell) -> Unit {
if self.desk_hover && !self.desk_grab {
self.desk_hover = false
ignore(self.desktop_event(PointerLeave))
}
}
///| A left press on the empty desktop: the desktop takes the keyboard and the module the press.
/// True when a desktop module took it.
pub fn DesktopShell::desktop_press(self : DesktopShell, x : Int, y : Int) -> Bool {
self.ensure_booted()
if self.desk_mod is None {
return false
}
self.dismiss_popups("")
self.desk_focus = true
self.desk_grab = true
self.desk_hover = true
self.desktop_event(PointerDown(x, y))
}
///| True while a press on the desktop is held: motion and the release go to the module wherever
/// the pointer is.
pub fn DesktopShell::desktop_grabbing(self : DesktopShell) -> Bool {
self.desk_grab && self.desk_mod is Some(_)
}
///| The release of a press that went to the desktop. True when there was one.
pub fn DesktopShell::desktop_release(self : DesktopShell, x : Int, y : Int) -> Bool {
if !self.desk_grab {
return false
}
self.desk_grab = false
ignore(self.desktop_event(PointerUp(x, y), save=true))
true
}
///| Wheel over the empty desktop. True when a desktop module took it.
pub fn DesktopShell::desktop_scroll(self : DesktopShell, x : Int, y : Int, dx : Int, dy : Int) -> Bool {
match self.desk_mod {
None => false
Some(d) => {
self.publish_host_data()
let redraw = d.pl.desktop_scroll(x, y, dx, dy)
self.process_requests()
self.desktop_changed(redraw, save=true)
true
}
}
}
///| Does the desktop have the keyboard (clicked, and no window focused since)?
pub fn DesktopShell::desktop_has_focus(self : DesktopShell) -> Bool {
self.desk_focus && self.desk_mod is Some(_)
}
///| A key for the desktop module (only while it has the keyboard). True when it took it.
pub fn DesktopShell::desktop_key(self : DesktopShell, name : String) -> Bool {
if !self.desktop_has_focus() {
return false
}
self.desktop_event(KeyDown(name), save=true)
}
///| A window took the keyboard: the desktop module hears `focus-lost`.
pub fn DesktopShell::desktop_unfocus(self : DesktopShell) -> Unit {
if self.desk_focus {
self.desk_focus = false
ignore(self.desktop_event(FocusLost))
}
}
///| Right-click on the empty desktop: the desktop menu at the pointer, with the module's settings
/// (when it has any) and System settings on the Desktop tab. Always consumed.
pub fn DesktopShell::desktop_menu(self : DesktopShell, x : Int, y : Int) -> Bool {
self.ensure_booted()
if self.inspect_on {
return false
}
self.dismiss_popups("")
let rows : Array[MenuRow] = []
let title = match self.desk_mod {
Some(d) => {
if config_rows(d.pl.config_items()).length() > 0 {
rows.push(menu_row("Module settings...", 3))
} else {
rows.push(menu_row("No module settings", 5))
}
d.pl.name
}
None => "Desktop"
}
rows.push(menu_row("System settings...", 2, arg="desktop"))
self.open_menu(title, rows, desktop_slot, x, y~)
true
}
+3
View File
@@ -57,6 +57,9 @@ fn DesktopShell::desk_pos(self : DesktopShell) -> (Int, Int) {
///| True if (x, y) is over the desktop card.
pub fn DesktopShell::desk_hit(self : DesktopShell, x : Int, y : Int) -> Bool {
if self.desk_choice != clock_card_desktop || self.desk_mod is Some(_) {
return false // the clock card isn't on the desktop
}
let (cx, cy) = self.desk_pos()
x >= cx && x < cx + desktop_widget_w && y >= cy && y < cy + desktop_widget_h
}
+1 -1
View File
@@ -26,7 +26,7 @@ pub fn DesktopShell::set_network_backend(self : DesktopShell, b : &@network.NetB
///| Refresh and publish network state while a module imports `network`; drop the service (and
/// its bus connection) when none does any more.
fn DesktopShell::publish_network(self : DesktopShell) -> Unit {
if !self.widgets.iter().any(w => w.has_cap("network")) {
if !self.any_cap("network") {
match self.net {
Some(n) if n.probe() => {
n.close()
+97 -17
View File
@@ -197,6 +197,9 @@ pub(all) struct CatalogEntry {
path : String
/// Filled in (by loading the module once) the first time names are needed; "" until then.
mut name : String
/// A desktop module (it exports `desktop-module`): it fills the desktop, not a panel slot.
/// Known once names are (see `name_catalog`).
mut desktop : Bool
}
///| One row of the right-click menu. `kind`: 0 replace with module `arg` (a path), 1 remove,
@@ -359,6 +362,34 @@ pub(all) struct DesktopShell {
/// The launcher module's state as the shell follows it (dock.mbt `follow_launcher`): what its
/// popup lists, for the right-click "Pin to dock" menu.
launcher_state : @launcher.State
/// What fills the desktop (desktop_module.mbt): "wallpaper", "clock-card" or a desktop
/// module's id (or path); saved in state.edn as `:desktop`.
mut desk_choice : String
mut desk_choice_read : Bool
/// The loaded desktop module (slot "desktop"), its backdrop (0 over the wallpaper, 1 instead of
/// it) and when it was loaded (its clock's zero, monotonic ms).
mut desk_mod : Widget?
mut desk_backdrop : Int
mut desk_t0 : UInt64
/// When its next animation frame is due (monotonic ms, 0: none asked for).
mut desk_next : UInt64
/// Its last drawing (desktop coordinates), a counter bumped each time it is redrawn, whether it
/// must be redrawn, and the size and theme it was drawn for.
mut desk_ops : Array[@scene.DrawOp]
mut desk_gen : Int
mut desk_dirty : Bool
mut desk_drawn : (Int, Int, @scene.ThemeTokens)?
/// Input: the desktop has the keyboard (clicked, no window focused since), the pointer is over
/// it, a press on it is held (moves and the release go to it even over a window).
mut desk_focus : Bool
mut desk_hover : Bool
mut desk_grab : Bool
/// A window covers the whole desktop: animation frames pause (the compositor sets it).
mut desk_covered : Bool
/// The module's hidden settings (empty label) as last saved, to save only changes.
desk_saved : Map[String, String]
/// The menu's top edge when it opens at the pointer (the desktop menu); -1 above the panel.
mut menu_anchor_y : Int
}
///|
@@ -469,6 +500,22 @@ pub fn DesktopShell::new(display : String) -> DesktopShell {
wit_cache: {},
recents: [],
launcher_state: @launcher.State::new(),
desk_choice: default_desktop,
desk_choice_read: false,
desk_mod: None,
desk_backdrop: 0,
desk_t0: 0,
desk_next: 0,
desk_ops: [],
desk_gen: 0,
desk_dirty: false,
desk_drawn: None,
desk_focus: false,
desk_hover: false,
desk_grab: false,
desk_covered: false,
desk_saved: {},
menu_anchor_y: -1,
}
sh.order_apps()
sh
@@ -608,6 +655,9 @@ fn DesktopShell::widget_at(self : DesktopShell, x : Int, y : Int) -> Widget? {
///|
fn DesktopShell::widget_by_slot(self : DesktopShell, slot : String) -> Widget? {
if slot == desktop_slot {
return self.desk_mod
}
for w in self.widgets {
if w.slot == slot {
return Some(w)
@@ -920,6 +970,9 @@ struct BgKey {
date : (UInt, Byte, Byte, Byte)
hour : Byte
minute : Byte
/// The desktop module's drawing (`desk_gen`), -1 with the clock card, -2 with the wallpaper
/// alone.
desk : Int
}
///| The wallpaper and the desktop card. Building them costs most of an idle frame (the card's
@@ -932,26 +985,53 @@ fn DesktopShell::background(
theme : @scene.ThemeTokens,
tokens : @theme.Tokens,
) -> Array[@scene.DrawOp] {
let at = now()
let (dx, dy) = self.desk_pos()
let key : BgKey = {
w,
h,
theme,
wallpaper: self.wallpaper_sel,
x: dx,
y: dy,
date: (at.year, at.month, at.day, at.weekday),
hour: at.hour,
minute: at.minute,
let card = self.desk_choice == clock_card_desktop && self.desk_mod is None
let key : BgKey = if card {
let at = now()
let (dx, dy) = self.desk_pos()
{
w,
h,
theme,
wallpaper: self.wallpaper_sel,
x: dx,
y: dy,
date: (at.year, at.month, at.day, at.weekday),
hour: at.hour,
minute: at.minute,
desk: -1,
}
} else {
// The wallpaper alone, or under / instead of the desktop module: nothing follows the clock.
self.desktop_render(w, h, theme, tokens)
let replaced = self.desk_mod is Some(_) && self.desk_backdrop == 1
{
w,
h,
theme,
wallpaper: if replaced { -1 } else { self.wallpaper_sel },
x: 0,
y: 0,
date: (0, b'\x00', b'\x00', b'\x00'),
hour: b'\x00',
minute: b'\x00',
desk: if self.desk_mod is Some(_) { self.desk_gen } else { -2 },
}
}
if self.bg_key == Some(key) {
return self.bg_cache
}
let bg : Array[@scene.DrawOp] = []
translate(@libshell.render_wallpaper(@ui.size(w, h), tokens, self.wallpaper_sel), 0, 0, bg)
if key.wallpaper >= 0 {
translate(@libshell.render_wallpaper(@ui.size(w, h), tokens, self.wallpaper_sel), 0, 0, bg)
}
self.wallpaper_len = bg.length()
render_desktop_widget(dx, dy, tokens, at, bg)
if card {
let (dx, dy) = self.desk_pos()
render_desktop_widget(dx, dy, tokens, now(), bg)
} else if self.desk_mod is Some(_) {
bg.append(self.desk_ops)
}
self.bg_cache = bg
self.bg_key = Some(key)
bg
@@ -973,14 +1053,14 @@ pub fn DesktopShell::render(
self.poll_picker()
self.publish_host_data()
// Machine state and the rich window list only matter to modules that import them.
if self.widgets.iter().any(w => w.has_cap("system")) {
if self.any_cap("system") {
self.sampler.refresh() // reads /proc once a second
}
if self.widgets.iter().any(w => w.has_cap("system") || w.has_cap("windows")) {
if self.any_cap("system") || self.any_cap("windows") {
@wasm.set_windows(self.win_list)
}
self.settle_dock_pins()
if self.widgets.iter().any(w => w.has_cap("icons")) {
if self.any_cap("icons") {
self.publish_icons()
}
let bg = self.background(w, h, theme, tokens)
+70 -16
View File
@@ -138,7 +138,7 @@ fn DesktopShell::scan_catalog(self : DesktopShell) -> Unit {
let id = stem(f)
if !seen.contains(id) && !retired_modules.contains(id) {
seen.set(id, true)
self.catalog.push({ id, path: dir + "/" + f, name: "" })
self.catalog.push({ id, path: dir + "/" + f, name: "", desktop: false })
}
}
}
@@ -156,6 +156,7 @@ fn DesktopShell::name_catalog(self : DesktopShell) -> Unit {
match @wasm.load(e.path, self.tokens) {
Ok(pl) => {
e.name = pl.name
e.desktop = pl.desktop_backdrop() >= 0
pl.unload()
keep.push(e)
}
@@ -316,6 +317,7 @@ fn DesktopShell::ensure_booted(self : DesktopShell) -> Unit {
Some(old) if old != entries => write_panel_file(entries)
_ => ()
}
self.boot_desktop()
for e in entries {
let path = if e.has_prefix("/") {
e
@@ -350,6 +352,11 @@ pub fn DesktopShell::load_path(self : DesktopShell, path : String) -> Widget? {
fn DesktopShell::try_load_path(self : DesktopShell, path : String) -> Result[Widget, String] {
match @wasm.load(path, self.tokens) {
Err(e) => Err("Can't load module: " + e)
Ok(pl) if pl.desktop_backdrop() >= 0 => {
// It fills the desktop, not a panel slot: Settings > Desktop puts it there.
pl.unload()
Err(pl.name + " is a desktop module: choose it in Settings > Desktop")
}
Ok(pl) => {
let id = stem(path)
let known = match self.find_module(id) {
@@ -510,6 +517,12 @@ pub fn DesktopShell::reload_modules(self : DesktopShell, name : String) -> (Arra
targets.push((w.slot, w.module_id, w.path))
}
}
// The desktop module too (slot "desktop").
match self.desk_mod {
Some(d) if name == "" || d.module_id == name || stem(d.path) == name || name == desktop_slot || d.path == name =>
targets.push((d.slot, d.module_id, d.path))
_ => ()
}
if targets.length() == 0 {
let msg = if name == "" {
"no modules are loaded"
@@ -523,7 +536,8 @@ pub fn DesktopShell::reload_modules(self : DesktopShell, name : String) -> (Arra
let mut last_err = ""
for t in targets {
let (slot, id, path) = t
match self.replace_slot_result(slot, path) {
let res = if slot == desktop_slot { self.reload_desktop() } else { self.replace_slot_result(slot, path) }
match res {
Ok(nw) => lines.push("reloaded " + nw.slot + " " + id)
Err(e) => {
failed = failed + 1
@@ -551,7 +565,12 @@ pub fn DesktopShell::reload_modules(self : DesktopShell, name : String) -> (Arra
///| The loaded modules in panel order: (slot, module id, path).
pub fn DesktopShell::loaded_modules(self : DesktopShell) -> Array[(String, String, String)] {
self.ensure_booted()
self.widgets.map(w => (w.slot, w.module_id, w.path))
let out = self.widgets.map(w => (w.slot, w.module_id, w.path))
match self.desk_mod {
Some(d) => out.push((d.slot, d.module_id, d.path)) // the desktop module, last
None => ()
}
out
}
///| Unload every module (the compositor is closing).
@@ -560,6 +579,7 @@ pub fn DesktopShell::shutdown(self : DesktopShell) -> Unit {
w.pl.unload()
}
self.widgets.clear()
self.unload_desktop()
}
///| Slot ids of the loaded modules, in panel order.
@@ -652,7 +672,7 @@ fn DesktopShell::publish_host_data(self : DesktopShell) -> Unit {
}
@wasm.publish(Layouts, lt)
@wasm.publish_state(self.layout_sel, self.inspect_on)
if self.widgets.iter().any(w => w.has_cap("wallpaper")) {
if self.any_cap("wallpaper") {
let pt = @wasm.Table::new()
for n in @libshell.wallpaper_names {
pt.str(n)
@@ -660,8 +680,9 @@ fn DesktopShell::publish_host_data(self : DesktopShell) -> Unit {
}
@wasm.publish(Wallpapers, pt)
@wasm.publish_wallpaper(self.wallpaper_sel)
self.publish_desktops()
}
if self.widgets.iter().any(w => w.has_cap("session-settings")) {
if self.any_cap("session-settings") {
let st = @wasm.Table::new()
for n in session_setting_names {
st.str(n)
@@ -670,19 +691,19 @@ fn DesktopShell::publish_host_data(self : DesktopShell) -> Unit {
@wasm.publish(SessionSettings, st)
@wasm.publish_session_settings(self.session_settings_bits())
}
if self.widgets.iter().any(w => w.has_cap("power")) {
if self.any_cap("power") {
@wasm.publish_power(self.screen_off_min, self.suspend_min)
}
if self.widgets.iter().any(w => w.has_cap("shortcuts")) {
if self.any_cap("shortcuts") {
self.publish_shortcuts()
}
if self.widgets.iter().any(w => w.has_cap("default-apps")) {
if self.any_cap("default-apps") {
self.publish_default_apps()
}
if self.widgets.iter().any(w => w.has_cap("screenshot")) {
if self.any_cap("screenshot") {
@wasm.publish_screenshot(self.screenshot_state)
}
if self.widgets.iter().any(w => w.has_cap("battery")) {
if self.any_cap("battery") {
let b = self.battery.refresh()
@wasm.publish_battery(b.percent, b.state, b.secs_left)
}
@@ -705,6 +726,9 @@ fn DesktopShell::publish_host_data(self : DesktopShell) -> Unit {
self.name_catalog()
let mt = @wasm.Table::new()
for e in self.catalog {
if e.desktop {
continue // chosen in Settings > Desktop, not added to the panel
}
let mut n = 0
for w in self.widgets {
if w.path == e.path {
@@ -810,6 +834,7 @@ fn DesktopShell::process_requests(self : DesktopShell) -> Unit {
self.actions.push(ModulesReloaded)
}
Some(SetWallpaper(i)) => self.set_wallpaper(i)
Some(SetDesktop(i)) => self.set_desktop_index(i)
Some(SetSessionSetting(i, on)) => self.set_session_setting(i, on)
Some(Screenshot(code)) => self.actions.push(Screenshot(code))
Some(SetScreenOff(m)) => self.set_power_timeouts(m, self.suspend_min)
@@ -900,7 +925,7 @@ fn DesktopShell::poll_picker(self : DesktopShell) -> Unit {
pub(all) enum SettingsTab {
/// The session switches.
Session
/// What is on the desktop: the wallpaper (modules can't be placed on the desktop yet).
/// What is on the desktop: the wallpaper, the clock card or a desktop module.
Desktop
/// The panel's modules: remove, add, browse.
Panel
@@ -1065,7 +1090,7 @@ fn DesktopShell::open_module_settings(self : DesktopShell, slot : String) -> Uni
if rows.length() == 0 {
rows.push(menu_row("No module settings", 5))
}
self.open_menu(w.pl.name + " settings", rows, slot, self.menu_anchor_x)
self.open_menu(w.pl.name + " settings", rows, slot, self.menu_anchor_x, y=self.menu_anchor_y)
}
}
}
@@ -1081,6 +1106,13 @@ pub fn DesktopShell::set_module_setting(self : DesktopShell, slot : String, key
ignore(o.pl.set_config(key, value))
}
}
match self.desk_mod {
Some(d) if d.module_id == w.module_id => {
ignore(d.pl.set_config(key, value))
self.desktop_changed(true)
}
_ => ()
}
}
}
}
@@ -1110,7 +1142,15 @@ fn DesktopShell::choose_module_setting(self : DesktopShell, slot : String, key :
}
///|
fn DesktopShell::open_menu(self : DesktopShell, title : String, rows : Array[MenuRow], slot : String, x : Int) -> Unit {
fn DesktopShell::open_menu(
self : DesktopShell,
title : String,
rows : Array[MenuRow],
slot : String,
x : Int,
y? : Int = -1,
) -> Unit {
self.menu_anchor_y = y
self.menu_open = true
self.menu_title = title
self.menu_rows = rows
@@ -1143,7 +1183,16 @@ fn DesktopShell::menu_pointer_down(self : DesktopShell, x : Int, y : Int) -> Boo
match row.kind {
0 => ignore(self.replace_slot(slot, row.arg))
1 => ignore(self.unload_slot(slot))
2 => ignore(self.open_settings(if row.arg == "panel" { Panel } else { Session }))
2 =>
ignore(
self.open_settings(
match row.arg {
"panel" => Panel
"desktop" => Desktop
_ => Session
},
),
)
3 => self.open_module_settings(slot)
4 => self.choose_module_setting(slot, row.arg, row.value)
6 => self.actions.push(Window(row.window_id, row.window_cmd))
@@ -1170,8 +1219,13 @@ fn DesktopShell::render_menu(
) -> Unit {
let n = self.menu_rows.length()
let mh = menu_title_h + n * menu_item_h + 6
let mx = clamp_menu(self.menu_anchor_x - menu_w / 2, 4, w - menu_w - 4)
let my = self.panel_rect.y - mh - 6
// Above the panel, centred on the click; or (the desktop menu) at the pointer.
let (mx, my) = if self.menu_anchor_y >= 0 {
let bottom = if self.panel_rect.h > 0 { self.panel_rect.y } else { self.screen_h }
(clamp_menu(self.menu_anchor_x, 4, w - menu_w - 4), clamp_menu(self.menu_anchor_y, 4, bottom - mh - 4))
} else {
(clamp_menu(self.menu_anchor_x - menu_w / 2, 4, w - menu_w - 4), self.panel_rect.y - mh - 6)
}
self.menu_rect = { x: mx, y: my, w: menu_w, h: mh }
let fs = tokens.font_size.reinterpret_as_int()
let r = tokens.radius.reinterpret_as_int()
+31
View File
@@ -246,6 +246,26 @@ pub fn Plasmoid::set_config(self : Plasmoid, key : String, value : String) -> Bo
@hostpl.set_config(self.handle.reinterpret_as_uint(), key, value)
}
///| A desktop module's backdrop: 0 over the wallpaper, 1 replacing it, -1 for a panel module.
pub fn Plasmoid::desktop_backdrop(self : Plasmoid) -> Int {
@hostpl.desktop_backdrop(self.handle.reinterpret_as_uint())
}
///| Scroll over a desktop module at (x, y) by (dx, dy) pixels. True when it wants a redraw.
pub fn Plasmoid::desktop_scroll(self : Plasmoid, x : Int, y : Int, dx : Int, dy : Int) -> Bool {
@hostpl.desktop_scroll(self.handle.reinterpret_as_uint(), x, y, dx, dy)
}
///| A desktop module's animation clock: ms until it wants the next frame, 0 for none.
pub fn Plasmoid::desktop_tick(self : Plasmoid, now_ms : Int) -> Int {
let r = @hostpl.desktop_tick(self.handle.reinterpret_as_uint(), now_ms.reinterpret_as_uint()).reinterpret_as_int()
if r < 0 {
0
} else {
r
}
}
///|
pub fn Plasmoid::imports(self : Plasmoid) -> Array[String] {
self.imports
@@ -309,6 +329,8 @@ pub(all) enum HostTable {
AppCategories
/// Default-app choices (issii: category id name icon current).
AppChoices
/// What can fill the desktop (s: name), served by `wallpaper.desktops`.
Desktops
}
///|
@@ -336,6 +358,7 @@ pub fn publish(which : HostTable, t : Table) -> Unit {
Shortcuts => 10U
AppCategories => 11U
AppChoices => 12U
Desktops => 20U
}
// Most tables are republished every frame unchanged: skip the copy into the host then.
let slot = id.reinterpret_as_int()
@@ -363,6 +386,11 @@ pub fn publish_wallpaper(current : Int) -> Unit {
@hostpl.publish_wallpaper(current)
}
///| What fills the desktop now (an index into the Desktops table).
pub fn publish_desktop(current : Int) -> Unit {
@hostpl.publish_desktop(current)
}
///| Which session settings are on: bit i for row i of the SessionSettings table.
pub fn publish_session_settings(on : Int) -> Unit {
@hostpl.publish_session_settings(on.reinterpret_as_uint())
@@ -416,6 +444,8 @@ pub(all) enum HostRequest {
DefaultApp(Int)
/// The session capability's `reload`: reload every loaded panel module from disk.
ReloadSession
/// The wallpaper capability's `set-desktop`: an index into the Desktops table.
SetDesktop(Int)
} derive(Eq, Debug)
///|
@@ -441,6 +471,7 @@ pub fn next_request() -> HostRequest? {
Some(Shortcut(code)) => Some(Shortcut(code))
Some(DefaultApp(code)) => Some(DefaultApp(code))
Some(ReloadSession) => Some(ReloadSession)
Some(SetDesktop(i)) => Some(SetDesktop(i))
}
}
+47
View File
@@ -152,6 +152,15 @@ let ex_post_cfg_items : Int = 9
///|
let ex_set_cfg : Int = 10
///| The optional `desktop-module` exports (wit/desktop.wit): absent in panel modules.
let ex_desk_backdrop : Int = 11
///|
let ex_desk_scroll : Int = 12
///|
let ex_desk_tick : Int = 13
///| The 64-byte `theme.tokens` record (name left empty) the guest reads through get-tokens.
pub fn pack_tokens(t : @theme.Tokens) -> Bytes {
let b = FixedArray::make(64, b'\x00')
@@ -625,6 +634,33 @@ pub fn Plasmoid::set_config(self : Plasmoid, key : String, value : String) -> Bo
}
}
///| A desktop module's backdrop (`desktop-module.backdrop`): 0 drawn over the wallpaper, 1
/// replacing it; -1 when the module doesn't export `desktop-module` (a panel module).
pub fn Plasmoid::desktop_backdrop(self : Plasmoid) -> Int {
let r = c_wasm_call(self.handle, ex_desk_backdrop, 0, 0, 0, 0)
if r == 0 || r == 1 {
r
} else {
-1
}
}
///| Scroll over a desktop module at (x, y) by (dx, dy) pixels. True when it wants a redraw.
pub fn Plasmoid::desktop_scroll(self : Plasmoid, x : Int, y : Int, dx : Int, dy : Int) -> Bool {
c_wasm_call(self.handle, ex_desk_scroll, x, y, dx, dy) == 1
}
///| A desktop module's animation clock (`now_ms` since it was loaded): the ms until it wants the
/// next frame, 0 for none (also when it trapped or has no `desktop-module`).
pub fn Plasmoid::desktop_tick(self : Plasmoid, now_ms : Int) -> Int {
let r = c_wasm_call(self.handle, ex_desk_tick, now_ms, 0, 0, 0)
if r < 0 {
0
} else {
r
}
}
///| Host interfaces (capabilities) the module's binary imports, e.g. ["theme", "clock"].
pub fn Plasmoid::imports(self : Plasmoid) -> Array[String] {
let buf = Bytes::make(512, b'\x00')
@@ -701,6 +737,8 @@ pub(all) enum HostTable {
AppCategories
/// Default-app choices (issii: category id name icon current).
AppChoices
/// What can fill the desktop (s: name), served by `wallpaper.desktops`.
Desktops
}
///| Publish `t` as the data behind `which` for every module's next call.
@@ -719,6 +757,7 @@ pub fn publish(which : HostTable, t : Table) -> Unit {
Shortcuts => 10
AppCategories => 11
AppChoices => 12
Desktops => 20
}
let out : Array[Byte] = []
for k = 0; k < 4; k = k + 1 {
@@ -741,6 +780,11 @@ pub fn publish_wallpaper(current : Int) -> Unit {
c_host_set_int(2, current)
}
///| Publish what fills the desktop now (an index into the Desktops table).
pub fn publish_desktop(current : Int) -> Unit {
c_host_set_int(20, current)
}
///| Publish which session settings are on: bit i for row i of the SessionSettings table.
pub fn publish_session_settings(on : Int) -> Unit {
c_host_set_int(3, on)
@@ -798,6 +842,8 @@ pub(all) enum HostRequest {
DefaultApp(Int)
/// The session capability's `reload`: reload every loaded panel module from disk.
ReloadSession
/// The wallpaper capability's `set-desktop`: an index into the Desktops table.
SetDesktop(Int)
} derive(Eq, Debug)
///|
@@ -839,6 +885,7 @@ pub fn next_request() -> HostRequest? {
}
18 => Some(Shortcut(c_host_req_int()))
19 => Some(DefaultApp(c_host_req_int()))
40 => Some(SetDesktop(c_host_req_int()))
_ => None
}
}
+20 -5
View File
@@ -17,7 +17,7 @@
#define MAX_INST 2048
enum { EX_INFO, EX_RENDER, EX_ONEVENT, EX_POPUP, EX_POST_INFO, EX_POST_RENDER, EX_POST_POPUP, EX_REALLOC,
EX_CFG_ITEMS, EX_POST_CFG_ITEMS, EX_SET_CFG, EX_N };
EX_CFG_ITEMS, EX_POST_CFG_ITEMS, EX_SET_CFG, EX_DESK_BACKDROP, EX_DESK_SCROLL, EX_DESK_TICK, EX_N };
static const char *ex_names[EX_N] = {
"chandra:desktop/plasmoid@0.1.0#info",
"chandra:desktop/plasmoid@0.1.0#render",
@@ -31,6 +31,10 @@ static const char *ex_names[EX_N] = {
"chandra:desktop/module-config@0.1.0#config-items",
"cabi_post_chandra:desktop/module-config@0.1.0#config-items",
"chandra:desktop/module-config@0.1.0#set-config",
/* optional: chandra:desktop/desktop-module (a module that fills the desktop, wit/desktop.wit) */
"chandra:desktop/desktop-module@0.1.0#backdrop",
"chandra:desktop/desktop-module@0.1.0#on-scroll",
"chandra:desktop/desktop-module@0.1.0#tick",
};
typedef struct Inst {
@@ -61,17 +65,20 @@ enum {
K_NET_STATUS, K_NET_LIST, K_NET_SCAN, K_NET_CONNECT, K_NET_DISCONNECT, K_NET_FORGET, K_NET_SET_WIFI,
K_NET_SET_AIRPLANE,
K_SC_LIST, K_SC_CAPTURE, K_SC_CANCEL, K_SC_RESET, K_SC_RESET_ALL,
K_DA_CATS, K_DA_APPS, K_DA_SET, K_DA_REFRESH
K_DA_CATS, K_DA_APPS, K_DA_SET, K_DA_REFRESH,
K_DESKTOPS, K_DESKTOP_CUR, K_SET_DESKTOP
};
typedef struct Env { Inst *inst; int kind; } Env;
/* ---- host data the shell publishes before calling a guest ----
* A table is [u32 rows] then rows of fields, each field a LE i32 ('i') or a u32 byte length + UTF-8 ('s'). */
enum { T_APPS, T_WINDOWS, T_LAYOUTS, T_MODULES, T_INSTANCES, T_WALLPAPERS, T_SESSION_SETTINGS, T_ICONS,
T_NET_STATUS, T_NET_NETWORKS, T_SHORTCUTS, T_APP_CATS, T_APP_CHOICES, T_N };
T_NET_STATUS, T_NET_NETWORKS, T_SHORTCUTS, T_APP_CATS, T_APP_CHOICES,
/* fixed numbers from here (wit/host/plasmoids.wit publish-table) */
T_DESKTOPS = 20, T_N = 32 };
typedef struct { uint8_t *data; size_t len; } Blob;
static Blob tables[T_N];
static int32_t host_ints[12] = {[7] = -1}; /* 0 layout mode, 1 inspector on, 2 current wallpaper, 3 session settings on (bit i: row i),
static int32_t host_ints[24] = {[7] = -1}; /* 0 layout mode, 1 inspector on, 2 current wallpaper, 3 session settings on (bit i: row i),
4 screenshot state (bit 0 recording, bit 1 pointer in recordings),
5 screen-off minutes, 6 suspend minutes (0: never),
7 battery percent (-1: none), 8 battery charge-state, 9 battery seconds left */
@@ -375,6 +382,11 @@ static wasm_trap_t *host_import(void *env, const wasm_val_vec_t *args, wasm_val_
case K_LOGOUT: push_req(8, 0, NULL); break;
case K_RELOAD: push_req(8, 1, NULL); break; /* 8: session; n 0 log out, 1 reload */
case K_SET_WALLPAPER: push_req(9, a0, NULL); break;
case K_DESKTOPS: build_list(in, T_DESKTOPS, "s", a0); break;
case K_DESKTOP_CUR:
if (results->size >= 1) results->data[0] = (wasm_val_t)WASM_I32_VAL(host_ints[20]);
break;
case K_SET_DESKTOP: push_req(40, a0, NULL); break;
case K_SESSION_SET: push_req(a1 ? 11 : 10, a0, NULL); break; /* 10 off, 11 on */
/* Request 12 carries the screenshot code: 0-3 take, 10-12 record, 20 stop, 30/31 pointer. */
case K_SHOT_TAKE: if (a0 >= 0 && a0 <= 3) push_req(12, a0, NULL); break;
@@ -589,6 +601,9 @@ static int classify_import(const wasm_name_t *m, const wasm_name_t *f) {
if (!strcmp(iface, "wallpaper") && !strcmp(fn, "wallpapers")) return K_WALLPAPERS;
if (!strcmp(iface, "wallpaper") && !strcmp(fn, "current-wallpaper")) return K_WALLPAPER_CUR;
if (!strcmp(iface, "wallpaper") && !strcmp(fn, "set-wallpaper")) return K_SET_WALLPAPER;
if (!strcmp(iface, "wallpaper") && !strcmp(fn, "desktops")) return K_DESKTOPS;
if (!strcmp(iface, "wallpaper") && !strcmp(fn, "current-desktop")) return K_DESKTOP_CUR;
if (!strcmp(iface, "wallpaper") && !strcmp(fn, "set-desktop")) return K_SET_DESKTOP;
if (!strcmp(iface, "session-settings") && !strcmp(fn, "settings")) return K_SESSION_SETTINGS;
if (!strcmp(iface, "session-settings") && !strcmp(fn, "is-on")) return K_SESSION_IS_ON;
if (!strcmp(iface, "session-settings") && !strcmp(fn, "set-setting")) return K_SESSION_SET;
@@ -882,7 +897,7 @@ void mp_host_set_table(int32_t which, moonbit_bytes_t data) {
MOONBIT_FFI_EXPORT
void mp_host_set_int(int32_t which, int32_t v) {
if (which >= 0 && which < 12) host_ints[which] = v;
if (which >= 0 && which < 24) host_ints[which] = v; /* 20: the desktop (wallpaper.current-desktop) */
}
/* Pop the next guest request: returns its kind (0 = none). Fetch its arguments with req_int / req_str. */