CI / test (push) Failing after 18s
clock-digital12 and clock-digital had become the same module (same lib/clock code, same right-click settings), differing only in the starting format. Keep one: the Digital Clock now starts in 12-hour mode with AM/PM, and its "Time format" setting switches to 24-hour. - components/clock-digital12 is gone (and from scripts/components.txt); lib/clock drops digital12_info/digital12_config. - The default panel loads clock-digital. - "clock-digital12" stays an alias (find_module, slot_of, saved panels) and is never listed in the module picker, even if an old .wasm is still installed. - panel.edn gets :version 2. A version-1 panel is migrated once: the old 12-hour clock becomes the Digital Clock with its settings (state.edn :module-config) moved over in 12-hour mode; a panel that had the old 24-hour Digital Clock keeps 24-hour. The file is rewritten afterwards. - Tests updated, plus a migration test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DPL5hgnZsS91fwCreut8FL
169 lines
5.7 KiB
MoonBit
169 lines
5.7 KiB
MoonBit
///| 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)
|
|
}
|
|
}
|
|
self.write_module_cfg()
|
|
}
|
|
|
|
///| Write `:module-config` to state.edn, keeping every other key (a no-op without `persist`).
|
|
fn DesktopShell::write_module_cfg(self : DesktopShell) -> Unit {
|
|
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)
|
|
}
|
|
|
|
///| A panel saved before the 12-hour clock (clock-digital12) became part of the Digital Clock
|
|
/// (panel.edn version 1, `saved` its module list as written): keep each clock as it was. The
|
|
/// 12-hour clock's settings become the Digital Clock's, starting from 12-hour mode; a panel that
|
|
/// had only the Digital Clock keeps it in 24-hour mode (its old default) unless a format was chosen.
|
|
/// A panel with both now has two Digital Clocks sharing the Digital Clock's settings.
|
|
fn DesktopShell::migrate_module_cfg(self : DesktopShell, saved : Array[String]) -> Unit {
|
|
self.read_module_cfg()
|
|
let had12 = saved.contains("clock-digital12")
|
|
let had24 = saved.contains("clock-digital")
|
|
let old12 = self.module_cfg.get("clock-digital12")
|
|
if had12 && !had24 {
|
|
let m : Map[String, String] = { "format": "12-hour" }
|
|
match old12 {
|
|
Some(o) => for k, v in o { m.set(k, v) }
|
|
None => ()
|
|
}
|
|
self.module_cfg.set("clock-digital", m)
|
|
} else if had24 && !had12 {
|
|
match self.module_cfg.get("clock-digital") {
|
|
Some(m) => if !m.contains("format") { m.set("format", "24-hour") }
|
|
None => self.module_cfg.set("clock-digital", { "format": "24-hour" })
|
|
}
|
|
}
|
|
if (had12 && !had24) || (had24 && !had12) || old12 is Some(_) {
|
|
self.module_cfg.remove("clock-digital12")
|
|
self.write_module_cfg()
|
|
}
|
|
}
|
|
|
|
///| 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"))
|
|
}
|