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
@@ -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 }])
|
||||
}
|
||||
Reference in new issue
Block a user