diff --git a/CHANGELOG.md b/CHANGELOG.md index f33b945..70ff4d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,8 +29,9 @@ User-visible changes of the apps and the `tsgw` gateway. The format follows the Slint app are there, and the other way round). Add, edit, select and remove a server, connect and disconnect voice, mute the microphone and the sound, hold to talk and copy an invite link, as in the Slint app; the window's - title names the server on screen, and its size is kept for the next run - (setting `ui.window`). The channel tree, the chat and the other pages are the + title names the server on screen, and its size and whether it was maximized + are kept for the next run (setting `ui.window`; on X11 the window is + maximized once it is shown, not before the window manager has it). The channel tree, the chat and the other pages are the next slices; the search and the settings buttons only log that. `VOELIN_DEMO_UI=1` shows the Slint app's sample servers without sending or keeping anything (docs/ui.md, "The desktop shell"). diff --git a/crates/voelin-gpui/src/app.rs b/crates/voelin-gpui/src/app.rs index 93a1f8a..909911d 100644 --- a/crates/voelin-gpui/src/app.rs +++ b/crates/voelin-gpui/src/app.rs @@ -21,11 +21,11 @@ //! //! The root notifies on every batch of events and on every move of the user's; //! it is the views (`crate::shell`) that decide whether what they show changed -//! and tell the window only then (the window draws every part). What a transition asks for besides the state -//! (`Update`) is carried out as far as this front end has the parts: the -//! engine's commands go out, the status line is set, what is to be kept goes -//! to the store, a setting the theme follows is applied and the warnings are -//! logged; the redraw flags, the notices, the pictures to decode and the +//! and tell the window only then (the window draws every part). What a +//! transition asks for besides the state (`Update`) is carried out as far as +//! this front end has the parts: the engine's commands go out, the status line +//! is set, what is to be kept goes to the store, a setting the theme follows +//! is applied and the warnings are logged; the redraw flags, the notices, the pictures to decode and the //! streams' capture and decoder are for the slices that have those parts. use std::task::Poll; @@ -174,6 +174,14 @@ struct WindowKeeper { latest: Option, /// What the settings hold. saved: Option, + /// Where the window returns to when it is not maximized: the latest + /// windowed place of this run, else the remembered one. (X11 reports the + /// 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, } /// Give up the foreground thread once, so that GPUI's executor runs what is @@ -285,12 +293,18 @@ impl AppState { engine, backend, switches, - window: WindowKeeper { latest: None, saved }, + window: WindowKeeper { + latest: None, + saved, + restore: saved.filter(WindowState::is_sane), + awaiting_maximize: false, + }, counters, later: Later(later), #[cfg(test)] sent: Default::default(), }; + state.window.awaiting_maximize = state.wants_maximized(); if state.switches.demo_ui { state.load_demo(); } @@ -507,10 +521,29 @@ impl AppState { /// The window moved or changed size: remember it, to keep at the next tick /// (and at close). Not with a development switch that sets the size, and /// not with the sample data: neither is the user's window. + /// + /// 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. pub fn window_moved(&mut self, state: WindowState) { - if self.switches.window.is_none() && !self.switches.demo_ui { - self.window.latest = Some(state); + 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.restore.map_or(state, |back| WindowState { maximized: true, ..back }) + } else if keeper.awaiting_maximize { + return; + } else { + keeper.restore = Some(state); + state + }; + keeper.latest = Some(state); } /// The window's place as the last run left it. @@ -518,6 +551,18 @@ impl AppState { self.window.saved.filter(WindowState::is_sane) } + /// Whether the window is opened to be maximized: the last run left it so + /// and no development switch sets its size. + pub fn wants_maximized(&self) -> bool { + 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). + pub fn awaiting_maximize(&self) -> bool { + self.window.awaiting_maximize + } + /// Write the window's place to the settings if it changed. fn flush_window(&mut self) { let Some(latest) = self.window.latest.take() else { return }; @@ -1568,6 +1613,71 @@ mod tests { next.read_with(cx, |app, _| assert_eq!(app.remembered_window(), Some(moved))); } + /// A window opened to be maximized reports the place it opened with first; + /// that is not what the user left, so `maximized: true` stays until the + /// window has been seen maximized. After that it is the user's: restoring + /// is kept, and a maximized window is kept with the size it returns to + /// (X11 reports the screen's). + #[gpui::test] + fn a_maximized_window_is_not_overwritten_before_it_was_seen_maximized(cx: &mut TestAppContext) { + let dir = TempDir::new("window-maximized"); + 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 tick = |cx: &mut TestAppContext| { + cx.executor().advance_clock(Duration::from_secs(1)); + cx.run_until_parked(); + }; + let kept = |cx: &mut TestAppContext| { + app.read_with(cx, |app, _| app.backend().prefs.get(&UI_WINDOW)).unwrap() + }; + app.read_with(cx, |app, _| { + assert!(app.wants_maximized() && app.awaiting_maximize()); + }); + + // The window as it opened, before the window manager maximized it. + app.update(cx, |app, _| { + app.window_moved(WindowState { maximized: false, ..place() }); + app.window_moved(WindowState { maximized: false, ..left }); + }); + tick(cx); + assert_eq!(kept(cx), left); + + // Maximized: the screen's size is not the size it returns to. + let screen = WindowState { width: 1920, height: 1080, x: Some(0), y: Some(0), ..left }; + app.update(cx, |app, _| app.window_moved(screen)); + tick(cx); + assert_eq!(kept(cx), left, "kept as left, with the size it returns to"); + app.read_with(cx, |app, _| assert!(!app.awaiting_maximize())); + + // The user restores it and moves it: that is kept. + let restored = WindowState { width: 800, height: 600, maximized: false, ..place() }; + app.update(cx, |app, _| app.window_moved(restored)); + tick(cx); + assert_eq!(kept(cx), restored); + + // Maximized again: the place it returns to is the last windowed one. + app.update(cx, |app, _| app.window_moved(screen)); + tick(cx); + assert_eq!(kept(cx), WindowState { maximized: true, ..restored }); + } + + /// A window that was not opened maximized keeps what it reports from the + /// first report on. + #[gpui::test] + fn a_window_not_opened_maximized_is_kept_from_its_first_report(cx: &mut TestAppContext) { + let (app, _, _) = entity_over(cx, Backend::in_memory().unwrap(), Switches::default()); + app.read_with(cx, |app, _| assert!(!app.wants_maximized() && !app.awaiting_maximize())); + app.update(cx, |app, _| app.window_moved(place())); + cx.executor().advance_clock(Duration::from_secs(1)); + cx.run_until_parked(); + app.read_with(cx, |app, _| { + assert_eq!(app.backend().prefs.get(&UI_WINDOW), Some(place())); + }); + } + /// A window sized by a development switch, or showing the sample data, is /// not the user's: what it reports is not kept. #[gpui::test] diff --git a/crates/voelin-gpui/src/lib.rs b/crates/voelin-gpui/src/lib.rs index f3e45ea..3186e8f 100644 --- a/crates/voelin-gpui/src/lib.rs +++ b/crates/voelin-gpui/src/lib.rs @@ -74,7 +74,9 @@ pub fn init_logging() { /// remembered place if a screen still shows its corner (a monitor can have /// gone since), else the middle of the screen at the default size. A place /// that has no position (Wayland does not tell one) is the remembered size -/// in the middle. +/// in the middle. A window left maximized is asked for as maximized here, and +/// again by [`MainView`] once it is shown (an X11 window manager loses this +/// request: it manages a window only from its map). fn window_bounds(switches: &Switches, remembered: Option, cx: &App) -> WindowBounds { let sized = |(width, height): (f32, f32)| size(px(width), px(height)); if let Some(forced) = switches.window { diff --git a/crates/voelin-gpui/src/view.rs b/crates/voelin-gpui/src/view.rs index f54b6c6..0b8d1c5 100644 --- a/crates/voelin-gpui/src/view.rs +++ b/crates/voelin-gpui/src/view.rs @@ -102,6 +102,11 @@ impl MainView { cx.observe_window_bounds(window, |this, window, cx| report(&this.app, window, cx)), ]; report(&app, window, cx); + // The window is mapped by now: a window the last run left maximized + // is maximized when its first frame is drawn. + if app.read(cx).awaiting_maximize() { + window.on_next_frame(maximize); + } Self { rail: cx.new(|cx| ServerRail::new(&app, cx)), topbar: cx.new(|cx| TopBar::new(&app, cx)), @@ -225,6 +230,29 @@ impl MainView { } } +/// Maximize a window the last run left maximized, once it is shown. GPUI asks +/// for it when it creates the window (`WindowBounds::Maximized`), before the +/// window is mapped, and an X11 window manager manages a window only from its +/// map: the request is lost there (openbox), and the window opens at its +/// restored size. `zoom_window` toggles on X11, so one that is maximized by now +/// (Wayland, a window manager that honours the early request) is left alone. +fn maximize(window: &mut Window, _: &mut App) { + if !window.is_maximized() { + zoom(window); + } +} + +#[cfg(not(test))] +fn zoom(window: &Window) { + window.zoom_window(); +} + +/// GPUI's test platform has no zoom (`unimplemented!`): the tests count it. +#[cfg(test)] +fn zoom(_: &Window) { + tests::ZOOMS.with(|zooms| zooms.set(zooms.get() + 1)); +} + /// Tell `app` where `window` is: it keeps the place at the next tick. fn report(app: &Entity, window: &Window, cx: &mut App) { if let Some(state) = window_state(window, cx) { @@ -325,6 +353,15 @@ mod tests { use crate::theme::Choice; use crate::window_bounds; + thread_local! { + /// How many times a window asked to be zoomed (`zoom` in this module). + pub(super) static ZOOMS: Cell = const { Cell::new(0) }; + } + + fn zooms() -> usize { + ZOOMS.with(Cell::get) + } + /// What a test of the shell holds: the root state, the window's view, the /// engine's events and the meter's counters. struct Shell { @@ -737,6 +774,75 @@ mod tests { assert_eq!(bounds.size, size(px(900.0), px(600.0))); } + /// A window the last run left maximized is maximized once, when it is + /// shown (X11 ignores the request GPUI makes before the map), and what it + /// reports before the window manager has answered does not overwrite the + /// setting: `maximized: true` survives a run that ends first. (GPUI's test + /// platform cannot zoom, so the request is counted, and the window manager's + /// answer is the test's own report; on X11 openbox it was seen at run time, + /// docs/ui.md.) + #[gpui::test] + fn a_window_left_maximized_asks_to_be_maximized_once_and_stays_remembered( + cx: &mut TestAppContext, + ) { + let dir = TempDir::new("shell-maximized"); + 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(); + ZOOMS.with(|zooms| zooms.set(0)); + let (shell, vcx) = open(cx, backend, Switches::default()); + assert_eq!(zooms(), 0, "not before the window's first frame"); + // The test platform draws without delivering the platform's next-frame + // callbacks: this is the first frame of a real window. + let delivered = vcx.update(|window, cx| window.simulate_next_frame(cx)); + assert_eq!((delivered, zooms()), (1, 1), "asked at the first frame"); + 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. + let handle = vcx.update(|window, _| window.window_handle()); + vcx.simulate_window_resize(handle, size(px(900.0), px(600.0))); + frame(vcx); + vcx.executor().advance_clock(Duration::from_secs(2)); + frame(vcx); + 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)); + } + + /// A window that was not left maximized is not zoomed. + #[gpui::test] + fn a_window_not_left_maximized_is_not_zoomed(cx: &mut TestAppContext) { + let backend = Backend::in_memory().unwrap(); + let left = WindowState { width: 1000, height: 700, x: None, y: None, maximized: false }; + backend.prefs.set(&UI_WINDOW, Some(left)).unwrap(); + ZOOMS.with(|zooms| zooms.set(0)); + let (shell, vcx) = open(cx, backend, Switches::default()); + frame(vcx); + assert_eq!(zooms(), 0); + assert!(!shell.app.read_with(vcx, |app, _| app.awaiting_maximize())); + } + + /// A development switch that sets the size wins over a maximized window. + #[gpui::test] + fn a_size_switch_wins_over_a_maximized_window(cx: &mut TestAppContext) { + let backend = Backend::in_memory().unwrap(); + let left = WindowState { width: 1000, height: 700, x: None, y: None, maximized: true }; + backend.prefs.set(&UI_WINDOW, Some(left)).unwrap(); + ZOOMS.with(|zooms| zooms.set(0)); + let switches = Switches { window: Some((1440.0, 960.0)), ..Switches::default() }; + let (shell, vcx) = open(cx, backend, switches); + frame(vcx); + assert_eq!(zooms(), 0); + assert!(!shell.app.read_with(vcx, |app, _| app.wants_maximized())); + } + /// Where the window opens: a remembered position that no screen shows is /// the middle of a screen; a maximized window stays maximized; a size the /// development switch names wins; nothing remembered is the default size. diff --git a/docs/ui.md b/docs/ui.md index ecf6f8b..86f5402 100644 --- a/docs/ui.md +++ b/docs/ui.md @@ -1230,8 +1230,33 @@ it at the next tick and at close, and the next run opens there: the size always, the position when a screen still shows its corner and the window manager lets a window choose (GPUI asks X11 for the place, and openbox, like most window managers, places new windows itself; Wayland tells no position at all, so none -is kept there), maximized if it was. A development switch that sets the size or -the sample data leaves it alone. +is kept there), maximized if it was. **Maximized** is asked for twice: GPUI +asks when it creates the window (`WindowBounds::Maximized`: Wayland and the +other platforms honour that), 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 +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 +window manager's answer themselves; it was seen at run time under openbox +(below). A development switch that sets the size or the sample data leaves the +setting alone. + +Seen at run time (the debug build of the commit that made it, Xvfb `:143` of +3840 by 2160 with openbox and Mesa's lavapipe, a state directory under the +checkout's `.scratch/`, no contact with the desktop's session): a first run +opened at 1280 by 800, openbox's `A-F10` (`ToggleMaximize`, bound in the run's +private `rc.xml`) maximized it (`_NET_WM_STATE_MAXIMIZED_VERT` and `_HORZ`, 3840 +by 2141 below the title bar), Alt+F4 closed it with exit status 0, and `ui.window` +held `{"width":1280,"height":800,"x":1280,"y":687,"maximized":true}`: the size +it returns to, not the screen's. A second run came up maximized with no one +touching it, and closed the same way leaving the same value. **What the shell costs idle** (`VOELIN_METER=1`, release build of the shell commit `6b4341a`, the later ones change nothing that runs while idle; 1440 by