From 57dbc644a423915d59d1a9e1c0072fd2f62a45dc Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 21:23:35 -0800 Subject: [PATCH] Window: a restored window fitted to a small work area is not held at the minimum size When the work area is smaller than the smallest window (800 x 500 points), `place` fits the restored window to it, but the native minimum inner size from `native_options` stayed at 800 x 500, so the system kept the window bigger than the work area. `restore` now lowers the minimum to the fitted size in each dimension where it is smaller, and keeps the normal minimum where the work area has room for it. --- apps/vectorcraft/src/window.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/apps/vectorcraft/src/window.rs b/apps/vectorcraft/src/window.rs index ce224584fd..e36add3d08 100644 --- a/apps/vectorcraft/src/window.rs +++ b/apps/vectorcraft/src/window.rs @@ -170,6 +170,12 @@ fn fit(size: [f32; 2], room: [f32; 2]) -> [f32; 2] { std::array::from_fn(|i| size[i].min(room[i]).max(MIN_SIZE[i].min(room[i])).max(1.0)) } +/// The smallest size a window of `size` points may take: [`MIN_SIZE`], lowered to `size` in each +/// dimension where [`fit`] made the window smaller than that to fit a small work area. +fn min_size(size: [f32; 2]) -> [f32; 2] { + std::array::from_fn(|i| MIN_SIZE[i].min(size[i])) +} + /// Whether to list the monitors again: on the first frame, when the UI zoom changed, and at most /// every [`MONITORS_EVERY`] seconds while the app draws. `listed` holds the last list's time and zoom. pub fn monitors_due(listed: Option<(f64, f32)>, now: f64, zoom: f32) -> bool { @@ -302,6 +308,12 @@ pub fn restore(window: &winit::window::Window, saved: Option) -> if let Some([x, y]) = g.pos { window.set_outer_position(PhysicalPosition::new(x, y)); } + // The native minimum (`MIN_SIZE`, set in `native_options`) would keep the window bigger than a + // size fitted to a small work area, so it is lowered to that size first. + let min = min_size(g.size); + if min != MIN_SIZE { + window.set_min_inner_size(Some(LogicalSize::new(min[0], min[1]))); + } // Returns the new size when it applies at once; either way the window gets it. let _ = window.request_inner_size(LogicalSize::new(g.size[0], g.size[1])); #[cfg(not(target_os = "linux"))] @@ -444,6 +456,21 @@ mod tests { assert_eq!(monitor(0, 0, 1920, 1080, 1.0).less_frame([16, 39]).work, None); } + #[test] + fn a_window_fitted_below_the_smallest_size_lowers_the_minimum_to_its_size() { + // 1366 × 768 at 150% with a 48-pixel taskbar along the bottom: a work area 480 points high. + let areas = [WorkArea { monitor: [0, 0, 1366, 768], work: [0, 0, 1366, 720] }]; + let m = [monitor(0, 0, 1366, 768, 1.5).with_work_area(&areas)]; + let g = place(saved(Some([0, 0]), [900.0, 500.0], false), &m, Some(0)); + assert_eq!(g.size, [900.0, 480.0]); + // The minimum height drops to the fitted 480 points; the width keeps the normal minimum. + assert_eq!(min_size(g.size), [800.0, 480.0]); + // A window with room for the smallest size keeps the normal minimum. + let big = [monitor(0, 0, 1920, 1080, 1.0)]; + assert_eq!(min_size(place(saved(Some([0, 0]), [1000.0, 700.0], false), &big, Some(0)).size), MIN_SIZE); + assert_eq!(min_size([1.0, 1.0]), [1.0, 1.0]); + } + #[test] fn macos_screen_rects_turn_into_winits_top_left_pixels() { // A 1512 × 982 point primary screen at 2× with a 37-point menu bar and an 80-point Dock,