don't set window off to the bottom right

This commit is contained in:
Brandon Thomas committed 2025-07-16 19:01:44 -04:00
1 parent 5e7f8ca8d1
commit 16c14a49b4
2 files changed
+41 -3

No files matched your search

@@ -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(())
@@ -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(())
}