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

+78 -2
View File
@@ -327,7 +327,7 @@ test "right-click a panel module to replace it in place, or remove it" {
let bin = find_row("Replace with Binary Clock")
assert_true(bin >= 0)
assert_true(find_row("Remove widget") >= 0)
assert_true(find_row("Module manager...") >= 0)
assert_true(find_row("System settings...") >= 0)
// Hover the row (hand cursor), click it: the binary clock takes the digital clock's place.
let ix = m.x + m.w / 2
let iy = m.y + 26 + 28 * bin + 14
@@ -353,6 +353,82 @@ test "right-click a panel module to replace it in place, or remove it" {
assert_eq(comp.shell.slots().length(), before.length() - 1)
}
///|
test "right-click a module for its own settings: they apply at once and are remembered" {
let comp = new_comp("modcfg")
assert_true(comp.shell.load_module("clock-digital"))
ignore(comp.compose_frame())
let clock = rect_of(comp, "clock-digital")
assert_true(clock.w > 0)
let row_y = fn(k : Int) { comp.shell.menu_rect.y + 26 + 28 * k + 14 }
let find_row = fn(label : String) {
let mut i = -1
for k = 0; k < comp.shell.menu_rows.length(); k = k + 1 {
if comp.shell.menu_rows[k].label == label {
i = k
}
}
i
}
let time_text = fn() {
let mut t = ""
for op in comp.shell.slot_ops(comp.shell.slot_of("clock-digital")) {
match op {
Text(x) => if t == "" { t = x.text }
_ => ()
}
}
t
}
assert_false(time_text().has_suffix("M")) // 24-hour by default
click_at(comp, clock.x + clock.w / 2, clock.y + clock.h / 2, 1U, btn=273U)
assert_true(comp.shell.menu_open)
assert_eq(comp.shell.menu_rows[0].label, "Module settings...")
assert_eq(comp.shell.menu_rows[1].label, "System settings...")
click_at(comp, comp.shell.menu_rect.x + 60, row_y(0), 10U)
// The module's own menu: a "Time format" heading, its two choices, and the "Show date" switch.
assert_true(comp.shell.menu_open)
assert_eq(comp.shell.menu_title, "Digital Clock settings")
let h12 = find_row("12-hour")
assert_true(h12 > 0 && find_row("Time format") == h12 - 2)
assert_true(comp.shell.menu_rows[h12 - 1].ticked) // 24-hour
click_at(comp, comp.shell.menu_rect.x + 60, row_y(h12), 20U)
assert_true(comp.shell.menu_open) // stays open, now ticking 12-hour
assert_true(comp.shell.menu_rows[find_row("12-hour")].ticked)
assert_true(time_text().has_suffix("AM") || time_text().has_suffix("PM"))
assert_eq(comp.shell.module_setting("clock-digital", "format"), Some("12-hour"))
// A heading does nothing; Escape closes.
click_at(comp, comp.shell.menu_rect.x + 60, row_y(find_row("Time format")), 30U)
assert_true(comp.shell.menu_open)
comp.handle_input(@nested.InputEvent::Key(1U, true), 40U)
assert_false(comp.shell.menu_open)
// A second instance of the module starts with the chosen setting.
assert_true(comp.shell.unload_slot(comp.shell.slot_of("clock-digital")))
assert_true(comp.shell.load_module("clock-digital"))
ignore(comp.compose_frame())
assert_true(time_text().has_suffix("AM") || time_text().has_suffix("PM"))
}
///|
test "module settings are saved in state.edn and handed back at the next start" {
if @sys.getenv("CHANDRA_TEST_CONFIG") == "1" {
let theme = @scene.ThemeTokens::moon_night()
let a = @shell.DesktopShell::new("x")
a.persist = true
a.set_output(theme, 1280, 720)
assert_true(a.load_module("clock-binary"))
a.set_module_setting(a.slot_of("clock-binary"), "seconds", "off")
a.save_state() // the rest of state.edn is rewritten: :module-config stays
let b = @shell.DesktopShell::new("x")
b.persist = true
b.set_output(theme, 1280, 720)
assert_eq(b.module_setting("clock-binary", "seconds"), Some("off"))
let slot = b.slot_of("clock-binary")
assert_true(slot != "")
assert_eq(b.slot_config(slot)[0].value, "off")
}
}
///|
test "tapping Super toggles the launcher; Super+key does not" {
let comp = new_comp("super")
@@ -404,7 +480,7 @@ test "the wasm picker opens the settings module's Panel tab, which loads and unl
let panel = comp.shell.panel_rect
click_at(comp, 1, panel.y + 5, 1U, btn=273U)
assert_true(comp.shell.menu_open)
assert_eq(comp.shell.menu_rows[0].label, "Modules...")
assert_eq(comp.shell.menu_rows[0].label, "System settings...")
let m = comp.shell.menu_rect
click_at(comp, m.x + m.w / 2, m.y + 26 + 14, 10U)
assert_eq(comp.shell.slots().length(), n0) // nothing new was loaded for it
+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)
}
+24
View File
@@ -182,6 +182,30 @@ pub fn Plasmoid::on_event(self : Plasmoid, popup : Bool, ev : PlasmoidEvent) ->
@hostpl.on_event(self.handle.reinterpret_as_uint(), popup, e)
}
///| One of a module's own settings (the WIT `module-config.config-item`): `choices` empty means
/// an on/off switch whose value is "on" or "off".
pub(all) struct ConfigItem {
key : String
label : String
choices : Array[String]
value : String
} derive(Eq, Debug)
///| The module's settings for its right-click menu; empty when it doesn't export `module-config`.
pub fn Plasmoid::config_items(self : Plasmoid) -> Array[ConfigItem] {
@hostpl.config_items(self.handle.reinterpret_as_uint()).map(c => {
key: c.key,
label: c.label,
choices: c.choices,
value: c.value,
})
}
///| Set one of the module's settings. True when the widget asks for a redraw.
pub fn Plasmoid::set_config(self : Plasmoid, key : String, value : String) -> Bool {
@hostpl.set_config(self.handle.reinterpret_as_uint(), key, value)
}
///|
pub fn Plasmoid::imports(self : Plasmoid) -> Array[String] {
self.imports
+105 -27
View File
@@ -143,6 +143,15 @@ let ex_post_popup : Int = 6
///|
let ex_realloc : Int = 7
///| The optional `module-config` exports (absent in most modules: calls then return -1).
let ex_cfg_items : Int = 8
///|
let ex_post_cfg_items : Int = 9
///|
let ex_set_cfg : Int = 10
///| The 64-byte `theme.tokens` record (name left empty) the guest reads through get-tokens.
pub fn pack_tokens(t : @theme.Tokens) -> Bytes {
let b = FixedArray::make(64, b'\x00')
@@ -513,37 +522,106 @@ pub fn Plasmoid::on_event(self : Plasmoid, popup : Bool, ev : PlasmoidEvent) ->
PointerUp(x, y) => c_wasm_call(self.handle, ex_onevent, target, 2, x, y)
PointerLeave => c_wasm_call(self.handle, ex_onevent, target, 3, 0, 0)
FocusLost => c_wasm_call(self.handle, ex_onevent, target, 4, 0, 0)
KeyDown(k) => {
// The guest owns a UTF-16 copy of the key name.
let b : Array[Byte] = []
let mut units = 0
for ch in k {
let c = ch.to_int()
if c >= 0x10000 {
let v = c - 0x10000
let hi = 0xD800 + (v >> 10)
let lo = 0xDC00 + (v & 0x3FF)
b.push((hi & 0xFF).to_byte())
b.push((hi >> 8).to_byte())
b.push((lo & 0xFF).to_byte())
b.push((lo >> 8).to_byte())
units = units + 2
} else {
b.push((c & 0xFF).to_byte())
b.push((c >> 8).to_byte())
units = units + 1
}
KeyDown(k) =>
match self.guest_str(k) {
None => -1
Some((ptr, units)) => c_wasm_call(self.handle, ex_onevent, target, 5, ptr, units)
}
let ptr = c_wasm_call(self.handle, ex_realloc, 0, 0, 2, if units * 2 < 4 { 4 } else { units * 2 })
if ptr < 0 {
-1
} else {
ignore(c_wasm_write(self.handle, ptr, Bytes::from_array(b), b.length()))
c_wasm_call(self.handle, ex_onevent, target, 5, ptr, units)
}
r == 1
}
///| Copy `s` into guest memory as UTF-16 (the guest owns the copy): (pointer, code units).
fn Plasmoid::guest_str(self : Plasmoid, s : String) -> (Int, Int)? {
let b : Array[Byte] = []
let mut units = 0
for ch in s {
let c = ch.to_int()
if c >= 0x10000 {
let v = c - 0x10000
let hi = 0xD800 + (v >> 10)
let lo = 0xDC00 + (v & 0x3FF)
b.push((hi & 0xFF).to_byte())
b.push((hi >> 8).to_byte())
b.push((lo & 0xFF).to_byte())
b.push((lo >> 8).to_byte())
units = units + 2
} else {
b.push((c & 0xFF).to_byte())
b.push((c >> 8).to_byte())
units = units + 1
}
}
let ptr = c_wasm_call(self.handle, ex_realloc, 0, 0, 2, if units * 2 < 4 { 4 } else { units * 2 })
if ptr <= 0 {
return None
}
if units > 0 {
ignore(c_wasm_write(self.handle, ptr, Bytes::from_array(b), b.length()))
}
Some((ptr, units))
}
///| One of a module's own settings (the WIT `module-config.config-item`): `choices` empty means
/// an on/off switch whose value is "on" or "off".
pub(all) struct ConfigItem {
key : String
label : String
choices : Array[String]
value : String
} derive(Eq, Debug)
///| The module's settings for its right-click menu; empty when it doesn't export `module-config`.
pub fn Plasmoid::config_items(self : Plasmoid) -> Array[ConfigItem] {
let out : Array[ConfigItem] = []
let ra = c_wasm_call(self.handle, ex_cfg_items, 0, 0, 0, 0)
if ra <= 0 {
return out
}
match self.mem(ra, 8) {
None => ()
Some(hdr) => {
let base = rd32(hdr, 0)
let n = rd32(hdr, 4)
if n > 0 && n <= 64 {
match self.mem(base, n * 32) {
None => ()
Some(b) =>
for i = 0; i < n; i = i + 1 {
let o = i * 32
let choices : Array[String] = []
let cn = rd32(b, o + 20)
if cn > 0 && cn <= 64 {
match self.mem(rd32(b, o + 16), cn * 8) {
None => ()
Some(cb) =>
for k = 0; k < cn; k = k + 1 {
choices.push(self.str(rd32(cb, k * 8), rd32(cb, k * 8 + 4)))
}
}
}
out.push({
key: self.str(rd32(b, o), rd32(b, o + 4)),
label: self.str(rd32(b, o + 8), rd32(b, o + 12)),
choices,
value: self.str(rd32(b, o + 24), rd32(b, o + 28)),
})
}
}
}
}
}
r == 1
ignore(c_wasm_call(self.handle, ex_post_cfg_items, ra, 0, 0, 0))
out
}
///| Set one of the module's settings. True when the widget asks for a redraw; false when it has no
/// `module-config` export, ignored the value, or trapped.
pub fn Plasmoid::set_config(self : Plasmoid, key : String, value : String) -> Bool {
match (self.guest_str(key), self.guest_str(value)) {
(Some((kp, kn)), Some((vp, vn))) => c_wasm_call(self.handle, ex_set_cfg, kp, kn, vp, vn) == 1
_ => false
}
}
///| Host interfaces (capabilities) the module's binary imports, e.g. ["theme", "clock"].
+6 -1
View File
@@ -16,7 +16,8 @@
#include "moonbit.h"
#define MAX_INST 512
enum { EX_INFO, EX_RENDER, EX_ONEVENT, EX_POPUP, EX_POST_INFO, EX_POST_RENDER, EX_POST_POPUP, EX_REALLOC, EX_N };
enum { EX_INFO, EX_RENDER, EX_ONEVENT, EX_POPUP, EX_POST_INFO, EX_POST_RENDER, EX_POST_POPUP, EX_REALLOC,
EX_CFG_ITEMS, EX_POST_CFG_ITEMS, EX_SET_CFG, EX_N };
static const char *ex_names[EX_N] = {
"chandra:desktop/plasmoid@0.1.0#info",
"chandra:desktop/plasmoid@0.1.0#render",
@@ -26,6 +27,10 @@ static const char *ex_names[EX_N] = {
"cabi_post_chandra:desktop/plasmoid@0.1.0#render",
"cabi_post_chandra:desktop/plasmoid@0.1.0#popup",
"cabi_realloc",
/* optional: chandra:desktop/module-config (the module's own right-click settings) */
"chandra:desktop/module-config@0.1.0#config-items",
"cabi_post_chandra:desktop/module-config@0.1.0#config-items",
"chandra:desktop/module-config@0.1.0#set-config",
};
typedef struct Inst {
+27
View File
@@ -93,6 +93,33 @@ fn drain() -> Array[HostRequest] {
out
}
///|
test "a module's own settings (module-config) are listed and set through wasmer" {
let p = load_module("clock-digital")
let items = p.config_items()
assert_eq(items.length(), 2)
assert_eq(items[0], { key: "format", label: "Time format", choices: ["24-hour", "12-hour"], value: "24-hour" })
assert_eq(items[1].choices.length(), 0) // an on/off switch
assert_true(p.set_config("format", "12-hour"))
assert_false(p.set_config("format", "12-hour")) // unchanged
assert_false(p.set_config("bogus", "x"))
assert_eq(p.config_items()[0].value, "12-hour")
let mut ampm = false
for op in p.render(p.pref_w, p.pref_h, test_tokens()) {
match op {
Text(t) => if t.text.has_suffix("AM") || t.text.has_suffix("PM") { ampm = true }
_ => ()
}
}
assert_true(ampm)
p.unload()
// Modules without the export have no settings, and setting one is a no-op.
let l = load_module("layout")
assert_eq(l.config_items().length(), 0)
assert_false(l.set_config("format", "12-hour"))
l.unload()
}
///|
test "task list module draws the host's windows and asks to focus one" {
let p = load_module("tasks")