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,515 @@
|
||||
///| Icon desktop: the interactive sample desktop module (components/icon-desktop). App icons sit
|
||||
/// in a grid on the wallpaper, top-left first, a column at a time. A click starts the app; a drag
|
||||
/// moves its icon to another cell (swapping with the icon there), and the arrangement is kept as
|
||||
/// the hidden `places` setting, which the shell saves in state.edn. The keyboard works while the
|
||||
/// desktop has focus: arrows move the selection, Enter starts it, a letter jumps to the next app
|
||||
/// whose name starts with it, Escape clears it.
|
||||
///
|
||||
/// Pure: the component hands in the app catalog, the theme, the size, an icon lookup and the
|
||||
/// events; it carries out the `Launch` this returns.
|
||||
|
||||
///| One item of the module's right-click settings (the `module-config` record). An empty label
|
||||
/// hides it from the menu (the shell still saves it): that is the arrangement.
|
||||
#derive(Eq, Debug)
|
||||
pub(all) struct ConfigEntry {
|
||||
key : String
|
||||
label : String
|
||||
choices : Array[String]
|
||||
value : String
|
||||
}
|
||||
|
||||
///|
|
||||
pub let count_choices : Array[String] = ["8", "12", "24", "All"]
|
||||
|
||||
///|
|
||||
pub let size_choices : Array[String] = ["Small", "Medium", "Large"]
|
||||
|
||||
///| A non-negative decimal number, or `dflt` when `s` isn't one.
|
||||
fn nat(s : String, dflt : Int) -> Int {
|
||||
if s.length() == 0 || s.length() > 6 {
|
||||
return dflt
|
||||
}
|
||||
let mut v = 0
|
||||
for c in s {
|
||||
if c < '0' || c > '9' {
|
||||
return dflt
|
||||
}
|
||||
v = v * 10 + (c.to_int() - '0'.to_int())
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
///| Space kept free around the grid, in pixels.
|
||||
let margin : Int = 20
|
||||
|
||||
///| A drag starts once the pointer has moved this far from the press.
|
||||
let drag_threshold : Int = 5
|
||||
|
||||
///| What the module asks of the host after an event.
|
||||
#derive(Eq, Debug)
|
||||
pub(all) enum Action {
|
||||
NoAction
|
||||
Launch(String)
|
||||
}
|
||||
|
||||
///| The desktop's state.
|
||||
pub(all) struct Desk {
|
||||
mut count : String
|
||||
mut size : String
|
||||
/// Where icons were put: app id -> (column, row).
|
||||
places : Map[String, (Int, Int)]
|
||||
mut w : Int
|
||||
mut h : Int
|
||||
/// Kept free at the bottom (the panel).
|
||||
mut bottom : Int
|
||||
/// Shown app under the pointer / selected (by id, "" for none), and whether the desktop has the
|
||||
/// keyboard (the selection is drawn brighter then).
|
||||
mut hover : String
|
||||
mut selected : String
|
||||
mut focused : Bool
|
||||
/// A press on an icon: its id and where; whether it became a drag, and where the pointer is.
|
||||
mut press_id : String
|
||||
mut press_x : Int
|
||||
mut press_y : Int
|
||||
mut dragging : Bool
|
||||
mut ptr_x : Int
|
||||
mut ptr_y : Int
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Desk::new() -> Desk {
|
||||
{
|
||||
count: "12",
|
||||
size: "Medium",
|
||||
places: {},
|
||||
w: 0,
|
||||
h: 0,
|
||||
bottom: 40,
|
||||
hover: "",
|
||||
selected: "",
|
||||
focused: false,
|
||||
press_id: "",
|
||||
press_x: 0,
|
||||
press_y: 0,
|
||||
dragging: false,
|
||||
ptr_x: 0,
|
||||
ptr_y: 0,
|
||||
}
|
||||
}
|
||||
|
||||
///| (cell width, cell height, icon size) for the size setting.
|
||||
pub fn Desk::cell(self : Desk) -> (Int, Int, Int) {
|
||||
match self.size {
|
||||
"Small" => (76, 76, 32)
|
||||
"Large" => (120, 116, 64)
|
||||
_ => (96, 96, 48)
|
||||
}
|
||||
}
|
||||
|
||||
///| Columns and rows that fit on the desktop (at least one of each).
|
||||
pub fn Desk::grid(self : Desk) -> (Int, Int) {
|
||||
let (cw, ch, _) = self.cell()
|
||||
let cols = (self.w - 2 * margin) / cw
|
||||
let rows = (self.h - self.bottom - 2 * margin) / ch
|
||||
(if cols < 1 { 1 } else { cols }, if rows < 1 { 1 } else { rows })
|
||||
}
|
||||
|
||||
///| The apps on the desktop: the first `count` of the catalog (its defaults and recent apps come
|
||||
/// first).
|
||||
pub fn Desk::shown(self : Desk, apps : Array[@types.AppEntry]) -> Array[@types.AppEntry] {
|
||||
let n = match self.count {
|
||||
"All" => apps.length()
|
||||
c => nat(c, 12)
|
||||
}
|
||||
if n >= apps.length() {
|
||||
apps
|
||||
} else {
|
||||
apps[:n].to_array()
|
||||
}
|
||||
}
|
||||
|
||||
///| Each shown app's cell, in `shown` order: its saved place when that is on the grid and free,
|
||||
/// else the first free cell, a column at a time. (-1, -1) when the grid is full.
|
||||
pub fn Desk::layout(self : Desk, apps : Array[@types.AppEntry]) -> Array[(Int, Int)] {
|
||||
let shown = self.shown(apps)
|
||||
let (cols, rows) = self.grid()
|
||||
let taken : Map[(Int, Int), Bool] = {}
|
||||
let out : Array[(Int, Int)] = Array::make(shown.length(), (-1, -1))
|
||||
for i, a in shown {
|
||||
match self.places.get(a.id) {
|
||||
Some((c, r)) if c >= 0 && r >= 0 && c < cols && r < rows && !taken.contains((c, r)) => {
|
||||
taken.set((c, r), true)
|
||||
out[i] = (c, r)
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
let mut next = 0
|
||||
for i = 0; i < shown.length(); i = i + 1 {
|
||||
if out[i] != (-1, -1) {
|
||||
continue
|
||||
}
|
||||
while next < cols * rows && taken.contains((next / rows, next % rows)) {
|
||||
next = next + 1
|
||||
}
|
||||
if next < cols * rows {
|
||||
let cr = (next / rows, next % rows)
|
||||
taken.set(cr, true)
|
||||
out[i] = cr
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
///| Top-left corner of cell (col, row).
|
||||
pub fn Desk::cell_origin(self : Desk, col : Int, row : Int) -> (Int, Int) {
|
||||
let (cw, ch, _) = self.cell()
|
||||
(margin + col * cw, margin + row * ch)
|
||||
}
|
||||
|
||||
///| The cell under (x, y), clamped to the grid.
|
||||
pub fn Desk::cell_at(self : Desk, x : Int, y : Int) -> (Int, Int) {
|
||||
let (cw, ch, _) = self.cell()
|
||||
let (cols, rows) = self.grid()
|
||||
let clamp = fn(v : Int, hi : Int) { if v < 0 { 0 } else if v > hi { hi } else { v } }
|
||||
(clamp((x - margin) / cw, cols - 1), clamp((y - margin) / ch, rows - 1))
|
||||
}
|
||||
|
||||
///| The id of the shown app whose icon cell holds (x, y), or "".
|
||||
pub fn Desk::app_at(self : Desk, apps : Array[@types.AppEntry], x : Int, y : Int) -> String {
|
||||
if x < margin || y < margin {
|
||||
return ""
|
||||
}
|
||||
let (cw, ch, _) = self.cell()
|
||||
let col = (x - margin) / cw
|
||||
let row = (y - margin) / ch
|
||||
let shown = self.shown(apps)
|
||||
for i, cr in self.layout(apps) {
|
||||
if cr == (col, row) {
|
||||
return shown[i].id
|
||||
}
|
||||
}
|
||||
""
|
||||
}
|
||||
|
||||
///| Remember every shown icon where it is now, then put `id` at `to`, swapping with the icon
|
||||
/// there (which takes `id`'s old cell).
|
||||
fn Desk::move_to(self : Desk, apps : Array[@types.AppEntry], id : String, to : (Int, Int)) -> Unit {
|
||||
let shown = self.shown(apps)
|
||||
let cells = self.layout(apps)
|
||||
let mut from = (-1, -1)
|
||||
for i, a in shown {
|
||||
if cells[i] != (-1, -1) {
|
||||
self.places.set(a.id, cells[i])
|
||||
}
|
||||
if a.id == id {
|
||||
from = cells[i]
|
||||
}
|
||||
}
|
||||
for i, a in shown {
|
||||
if a.id != id && cells[i] == to {
|
||||
self.places.set(a.id, from)
|
||||
}
|
||||
}
|
||||
self.places.set(id, to)
|
||||
}
|
||||
|
||||
///| The `places` setting's value: "id=col,row" joined by ";".
|
||||
pub fn Desk::encode_places(self : Desk) -> String {
|
||||
let parts : Array[String] = []
|
||||
for id, cr in self.places {
|
||||
parts.push(id + "=" + cr.0.to_string() + "," + cr.1.to_string())
|
||||
}
|
||||
parts.join(";")
|
||||
}
|
||||
|
||||
///| Read the `places` setting back (bad entries are skipped).
|
||||
pub fn Desk::decode_places(self : Desk, s : String) -> Unit {
|
||||
self.places.clear()
|
||||
for part in s.split(";") {
|
||||
let part = part.to_string()
|
||||
match part.rev_find("=") {
|
||||
None => ()
|
||||
Some(eq) => {
|
||||
let id = part[:eq].to_string()
|
||||
let nums = part[eq + 1:].to_string().split(",").to_array()
|
||||
if id.length() > 0 && nums.length() == 2 {
|
||||
let c = nat(nums[0].to_string(), -1)
|
||||
let r = nat(nums[1].to_string(), -1)
|
||||
if c >= 0 && r >= 0 {
|
||||
self.places.set(id, (c, r))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///| Move the selection one step in direction (dc, dr): the nearest icon that way.
|
||||
fn Desk::step_selection(self : Desk, apps : Array[@types.AppEntry], dc : Int, dr : Int) -> Unit {
|
||||
let shown = self.shown(apps)
|
||||
let cells = self.layout(apps)
|
||||
let mut cur = -1
|
||||
for i, a in shown {
|
||||
if a.id == self.selected {
|
||||
cur = i
|
||||
}
|
||||
}
|
||||
if cur < 0 {
|
||||
if shown.length() > 0 {
|
||||
self.selected = shown[0].id
|
||||
}
|
||||
return
|
||||
}
|
||||
let (c0, r0) = cells[cur]
|
||||
let mut best = -1
|
||||
let mut best_score = 0
|
||||
for i = 0; i < shown.length(); i = i + 1 {
|
||||
let (c, r) = cells[i]
|
||||
if i == cur || c < 0 {
|
||||
continue
|
||||
}
|
||||
let along = (c - c0) * dc + (r - r0) * dr
|
||||
if along <= 0 {
|
||||
continue
|
||||
}
|
||||
let across = if dc != 0 { (r - r0).abs() } else { (c - c0).abs() }
|
||||
let score = along * 10 + across * 11
|
||||
if best < 0 || score < best_score {
|
||||
best = i
|
||||
best_score = score
|
||||
}
|
||||
}
|
||||
if best >= 0 {
|
||||
self.selected = shown[best].id
|
||||
}
|
||||
}
|
||||
|
||||
///| Select the next app (after the selected one, wrapping) whose name starts with `k`.
|
||||
fn Desk::jump_to(self : Desk, apps : Array[@types.AppEntry], k : String) -> Bool {
|
||||
let shown = self.shown(apps)
|
||||
let n = shown.length()
|
||||
if n == 0 {
|
||||
return false
|
||||
}
|
||||
let mut start = 0
|
||||
for i, a in shown {
|
||||
if a.id == self.selected {
|
||||
start = i + 1
|
||||
}
|
||||
}
|
||||
let key = k.to_lower()
|
||||
for j = 0; j < n; j = j + 1 {
|
||||
let a = shown[(start + j) % n]
|
||||
if a.name.to_lower().has_prefix(key) {
|
||||
self.selected = a.id
|
||||
return true
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
///| An input event. Returns (redraw, what to do).
|
||||
pub fn Desk::handle(self : Desk, apps : Array[@types.AppEntry], ev : @types.InputEvent) -> (Bool, Action) {
|
||||
match ev {
|
||||
PointerMove(p) => {
|
||||
self.ptr_x = p.x
|
||||
self.ptr_y = p.y
|
||||
if self.press_id != "" && !self.dragging &&
|
||||
((p.x - self.press_x).abs() > drag_threshold || (p.y - self.press_y).abs() > drag_threshold) {
|
||||
self.dragging = true
|
||||
}
|
||||
if self.dragging {
|
||||
return (true, NoAction)
|
||||
}
|
||||
let h = self.app_at(apps, p.x, p.y)
|
||||
let changed = h != self.hover
|
||||
self.hover = h
|
||||
(changed, NoAction)
|
||||
}
|
||||
PointerLeave => {
|
||||
let changed = self.hover != ""
|
||||
self.hover = ""
|
||||
(changed, NoAction)
|
||||
}
|
||||
PointerDown(p) => {
|
||||
self.focused = true
|
||||
self.ptr_x = p.x
|
||||
self.ptr_y = p.y
|
||||
let id = self.app_at(apps, p.x, p.y)
|
||||
self.selected = id
|
||||
self.press_id = id
|
||||
self.press_x = p.x
|
||||
self.press_y = p.y
|
||||
self.dragging = false
|
||||
(true, NoAction)
|
||||
}
|
||||
PointerUp(p) => {
|
||||
let id = self.press_id
|
||||
let was_drag = self.dragging
|
||||
self.press_id = ""
|
||||
self.dragging = false
|
||||
if id == "" {
|
||||
(false, NoAction)
|
||||
} else if was_drag {
|
||||
self.move_to(apps, id, self.cell_at(p.x, p.y))
|
||||
self.hover = self.app_at(apps, p.x, p.y)
|
||||
(true, NoAction)
|
||||
} else if self.app_at(apps, p.x, p.y) == id {
|
||||
(true, Launch(id))
|
||||
} else {
|
||||
(true, NoAction)
|
||||
}
|
||||
}
|
||||
FocusLost => {
|
||||
self.focused = false
|
||||
self.press_id = ""
|
||||
self.dragging = false
|
||||
(true, NoAction)
|
||||
}
|
||||
KeyDown(k) => {
|
||||
self.focused = true
|
||||
match k {
|
||||
"ArrowLeft" | "Left" => self.step_selection(apps, -1, 0)
|
||||
"ArrowRight" | "Right" => self.step_selection(apps, 1, 0)
|
||||
"ArrowUp" | "Up" => self.step_selection(apps, 0, -1)
|
||||
"ArrowDown" | "Down" => self.step_selection(apps, 0, 1)
|
||||
"Enter" | " " =>
|
||||
return (false, if self.selected != "" { Launch(self.selected) } else { NoAction })
|
||||
"Escape" => self.selected = ""
|
||||
_ => if k.length() == 1 { ignore(self.jump_to(apps, k)) } else { return (false, NoAction) }
|
||||
}
|
||||
(true, NoAction)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///| A label that fits `max_w` pixels at text size `fs`: cut with "..." when too long.
|
||||
fn fit_label(s : String, fs : Int, max_w : Int) -> String {
|
||||
if @ui.text_width(s, fs) <= max_w {
|
||||
return s
|
||||
}
|
||||
let mut n = s.length()
|
||||
while n > 1 && @ui.text_width(s[:n].to_string() + "...", fs) > max_w {
|
||||
n = n - 1
|
||||
}
|
||||
s[:n].to_string() + "..."
|
||||
}
|
||||
|
||||
///| One icon with its label, its cell's top-left at (x, y).
|
||||
fn Desk::draw_icon(
|
||||
self : Desk,
|
||||
ops : Array[@types.DrawOp],
|
||||
a : @types.AppEntry,
|
||||
x : Int,
|
||||
y : Int,
|
||||
tokens : @theme.Tokens,
|
||||
icon : (String) -> UInt,
|
||||
lifted~ : Bool = false,
|
||||
) -> Unit {
|
||||
let (cw, ch, isz) = self.cell()
|
||||
let fs = tokens.font_size.reinterpret_as_int() - 2
|
||||
let r = tokens.radius.reinterpret_as_int()
|
||||
let sel = a.id == self.selected
|
||||
if lifted || sel || a.id == self.hover {
|
||||
let base = if sel || lifted { tokens.palette.accent } else { tokens.palette.surface_hover }
|
||||
let alpha = if lifted { 120 } else if sel && self.focused { 110 } else if sel { 60 } else { 90 }
|
||||
ops.push(@ui.fill_rect(@ui.rect(x + 4, y + 2, cw - 8, ch - 4), @ui.with_alpha(base, alpha), radius=r))
|
||||
if sel && self.focused {
|
||||
ops.push(@ui.stroke_rect(@ui.rect(x + 4, y + 2, cw - 8, ch - 4), tokens.palette.accent, radius=r))
|
||||
}
|
||||
}
|
||||
let ix = x + (cw - isz) / 2
|
||||
let iy = y + 8
|
||||
let h = icon(a.id)
|
||||
if h != 0U {
|
||||
ops.push(@types.DrawOp::Image({ rect: @ui.rect(ix, iy, isz, isz), image: h }))
|
||||
} else {
|
||||
let glyph = if a.glyph.length() > 0 { a.glyph } else { @dock.app_glyph(a.id, a.name) }
|
||||
for op in @dock.app_icon(ix + isz / 2, iy + isz / 2, isz, a.id, glyph) {
|
||||
ops.push(op)
|
||||
}
|
||||
}
|
||||
// The label, with a soft shadow so it reads on any wallpaper.
|
||||
let label = fit_label(a.name, fs, cw - 8)
|
||||
let ty = iy + isz + 6 + fs / 2 + 2
|
||||
ops.push(@ui.text(x + cw / 2 + 1, ty + 1, label, fs, @ui.color(0, 0, 0, 170), align=CENTER))
|
||||
ops.push(@ui.text(x + cw / 2, ty, label, fs, @ui.color(255, 255, 255, 255), align=CENTER))
|
||||
}
|
||||
|
||||
///| Draw the icons over the wallpaper. `icon` gives an app id's theme icon handle (0: none, a
|
||||
/// letter tile is drawn instead).
|
||||
pub fn Desk::render(
|
||||
self : Desk,
|
||||
apps : Array[@types.AppEntry],
|
||||
size : @types.Size,
|
||||
tokens : @theme.Tokens,
|
||||
icon : (String) -> UInt,
|
||||
) -> Array[@types.DrawOp] {
|
||||
self.w = size.w.reinterpret_as_int()
|
||||
self.h = size.h.reinterpret_as_int()
|
||||
self.bottom = tokens.panel_height.reinterpret_as_int()
|
||||
let ops : Array[@types.DrawOp] = []
|
||||
let shown = self.shown(apps)
|
||||
let cells = self.layout(apps)
|
||||
let mut dragged : @types.AppEntry? = None
|
||||
for i, a in shown {
|
||||
let (c, r) = cells[i]
|
||||
if c < 0 {
|
||||
continue
|
||||
}
|
||||
if self.dragging && a.id == self.press_id {
|
||||
dragged = Some(a)
|
||||
// Where it will land.
|
||||
let (cw, ch, _) = self.cell()
|
||||
let (tc, tr) = self.cell_at(self.ptr_x, self.ptr_y)
|
||||
let (tx, ty) = self.cell_origin(tc, tr)
|
||||
ops.push(
|
||||
@ui.stroke_rect(@ui.rect(tx + 4, ty + 2, cw - 8, ch - 4), @ui.with_alpha(tokens.palette.foreground, 140), width=2, radius=tokens.radius.reinterpret_as_int()),
|
||||
)
|
||||
continue
|
||||
}
|
||||
let (x, y) = self.cell_origin(c, r)
|
||||
self.draw_icon(ops, a, x, y, tokens, icon)
|
||||
}
|
||||
match dragged {
|
||||
Some(a) => {
|
||||
let (cw, ch, _) = self.cell()
|
||||
self.draw_icon(ops, a, self.ptr_x - cw / 2, self.ptr_y - ch / 2, tokens, icon, lifted=true)
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
ops
|
||||
}
|
||||
|
||||
///| The right-click settings; `places` has no label (hidden, saved by the shell).
|
||||
pub fn Desk::config_items(self : Desk) -> Array[ConfigEntry] {
|
||||
[
|
||||
{ key: "count", label: "Apps shown", choices: count_choices, value: self.count },
|
||||
{ key: "size", label: "Icon size", choices: size_choices, value: self.size },
|
||||
{ key: "places", label: "", choices: [], value: self.encode_places() },
|
||||
]
|
||||
}
|
||||
|
||||
///| Set one; true when it changed something on screen.
|
||||
pub fn Desk::set_config(self : Desk, key : String, value : String) -> Bool {
|
||||
match key {
|
||||
"count" if count_choices.contains(value) => {
|
||||
self.count = value
|
||||
true
|
||||
}
|
||||
"size" if size_choices.contains(value) => {
|
||||
self.size = value
|
||||
true
|
||||
}
|
||||
"places" => {
|
||||
self.decode_places(value)
|
||||
true
|
||||
}
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
///| The one instance the component drives (its plasmoid, desktop-module and module-config
|
||||
/// exports share it).
|
||||
pub let state : Desk = Desk::new()
|
||||
Reference in new issue
Block a user