From d33ee53c921e024a0f0523735fc0cc1e84d91fbf Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Wed, 25 Nov 2026 18:53:21 +0800 Subject: [PATCH] fix(windows): open a project window no larger than the screen's work area (#5817) On Windows a project window opened at the configured 1440x900 and Restore Down returned to it, which ran under the taskbar on small or scaled screens (1920x1080 at 150%). The new window's size and minimum size are now shrunk to the primary monitor's work area. Fixes #5750 --- src-tauri/src/windows.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src-tauri/src/windows.rs b/src-tauri/src/windows.rs index a2eb1f530..030cfeeed 100644 --- a/src-tauri/src/windows.rs +++ b/src-tauri/src/windows.rs @@ -850,6 +850,16 @@ fn build_project_window(app: &AppHandle, mut config: WindowConfig) -> IdeResult< } #[cfg(target_os = "linux")] let show_after_build = crate::linux_window_state::prepare(app, &mut config); + // Windows opens a project window at the configured size (window-state restores only the + // maximized flag there), which Restore Down returns to. + #[cfg(windows)] + if let Ok(Some(monitor)) = app.primary_monitor() { + let area = monitor.work_area().size.to_logical::(monitor.scale_factor()); + let min = (config.min_width.unwrap_or(0.0), config.min_height.unwrap_or(0.0)); + let (size, min) = fit_new_window((config.width, config.height), min, (area.width, area.height)); + (config.width, config.height) = size; + (config.min_width, config.min_height) = (Some(min.0), Some(min.1)); + } let window = WebviewWindowBuilder::from_config(app, &config) .and_then(|b| dev_font_preferences(b).on_new_window(open_popup_in_browser).build()) .map_err(|e| IdeError::internal(format!("cannot create window: {e}")))?; @@ -2104,6 +2114,18 @@ fn fit_over_parent( rx.recv_timeout(FIT_ON_MAIN_TIMEOUT).map_err(|_| tauri::Error::FailedToReceiveMessage) } +/// A new window's size and minimum size (logical pixels) shrunk to fit `area`, the work area of +/// the monitor it opens on: the default 1440×900 is taller than a 1920×1080 laptop screen at +/// 150 % scaling once the taskbar is taken off. +#[cfg_attr(not(windows), allow(dead_code))] +fn fit_new_window(size: (f64, f64), min: (f64, f64), area: (f64, f64)) -> ((f64, f64), (f64, f64)) { + if area.0 <= 0.0 || area.1 <= 0.0 { + return (size, min); + } + let size = (size.0.min(area.0), size.1.min(area.1)); + (size, (min.0.min(size.0), min.1.min(size.1))) +} + /// Centres the settings window over its owner, inside the owner's monitor work area. The window /// may still be on another display with a different scale, so its size is converted with its own /// scale and its position is given in physical pixels. @@ -2926,6 +2948,19 @@ mod tests { } } + #[test] + fn a_new_window_fits_the_work_area() { + // 1920×1080 at 150 % with a 48-pixel taskbar (#5750). + let area = (1280.0, 1032.0 / 1.5); + assert_eq!(fit_new_window((1440.0, 900.0), (400.0, 500.0), area), ((1280.0, 688.0), (400.0, 500.0))); + // A minimum larger than the screen gives way too. + assert_eq!(fit_new_window((1440.0, 900.0), (400.0, 800.0), area).1, (400.0, 688.0)); + // A large screen keeps the configured size; an unknown work area changes nothing. + let big = ((1440.0, 900.0), (400.0, 500.0)); + assert_eq!(fit_new_window(big.0, big.1, (2560.0, 1400.0)), big); + assert_eq!(fit_new_window(big.0, big.1, (0.0, 0.0)), big); + } + #[test] fn settings_bounds_fit_current_work_area() { for (parent, size, work, expected) in [