Save the layout mode and recent apps as EDN (state.edn) and restore them at startup
CI / test (push) Failing after 30s
CI / test (push) Failing after 30s
state.edn sits next to panel.edn in $XDG_CONFIG_HOME/moon-plasma: {:layout :tiling|:monocle|:floating
:recent [...]}. It is written whenever the layout changes (the Tile | Float switcher, or anything else)
or an app is launched, and read at startup by both the native and wasm compositors. Saving merges
into the existing file, so keys this build doesn't know survive; unreadable files are ignored. Uses
jqlssg/moonedn for reading and writing. The launcher's recents used to live in a plain-text file under
~/.cache; they are migrated once when there is no state.edn yet. The Interface Explorer toggle is
deliberately not restored (while on, clicks don't reach windows).
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0ba975da67
commit
b50e79acaa
7 files changed
+238
-6
No files matched your search
@@ -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)
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user