From 39942953c16302838868a43c68f6e055161bb4b4 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Tue, 19 Nov 2024 01:04:10 +0200 Subject: [PATCH] fix(windows): use physical position when creating the window closes tauri-apps/tauri#11718 --- .changes/inital-position-windows.md | 5 +++ src/platform_impl/windows/window.rs | 51 ++++++++++++++++------------- 2 files changed, 33 insertions(+), 23 deletions(-) create mode 100644 .changes/inital-position-windows.md diff --git a/.changes/inital-position-windows.md b/.changes/inital-position-windows.md new file mode 100644 index 000000000..58ff0fbf9 --- /dev/null +++ b/.changes/inital-position-windows.md @@ -0,0 +1,5 @@ +--- +"tao": "patch" +--- + +On Windows, fix regression in initial window position when using logical positions. diff --git a/src/platform_impl/windows/window.rs b/src/platform_impl/windows/window.rs index 46c78e541..c571716cc 100644 --- a/src/platform_impl/windows/window.rs +++ b/src/platform_impl/windows/window.rs @@ -45,7 +45,8 @@ use crate::{ drop_handler::FileDropHandler, event_loop::{self, EventLoopWindowTarget, DESTROY_MSG_ID}, icon::{self, IconType}, - monitor, util, + monitor::{self}, + util, window_state::{CursorFlags, SavedWindow, WindowFlags, WindowState}, OsError, Parent, PlatformSpecificWindowBuilderAttributes, WindowId, }, @@ -1170,37 +1171,41 @@ unsafe fn init( let (style, ex_style) = window_flags.to_window_styles(); let title = util::encode_wide(&attributes.title); - let primary_monitor = event_loop.primary_monitor().map(|m| m.inner); - - let position = attributes.position.map(|p| { - p.to_logical::( - primary_monitor - .as_ref() - .map(|m| m.scale_factor()) - .unwrap_or(1.0), - ) - }); - - let default_scale_factor = position - .and_then(|p| event_loop.monitor_from_point(p.x, p.y)) - .or_else(|| primary_monitor) - .map(|m| m.scale_factor()) - .unwrap_or(1.0); + let (target_monitor, position) = attributes + .position + .and_then(|p| { + monitor::available_monitors() + .into_iter() + .find_map(|monitor| { + let position = p.to_physical::(monitor.scale_factor()); + let (x, y): (i32, i32) = monitor.position().into(); + let (width, height): (i32, i32) = monitor.size().into(); + + if x <= position.x + && position.x <= x + width + && y <= position.y + && position.y <= y + height + { + Some((monitor, position.into())) + } else { + None + } + }) + }) + .unwrap_or_else(|| (monitor::primary_monitor(), (CW_USEDEFAULT, CW_USEDEFAULT))); let desired_size = attributes .inner_size .unwrap_or_else(|| PhysicalSize::new(800, 600).into()); let clamped_size = attributes .inner_size_constraints - .clamp(desired_size, default_scale_factor); - - let position = position - .map(|p| p.into()) - .unwrap_or((CW_USEDEFAULT, CW_USEDEFAULT)); + .clamp(desired_size, target_monitor.scale_factor()); // Best effort: try to create the window with the requested inner size let adjusted_size = { - let (w, h): (i32, i32) = clamped_size.to_physical::(default_scale_factor).into(); + let (w, h): (i32, i32) = clamped_size + .to_physical::(target_monitor.scale_factor()) + .into(); let mut rect = RECT { left: 0, top: 0,