From 16c14a49b430d12ab649ad3bceff5cefb3c5ca10 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Wed, 16 Jul 2025 19:01:44 -0400 Subject: [PATCH] don't set window off to the bottom right --- .../core/utils/window/position_main_window.rs | 35 +++++++++++++++++++ .../core/utils/window/resize_main_window.rs | 9 +++-- 2 files changed, 41 insertions(+), 3 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 f6166639c7..df620c1f9c 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 @@ -3,6 +3,9 @@ use crate::core::windows::main_window::constants::MAIN_WINDOW_NAME; use errors::AnyhowResult; use tauri::{AppHandle, Manager, PhysicalPosition}; +/// Try to position the main window at startup. +/// If it doesn't meet certain placement heuristics, abandon manual placement and let the OS +/// decide where to place the app. pub fn position_main_window( app: &AppHandle, position: &MainWindowPosition, @@ -10,6 +13,8 @@ pub fn position_main_window( // NB: This non-negative position heuristic is good for MacBook (single screen). // Not sure how this fares on Windows, Linux, or multiscreen setups. + // 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 { return Err(anyhow::anyhow!("X position must be non-negative")); } else if position.y < 0 { @@ -17,9 +22,39 @@ pub fn position_main_window( } let windows = app.windows(); + let window = windows.get(MAIN_WINDOW_NAME) .ok_or_else(|| anyhow::anyhow!("Main window not found"))?; + if let Ok(Some(monitor)) = window.current_monitor() { + // Reports the size of the monitor, e.g. PhysicalSize { width: 4112, height: 2658 } + // This is not impacted by `scale_factor`, even if that's a multiple like "2". + // i.e., positioning the app at `monitor.size().width/2` and `monitor.size().height/2` + // will place the **top left corner of the app** in roughly the center of the monitor. + let monitor_size = monitor.size(); + + println!("Current monitor - size: {:?}", monitor_size); + 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 max_width = monitor_size.width.saturating_sub(width_padding); + let max_height = monitor_size.height.saturating_sub(height_padding); + + let max_width = i32::try_from(max_width)?; + let max_height = i32::try_from(max_height)?; + + 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)); + } else if position.y > max_height { + return Err(anyhow::anyhow!("Y position must be less than or equal to {} (monitor height {})", + max_height, monitor_size.height)); + } + } + window.set_position(PhysicalPosition::new(position.x, position.y))?; Ok(()) 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 e05ccdd4e3..e09470af8b 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 @@ -2,6 +2,9 @@ use crate::core::state::window::main_window_size::MainWindowSize; use crate::core::windows::main_window::constants::{MAIN_WINDOW_MIN_HEIGHT, MAIN_WINDOW_MIN_WIDTH, MAIN_WINDOW_NAME}; use tauri::{AppHandle, Manager, PhysicalSize}; +/// Try to resize the main window at startup. +/// If it doesn't meet certain sizing heuristics, abandon manual resize and let the OS +/// decide how to size the app. pub fn resize_main_window( app: &AppHandle, size: &MainWindowSize, @@ -12,12 +15,12 @@ pub fn resize_main_window( } else if size.height < MAIN_WINDOW_MIN_HEIGHT { return Err(anyhow::anyhow!("Height must be at least {}", MAIN_WINDOW_MIN_HEIGHT)); } - + let windows = app.windows(); let window = windows.get(MAIN_WINDOW_NAME) .ok_or_else(|| anyhow::anyhow!("Main window not found"))?; - + window.set_size(PhysicalSize::new(size.width, size.height))?; - + Ok(()) } \ No newline at end of file