Restore the session on login: reopen the windows the last session ended with
CI / test (push) Failing after 20s
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:
10 files changed
+435
-2
No files matched your search
@@ -71,6 +71,16 @@ pub(all) struct ChandraCompositor {
|
||||
mut frame_changed : Bool
|
||||
mut prof_shell_ms : UInt64
|
||||
mut prof_raster_ms : UInt64
|
||||
/// Session restore (session.mbt): apps launched at start whose window hasn't appeared yet,
|
||||
/// until when they are waited for, and the windows that took their place.
|
||||
restore_pending : Array[@shell.SessionEntry]
|
||||
mut restore_until : UInt64
|
||||
restore_claimed : Map[UInt64, Bool]
|
||||
/// The session as last written, as last seen (and since when), and when to look again.
|
||||
mut session_saved : String
|
||||
mut session_seen : String
|
||||
mut session_seen_at : UInt64
|
||||
mut session_check_at : UInt64
|
||||
}
|
||||
|
||||
///|
|
||||
@@ -127,6 +137,13 @@ pub fn ChandraCompositor::new(
|
||||
frame_changed: true,
|
||||
prof_shell_ms: 0UL,
|
||||
prof_raster_ms: 0UL,
|
||||
restore_pending: [],
|
||||
restore_until: 0UL,
|
||||
restore_claimed: {},
|
||||
session_saved: "",
|
||||
session_seen: "",
|
||||
session_seen_at: 0UL,
|
||||
session_check_at: 0UL,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -306,12 +323,14 @@ pub fn ChandraCompositor::step(self : ChandraCompositor) -> Unit {
|
||||
}
|
||||
let actions = self.wm.open_window(rec, self.work_area)
|
||||
self.apply_actions(actions)
|
||||
self.claim_restored(wid, app_id)
|
||||
}
|
||||
@wayland.CompositorEvent::ToplevelUpdated(wid, app_id, title) =>
|
||||
match self.windows.get(wid) {
|
||||
Some(win) => {
|
||||
if app_id.length() > 0 {
|
||||
win.app_id = app_id
|
||||
self.claim_restored(wid, app_id)
|
||||
}
|
||||
if title.length() > 0 {
|
||||
win.title = title
|
||||
@@ -359,6 +378,7 @@ pub fn ChandraCompositor::step(self : ChandraCompositor) -> Unit {
|
||||
None => ()
|
||||
}
|
||||
ignore(self.windows.remove(wid))
|
||||
ignore(self.restore_claimed.remove(wid))
|
||||
if self.ptr_win == Some(wid) {
|
||||
self.ptr_win = None
|
||||
}
|
||||
@@ -395,6 +415,7 @@ pub fn ChandraCompositor::step(self : ChandraCompositor) -> Unit {
|
||||
@wayland.CompositorEvent::ToplevelMoveRequest(_) => ()
|
||||
}
|
||||
}
|
||||
self.session_tick()
|
||||
}
|
||||
|
||||
///|
|
||||
|
||||
@@ -1210,3 +1210,46 @@ test "state saved by a build from before the rename to Chandra is still read" {
|
||||
@sys.unlink(dir + "/moon-plasma/state.edn")
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
test "the open windows are saved as the session and a restored app's window gets its state back" {
|
||||
if @sys.getenv("CHANDRA_TEST_CONFIG") == "1" {
|
||||
write_state_edn("{:note \"keep\"}\n")
|
||||
let sock1 = "/tmp/wayland-moon-ss1-" + @sys.getpid().to_string() + ".sock"
|
||||
let theme = @scene.ThemeTokens::moon_night()
|
||||
let a = @host.ChandraCompositor::new(sock1, theme, 1280, 720).unwrap()
|
||||
a.shell.persist = true
|
||||
ignore(open_and_configure_client(a, sock1, "A", "org.test.alpha"))
|
||||
ignore(open_and_configure_client(a, sock1, "B", "org.test.beta"))
|
||||
ignore(a.compose_frame())
|
||||
ignore(a.wm.handle_request(2UL, @wm.WindowRequest::Minimize(true), a.work_area))
|
||||
a.save_session()
|
||||
let text = read_state_edn()
|
||||
assert_true(text.contains(":session"))
|
||||
assert_true(text.contains("\"org.test.alpha\"") && text.contains("\"org.test.beta\""))
|
||||
assert_true(text.contains(":minimized true"))
|
||||
assert_true(text.contains(":note \"keep\""))
|
||||
// Saving the other state keeps the session, and the session reads back in order.
|
||||
a.shell.save_state()
|
||||
let saved = a.shell.load_session()
|
||||
assert_eq(saved.length(), 2)
|
||||
assert_eq(saved[0], { app: "org.test.alpha", minimized: false, maximized: false })
|
||||
assert_eq(saved[1], { app: "org.test.beta", minimized: true, maximized: false })
|
||||
// A new session: beta was launched and is waiting for its window. Until it shows up it still
|
||||
// counts as part of the session; when it opens it is minimized again.
|
||||
let sock2 = "/tmp/wayland-moon-ss2-" + @sys.getpid().to_string() + ".sock"
|
||||
let b = @host.ChandraCompositor::new(sock2, theme, 1280, 720).unwrap()
|
||||
b.shell.persist = true
|
||||
b.restore_pending.push(saved[1])
|
||||
b.restore_until = @sys.monotonic_ms() + 30000UL
|
||||
assert_eq(b.session_snapshot(@sys.monotonic_ms()), [saved[1]])
|
||||
ignore(open_and_configure_client(b, sock2, "B", "org.test.beta"))
|
||||
ignore(b.compose_frame())
|
||||
assert_eq(b.restore_pending.length(), 0)
|
||||
assert_true(b.wm.windows.get(1UL).unwrap().minimized)
|
||||
assert_eq(b.session_snapshot(@sys.monotonic_ms()), [saved[1]])
|
||||
// :restore-session false turns reopening off.
|
||||
write_state_edn("{:restore-session false :session [{:app \"foot\"}]}\n")
|
||||
assert_eq(b.shell.load_session().length(), 0)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user