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()
|
||||
@@ -0,0 +1,87 @@
|
||||
///|
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import {
|
||||
"chandra/desktop/interface/chandra/desktop/types" @types,
|
||||
"chandra/desktop/interface/chandra/desktop/theme" @theme,
|
||||
"chandra/desktop/lib/ui" @ui,
|
||||
"chandra/desktop/lib/dock" @dock,
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import {
|
||||
"chandra/desktop/interface/chandra/desktop/types" @types,
|
||||
"chandra/desktop/interface/chandra/desktop/theme" @theme,
|
||||
"chandra/desktop/lib/ui" @ui,
|
||||
}
|
||||
@@ -0,0 +1,395 @@
|
||||
///| Starfield: the animated sample desktop module (components/starfield). Stars fly towards the
|
||||
/// viewer out of a vanishing point that follows the pointer; the wheel changes speed, a click (or
|
||||
/// Space) sends a warp pulse that streaks the stars for a moment. It replaces the wallpaper.
|
||||
///
|
||||
/// Pacing: `tick` asks for a frame every 33 ms (30 a second) while anything moves and returns 0
|
||||
/// once nothing does, so with Motion set to Still the desktop costs nothing between events (a
|
||||
/// pulse or a steer then animates until it settles, and stops again).
|
||||
///
|
||||
/// Pure: the component hands in the theme, the size and the events; everything is in `Field`.
|
||||
|
||||
///| Frame interval while animating, in ms.
|
||||
pub let frame_ms : UInt = 33
|
||||
|
||||
///| Speed choices and their depth units per second.
|
||||
pub let speed_choices : Array[String] = ["Slow", "Normal", "Fast"]
|
||||
|
||||
///|
|
||||
pub let density_choices : Array[String] = ["Sparse", "Normal", "Dense"]
|
||||
|
||||
///|
|
||||
pub let motion_choices : Array[String] = ["Moving", "Still"]
|
||||
|
||||
///| One item of the module's right-click settings (the `module-config` record).
|
||||
#derive(Eq, Debug)
|
||||
pub(all) struct ConfigEntry {
|
||||
key : String
|
||||
label : String
|
||||
choices : Array[String]
|
||||
value : String
|
||||
}
|
||||
|
||||
///| A star in normalised space: x, y in [-1, 1], depth z in (0, 1] (1: far).
|
||||
struct Star {
|
||||
mut x : Double
|
||||
mut y : Double
|
||||
mut z : Double
|
||||
/// Depth one frame ago, for the warp streaks.
|
||||
mut pz : Double
|
||||
}
|
||||
|
||||
///| The starfield's state.
|
||||
pub(all) struct Field {
|
||||
stars : Array[Star]
|
||||
mut seed : UInt
|
||||
mut speed : String
|
||||
mut density : String
|
||||
mut motion : String
|
||||
/// Wheel multiplier on the speed, 0.25 .. 4.
|
||||
mut boost : Double
|
||||
/// Warp pulse, 1 right after a click, decaying to 0.
|
||||
mut pulse : Double
|
||||
/// Vanishing point offset from the centre (pixels), and where the pointer pulls it.
|
||||
mut cx : Double
|
||||
mut cy : Double
|
||||
mut tx : Double
|
||||
mut ty : Double
|
||||
mut w : Int
|
||||
mut h : Int
|
||||
/// The clock at the last tick (ms since load), -1 before the first.
|
||||
mut last : Int
|
||||
/// Frames drawn, for tests.
|
||||
mut frames : Int
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Field::new() -> Field {
|
||||
let f : Field = {
|
||||
stars: [],
|
||||
seed: 0x2545F491U,
|
||||
speed: "Normal",
|
||||
density: "Normal",
|
||||
motion: "Moving",
|
||||
boost: 1.0,
|
||||
pulse: 0.0,
|
||||
cx: 0.0,
|
||||
cy: 0.0,
|
||||
tx: 0.0,
|
||||
ty: 0.0,
|
||||
w: 0,
|
||||
h: 0,
|
||||
last: -1,
|
||||
frames: 0,
|
||||
}
|
||||
f.populate()
|
||||
f
|
||||
}
|
||||
|
||||
///| Uniform in [0, 1).
|
||||
fn Field::rand(self : Field) -> Double {
|
||||
// xorshift32
|
||||
let mut s = self.seed
|
||||
s = s ^ (s << 13)
|
||||
s = s ^ (s >> 17)
|
||||
s = s ^ (s << 5)
|
||||
self.seed = s
|
||||
(s >> 8).to_double() / 16777216.0
|
||||
}
|
||||
|
||||
///|
|
||||
fn Field::star_count(self : Field) -> Int {
|
||||
match self.density {
|
||||
"Sparse" => 180
|
||||
"Dense" => 640
|
||||
_ => 360
|
||||
}
|
||||
}
|
||||
|
||||
///| (Re)create the stars for the density, spread through the whole depth.
|
||||
fn Field::populate(self : Field) -> Unit {
|
||||
self.stars.clear()
|
||||
for i = 0; i < self.star_count(); i = i + 1 {
|
||||
// Spread over what is on screen at that depth (|x| < z), as if they had flown in from afar.
|
||||
let z = 0.05 + 0.95 * self.rand()
|
||||
self.stars.push({ x: (self.rand() * 2.0 - 1.0) * z, y: (self.rand() * 2.0 - 1.0) * z, z, pz: z })
|
||||
}
|
||||
}
|
||||
|
||||
///| Depth units per second.
|
||||
fn Field::velocity(self : Field) -> Double {
|
||||
let base = match self.speed {
|
||||
"Slow" => 0.05
|
||||
"Fast" => 0.32
|
||||
_ => 0.13
|
||||
}
|
||||
let still = self.motion == "Still"
|
||||
(if still { 0.0 } else { base * self.boost }) + self.pulse * 1.6
|
||||
}
|
||||
|
||||
///| True while something on screen still moves (a frame is wanted).
|
||||
pub fn Field::animating(self : Field) -> Bool {
|
||||
self.velocity() > 0.0 ||
|
||||
self.pulse > 0.0 ||
|
||||
(self.tx - self.cx).abs() > 0.5 ||
|
||||
(self.ty - self.cy).abs() > 0.5
|
||||
}
|
||||
|
||||
///| The animation clock (`desktop-module.tick`): advance to `now` (ms since load) and return the
|
||||
/// ms until the next frame, 0 when nothing moves.
|
||||
pub fn Field::tick(self : Field, now : UInt) -> UInt {
|
||||
let now = now.reinterpret_as_int()
|
||||
let dt = if self.last < 0 {
|
||||
0.0
|
||||
} else {
|
||||
let d = now - self.last
|
||||
// A long gap (paused while covered, or idle) restarts smoothly instead of jumping.
|
||||
(if d < 0 { 0 } else if d > 100 { 33 } else { d }).to_double() / 1000.0
|
||||
}
|
||||
self.last = now
|
||||
self.step(dt)
|
||||
if self.animating() {
|
||||
frame_ms
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
///| Advance the simulation by `dt` seconds.
|
||||
pub fn Field::step(self : Field, dt : Double) -> Unit {
|
||||
let v = self.velocity()
|
||||
for s in self.stars {
|
||||
s.pz = s.z
|
||||
s.z = s.z - v * dt
|
||||
// Flown past the viewer, or out of the side of the view: start again far away.
|
||||
if s.z <= 0.03 || s.x.abs() > s.z * 1.2 || s.y.abs() > s.z * 1.2 {
|
||||
s.x = self.rand() * 2.0 - 1.0
|
||||
s.y = self.rand() * 2.0 - 1.0
|
||||
s.z = 1.0
|
||||
s.pz = 1.0
|
||||
}
|
||||
}
|
||||
// The pulse fades over about a second; the vanishing point eases towards the pointer.
|
||||
self.pulse = if self.pulse < 0.02 { 0.0 } else { self.pulse * (1.0 - (dt * 2.5).min(1.0)) }
|
||||
let k = (dt * 4.0).min(1.0)
|
||||
self.cx = self.cx + (self.tx - self.cx) * k
|
||||
self.cy = self.cy + (self.ty - self.cy) * k
|
||||
if (self.tx - self.cx).abs() <= 0.5 {
|
||||
self.cx = self.tx
|
||||
}
|
||||
if (self.ty - self.cy).abs() <= 0.5 {
|
||||
self.cy = self.ty
|
||||
}
|
||||
}
|
||||
|
||||
///| An input event (`plasmoid.on-event`). True when a redraw is wanted now (the tick that follows
|
||||
/// every event takes care of the animation).
|
||||
pub fn Field::handle(self : Field, ev : @types.InputEvent) -> Bool {
|
||||
match ev {
|
||||
PointerMove(p) => {
|
||||
// The pointer pulls the vanishing point a fifth of the way towards it.
|
||||
self.tx = (p.x - self.w / 2).to_double() * 0.2
|
||||
self.ty = (p.y - self.h / 2).to_double() * 0.2
|
||||
false
|
||||
}
|
||||
PointerLeave => {
|
||||
self.tx = 0.0
|
||||
self.ty = 0.0
|
||||
false
|
||||
}
|
||||
PointerDown(_) => {
|
||||
self.pulse = 1.0
|
||||
true
|
||||
}
|
||||
KeyDown(" ") => {
|
||||
self.pulse = 1.0
|
||||
true
|
||||
}
|
||||
KeyDown("+") | KeyDown("=") => {
|
||||
ignore(self.scroll(-10))
|
||||
true
|
||||
}
|
||||
KeyDown("-") => {
|
||||
ignore(self.scroll(10))
|
||||
true
|
||||
}
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
///| Wheel (`desktop-module.on-scroll`): up is faster, down slower. True: redraw.
|
||||
pub fn Field::scroll(self : Field, dy : Int) -> Bool {
|
||||
if dy == 0 {
|
||||
return false
|
||||
}
|
||||
let f = if dy < 0 { 1.25 } else { 0.8 }
|
||||
self.boost = (self.boost * f).max(0.25).min(4.0)
|
||||
true
|
||||
}
|
||||
|
||||
///|
|
||||
fn shade(c : @types.Color, num : Int, den : Int) -> @types.Color {
|
||||
@ui.color(
|
||||
c.r.to_int() * num / den,
|
||||
c.g.to_int() * num / den,
|
||||
c.b.to_int() * num / den,
|
||||
255,
|
||||
)
|
||||
}
|
||||
|
||||
///| Draw the whole desktop, `size` being the output.
|
||||
pub fn Field::render(self : Field, size : @types.Size, tokens : @theme.Tokens) -> Array[@types.DrawOp] {
|
||||
let w = size.w.reinterpret_as_int()
|
||||
let h = size.h.reinterpret_as_int()
|
||||
self.w = w
|
||||
self.h = h
|
||||
self.frames = self.frames + 1
|
||||
let ops : Array[@types.DrawOp] = []
|
||||
// Deep space: the theme's desktop colours, much darker, in bands from top to bottom.
|
||||
let top = shade(tokens.palette.desktop_top, 1, 4)
|
||||
let bottom = shade(tokens.palette.desktop_bottom, 1, 3)
|
||||
let bands = 16
|
||||
for i = 0; i < bands; i = i + 1 {
|
||||
let y0 = h * i / bands
|
||||
let y1 = h * (i + 1) / bands
|
||||
ops.push(@ui.fill_rect(@ui.rect(0, y0, w, y1 - y0), @ui.lerp_color(top, bottom, i, bands - 1)))
|
||||
}
|
||||
// Two faint nebulae in the accent colour, drifting a little with the vanishing point.
|
||||
let neb = tokens.palette.accent
|
||||
let big = if w > h { w } else { h }
|
||||
for k = 0; k < 6; k = k + 1 {
|
||||
let r = big * (12 - k) / 40
|
||||
ops.push(@ui.circle(w * 3 / 10 - self.cx.to_int() / 3, h * 2 / 5 - self.cy.to_int() / 3, r, @ui.with_alpha(neb, 3 + k)))
|
||||
ops.push(@ui.circle(w * 3 / 4 - self.cx.to_int() / 4, h * 3 / 4 - self.cy.to_int() / 4, r * 2 / 3, @ui.with_alpha(@ui.color(180, 90, 200, 255), 2 + k)))
|
||||
}
|
||||
let ox = w / 2 + self.cx.to_int()
|
||||
let oy = h / 2 + self.cy.to_int()
|
||||
let scale = (if w > h { w } else { h }).to_double() * 0.5
|
||||
let tint = tokens.palette.accent
|
||||
let streak = self.pulse > 0.15
|
||||
for s in self.stars {
|
||||
let sx = ox + (s.x / s.z * scale).to_int()
|
||||
let sy = oy + (s.y / s.z * scale).to_int()
|
||||
if sx < -4 || sy < -4 || sx > w + 4 || sy > h + 4 {
|
||||
continue
|
||||
}
|
||||
let near = 1.0 - s.z // 0 far .. 1 near
|
||||
let lum = 120 + (near * 135.0).to_int()
|
||||
// Near stars pick up a little of the accent colour.
|
||||
let c = @ui.lerp_color(@ui.color(lum, lum, lum, 255), tint, (near * near * 40.0).to_int(), 100)
|
||||
let r = 1 + (near * 3.4).to_int()
|
||||
if streak && s.pz > s.z {
|
||||
let px = ox + (s.x / s.pz * scale).to_int()
|
||||
let py = oy + (s.y / s.pz * scale).to_int()
|
||||
ops.push(@ui.line(px, py, sx, sy, c, width=r))
|
||||
} else if r <= 1 {
|
||||
ops.push(@ui.fill_rect(@ui.rect(sx, sy, 1, 1), c))
|
||||
} else {
|
||||
ops.push(@ui.fill_rect(@ui.rect(sx - r / 2, sy - r / 2, r, r), c, radius=r / 2))
|
||||
}
|
||||
}
|
||||
ops
|
||||
}
|
||||
|
||||
///| The right-click settings.
|
||||
pub fn Field::config_items(self : Field) -> Array[ConfigEntry] {
|
||||
[
|
||||
{ key: "speed", label: "Speed", choices: speed_choices, value: self.speed },
|
||||
{ key: "density", label: "Stars", choices: density_choices, value: self.density },
|
||||
{ key: "motion", label: "Motion", choices: motion_choices, value: self.motion },
|
||||
]
|
||||
}
|
||||
|
||||
///| Set one; true when it changed something on screen.
|
||||
pub fn Field::set_config(self : Field, key : String, value : String) -> Bool {
|
||||
match key {
|
||||
"speed" if speed_choices.contains(value) => {
|
||||
self.speed = value
|
||||
self.boost = 1.0
|
||||
true
|
||||
}
|
||||
"density" if density_choices.contains(value) => {
|
||||
if self.density != value {
|
||||
self.density = value
|
||||
self.populate()
|
||||
}
|
||||
true
|
||||
}
|
||||
"motion" if motion_choices.contains(value) => {
|
||||
self.motion = value
|
||||
true
|
||||
}
|
||||
_ => false
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
test "moving stars keep asking for frames; still ones stop once settled" {
|
||||
let f = Field::new()
|
||||
assert_eq(f.tick(0), frame_ms)
|
||||
assert_eq(f.tick(33), frame_ms)
|
||||
ignore(f.set_config("motion", "Still"))
|
||||
assert_eq(f.tick(66), 0)
|
||||
// A click warps: frames until the pulse has faded, then sleep again.
|
||||
ignore(f.handle(PointerDown({ x: 10, y: 10 })))
|
||||
assert_eq(f.tick(99), frame_ms)
|
||||
let mut t = 99
|
||||
let mut n = 0
|
||||
while f.tick(t.reinterpret_as_uint()) != 0 && n < 200 {
|
||||
t = t + 33
|
||||
n = n + 1
|
||||
}
|
||||
assert_true(n > 5 && n < 200)
|
||||
// Steering eases towards the pointer, then stops.
|
||||
f.w = 1000
|
||||
f.h = 800
|
||||
ignore(f.handle(PointerMove({ x: 900, y: 400 })))
|
||||
assert_eq(f.tick((t + 33).reinterpret_as_uint()), frame_ms)
|
||||
n = 0
|
||||
while f.tick((t + 66 + n * 33).reinterpret_as_uint()) != 0 && n < 200 {
|
||||
n = n + 1
|
||||
}
|
||||
assert_true(n < 200)
|
||||
assert_eq(f.cx, 80.0)
|
||||
}
|
||||
|
||||
///|
|
||||
test "render fills the output and draws the stars" {
|
||||
let f = Field::new()
|
||||
let tokens : @theme.Tokens = {
|
||||
name: "t",
|
||||
palette: {
|
||||
desktop_top: @ui.color(20, 30, 60, 255),
|
||||
desktop_bottom: @ui.color(10, 10, 30, 255),
|
||||
surface: @ui.color(0, 0, 0, 255),
|
||||
surface_hover: @ui.color(0, 0, 0, 255),
|
||||
surface_popup: @ui.color(0, 0, 0, 255),
|
||||
foreground: @ui.color(255, 255, 255, 255),
|
||||
foreground_muted: @ui.color(200, 200, 200, 255),
|
||||
accent: @ui.color(100, 150, 255, 255),
|
||||
accent_foreground: @ui.color(0, 0, 0, 255),
|
||||
border: @ui.color(50, 50, 50, 255),
|
||||
},
|
||||
font_size: 14,
|
||||
radius: 6,
|
||||
spacing: 6,
|
||||
panel_height: 40,
|
||||
}
|
||||
let ops = f.render(@ui.size(1280, 720), tokens)
|
||||
match ops[0] {
|
||||
FillRect(r) => assert_eq((r.rect.x, r.rect.y, r.rect.w), (0, 0, 1280))
|
||||
_ => fail("background first")
|
||||
}
|
||||
assert_true(ops.length() > 100)
|
||||
// Wheel speeds up and slows down within bounds.
|
||||
for _ in 0..<20 {
|
||||
ignore(f.scroll(-10))
|
||||
}
|
||||
assert_eq(f.boost, 4.0)
|
||||
ignore(f.set_config("density", "Dense"))
|
||||
assert_eq(f.stars.length(), 640)
|
||||
assert_false(f.set_config("density", "Huge"))
|
||||
}
|
||||
|
||||
///| The one instance the component drives (its plasmoid, desktop-module and module-config
|
||||
/// exports share it).
|
||||
pub let state : Field = Field::new()
|
||||
Reference in new issue
Block a user