From 235f17cd702b3e6f6d9674876260044556d22bb2 Mon Sep 17 00:00:00 2001 From: nandithebull Date: Wed, 25 Nov 2026 03:14:58 -0800 Subject: [PATCH] Remember window positions in state.edn, per app :windows {"app.id" {:x :y :w :h}} is written when a floating window is dragged or resized and read at startup; an app opening later gets its last place (floating, clamped on screen). Co-Authored-By: Claude Sonnet 5.5 --- compositor/host/compositor.mbt | 28 ++++++++++++++-- compositor/host/input_test.mbt | 35 +++++++++++++++++++ compositor/shell/shell.mbt | 3 ++ compositor/shell/state.mbt | 61 ++++++++++++++++++++++++++++++++-- 4 files changed, 121 insertions(+), 6 deletions(-) diff --git a/compositor/host/compositor.mbt b/compositor/host/compositor.mbt index 830499c..5b241ba 100644 --- a/compositor/host/compositor.mbt +++ b/compositor/host/compositor.mbt @@ -199,19 +199,32 @@ pub fn MoonPlasmaCompositor::step(self : MoonPlasmaCompositor) -> Unit { cfg_dirty: false, } self.windows.set(wid, win) + // An app whose window was last left somewhere reopens there, floating, kept on screen. + let saved = self.shell.saved_window(app_id) + let g : @wm.Rect = match saved { + Some((sx, sy, sw, sh)) => { + let w = if sw > self.output_w { self.output_w } else { sw } + let h = if sh > self.output_h { self.output_h } else { sh } + let x = if sx + w > self.output_w { self.output_w - w } else if sx < 0 { 0 } else { sx } + let y = if sy + h > self.output_h { self.output_h - h } else if sy < 32 { 32 } else { sy } + { x, y, w, h } + } + None => { x: 100, y: 100, w: 640, h: 480 } + } + win.geometry = @scene.Rect::new(g.x, g.y, g.w, g.h) let rec : @wm.WindowRecord = { id: wid, app_id, title, - geometry: { x: 100, y: 100, w: 640, h: 480 }, - floating_geometry: { x: 100, y: 100, w: 640, h: 480 }, + geometry: g, + floating_geometry: g, min_size: { w: min_w, h: min_h }, max_size: None, focused: true, maximized: false, fullscreen: false, minimized: false, - is_floating: false, + is_floating: saved is Some(_), workspace: 0U, can_move: true, can_resize: true, @@ -983,7 +996,16 @@ pub fn MoonPlasmaCompositor::handle_input( return } if !pressed && self.wm.drag is Some(_) { + let dragged = self.wm.drag.unwrap().win_id self.wm.end_drag() + // Remember where a floating window was left. + match (self.wm.windows.get(dragged), self.windows.get(dragged)) { + (Some(r), Some(win)) => + if r.is_floating && !r.maximized && !r.fullscreen { + self.shell.remember_window(win.app_id, win.geometry.x, win.geometry.y, win.geometry.w, win.geometry.h) + } + _ => () + } self.flush_configures() return } diff --git a/compositor/host/input_test.mbt b/compositor/host/input_test.mbt index 5be48d1..04bf972 100644 --- a/compositor/host/input_test.mbt +++ b/compositor/host/input_test.mbt @@ -943,3 +943,38 @@ test "clicking a floating window brings it in front of the others" { assert_eq(after[1], back) assert_eq(after[0], front) } + +///| +test "a window dragged somewhere reopens there, saved per app in state.edn" { + if @sys.getenv("MOON_PLASMA_TEST_CONFIG") == "1" { + write_state_edn("{:note \"keep\"}\n") + let sock1 = "/tmp/wayland-moon-wp1-" + @sys.getpid().to_string() + ".sock" + let theme = @scene.ThemeTokens::moon_night() + let a = @host.MoonPlasmaCompositor::new(sock1, theme, 1280, 720).unwrap() + a.shell.persist = true + a.set_layout_mode(@wm.LayoutMode::Floating) + ignore(open_and_configure_client(a, sock1, "T", "org.test.app")) + ignore(a.compose_frame()) + let g0 = a.windows.get(1UL).unwrap().geometry + // Drag its title bar 120px right and 60px down. + a.handle_input(@nested.InputEvent::PointerMove((g0.x + 20).to_double(), (g0.y - 16).to_double()), 10U) + a.handle_input(@nested.InputEvent::PointerButton(272U, true), 11U) + a.handle_input(@nested.InputEvent::PointerMove((g0.x + 140).to_double(), (g0.y + 44).to_double()), 12U) + a.handle_input(@nested.InputEvent::PointerButton(272U, false), 13U) + let g1 = a.windows.get(1UL).unwrap().geometry + assert_true(g1.x != g0.x || g1.y != g0.y) + let text = read_state_edn() + assert_true(text.contains("\"org.test.app\"") && text.contains(":windows")) + assert_true(text.contains(":note \"keep\"")) + // A new session: the same app opens where it was left. + let sock2 = "/tmp/wayland-moon-wp2-" + @sys.getpid().to_string() + ".sock" + let b = @host.MoonPlasmaCompositor::new(sock2, theme, 1280, 720).unwrap() + b.shell.persist = true + b.restore_state() + ignore(open_and_configure_client(b, sock2, "T", "org.test.app")) + ignore(b.compose_frame()) + let g2 = b.windows.get(1UL).unwrap().geometry + assert_eq(g2.x, g1.x) + assert_eq(g2.y, g1.y) + } +} diff --git a/compositor/shell/shell.mbt b/compositor/shell/shell.mbt index 446c512..efe0d72 100644 --- a/compositor/shell/shell.mbt +++ b/compositor/shell/shell.mbt @@ -191,6 +191,8 @@ pub(all) struct DesktopShell { mut booted : Bool /// Read and write panel.edn (the compositor binary turns this on; tests leave it off). mut persist : Bool + /// Last floating position and size (x, y, w, h) of each app's window, by app id (persisted). + win_geoms : Map[String, (Int, Int, Int, Int)] mut tokens : @theme.Tokens mut screen_w : Int /// Desktop card position (once moved) and drag state. @@ -268,6 +270,7 @@ pub fn DesktopShell::new(display : String) -> DesktopShell { catalog_named: false, booted: false, persist: false, + win_geoms: {}, tokens: conv_theme(@scene.ThemeTokens::moon_night()), screen_w: 0, desk_set: false, diff --git a/compositor/shell/state.mbt b/compositor/shell/state.mbt index 98867f1..55080cb 100644 --- a/compositor/shell/state.mbt +++ b/compositor/shell/state.mbt @@ -3,6 +3,7 @@ /// /// {:layout :tiling ; the Tile | Float switcher: :tiling, :monocle or :floating /// :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) /// /// Saving merges into what is already in the file, so keys this build doesn't know (a future @@ -17,6 +18,7 @@ pub(all) struct SavedState { recent : Array[String]? /// Top-left of the desktop card, when it was moved. desk : (Int, Int)? + windows : Map[String, (Int, Int, Int, Int)] } ///| @@ -28,7 +30,7 @@ fn state_path() -> String { let layout_names : Array[String] = ["tiling", "monocle", "floating"] ///| The keys this build owns; everything else in the file is kept as it is. -let owned_keys : Array[String] = ["layout", "recent", "desktop-card"] +let owned_keys : Array[String] = ["layout", "recent", "desktop-card", "windows"] ///| fn read_state_file() -> Array[(@edn.Value, @edn.Value)] { @@ -60,7 +62,7 @@ fn lookup_key( ///| Read state.edn. Without `persist` nothing is read. pub fn DesktopShell::load_state(self : DesktopShell) -> SavedState { if !self.persist { - return { layout: None, recent: None, desk: None } + return { layout: None, recent: None, desk: None, windows: {} } } let entries = read_state_file() let layout = match lookup_key(entries, "layout") { @@ -111,11 +113,36 @@ pub fn DesktopShell::load_state(self : DesktopShell) -> SavedState { } _ => None } - { layout, recent, desk } + let windows : Map[String, (Int, Int, Int, Int)] = {} + match lookup_key(entries, "windows") { + Some(Mapping(ws)) => + for e in ws { + match e { + (Text(app), Mapping(m)) => { + let f = fn(k : String) -> Int? { + match lookup_key(m, k) { + Some(Integer(v)) => parse_int_str(v) + _ => None + } + } + match (f("x"), f("y"), f("w"), f("h")) { + (Some(x), Some(y), Some(w), Some(h)) if w > 0 && h > 0 => windows.set(app, (x, y, w, h)) + _ => () + } + } + _ => () + } + } + _ => () + } + { layout, recent, desk, windows } } ///| Take the saved recents (the layout is the compositor's to apply). pub fn DesktopShell::apply_state(self : DesktopShell, st : SavedState) -> Unit { + for app, g in st.windows { + self.win_geoms.set(app, g) + } match st.desk { Some((x, y)) => { self.desk_set = true @@ -163,6 +190,20 @@ pub fn DesktopShell::save_state(self : DesktopShell) -> Unit { } entries.insert(0, (@edn.Value::Keyword("layout"), @edn.Value::Keyword(layout))) entries.insert(1, (@edn.Value::Keyword("recent"), @edn.Value::Vector(recent))) + if self.win_geoms.length() > 0 { + let ws : Array[(@edn.Value, @edn.Value)] = [] + for app, g in self.win_geoms { + let (x, y, w, h) = g + let n = fn(v : Int) { @edn.Value::Integer(v.to_string()) } + ws.push((@edn.Value::Text(app), @edn.Value::Mapping([ + (@edn.Value::Keyword("x"), n(x)), + (@edn.Value::Keyword("y"), n(y)), + (@edn.Value::Keyword("w"), n(w)), + (@edn.Value::Keyword("h"), n(h)), + ]))) + } + entries.insert(2, (@edn.Value::Keyword("windows"), @edn.Value::Mapping(ws))) + } if self.desk_set { let (x, y) = self.desk_pos() entries.insert(2, (@edn.Value::Keyword("desktop-card"), @edn.Value::Mapping([ @@ -200,3 +241,17 @@ fn parse_int_str(s : String) -> Int? { } Some(if neg { -n } else { n }) } + +///| Remember where an app's window was left (a no-op without an app id) and save. +pub fn DesktopShell::remember_window(self : DesktopShell, app_id : String, x : Int, y : Int, w : Int, h : Int) -> Unit { + if app_id.length() == 0 || w <= 0 || h <= 0 { + return + } + self.win_geoms.set(app_id, (x, y, w, h)) + self.save_state() +} + +///| The saved place of an app's window, if persistence is on and one was saved. +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 } +}