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"))
}