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

+12
View File
@@ -166,6 +166,18 @@ impl chandra::host::plasmoids::Host for State {
self.pl().on_event(handle, on_popup, event)
}
fn config_items(&mut self, handle: u32) -> Vec<chandra::host::plasmoids::ConfigItem> {
self.pl()
.config_items(handle)
.into_iter()
.map(|c| chandra::host::plasmoids::ConfigItem { key: c.key, label: c.label, choices: c.choices, value: c.value })
.collect()
}
fn set_config(&mut self, handle: u32, key: String, value: String) -> bool {
self.pl().set_config(handle, &key, &value)
}
fn publish_table(&mut self, which: u32, rows: Vec<u8>) {
if let Some(t) = self.pl().shared.borrow_mut().tables.get_mut(which as usize) {
*t = rows;
+51 -3
View File
@@ -22,6 +22,15 @@ pub mod ph {
additional_derives: [PartialEq],
});
}
/// The optional `module-config` export (wit/worlds.wit `configurable`): a module's own settings
/// for its right-click menu. Bound separately so modules without it still instantiate.
pub mod cfg {
wasmtime::component::bindgen!({
path: "../wit",
world: "configurable",
});
}
pub use cfg::exports::chandra::desktop::module_config::ConfigItem;
use ph::exports::chandra::desktop as ex;
use ph::chandra::desktop as d;
@@ -337,6 +346,8 @@ impl d::system::Host for PState {
struct Loaded {
store: Store<PState>,
inst: ph::PlasmoidHost,
/// Present when the module exports `module-config`.
cfg: Option<cfg::Configurable>,
}
/// A file-chooser dialog in flight (zenity on the host session).
@@ -389,13 +400,16 @@ impl Runtime {
}
}
let mut store = Store::new(&self.engine, PState { shared: self.shared.clone() });
let inst = ph::PlasmoidHost::instantiate(&mut store, &component, &self.linker)
.map_err(|_| "wasm runtime error".to_string())?;
let instance =
self.linker.instantiate(&mut store, &component).map_err(|_| "wasm runtime error".to_string())?;
let inst = ph::PlasmoidHost::new(&mut store, &instance)
.map_err(|_| "no plasmoid (chandra:desktop/plasmoid) in this module".to_string())?;
let cfg = cfg::Configurable::new(&mut store, &instance).ok();
let info = inst
.chandra_desktop_plasmoid()
.call_info(&mut store)
.map_err(|_| "widget trapped in info()".to_string())?;
let l = Loaded { store, inst };
let l = Loaded { store, inst, cfg };
let handle = match self.slots.iter().position(|s| s.is_none()) {
Some(h) => {
self.slots[h] = Some(l);
@@ -430,6 +444,20 @@ impl Runtime {
l.inst.chandra_desktop_plasmoid().call_popup(&mut l.store).ok().flatten()
}
/// The module's settings; empty without `module-config` (or when it traps).
pub fn config_items(&mut self, handle: u32) -> Vec<ConfigItem> {
let Some(l) = self.get(handle) else { return Vec::new() };
let Some(c) = &l.cfg else { return Vec::new() };
c.chandra_desktop_module_config().call_config_items(&mut l.store).unwrap_or_default()
}
/// Set one of the module's settings; true when it wants a redraw.
pub fn set_config(&mut self, handle: u32, key: &str, value: &str) -> bool {
let Some(l) = self.get(handle) else { return false };
let Some(c) = &l.cfg else { return false };
c.chandra_desktop_module_config().call_set_config(&mut l.store, key, value).unwrap_or(false)
}
pub fn on_event(&mut self, handle: u32, on_popup: bool, ev: d::types::InputEvent) -> bool {
let Some(l) = self.get(handle) else { return false };
let target = if on_popup { ex::plasmoid::Surface::Popup } else { ex::plasmoid::Surface::Widget };
@@ -557,6 +585,26 @@ mod tests {
assert!(rt.load(junk.to_str().unwrap()).is_err());
}
#[test]
fn module_settings_are_listed_and_set_when_the_module_exports_them() {
let mut rt = runtime();
let c = rt.load(&module("clock-digital")).expect("clock loads");
let items = rt.config_items(c.handle);
assert_eq!(items.len(), 2);
assert_eq!(items[0].key, "format");
assert_eq!(items[0].choices, vec!["24-hour".to_string(), "12-hour".to_string()]);
assert_eq!(items[0].value, "24-hour");
assert!(rt.set_config(c.handle, "format", "12-hour"));
assert!(!rt.set_config(c.handle, "bogus", "x"));
assert_eq!(rt.config_items(c.handle)[0].value, "12-hour");
let ops = rt.render(c.handle, d::types::Size { w: 120, h: 36 });
assert!(texts(&ops).iter().any(|t| t.ends_with("AM") || t.ends_with("PM")));
// A module without the export: no settings, and setting one does nothing.
let l = rt.load(&module("layout")).expect("layout loads");
assert!(rt.config_items(l.handle).is_empty());
assert!(!rt.set_config(l.handle, "format", "12-hour"));
}
#[test]
fn layout_module_follows_the_published_mode_and_sets_it() {
let mut rt = runtime();