From 87b963981e9cf8c1c2fcbecd82cec16772a69a0b Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Wed, 6 Jul 2022 13:43:46 -0700 Subject: [PATCH] win32: fixup initial window pos after screens changes Ensure that we correctly use CW_USEDEFAULT for the case where `--position` was not used. --- window/src/os/windows/window.rs | 41 ++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/window/src/os/windows/window.rs b/window/src/os/windows/window.rs index d2f5feb0e..08720bdc1 100644 --- a/window/src/os/windows/window.rs +++ b/window/src/os/windows/window.rs @@ -377,27 +377,32 @@ impl Window { let (width, height) = adjust_client_to_window_dimensions(style, geometry.width, geometry.height); - let (x, y) = if (style & WS_POPUP) == 0 { - (geometry.x, geometry.y) - } else { - // WS_POPUP windows need to specify the initial position. - // We pick the middle of the primary monitor + let (x, y) = match (geometry.x, geometry.y) { + (Some(x), Some(y)) => (x, y), + _ => { + if (style & WS_POPUP) == 0 { + (CW_USEDEFAULT, CW_USEDEFAULT) + } else { + // WS_POPUP windows need to specify the initial position. + // We pick the middle of the primary monitor - unsafe { - let mut mi: MONITORINFO = std::mem::zeroed(); - mi.cbSize = std::mem::size_of::() as u32; - GetMonitorInfoW( - MonitorFromWindow(std::ptr::null_mut(), MONITOR_DEFAULTTOPRIMARY), - &mut mi, - ); + unsafe { + let mut mi: MONITORINFO = std::mem::zeroed(); + mi.cbSize = std::mem::size_of::() as u32; + GetMonitorInfoW( + MonitorFromWindow(std::ptr::null_mut(), MONITOR_DEFAULTTOPRIMARY), + &mut mi, + ); - let mon_width = mi.rcMonitor.right - mi.rcMonitor.left; - let mon_height = mi.rcMonitor.bottom - mi.rcMonitor.top; + let mon_width = mi.rcMonitor.right - mi.rcMonitor.left; + let mon_height = mi.rcMonitor.bottom - mi.rcMonitor.top; - ( - mi.rcMonitor.left + (mon_width - width) / 2, - mi.rcMonitor.top + (mon_height - height) / 2, - ) + ( + mi.rcMonitor.left + (mon_width - width) / 2, + mi.rcMonitor.top + (mon_height - height) / 2, + ) + } + } } };