From 9ab56ec973df0556171395e53007b59510994c12 Mon Sep 17 00:00:00 2001 From: faumaray Date: Tue, 27 Oct 2026 23:22:40 +0300 Subject: [PATCH] voelin-gpui: the maximize guard covers the first two reports only A window opened to be maximized dropped every windowed report until it was seen maximized, so a resize the user made before the window manager answered (or under one that never maximizes, a tiling one) was never kept. The guard is now a budget of OPENING_REPORTS = 2 windowed reports, the place the window opened with and the configure an X11 window manager may send at the map; a maximized report ends it, and the reports after it are the user's. Tests on the GPUI test platform: the root state keeps the third report, the shell keeps a resize that follows the two. The sentence in docs/ui.md says so. Co-Authored-By: Claude Sonnet 5.5 --- crates/voelin-gpui/src/app.rs | 77 +++++++++++++++++++++++++++------- crates/voelin-gpui/src/view.rs | 16 +++++-- docs/ui.md | 13 +++--- 3 files changed, 83 insertions(+), 23 deletions(-) diff --git a/crates/voelin-gpui/src/app.rs b/crates/voelin-gpui/src/app.rs index a212846..d997c49 100644 --- a/crates/voelin-gpui/src/app.rs +++ b/crates/voelin-gpui/src/app.rs @@ -180,12 +180,21 @@ struct WindowKeeper { /// screen's size for a maximized window, Wayland the size it returns to.) restore: Option, /// The window was opened to be maximized and has not been seen maximized - /// yet: what it reports until then is the place it opened with, not one - /// the user chose, and is not kept. - awaiting_maximize: bool, + /// yet: how many of its windowed reports are still the place it opened + /// with, not one the user chose, and are not kept ([`OPENING_REPORTS`] at + /// start, none once the window was seen maximized or the reports ran out). + opening_reports: u8, } /// Give up the foreground thread once, so that GPUI's executor runs what is +/// How many windowed reports of a window opened to be maximized are taken to +/// be the place it opened with, not the user's: the first is the window's +/// own at its creation, the second the configure an X11 window manager may +/// send when it maps the window. A later one is the user's own move or +/// resize, kept even if the window manager has still not maximized the window +/// (a tiling one never does). +pub const OPENING_REPORTS: u8 = 2; + /// queued before this task goes on. async fn yield_now() { let mut yielded = false; @@ -298,14 +307,14 @@ impl AppState { latest: None, saved, restore: saved.filter(WindowState::is_sane), - awaiting_maximize: false, + opening_reports: 0, }, counters, later: Later(later), #[cfg(test)] sent: Default::default(), }; - state.window.awaiting_maximize = state.wants_maximized(); + state.window.opening_reports = if state.wants_maximized() { OPENING_REPORTS } else { 0 }; if state.switches.demo_ui { state.load_demo(); } @@ -545,20 +554,23 @@ impl AppState { /// /// A window that was opened to be maximized ([`Self::wants_maximized`]) /// reports the place it opened with first (the window manager maximizes it - /// after it is shown, on X11 when [`crate::view::MainView`] asks): that is - /// not kept, or a run that ended before the window was maximized would - /// overwrite `maximized: true` with `false`. Once it has been seen - /// maximized, what it reports is the user's. A maximized window is kept - /// with the place it returns to, not the screen's size. + /// after it is shown, on X11 when [`crate::view::MainView`] asks): its + /// first [`OPENING_REPORTS`] windowed reports are not kept, or a run that + /// ended before the window was maximized would overwrite `maximized: true` + /// with `false`. Once it has been seen maximized, or after those reports, + /// what it reports is the user's (a resize before the window manager + /// answered, or with one that never maximizes, is kept). A maximized + /// window is kept with the place it returns to, not the screen's size. pub fn window_moved(&mut self, state: WindowState) { if self.switches.window.is_some() || self.switches.demo_ui { return; } let keeper = &mut self.window; let state = if state.maximized { - keeper.awaiting_maximize = false; + keeper.opening_reports = 0; keeper.restore.map_or(state, |back| WindowState { maximized: true, ..back }) - } else if keeper.awaiting_maximize { + } else if keeper.opening_reports > 0 { + keeper.opening_reports -= 1; return; } else { keeper.restore = Some(state); @@ -578,10 +590,11 @@ impl AppState { self.switches.window.is_none() && self.remembered_window().is_some_and(|w| w.maximized) } - /// Whether the window was opened to be maximized and has not been seen so - /// yet (the window manager has not answered, or cannot). + /// Whether the window was opened to be maximized, has not been seen so yet + /// (the window manager has not answered, or cannot) and has not reported + /// more than [`OPENING_REPORTS`] places. pub fn awaiting_maximize(&self) -> bool { - self.window.awaiting_maximize + self.window.opening_reports > 0 } /// Write the window's place to the settings if it changed. @@ -1566,6 +1579,40 @@ mod tests { } /// A window that was not opened maximized keeps what it reports from the + /// The guard of a window opened to be maximized is for its first reports + /// only: after them what the window reports is the user's even if it was + /// never seen maximized (a window manager that does not maximize, a user + /// who resizes before it does), so a resize is not lost for good. + #[gpui::test] + fn the_maximize_guard_covers_the_first_reports_only(cx: &mut TestAppContext) { + let dir = TempDir::new("window-guard"); + let backend = Backend::open(&dir.0, Box::new(MemorySecrets::default())).unwrap(); + let left = + WindowState { width: 1000, height: 700, x: Some(20), y: Some(30), maximized: true }; + backend.prefs.set(&UI_WINDOW, Some(left)).unwrap(); + let (app, _, _) = entity_over(cx, backend, Switches::default()); + let kept = |cx: &mut TestAppContext| { + cx.executor().advance_clock(Duration::from_secs(1)); + cx.run_until_parked(); + app.read_with(cx, |app, _| app.backend().prefs.get(&UI_WINDOW)).unwrap() + }; + app.read_with(cx, |app, _| assert!(app.awaiting_maximize())); + + // The place it opened with, then the window manager's configure. + for width in [1000, 1001] { + app.update(cx, |app, _| { + app.window_moved(WindowState { width, maximized: false, ..left }); + }); + assert_eq!(kept(cx), left, "report of {width} is not the user's"); + } + app.read_with(cx, |app, _| assert!(!app.awaiting_maximize(), "the guard is spent")); + + // The third is the user's own, and nothing maximized the window. + let resized = WindowState { width: 800, height: 600, maximized: false, ..place() }; + app.update(cx, |app, _| app.window_moved(resized)); + assert_eq!(kept(cx), resized); + } + /// first report on. #[gpui::test] fn a_window_not_opened_maximized_is_kept_from_its_first_report(cx: &mut TestAppContext) { diff --git a/crates/voelin-gpui/src/view.rs b/crates/voelin-gpui/src/view.rs index 0b8d1c5..a5d97ab 100644 --- a/crates/voelin-gpui/src/view.rs +++ b/crates/voelin-gpui/src/view.rs @@ -800,8 +800,9 @@ mod tests { let kept = |shell: &Shell, vcx: &mut VisualTestContext| { shell.app.read_with(vcx, |app, _| app.backend().prefs.get(&UI_WINDOW)) }; - // The window as it opened (windowed, not yet maximized) and a resize - // before the window manager answered are not what the user left. + // The window as it opened (windowed, not yet maximized: the first + // report) and the configure a window manager sends when it maps the + // window (the second) are not what the user left. let handle = vcx.update(|window, _| window.window_handle()); vcx.simulate_window_resize(handle, size(px(900.0), px(600.0))); frame(vcx); @@ -810,10 +811,19 @@ mod tests { assert_eq!(vcx.update(|window, cx| window.simulate_next_frame(cx)), 0); assert_eq!(zooms(), 1, "once"); assert_eq!(kept(&shell, vcx), Some(left), "still maximized"); - assert!(shell.app.read_with(vcx, |app, _| app.awaiting_maximize())); // The close of a run the window manager never answered keeps it too. assert_eq!(shell.app.update(vcx, |app, cx| app.shutdown(cx)), 0); assert_eq!(kept(&shell, vcx), Some(left)); + // The guard is for those two reports. The user's own resize after them + // is kept, though the window manager still has not maximized the + // window (a tiling one never does). + assert!(!shell.app.read_with(vcx, |app, _| app.awaiting_maximize())); + vcx.simulate_window_resize(handle, size(px(700.0), px(500.0))); + frame(vcx); + vcx.executor().advance_clock(Duration::from_secs(2)); + frame(vcx); + let resized = kept(&shell, vcx).expect("kept"); + assert_eq!((resized.width, resized.height, resized.maximized), (700, 500, false)); } /// A window that was not left maximized is not zoomed. diff --git a/docs/ui.md b/docs/ui.md index 12f7d1e..baaed7c 100644 --- a/docs/ui.md +++ b/docs/ui.md @@ -1293,11 +1293,14 @@ compositor is expected to honour that, which was not run here), and the window asks again when its first frame is drawn if it is not maximized by then (`MainView`: `Window::zoom_window`, which toggles on X11, hence the check), because an X11 window manager manages a window only from its map, so the early request is lost (openbox: the window opened at -its restored size). Until the window has been seen maximized, what it reports -is the place it opened with and is not kept, so a run that ends before the -window manager answers, or one whose window manager never maximizes (a tiling -one), leaves `maximized: true` as it found it; once it has been seen maximized, -what the window reports is the user's (restoring it is kept). A maximized +its restored size). The first two windowed reports of a window opened to be +maximized (the place it opened with, and the configure an X11 window manager +may send when it maps the window) are not kept, so a run that ends before the +window manager answers leaves `maximized: true` as it found it; the reports +after them are the user's, kept whether or not the window was seen maximized (a +resize before the window manager answered, or under one that never maximizes, a +tiling one, is kept), and once it has been seen maximized what it reports is +the user's too (restoring it is kept). A maximized window is kept with the size it returns to (X11 reports the screen's; Wayland the size it returns to). The `maximized` request itself needs a real window: GPUI's test platform has no zoom, so the tests count the request and report the