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