Save the layout mode and recent apps as EDN (state.edn) and restore them at startup
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:
nandiandClaude Sonnet 5.5 committed 2026-11-25 02:52:07 -08:00
1 parent 0ba975da67
commit b50e79acaa
7 files changed
+238 -6

No files matched your search

+155
View File
@@ -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 => ()
}
}