Restore the session on login: reopen the windows the last session ended with
CI / test (push) Failing after 20s

- state.edn gets :session [{:app "foot"} {:app "org.gnome.Nautilus" :minimized true}]: the open
  windows by app id, in opening order, with whether each was minimized or maximized. Dialogs
  (xdg_toplevel.set_parent, now tracked by the Wayland server) are left out.
- Written at the end of a session (logout, or the display/window going away) and, against
  crashes, whenever the set of windows changes and then holds for 3 s; that delay also keeps a
  shutdown, where apps die a moment before the compositor, from saving an empty desktop.
- At start (both the native binary and the wasm compositor) each saved app is launched through
  its .desktop file: the app id is matched to the desktop file id, StartupWMClass (now parsed),
  case-insensitively, or by its last dotted part. Restored apps don't count as recent launches.
  A launched app stays in the session until its window shows up (30 s at most), and that window
  gets its minimized/maximized state back; placement is the existing layout mode and per-app
  floating geometry.
- --no-restore, or :restore-session false in state.edn, starts with an empty desktop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hjkHnYqipToGkWFC9P
This commit is contained in:
Claude committed 2026-11-25 18:31:31 +00:00
1 parent f7f90e1c41
commit 46780c399b
10 files changed
+435 -2

No files matched your search

+121 -1
View File
@@ -5,7 +5,11 @@
/// :recent ["foot" "dolphin"] ; the launcher's recently started apps, newest first
/// :windows {"foot" {:x 80 :y 60 :w 640 :h 480}} ; floating windows' last place, by app id
/// :desktop-card {:x 900 :y 24} ; where the desktop card was dragged to (absent: default spot)
/// :wallpaper :aurora} ; the wallpaper module's choice (absent: :plasma-glow)
/// :wallpaper :aurora ; the wallpaper module's choice (absent: :plasma-glow)
/// :session [{:app "foot"} {:app "org.gnome.Nautilus" :minimized true}]
/// ; the windows open when the last session ended, reopened at
/// ; the next start (see session.mbt in compositor/host)
/// :restore-session false} ; hand-written: don't reopen them (absent: reopen)
///
/// 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
@@ -236,6 +240,11 @@ pub fn DesktopShell::save_state(self : DesktopShell) -> Unit {
if self.wallpaper_sel > 0 && self.wallpaper_sel < wallpaper_keys.length() {
entries.insert(2, (@edn.Value::Keyword("wallpaper"), @edn.Value::Keyword(wallpaper_keys[self.wallpaper_sel])))
}
write_state_file(entries)
}
///|
fn write_state_file(entries : Array[(@edn.Value, @edn.Value)]) -> Unit {
let body : String? = try Some(@edn.pretty(@edn.Value::Mapping(entries), layout={ width: 100, indent: 1 })) catch { _ => None }
match body {
Some(b) => {
@@ -280,3 +289,114 @@ pub fn DesktopShell::remember_window(self : DesktopShell, app_id : String, x : I
pub fn DesktopShell::saved_window(self : DesktopShell, app_id : String) -> (Int, Int, Int, Int)? {
if self.persist { self.win_geoms.get(app_id) } else { None }
}
///| One window of a saved session: the app id its window carried and how it was left.
pub(all) struct SessionEntry {
app : String
minimized : Bool
maximized : Bool
} derive(Eq, Show, Debug)
///| Parse a `:session` vector; entries without an app id are skipped.
fn parse_session(v : @edn.Value) -> Array[SessionEntry] {
let out : Array[SessionEntry] = []
match v {
Vector(items) =>
for it in items {
match it {
Mapping(m) =>
match lookup_key(m, "app") {
Some(Text(app)) if app.length() > 0 => {
let flag = fn(k : String) { lookup_key(m, k) is Some(Bool(true)) }
out.push({ app, minimized: flag("minimized"), maximized: flag("maximized") })
}
_ => ()
}
Text(app) if app.length() > 0 => out.push({ app, minimized: false, maximized: false })
_ => ()
}
}
_ => ()
}
out
}
///|
fn session_value(entries : Array[SessionEntry]) -> @edn.Value {
let items : Array[@edn.Value] = []
for e in entries {
let m : Array[(@edn.Value, @edn.Value)] = [(@edn.Value::Keyword("app"), @edn.Value::Text(e.app))]
if e.minimized {
m.push((@edn.Value::Keyword("minimized"), @edn.Value::Bool(true)))
}
if e.maximized {
m.push((@edn.Value::Keyword("maximized"), @edn.Value::Bool(true)))
}
items.push(@edn.Value::Mapping(m))
}
@edn.Value::Vector(items)
}
///| The windows the last session ended with, to reopen. Empty without `persist`, or when
/// state.edn says `:restore-session false`.
pub fn DesktopShell::load_session(self : DesktopShell) -> Array[SessionEntry] {
if !self.persist {
return []
}
let entries = read_state_file()
if lookup_key(entries, "restore-session") is Some(Bool(false)) {
return []
}
match lookup_key(entries, "session") {
Some(v) => parse_session(v)
None => []
}
}
///| Write the open windows as `:session`, keeping every other key. A no-op without `persist`.
pub fn DesktopShell::save_session(self : DesktopShell, session : Array[SessionEntry]) -> 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("session") => {
entries.push((e.0, session_value(session)))
placed = true
}
_ => entries.push(e)
}
}
if !placed {
entries.push((@edn.Value::Keyword("session"), session_value(session)))
}
write_state_file(entries)
}
///| Start the app whose windows carry `app_id` (see `resolve_app_id`), without counting it as
/// a recent launch. False when no installed app matches or it could not be started.
pub fn DesktopShell::launch_app_id(self : DesktopShell, app_id : String) -> Bool {
match resolve_app_id(app_id, self.exec, self.wm_classes) {
Some(id) =>
match self.exec.get(id) {
Some(cmd) => @sys.spawn(cmd, self.display)
None => false
}
None => false
}
}
///|
test "session entries round-trip through EDN" {
let s : Array[SessionEntry] = [
{ app: "foot", minimized: false, maximized: false },
{ app: "org.gnome.Nautilus", minimized: true, maximized: false },
{ app: "firefox", minimized: false, maximized: true },
]
let text = @edn.pretty(session_value(s), layout={ width: 100, indent: 1 })
assert_eq(parse_session(@edn.read(text)), s)
// A plain vector of app ids works too (hand-written).
assert_eq(parse_session(@edn.read("[\"foot\" \"\" 3]")), [{ app: "foot", minimized: false, maximized: false }])
}