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
@@ -49,6 +49,7 @@ pub fn run(args : Array[String]) -> Int {
|
||||
let mut serve = false
|
||||
let mut profile = false
|
||||
let mut drm = false
|
||||
let mut restore = true
|
||||
let mut width = 1280
|
||||
let mut height = 720
|
||||
|
||||
@@ -67,6 +68,9 @@ pub fn run(args : Array[String]) -> Int {
|
||||
} else if a == "--drm" {
|
||||
drm = true
|
||||
i = i + 1
|
||||
} else if a == "--no-restore" {
|
||||
restore = false // don't reopen the apps of the last session
|
||||
i = i + 1
|
||||
} else if a == "--profile" {
|
||||
profile = true
|
||||
i = i + 1
|
||||
@@ -171,6 +175,9 @@ pub fn run(args : Array[String]) -> Int {
|
||||
None => ()
|
||||
}
|
||||
@sys.log("chandra compositor listening on " + socket_path)
|
||||
if restore {
|
||||
ignore(comp.restore_session()) // reopen the windows the last session ended with
|
||||
}
|
||||
let mut frames = 0
|
||||
// A changed frame waits here until the display can take it (no page flip in flight).
|
||||
let mut need_present = true
|
||||
@@ -311,6 +318,7 @@ pub fn run(args : Array[String]) -> Int {
|
||||
let spent = (@sys.monotonic_ms() - f0).to_int()
|
||||
@sys.sleep_ms(if spent >= 16 { 1 } else { 16 - spent })
|
||||
}
|
||||
comp.save_session() // what is open now is reopened at the next start
|
||||
match kms {
|
||||
Some(k) => k.close()
|
||||
None => ()
|
||||
|
||||
@@ -9,6 +9,7 @@ fn main {
|
||||
let widgets : Array[String] = []
|
||||
let mut windowed = false
|
||||
let mut drm = false
|
||||
let mut restore = true
|
||||
let mut width = 1280
|
||||
let mut height = 720
|
||||
|
||||
@@ -27,6 +28,9 @@ fn main {
|
||||
} else if a == "--drm" {
|
||||
drm = true
|
||||
i = i + 1
|
||||
} else if a == "--no-restore" {
|
||||
restore = false // don't reopen the apps of the last session
|
||||
i = i + 1
|
||||
} else if a == "--widget" && i + 1 < args.length() {
|
||||
widgets.push(args[i + 1])
|
||||
i = i + 2
|
||||
@@ -117,6 +121,9 @@ fn main {
|
||||
None => ()
|
||||
}
|
||||
println("chandra compositor listening on " + socket_path + " (wasi-gfx surface active)")
|
||||
if restore {
|
||||
ignore(comp.restore_session()) // reopen the windows the last session ended with
|
||||
}
|
||||
while true {
|
||||
comp.step()
|
||||
if comp.quit_requested {
|
||||
@@ -169,6 +176,7 @@ fn main {
|
||||
}
|
||||
@sys.sleep_ms(16)
|
||||
}
|
||||
comp.save_session() // what is open now is reopened at the next start
|
||||
match kms {
|
||||
Some(k) => k.close()
|
||||
None => ()
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,8 @@ priv struct DesktopApp {
|
||||
comment : String
|
||||
category : String
|
||||
command : String
|
||||
/// StartupWMClass: the app id its windows carry when that is not the desktop file's name.
|
||||
wm_class : String
|
||||
}
|
||||
|
||||
///| Directories scanned, lowest priority first (later ones override by file name).
|
||||
@@ -63,6 +65,7 @@ fn parse_desktop(id : String, text : String, have_terminal : Bool) -> DesktopApp
|
||||
let mut hidden = false
|
||||
let mut terminal = false
|
||||
let mut kind = "Application"
|
||||
let mut wm_class = ""
|
||||
for raw in text.split("\n") {
|
||||
let line = raw.trim(chars=" \r\t").to_string()
|
||||
if line.length() == 0 || line.has_prefix("#") {
|
||||
@@ -89,6 +92,7 @@ fn parse_desktop(id : String, text : String, have_terminal : Bool) -> DesktopApp
|
||||
"Type" => kind = val
|
||||
"NoDisplay" | "Hidden" => if val == "true" { hidden = true }
|
||||
"Terminal" => terminal = val == "true"
|
||||
"StartupWMClass" => wm_class = val
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
@@ -109,7 +113,7 @@ fn parse_desktop(id : String, text : String, have_terminal : Bool) -> DesktopApp
|
||||
_ => "Other"
|
||||
}
|
||||
let comment = if comment.length() > 0 { comment } else { generic }
|
||||
Some({ id, name, comment, category, command })
|
||||
Some({ id, name, comment, category, command, wm_class })
|
||||
}
|
||||
|
||||
///| Scan every applications dir; later dirs override earlier ones, result sorted by name.
|
||||
@@ -158,9 +162,67 @@ test "parse_desktop reads a normal entry and strips field codes" {
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
test "parse_desktop keeps StartupWMClass" {
|
||||
let t = "[Desktop Entry]\nType=Application\nName=Code\nExec=code %F\nStartupWMClass=Code\n"
|
||||
match parse_desktop("visual-studio-code", t, true) {
|
||||
Some(a) => assert_eq(a.wm_class, "Code")
|
||||
None => fail("expected entry")
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
test "parse_desktop hides NoDisplay entries" {
|
||||
let t = "[Desktop Entry]\nType=Application\nName=X\nExec=x\nNoDisplay=true\n"
|
||||
assert_true(parse_desktop("x", t, true) is None)
|
||||
}
|
||||
|
||||
|
||||
///| The desktop file id that launches the app whose windows carry `app_id`: the file of that
|
||||
/// name, else one whose StartupWMClass is it, else a case-insensitive match on either, else one
|
||||
/// whose last dotted part matches (a window "nautilus" for org.gnome.Nautilus.desktop).
|
||||
fn resolve_app_id(
|
||||
app_id : String,
|
||||
exec : Map[String, String],
|
||||
wm_classes : Map[String, String],
|
||||
) -> String? {
|
||||
if app_id.length() == 0 {
|
||||
return None
|
||||
}
|
||||
if exec.contains(app_id) {
|
||||
return Some(app_id)
|
||||
}
|
||||
let low = app_id.to_lower()
|
||||
match wm_classes.get(low) {
|
||||
Some(id) => return Some(id)
|
||||
None => ()
|
||||
}
|
||||
for id, _ in exec {
|
||||
if id.to_lower() == low {
|
||||
return Some(id)
|
||||
}
|
||||
}
|
||||
for id, _ in exec {
|
||||
let parts : Array[String] = id.split(".").map(x => x.to_string()).collect()
|
||||
if parts.length() > 1 && parts[parts.length() - 1].to_lower() == low {
|
||||
return Some(id)
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
///|
|
||||
test "resolve_app_id finds the desktop file for a window's app id" {
|
||||
let exec : Map[String, String] = {
|
||||
"foot": "foot",
|
||||
"org.gnome.Nautilus": "nautilus --new-window",
|
||||
"visual-studio-code": "code",
|
||||
}
|
||||
let wm : Map[String, String] = { "code": "visual-studio-code" }
|
||||
assert_eq(resolve_app_id("foot", exec, wm), Some("foot"))
|
||||
assert_eq(resolve_app_id("Code", exec, wm), Some("visual-studio-code"))
|
||||
assert_eq(resolve_app_id("org.gnome.nautilus", exec, wm), Some("org.gnome.Nautilus"))
|
||||
assert_eq(resolve_app_id("nautilus", exec, wm), Some("org.gnome.Nautilus"))
|
||||
assert_eq(resolve_app_id("unknown", exec, wm), None)
|
||||
assert_eq(resolve_app_id("", exec, wm), None)
|
||||
}
|
||||
@@ -180,6 +180,8 @@ pub(all) struct MenuRow {
|
||||
pub(all) struct DesktopShell {
|
||||
apps : Array[@types.AppEntry]
|
||||
exec : Map[String, String]
|
||||
/// Lower-cased StartupWMClass -> desktop file id, for apps whose windows' app id differs.
|
||||
wm_classes : Map[String, String]
|
||||
/// Wayland socket handed to launched apps as WAYLAND_DISPLAY.
|
||||
display : String
|
||||
/// Panel modules in panel order; a module with `stretch` splits the panel into a left and a right group.
|
||||
@@ -255,7 +257,11 @@ pub(all) struct DesktopShell {
|
||||
pub fn DesktopShell::new(display : String) -> DesktopShell {
|
||||
let apps : Array[@types.AppEntry] = []
|
||||
let exec : Map[String, String] = {}
|
||||
let wm_classes : Map[String, String] = {}
|
||||
for a in scan_desktop_apps() {
|
||||
if a.wm_class.length() > 0 {
|
||||
wm_classes.set(a.wm_class.to_lower(), a.id)
|
||||
}
|
||||
let glyph = match a.name.to_array().get(0) {
|
||||
Some(c) => c.to_string().to_upper()
|
||||
None => "?"
|
||||
@@ -267,6 +273,7 @@ pub fn DesktopShell::new(display : String) -> DesktopShell {
|
||||
let sh : DesktopShell = {
|
||||
apps,
|
||||
exec,
|
||||
wm_classes,
|
||||
display,
|
||||
widgets: [],
|
||||
next_slot: 1,
|
||||
|
||||
+121
-1
@@ -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 }])
|
||||
}
|
||||
@@ -94,6 +94,8 @@ struct ToplevelState {
|
||||
mut min_w : Int
|
||||
mut min_h : Int
|
||||
mut opened_sent : Bool
|
||||
/// xdg_toplevel.set_parent named another toplevel: a dialog or tool window of its app.
|
||||
mut has_parent : Bool
|
||||
/// Set for xdg_popup surfaces (they live in `toplevels` so input routing is shared).
|
||||
popup_parent : UInt64?
|
||||
}
|
||||
@@ -839,6 +841,7 @@ fn WaylandServer::dispatch_msg(
|
||||
min_w: 160,
|
||||
min_h: 120,
|
||||
opened_sent: false,
|
||||
has_parent: false,
|
||||
popup_parent: None,
|
||||
})
|
||||
self.win_to_client.set(wid, c.client_id)
|
||||
@@ -884,6 +887,7 @@ fn WaylandServer::dispatch_msg(
|
||||
min_w: 1,
|
||||
min_h: 1,
|
||||
opened_sent: true,
|
||||
has_parent: true,
|
||||
popup_parent: Some(parent_wid),
|
||||
})
|
||||
self.win_to_client.set(wid, c.client_id)
|
||||
@@ -945,6 +949,9 @@ fn WaylandServer::dispatch_msg(
|
||||
c.objects.remove(obj_id)
|
||||
send_delete_id(c, obj_id)
|
||||
events.push(ToplevelClosed(wid))
|
||||
} else if opcode == 1U {
|
||||
// set_parent(parent: object or null)
|
||||
tl.has_parent = reader.u32() != 0U
|
||||
} else if opcode == 2U {
|
||||
// set_title
|
||||
tl.title = reader.str()
|
||||
@@ -1135,6 +1142,22 @@ fn WaylandServer::find_client(self : WaylandServer, cid : UInt) -> ClientConn? {
|
||||
None
|
||||
}
|
||||
|
||||
///| Whether a window is a dialog or tool window of another one (xdg_toplevel.set_parent).
|
||||
pub fn WaylandServer::toplevel_has_parent(self : WaylandServer, win_id : UInt64) -> Bool {
|
||||
match self.win_to_client.get(win_id) {
|
||||
Some(cid) =>
|
||||
match self.find_client(cid) {
|
||||
Some(c) =>
|
||||
match c.toplevels.get(win_id) {
|
||||
Some(tl) => tl.has_parent
|
||||
None => false
|
||||
}
|
||||
None => false
|
||||
}
|
||||
None => false
|
||||
}
|
||||
}
|
||||
|
||||
///| Send xdg_toplevel.configure + xdg_surface.configure to a client window.
|
||||
pub fn WaylandServer::send_toplevel_configure(
|
||||
self : WaylandServer,
|
||||
|
||||
Reference in new issue
Block a user