Module settings in each module's right-click menu, and System settings
CI / test (push) Failing after 15s

- New optional WIT export `chandra:desktop/module-config` (wit/plasmoid.wit): config-items (key,
  label, choices, current value; no choices = an on/off switch) and set-config. Hosts look it up
  by name, so modules without it (or prebuilt ones like Pulse) load as before.
- Right-click a panel module: "Module settings..." (or a greyed "No module settings"), then
  "System settings..." (the settings module, Session tab), then Replace with... and Remove widget.
  The "Modules..." row is gone; right-clicking empty panel space offers "System settings..." on the
  Panel tab instead. Module settings open in the same menu: on/off rows with a check box, choices
  under a heading with the current one ticked; clicking applies at once and the menu stays open.
- Choices are saved per module id in state.edn as :module-config {"clock-digital" {"format"
  "12-hour"}} (module_config.mbt; not an owned key, so save_state keeps it) and handed back through
  set-config whenever the module is loaded. Other instances of the module follow a change.
- Both hosts: wasmer stub (optional exports 8-10, canonical-ABI decoding in wasm_native.mbt) and
  host-wasm (chandra:host/plasmoids config-items/set-config, bound with a separate `configurable`
  world so the export stays optional).
- Adopted by the digital clock (Time format 24-hour/12-hour, Show date) and the binary clock (Show
  seconds); clock-widget now exports module-config.
- Tests: native menu end-to-end (open, pick 12-hour, heading inert, reload keeps it), state.edn
  round trip, EDN unit test, wasmer and host-wasm module-config tests, lib/clock settings test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hjuApkJR4sVRYDpDLy
This commit is contained in:
Claude committed 2026-11-25 18:58:43 +00:00
1 parent a7909014b8
commit 4fc4a963b4
37 files changed
+1285 -128

No files matched your search

+134
View File
@@ -0,0 +1,134 @@
///| Each module's own settings (the optional `module-config` export, shown under "Module settings"
/// in its right-click menu), remembered by module id in state.edn:
///
/// :module-config {"clock-digital" {"format" "12-hour" "date" "off"}}
///
/// Saved values go back to a module through `set-config` right after it is loaded, so every
/// instance of a module starts with what was last chosen. Like `:session`, the key is not in
/// state.mbt's `owned_keys`, so `save_state` keeps it as it is.
///| Read `:module-config` from state.edn once (nothing without `persist`).
fn DesktopShell::read_module_cfg(self : DesktopShell) -> Unit {
if self.module_cfg_read {
return
}
self.module_cfg_read = true
if !self.persist {
return
}
match lookup_key(read_state_file(), "module-config") {
Some(v) =>
for id, m in parse_module_cfg(v) {
self.module_cfg.set(id, m)
}
None => ()
}
}
///| `{"module" {"key" "value"}}` -> map; entries that aren't strings are skipped.
fn parse_module_cfg(v : @edn.Value) -> Map[String, Map[String, String]] {
let out : Map[String, Map[String, String]] = {}
match v {
Mapping(mods) =>
for e in mods {
match e {
(Text(id), Mapping(kvs)) => {
let m : Map[String, String] = {}
for kv in kvs {
match kv {
(Text(k), Text(val)) => m.set(k, val)
(Text(k), Bool(b)) => m.set(k, if b { "on" } else { "off" })
_ => ()
}
}
out.set(id, m)
}
_ => ()
}
}
_ => ()
}
out
}
///|
fn module_cfg_value(cfg : Map[String, Map[String, String]]) -> @edn.Value {
let mods : Array[(@edn.Value, @edn.Value)] = []
for id, m in cfg {
let kvs : Array[(@edn.Value, @edn.Value)] = []
for k, v in m {
kvs.push((@edn.Value::Text(k), @edn.Value::Text(v)))
}
mods.push((@edn.Value::Text(id), @edn.Value::Mapping(kvs)))
}
@edn.Value::Mapping(mods)
}
///| Hand a freshly loaded module its saved settings.
fn DesktopShell::apply_module_cfg(self : DesktopShell, w : Widget) -> Unit {
self.read_module_cfg()
match self.module_cfg.get(w.module_id) {
Some(m) =>
for k, v in m {
ignore(w.pl.set_config(k, v))
}
None => ()
}
}
///| Remember a module's setting and write `:module-config` (keeping every other key).
fn DesktopShell::remember_module_setting(self : DesktopShell, id : String, key : String, value : String) -> Unit {
self.read_module_cfg()
match self.module_cfg.get(id) {
Some(m) => m.set(key, value)
None => {
let m : Map[String, String] = {}
m.set(key, value)
self.module_cfg.set(id, m)
}
}
if !self.persist {
return
}
let entries : Array[(@edn.Value, @edn.Value)] = []
let mut placed = false
for e in read_state_file() {
match e.0 {
Keyword("module-config") => {
entries.push((e.0, module_cfg_value(self.module_cfg)))
placed = true
}
_ => entries.push(e)
}
}
if !placed {
entries.push((@edn.Value::Keyword("module-config"), module_cfg_value(self.module_cfg)))
}
write_state_file(entries)
}
///| The setting `key` of module `id` as last chosen, if any.
pub fn DesktopShell::module_setting(self : DesktopShell, id : String, key : String) -> String? {
self.read_module_cfg()
match self.module_cfg.get(id) {
Some(m) => m.get(key)
None => None
}
}
///|
test "module settings round-trip through EDN" {
let cfg : Map[String, Map[String, String]] = {
"clock-digital": { "format": "12-hour", "date": "off" },
"clock-binary": { "seconds": "on" },
}
let text = @edn.pretty(module_cfg_value(cfg), layout={ width: 100, indent: 1 })
let back = parse_module_cfg(@edn.read(text))
assert_eq(back.get("clock-digital").unwrap().get("format"), Some("12-hour"))
assert_eq(back.get("clock-digital").unwrap().get("date"), Some("off"))
assert_eq(back.get("clock-binary").unwrap().get("seconds"), Some("on"))
// Hand-written booleans read as on/off.
let hand = parse_module_cfg(@edn.read("{\"x\" {\"a\" true \"b\" false} 3 4}"))
assert_eq(hand.get("x").unwrap().get("a"), Some("on"))
assert_eq(hand.get("x").unwrap().get("b"), Some("off"))
}
+19 -2
View File
@@ -169,11 +169,22 @@ pub(all) struct CatalogEntry {
mut name : String
}
///| One row of the right-click menu: kind 0 replace with module `arg` (a path), 1 remove, 2 open the module manager.
///| One row of the right-click menu. `kind`: 0 replace with module `arg` (a path), 1 remove,
/// 2 open System settings, 3 open the module's own settings, 4 set the module's setting `arg` to
/// `value`, 5 a heading or note (not clickable).
pub(all) struct MenuRow {
label : String
kind : Int
arg : String
/// The setting value a kind-4 row sets.
value : String
/// Draw a tick: the value a kind-4 row sets is the current one.
ticked : Bool
}
///| A plain menu row (no setting value, no tick).
fn menu_row(label : String, kind : Int, arg? : String = "") -> MenuRow {
{ label, kind, arg, value: "", ticked: false }
}
///| The desktop shell: wallpaper + a bottom panel made entirely of wasm modules
@@ -246,6 +257,10 @@ pub(all) struct DesktopShell {
mut menu_hover : Int
mut menu_anchor_x : Int
mut menu_slot : String
/// Each module's own settings (`module-config`), by module id, as saved in state.edn
/// `:module-config`; read on first use.
module_cfg : Map[String, Map[String, String]]
mut module_cfg_read : Bool
/// Panel widget being dragged (slot id, "" if none), press point, and whether it moved past the click threshold.
mut drag_slot : String
/// Panel widget the pointer is over (it gets pointer-move / pointer-leave), "" for none.
@@ -334,6 +349,8 @@ pub fn DesktopShell::new(display : String) -> DesktopShell {
menu_hover: -1,
menu_anchor_x: 0,
menu_slot: "",
module_cfg: {},
module_cfg_read: false,
drag_slot: "",
hover_slot: "",
drag_x0: 0,
@@ -575,7 +592,7 @@ pub fn DesktopShell::cursor_at(self : DesktopShell, x : Int, y : Int) -> UInt {
} else if self.drag_active {
17U // grabbing
} else if self.menu_open && inside(self.menu_rect, x, y) {
if self.menu_hover >= 0 { 4U } else { 1U }
if self.menu_hover >= 0 && self.menu_rows[self.menu_hover].kind != 5 { 4U } else { 1U }
} else if self.popup_at(x, y) is Some(_) {
1U
} else if self.widget_at(x, y) is Some(_) {
+113 -12
View File
@@ -7,8 +7,9 @@
/// `app-launch` for the application menu, `windows` for the task list, `layout-control`
/// for the layout switcher, `inspector` for the Interface Explorer button, and
/// `session-settings`, `wallpaper` and `modules` for the settings module. Managing modules is a
/// tab of that module: the wasm picker ("Modules..." in the panel menu) loads it on demand and
/// opens that tab.
/// tab of that module: "System settings..." in the empty-panel right-click menu loads it on demand
/// and opens that tab. A module may also export `module-config`: its own settings, listed under
/// "Module settings..." in its right-click menu and remembered in state.edn (module_config.mbt).
///| Modules loaded into a fresh panel, in order. `tasks` stretches, so what follows it packs right.
let default_panel : Array[String] = [
@@ -253,6 +254,7 @@ pub fn DesktopShell::load_path(self : DesktopShell, path : String) -> Widget? {
popup_hot: false,
}
self.next_slot = self.next_slot + 1
self.apply_module_cfg(w)
self.widgets.push(w)
if self.screen_w > 0 && !self.panel_fits() {
self.widgets.retain(x => x.slot != w.slot)
@@ -657,8 +659,8 @@ let menu_w : Int = 250
///| Most "Replace with" rows listed.
let max_replace_rows : Int = 8
///| Right-click: on a module its replace/remove menu, on empty panel space the picker entry.
/// True when consumed.
///| Right-click: on a module its menu (Module settings, System settings, replace, remove); on
/// empty panel space System settings (whose Panel tab adds modules). True when consumed.
pub fn DesktopShell::right_click(self : DesktopShell, x : Int, y : Int) -> Bool {
self.ensure_booted()
if self.inspect_on {
@@ -671,25 +673,33 @@ pub fn DesktopShell::right_click(self : DesktopShell, x : Int, y : Int) -> Bool
Some(w) => {
self.name_catalog()
let rows : Array[MenuRow] = []
if w.pl.config_items().length() > 0 {
rows.push(menu_row("Module settings...", 3))
} else {
rows.push(menu_row("No module settings", 5))
}
rows.push(menu_row("System settings...", 2))
// Only modules of the same kind (same WIT world) can stand in for this one.
let world = module_world(w.path)
let mut replace = 0
for e in self.catalog {
if world.length() > 0 &&
e.id != w.module_id &&
e.path != w.path &&
rows.length() < max_replace_rows &&
replace < max_replace_rows &&
module_world(e.path) == world {
rows.push({ label: "Replace with " + e.name, kind: 0, arg: e.path })
rows.push(menu_row("Replace with " + e.name, 0, arg=e.path))
replace = replace + 1
}
}
rows.push({ label: "Remove widget", kind: 1, arg: "" })
rows.push({ label: "Modules...", kind: 2, arg: "" })
rows.push(menu_row("Remove widget", 1))
self.open_menu(w.pl.name, rows, w.slot, x)
true
}
None =>
if inside(self.panel_rect, x, y) {
self.open_menu("Add widget", [{ label: "Modules...", kind: 2, arg: "" }], "", x)
// Empty panel space: System settings on its Panel tab, where modules are added.
self.open_menu("Panel", [menu_row("System settings...", 2, arg="panel")], "", x)
true
} else {
was_open
@@ -697,6 +707,77 @@ pub fn DesktopShell::right_click(self : DesktopShell, x : Int, y : Int) -> Bool
}
}
///| The module's own settings as menu rows: an on/off setting is one row, ticked when on; a
/// setting with choices is a heading and one row per choice, the current one ticked.
fn config_rows(items : Array[@wasm.ConfigItem]) -> Array[MenuRow] {
let rows : Array[MenuRow] = []
for it in items {
if it.choices.length() == 0 {
let on = it.value == "on"
rows.push({ label: it.label, kind: 4, arg: it.key, value: if on { "off" } else { "on" }, ticked: on })
} else {
rows.push(menu_row(it.label, 5))
for c in it.choices {
rows.push({ label: c, kind: 4, arg: it.key, value: c, ticked: c == it.value })
}
}
}
rows
}
///| Show the "Module settings" menu of the module in `slot` (in place of its right-click menu).
fn DesktopShell::open_module_settings(self : DesktopShell, slot : String) -> Unit {
match self.widget_by_slot(slot) {
None => ()
Some(w) => {
let rows = config_rows(w.pl.config_items())
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)
}
}
}
///| Set a setting of the module in `slot` (and of its other instances) and remember it in state.edn.
pub fn DesktopShell::set_module_setting(self : DesktopShell, slot : String, key : String, value : String) -> Unit {
match self.widget_by_slot(slot) {
None => ()
Some(w) => {
self.remember_module_setting(w.module_id, key, value)
for o in self.widgets {
if o.module_id == w.module_id {
ignore(o.pl.set_config(key, value))
}
}
}
}
}
///| The settings the module in `slot` offers, with their current values.
pub fn DesktopShell::slot_config(self : DesktopShell, slot : String) -> Array[@wasm.ConfigItem] {
match self.widget_by_slot(slot) {
Some(w) => w.pl.config_items()
None => []
}
}
///| What the module in `slot` draws in its panel box now.
pub fn DesktopShell::slot_ops(self : DesktopShell, slot : String) -> Array[@types.DrawOp] {
match self.widget_by_slot(slot) {
Some(w) => w.pl.render(w.rect.w, w.rect.h, self.tokens)
None => []
}
}
///| A setting chosen in the module's menu: apply it, remember it, and refresh the menu.
fn DesktopShell::choose_module_setting(self : DesktopShell, slot : String, key : String, value : String) -> Unit {
self.set_module_setting(slot, key, value)
let hover = self.menu_hover
self.open_module_settings(slot)
self.menu_hover = hover
}
///|
fn DesktopShell::open_menu(self : DesktopShell, title : String, rows : Array[MenuRow], slot : String, x : Int) -> Unit {
self.menu_open = true
@@ -731,7 +812,11 @@ 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))
_ => ignore(self.open_module_manager())
2 => ignore(self.open_settings(if row.arg == "panel" { Panel } else { Session }))
3 => self.open_module_settings(slot)
4 => self.choose_module_setting(slot, row.arg, row.value)
// A heading: the menu stays open.
_ => self.menu_open = true
}
}
over
@@ -764,11 +849,27 @@ fn DesktopShell::render_menu(
]
for i = 0; i < n; i = i + 1 {
let top = menu_title_h + i * menu_item_h
let hot = i == self.menu_hover
let row = self.menu_rows[i]
let hot = i == self.menu_hover && row.kind != 5
if hot {
ops.push(@ui.fill_rect(@ui.rect(4, top, menu_w - 8, menu_item_h - 2), tokens.palette.surface_hover, radius=r))
}
ops.push(@ui.text(14, top + menu_item_h / 2, self.menu_rows[i].label, fs - 2, tokens.palette.foreground))
if row.kind == 5 {
ops.push(@ui.text(14, top + menu_item_h / 2, row.label, fs - 3, tokens.palette.foreground_muted))
} else if row.kind == 4 {
// A setting: a check box, filled with the accent colour when ticked.
let cy = top + menu_item_h / 2
if row.ticked {
ops.push(@ui.fill_rect(@ui.rect(16, cy - 6, 12, 12), tokens.palette.accent, radius=3))
ops.push(@ui.line(19, cy, 21, cy + 3, tokens.palette.accent_foreground, width=2))
ops.push(@ui.line(21, cy + 3, 26, cy - 3, tokens.palette.accent_foreground, width=2))
} else {
ops.push(@ui.stroke_rect(@ui.rect(16, cy - 6, 12, 12), tokens.palette.foreground_muted, radius=3))
}
ops.push(@ui.text(36, cy, row.label, fs - 2, tokens.palette.foreground))
} else {
ops.push(@ui.text(14, top + menu_item_h / 2, row.label, fs - 2, tokens.palette.foreground))
}
}
translate(ops, mx, my, out)
}