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

+138
View File
@@ -0,0 +1,138 @@
///| Session restore: the windows open when a session ends are reopened at the next start.
///
/// The open windows (by app id, in opening order, with whether each was minimized or maximized)
/// are kept in state.edn as `:session`. They are written when the session ends (logout, or the
/// display going away) and, so that a crash or power cut loses little, whenever the set of
/// windows changes and then stays the same for a few seconds. That delay also keeps a shutdown,
/// where the apps are killed a moment before the compositor, from saving an empty desktop.
///
/// At start `restore_session` launches each saved app through its .desktop file (the app id is
/// matched to a desktop file id or StartupWMClass). Until its window appears (or for
/// `restore_wait_ms` at most) a launched app still counts as part of the session, so an early
/// save doesn't forget windows that are slow to open. Dialogs (toplevels with a parent) are not
/// saved: reopening the app is what brings them back, if anything. Where windows go is the
/// existing machinery: the layout mode, and the remembered floating place of each app.
///|
let restore_wait_ms : UInt64 = 30000UL
///|
let session_settle_ms : UInt64 = 3000UL
///|
let session_check_ms : UInt64 = 500UL
///|
fn session_sig(entries : Array[@shell.SessionEntry]) -> String {
let sb = StringBuilder::new()
for e in entries {
sb.write_string(e.app)
sb.write_string(if e.minimized { "/m" } else { "" })
sb.write_string(if e.maximized { "/M" } else { "" })
sb.write_char('\n')
}
sb.to_string()
}
///| The windows open now, in opening order, plus restored apps whose window hasn't appeared yet.
pub fn ChandraCompositor::session_snapshot(self : ChandraCompositor, now : UInt64) -> Array[@shell.SessionEntry] {
let out : Array[@shell.SessionEntry] = []
for wid, win in self.windows {
if win.app_id.length() == 0 || self.server.toplevel_has_parent(wid) {
continue
}
let minimized = match self.wm.windows.get(wid) {
Some(r) => r.minimized
None => false
}
out.push({ app: win.app_id, minimized, maximized: win.maximized })
}
if now < self.restore_until {
for e in self.restore_pending {
out.push(e)
}
}
out
}
///| Write the session to state.edn now (at logout / exit). A no-op without persistence.
pub fn ChandraCompositor::save_session(self : ChandraCompositor) -> Unit {
if !self.shell.persist {
return
}
let snap = self.session_snapshot(@sys.monotonic_ms())
self.shell.save_session(snap)
self.session_saved = session_sig(snap)
self.session_seen = self.session_saved
}
///| Reopen the apps of the last session. Call once, before the main loop, with persistence on.
/// Returns how many were started.
pub fn ChandraCompositor::restore_session(self : ChandraCompositor) -> Int {
let saved = self.shell.load_session()
self.session_saved = session_sig(saved)
self.session_seen = self.session_saved
let mut n = 0
for e in saved {
if self.shell.launch_app_id(e.app) {
self.restore_pending.push(e)
n = n + 1
} else {
@sys.log("session: no installed app for \"" + e.app + "\", not reopened")
}
}
if n > 0 {
self.restore_until = @sys.monotonic_ms() + restore_wait_ms
@sys.log("session: reopening " + n.to_string() + " app(s) from the last session")
}
n
}
///| A window opened (or got its app id): if a restored app was waiting for it, it takes that
/// entry's place and gets back its minimized / maximized state.
fn ChandraCompositor::claim_restored(self : ChandraCompositor, wid : UInt64, app_id : String) -> Unit {
if app_id.length() == 0 || self.restore_claimed.contains(wid) || self.restore_pending.length() == 0 {
return
}
let mut idx = -1
for i = 0; i < self.restore_pending.length(); i = i + 1 {
if idx < 0 && self.restore_pending[i].app == app_id {
idx = i
}
}
if idx < 0 {
return
}
let e = self.restore_pending.remove(idx)
self.restore_claimed.set(wid, true)
if e.maximized {
self.apply_actions(self.wm.handle_request(wid, @wm.WindowRequest::Maximize(true), self.work_area))
}
if e.minimized {
self.apply_actions(self.wm.handle_request(wid, @wm.WindowRequest::Minimize(true), self.work_area))
}
}
///| Called every step: save the session once a change to the open windows has settled.
fn ChandraCompositor::session_tick(self : ChandraCompositor) -> Unit {
if !self.shell.persist {
return
}
let now = @sys.monotonic_ms()
if now < self.session_check_at {
return
}
self.session_check_at = now + session_check_ms
if now >= self.restore_until && self.restore_pending.length() > 0 {
self.restore_pending.clear() // never showed up
}
let snap = self.session_snapshot(now)
let sig = session_sig(snap)
if sig != self.session_seen {
self.session_seen = sig
self.session_seen_at = now
} else if sig != self.session_saved && now - self.session_seen_at >= session_settle_ms {
self.shell.save_session(snap)
self.session_saved = sig
}
}