From 44889e735bcab5cb7832acf8631180aaf96e52f6 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Wed, 16 Jul 2025 19:12:41 -0400 Subject: [PATCH] can't size too small --- .../core/utils/window/position_main_window.rs | 8 +++++-- .../core/utils/window/resize_main_window.rs | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/crates/desktop/artcraft/src/core/utils/window/position_main_window.rs b/crates/desktop/artcraft/src/core/utils/window/position_main_window.rs index df620c1f9c..5d01076995 100644 --- a/crates/desktop/artcraft/src/core/utils/window/position_main_window.rs +++ b/crates/desktop/artcraft/src/core/utils/window/position_main_window.rs @@ -16,6 +16,8 @@ pub fn position_main_window( // On Mac, this prevents the windows from being positioned off to the top-left, // as (0,0) is the top left directly under the top menu bar. if position.x < 0 { + // TODO(bt,2025-07-16): just in the case of x/width, allow slight placement off + // screen as long as the app is wide enough. return Err(anyhow::anyhow!("X position must be non-negative")); } else if position.y < 0 { return Err(anyhow::anyhow!("Y position must be non-negative")); @@ -37,8 +39,8 @@ pub fn position_main_window( println!("Current monitor - scale factor: {:?}", monitor.scale_factor()); println!("Current monitor - position: {:?}", monitor.position()); - let width_padding = monitor_size.width.div_ceil(4); - let height_padding = monitor_size.height.div_ceil(4); + let width_padding = monitor_size.width.div_ceil(6); + let height_padding = monitor_size.height.div_ceil(6); let max_width = monitor_size.width.saturating_sub(width_padding); let max_height = monitor_size.height.saturating_sub(height_padding); @@ -46,6 +48,8 @@ pub fn position_main_window( let max_width = i32::try_from(max_width)?; let max_height = i32::try_from(max_height)?; + // Prevent the window from being positioned off the screen to the bottom, right, and bottom right. + // This works on MacBook (single screen), but should be tested on Windows, Linux, and multiscreen setups. if position.x > max_width { return Err(anyhow::anyhow!("X position must be less than or equal to {} (monitor width {})", max_width, monitor_size.width)); diff --git a/crates/desktop/artcraft/src/core/utils/window/resize_main_window.rs b/crates/desktop/artcraft/src/core/utils/window/resize_main_window.rs index e09470af8b..f16a10cc82 100644 --- a/crates/desktop/artcraft/src/core/utils/window/resize_main_window.rs +++ b/crates/desktop/artcraft/src/core/utils/window/resize_main_window.rs @@ -20,6 +20,29 @@ pub fn resize_main_window( let window = windows.get(MAIN_WINDOW_NAME) .ok_or_else(|| anyhow::anyhow!("Main window not found"))?; + + if let Ok(Some(monitor)) = window.current_monitor() { + let scale_factor = monitor.scale_factor(); + + if scale_factor > 1.0 { + + let min_width = MAIN_WINDOW_MIN_WIDTH as f64 * scale_factor; + let min_height = MAIN_WINDOW_MIN_HEIGHT as f64 * scale_factor; + + // Is this safe? https://users.rust-lang.org/t/convert-an-f64-to-an-i32/50991/4 + let min_width = min_width as u32; + let min_height = min_height as u32; + + if size.width < min_width { + return Err(anyhow::anyhow!("Width must be at least {} (given scale factor {})", + min_width, scale_factor)); + } else if size.height < min_height { + return Err(anyhow::anyhow!("Height must be at least {} (given scale factor {})", + min_height, scale_factor)); + } + } + } + window.set_size(PhysicalSize::new(size.width, size.height))?; Ok(())