diff --git a/compositor/app/app.mbt b/compositor/app/app.mbt index 67c5c9f..8d0114a 100644 --- a/compositor/app/app.mbt +++ b/compositor/app/app.mbt @@ -131,6 +131,7 @@ pub fn run(args : Array[String]) -> Int { } Some(comp) => { comp.shell.persist = true // remember which modules are loaded in the panel + comp.restore_state() // and the layout mode and recent apps from the last session comp.shell.screen_w = width if gfx_out.length() > 0 && !serve { let pixmap = comp.present_to_gfx(gfx_ctx) diff --git a/compositor/bin/main.mbt b/compositor/bin/main.mbt index e07dd2d..eb8916e 100644 --- a/compositor/bin/main.mbt +++ b/compositor/bin/main.mbt @@ -76,6 +76,7 @@ fn main { match @host.MoonPlasmaCompositor::new(socket_path, theme, width, height) { Some(comp) => { comp.shell.persist = true // remember which modules are loaded in the panel + comp.restore_state() // and the layout mode and recent apps from the last session comp.shell.screen_w = width // the panel can't place widgets before it knows its width if widgets.length() > 0 { ignore(comp.compose_frame()) // boots the panel and sets the theme tokens modules are loaded with diff --git a/compositor/host/compositor.mbt b/compositor/host/compositor.mbt index 5dd9ee5..b9f70d2 100644 --- a/compositor/host/compositor.mbt +++ b/compositor/host/compositor.mbt @@ -312,6 +312,26 @@ pub fn MoonPlasmaCompositor::set_layout_mode( self.layout_mode = mode let actions = self.wm.set_layout_mode(mode, self.work_area) self.apply_actions(actions) + // Remember it (a no-op unless persistence is on). + self.shell.layout_sel = match mode { + @wm.LayoutMode::Tiling => 0 + @wm.LayoutMode::Monocle => 1 + @wm.LayoutMode::Floating => 2 + } + self.shell.save_state() +} + +///| Restore what the last session left (layout mode, recent apps) from state.edn. Call once, +/// after turning `shell.persist` on; a no-op without persistence. +pub fn MoonPlasmaCompositor::restore_state(self : MoonPlasmaCompositor) -> Unit { + let st = self.shell.load_state() + self.shell.apply_state(st) + match st.layout { + Some(0) => self.set_layout_mode(@wm.LayoutMode::Tiling) + Some(1) => self.set_layout_mode(@wm.LayoutMode::Monocle) + Some(2) => self.set_layout_mode(@wm.LayoutMode::Floating) + _ => () + } } ///| diff --git a/compositor/host/input_test.mbt b/compositor/host/input_test.mbt index 2f9c590..d49a2d9 100644 --- a/compositor/host/input_test.mbt +++ b/compositor/host/input_test.mbt @@ -842,3 +842,61 @@ test "the logout button asks first, then ends the session" { ignore(comp.compose_frame()) assert_true(comp.quit_requested) } + +///| Contents of state.edn in the scratch config dir. +fn read_state_edn() -> String { + let dir = match @sys.getenv("XDG_CONFIG_HOME") { + "" => @sys.getenv("HOME") + "/.config" + c => c + } + let raw = @sys.read_file_bytes(dir + "/moon-plasma/state.edn") + let sb = StringBuilder::new() + for i = 0; i < raw.length(); i = i + 1 { + sb.write_char(Int::unsafe_to_char(raw[i].to_int())) // the file is ASCII + } + sb.to_string() +} + +///| +fn write_state_edn(text : String) -> Unit { + let dir = match @sys.getenv("XDG_CONFIG_HOME") { + "" => @sys.getenv("HOME") + "/.config" + c => c + } + ignore(@sys.mkdir_p(dir + "/moon-plasma")) + ignore(@sys.write_file_bytes(dir + "/moon-plasma/state.edn", @sys.str_to_utf8_bytes(text))) +} + +///| +test "the layout mode and recent apps are saved to state.edn and restored; other keys survive" { + // Never touch the user's real config: only run when the test runner gave us a scratch dir. + if @sys.getenv("MOON_PLASMA_TEST_CONFIG") == "1" { + write_state_edn(";; hand-written\n{:note \"keep me\" :layout :tiling}\n") + let a = new_comp("state-a") + a.shell.persist = true + a.restore_state() // nothing but :layout :tiling saved: stays tiling + assert_eq(a.layout_mode, @wm.LayoutMode::Tiling) + // Changing the layout writes it (and keeps the key we don't own). + a.set_layout_mode(@wm.LayoutMode::Floating) + a.shell.recents.push("konsole") + a.shell.recents.push("foot") + a.shell.save_state() + let text = read_state_edn() + assert_true(text.contains(":layout :floating")) + assert_true(text.contains("\"konsole\"") && text.contains("\"foot\"")) + assert_true(text.contains(":note \"keep me\"")) + // A new compositor starts where the last one left off. + let b = new_comp("state-b") + assert_eq(b.layout_mode, @wm.LayoutMode::Tiling) // persistence is off until asked + b.shell.persist = true + b.restore_state() + assert_eq(b.layout_mode, @wm.LayoutMode::Floating) + assert_eq(b.shell.recents, ["konsole", "foot"]) + // Garbage in the file is ignored, not fatal. + write_state_edn("{:layout") + let c = new_comp("state-c") + c.shell.persist = true + c.restore_state() + assert_eq(c.layout_mode, @wm.LayoutMode::Tiling) + } +} diff --git a/compositor/shell/moon.pkg b/compositor/shell/moon.pkg index fd00693..5f5b97d 100644 --- a/compositor/shell/moon.pkg +++ b/compositor/shell/moon.pkg @@ -11,6 +11,7 @@ import { "moonplasma/desktop/lib/timefmt" @timefmt, "moonplasma/desktop/lib/launcher" @launcher, "moonplasma/desktop/compositor/scene" @scene, + "jqlssg/moonedn" @edn, "moonplasma/desktop/compositor/sys" @sys, "moonplasma/desktop/compositor/renderer" @renderer, "moonplasma/desktop/compositor/wasm" @wasm, diff --git a/compositor/shell/shell.mbt b/compositor/shell/shell.mbt index 6169425..72f4e96 100644 --- a/compositor/shell/shell.mbt +++ b/compositor/shell/shell.mbt @@ -309,7 +309,7 @@ pub fn DesktopShell::new(display : String) -> DesktopShell { panel_rect: zero, comps: [], wit_cache: {}, - recents: load_recents(), + recents: [], } sh.order_apps() sh @@ -410,11 +410,7 @@ fn DesktopShell::note_recent(self : DesktopShell, id : String) -> Unit { for r in next { self.recents.push(r) } - let sb = StringBuilder::new() - for r in self.recents { - sb.write_string(r + "\n") - } - ignore(@sys.write_file_bytes(recents_path(), @utf8.encode(sb.to_string()))) + self.save_state() self.order_apps() } diff --git a/compositor/shell/state.mbt b/compositor/shell/state.mbt new file mode 100644 index 0000000..13c0768 --- /dev/null +++ b/compositor/shell/state.mbt @@ -0,0 +1,155 @@ +///| Saved state: what the desktop remembers between sessions, as EDN in +/// $XDG_CONFIG_HOME/moon-plasma/state.edn (next to panel.edn, which holds the module list): +/// +/// {:layout :tiling ; the Tile | Float switcher: :tiling, :monocle or :floating +/// :recent ["foot" "dolphin"]} ; the launcher's recently started apps, newest first +/// +/// Saving merges into what is already in the file, so keys this build doesn't know (a future +/// widget's state, hand-written notes) survive. Everything is a no-op unless `persist` is on +/// (the compositor binary turns it on; tests leave it off). The Interface Explorer toggle is not +/// saved on purpose: while it is on, clicks don't reach windows, which is no way to start a session. + +///| What state.edn held. A field is None when the file doesn't say. +pub(all) struct SavedState { + /// 0 tiling, 1 monocle, 2 floating (the order of the layout list). + layout : Int? + recent : Array[String]? +} + +///| +fn state_path() -> String { + panel_config_dir() + "/state.edn" +} + +///| +let layout_names : Array[String] = ["tiling", "monocle", "floating"] + +///| The keys this build owns; everything else in the file is kept as it is. +let owned_keys : Array[String] = ["layout", "recent"] + +///| +fn read_state_file() -> Array[(@edn.Value, @edn.Value)] { + let text = @utf8.decode_lossy(@sys.read_file_bytes(state_path())) + if text.length() == 0 { + return [] + } + let parsed : @edn.Value? = try Some(@edn.read(text)) catch { _ => None } + match parsed { + Some(Mapping(entries)) => entries + _ => [] // unreadable or not a map: start afresh rather than refuse to run + } +} + +///| +fn lookup_key( + entries : Array[(@edn.Value, @edn.Value)], + name : String, +) -> @edn.Value? { + for e in entries { + match e.0 { + Keyword(k) if k == name => return Some(e.1) + _ => () + } + } + None +} + +///| Read state.edn. Without `persist` nothing is read. +pub fn DesktopShell::load_state(self : DesktopShell) -> SavedState { + if !self.persist { + return { layout: None, recent: None } + } + let entries = read_state_file() + let layout = match lookup_key(entries, "layout") { + Some(Keyword(k)) => { + let mut found : Int? = None + for i = 0; i < layout_names.length(); i = i + 1 { + if layout_names[i] == k { + found = Some(i) + } + } + found + } + _ => None + } + let recent = match lookup_key(entries, "recent") { + Some(Vector(items)) => { + let out : Array[String] = [] + for it in items { + match it { + Text(s) => out.push(s) + _ => () + } + } + Some(out) + } + _ => None + } + // Older builds kept the recents in a plain-text file under ~/.cache. Migrate them once: only + // when there is no state.edn yet (its first save then carries them over). + let recent = match recent { + Some(_) => recent + None => + if @sys.read_file_bytes(state_path()).length() == 0 { + let old = load_recents() + if old.length() > 0 { Some(old) } else { None } + } else { + None + } + } + { layout, recent } +} + +///| Take the saved recents (the layout is the compositor's to apply). +pub fn DesktopShell::apply_state(self : DesktopShell, st : SavedState) -> Unit { + match st.recent { + Some(r) => { + self.recents.clear() + for id in r { + self.recents.push(id) + } + self.order_apps() + } + None => () + } +} + +///| Write the current layout mode and recents, keeping any other keys. `layout_sel` is kept in +/// step with the window manager by the compositor. A no-op unless `persist` is on. +pub fn DesktopShell::save_state(self : DesktopShell) -> Unit { + if !self.persist { + return + } + let entries : Array[(@edn.Value, @edn.Value)] = [] + for e in read_state_file() { + let owned = match e.0 { + Keyword(k) => owned_keys.contains(k) + _ => false + } + if !owned { + entries.push(e) + } + } + let layout = if self.layout_sel >= 0 && self.layout_sel < layout_names.length() { + layout_names[self.layout_sel] + } else { + "tiling" + } + let recent : Array[@edn.Value] = [] + for r in self.recents { + recent.push(@edn.Value::Text(r)) + } + entries.insert(0, (@edn.Value::Keyword("layout"), @edn.Value::Keyword(layout))) + entries.insert(1, (@edn.Value::Keyword("recent"), @edn.Value::Vector(recent))) + let body : String? = try Some(@edn.pretty(@edn.Value::Mapping(entries), layout={ width: 100, indent: 1 })) catch { _ => None } + match body { + Some(b) => { + let dir = panel_config_dir() + if @sys.mkdir_p(dir) { + let text = ";; Moon Plasma state: restored at the next start\n" + b + "\n" + ignore(@sys.write_file_bytes(state_path(), @utf8.encode(text))) + } + } + None => () + } +}