From 57b891215ef7075dc7e4b42ce0fe657e01bcb1ee Mon Sep 17 00:00:00 2001 From: Puneet Dixit Date: Thu, 18 Jun 2026 10:53:02 +0530 Subject: [PATCH] fix: restore default size for collapsed window state (#7215) Co-authored-by: puneetdixit200 <236133619+puneetdixit200@users.noreply.github.com> Co-authored-by: wonfen <96291150+wonfen@users.noreply.github.com> --- src-tauri/src/utils/resolve/window.rs | 44 +++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src-tauri/src/utils/resolve/window.rs b/src-tauri/src/utils/resolve/window.rs index 884dc5d33..260e4f82e 100644 --- a/src-tauri/src/utils/resolve/window.rs +++ b/src-tauri/src/utils/resolve/window.rs @@ -23,6 +23,26 @@ const DEFAULT_DECORATIONS: bool = false; #[cfg(not(target_os = "linux"))] const DEFAULT_DECORATIONS: bool = true; +const fn restored_window_size_is_too_small(width: u32, height: u32) -> bool { + width < MINIMAL_WIDTH as u32 || height < MINIMAL_HEIGHT as u32 +} + +fn restore_default_size_if_needed(window: &WebviewWindow) { + let Ok(size) = window.outer_size() else { + return; + }; + + if !restored_window_size_is_too_small(size.width, size.height) { + return; + } + + logging_error!( + Type::Window, + window.set_size(tauri::LogicalSize::new(DEFAULT_WIDTH, DEFAULT_HEIGHT)) + ); + logging_error!(Type::Window, window.center()); +} + /// 构建新的 WebView 窗口 pub async fn build_new_window() -> Result { let app_handle = handle::Handle::app_handle(); @@ -88,8 +108,32 @@ pub async fn build_new_window() -> Result { match builder.build() { Ok(window) => { logging_error!(Type::Window, window.set_background_color(Some(background_color))); + restore_default_size_if_needed(&window); Ok(window) } Err(e) => Err(e.to_string()), } } + +#[cfg(test)] +mod tests { + use super::restored_window_size_is_too_small; + + #[test] + fn restored_window_size_rejects_zero_dimensions() { + assert!(restored_window_size_is_too_small(0, 700)); + assert!(restored_window_size_is_too_small(940, 0)); + } + + #[test] + fn restored_window_size_rejects_dimensions_below_minimum() { + assert!(restored_window_size_is_too_small(519, 700)); + assert!(restored_window_size_is_too_small(940, 519)); + } + + #[test] + fn restored_window_size_accepts_minimum_or_larger_dimensions() { + assert!(!restored_window_size_is_too_small(520, 520)); + assert!(!restored_window_size_is_too_small(940, 700)); + } +}