Skip to content

Commit 4c84770

Browse files
committed
switch to tao implementation of the fix
1 parent a1a9832 commit 4c84770

File tree

3 files changed

+103
-91
lines changed

3 files changed

+103
-91
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ windows-sys = { version = "0.52.0", features = [
269269
"Win32_UI_TextServices",
270270
"Win32_UI_WindowsAndMessaging",
271271
] }
272+
windows-version = "0.1.1"
272273

273274
# Linux
274275
[target.'cfg(all(unix, not(any(target_os = "redox", target_family = "wasm", target_os = "android", target_vendor = "apple"))))'.dependencies]

src/platform_impl/windows/event_loop.rs

Lines changed: 98 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -2439,103 +2439,110 @@ unsafe fn public_window_callback_inner(
24392439
}
24402440

24412441
let new_outer_rect: RECT;
2442-
{
2443-
let suggested_ul =
2444-
(suggested_rect.left + margin_left, suggested_rect.top + margin_top);
2445-
2446-
let mut conservative_rect = RECT {
2447-
left: suggested_ul.0,
2448-
top: suggested_ul.1,
2449-
right: suggested_ul.0 + new_physical_surface_size.width as i32,
2450-
bottom: suggested_ul.1 + new_physical_surface_size.height as i32,
2451-
};
2452-
2453-
conservative_rect = window_flags
2454-
.adjust_rect(window, conservative_rect)
2455-
.unwrap_or(conservative_rect);
2456-
2457-
// If we're dragging the window, offset the window so that the cursor's
2458-
// relative horizontal position in the title bar is preserved.
2459-
if dragging_window {
2460-
let bias = {
2461-
let cursor_pos = {
2462-
let mut pos = unsafe { mem::zeroed() };
2463-
unsafe { GetCursorPos(&mut pos) };
2464-
pos
2465-
};
2466-
let suggested_cursor_horizontal_ratio = (cursor_pos.x - suggested_rect.left)
2467-
as f64
2468-
/ (suggested_rect.right - suggested_rect.left) as f64;
2469-
2470-
(cursor_pos.x
2471-
- (suggested_cursor_horizontal_ratio
2472-
* (conservative_rect.right - conservative_rect.left) as f64)
2473-
as i32)
2474-
- conservative_rect.left
2442+
if util::WIN_VERSION.build < 22000 {
2443+
// The window position needs adjustment on Windows 10.
2444+
{
2445+
let suggested_ul =
2446+
(suggested_rect.left + margin_left, suggested_rect.top + margin_top);
2447+
2448+
let mut conservative_rect = RECT {
2449+
left: suggested_ul.0,
2450+
top: suggested_ul.1,
2451+
right: suggested_ul.0 + new_physical_surface_size.width as i32,
2452+
bottom: suggested_ul.1 + new_physical_surface_size.height as i32,
24752453
};
2476-
conservative_rect.left += bias;
2477-
conservative_rect.right += bias;
2478-
}
24792454

2480-
// Check to see if the new window rect is on the monitor with the new DPI factor.
2481-
// If it isn't, offset the window so that it is.
2482-
let new_dpi_monitor = unsafe { MonitorFromWindow(window, MONITOR_DEFAULTTONULL) };
2483-
let conservative_rect_monitor =
2484-
unsafe { MonitorFromRect(&conservative_rect, MONITOR_DEFAULTTONULL) };
2485-
new_outer_rect = if conservative_rect_monitor == new_dpi_monitor {
2486-
conservative_rect
2487-
} else {
2488-
let get_monitor_rect = |monitor| {
2489-
let mut monitor_info = MONITORINFO {
2490-
cbSize: mem::size_of::<MONITORINFO>() as _,
2491-
..unsafe { mem::zeroed() }
2455+
conservative_rect = window_flags
2456+
.adjust_rect(window, conservative_rect)
2457+
.unwrap_or(conservative_rect);
2458+
2459+
// If we're dragging the window, offset the window so that the cursor's
2460+
// relative horizontal position in the title bar is preserved.
2461+
if dragging_window {
2462+
let bias = {
2463+
let cursor_pos = {
2464+
let mut pos = unsafe { mem::zeroed() };
2465+
unsafe { GetCursorPos(&mut pos) };
2466+
pos
2467+
};
2468+
let suggested_cursor_horizontal_ratio =
2469+
(cursor_pos.x - suggested_rect.left) as f64
2470+
/ (suggested_rect.right - suggested_rect.left) as f64;
2471+
2472+
(cursor_pos.x
2473+
- (suggested_cursor_horizontal_ratio
2474+
* (conservative_rect.right - conservative_rect.left) as f64)
2475+
as i32)
2476+
- conservative_rect.left
24922477
};
2493-
unsafe { GetMonitorInfoW(monitor, &mut monitor_info) };
2494-
monitor_info.rcMonitor
2495-
};
2496-
let wrong_monitor = conservative_rect_monitor;
2497-
let wrong_monitor_rect = get_monitor_rect(wrong_monitor);
2498-
let new_monitor_rect = get_monitor_rect(new_dpi_monitor);
2499-
2500-
// The direction to nudge the window in to get the window onto the monitor with
2501-
// the new DPI factor. We calculate this by seeing which monitor edges are
2502-
// shared and nudging away from the wrong monitor based on those.
2503-
#[allow(clippy::bool_to_int_with_if)]
2504-
let delta_nudge_to_dpi_monitor = (
2505-
if wrong_monitor_rect.left == new_monitor_rect.right {
2506-
-1
2507-
} else if wrong_monitor_rect.right == new_monitor_rect.left {
2508-
1
2509-
} else {
2510-
0
2511-
},
2512-
if wrong_monitor_rect.bottom == new_monitor_rect.top {
2513-
1
2514-
} else if wrong_monitor_rect.top == new_monitor_rect.bottom {
2515-
-1
2516-
} else {
2517-
0
2518-
},
2519-
);
2478+
conservative_rect.left += bias;
2479+
conservative_rect.right += bias;
2480+
}
25202481

2521-
let abort_after_iterations = new_monitor_rect.right - new_monitor_rect.left
2522-
+ new_monitor_rect.bottom
2523-
- new_monitor_rect.top;
2524-
for _ in 0..abort_after_iterations {
2525-
conservative_rect.left += delta_nudge_to_dpi_monitor.0;
2526-
conservative_rect.right += delta_nudge_to_dpi_monitor.0;
2527-
conservative_rect.top += delta_nudge_to_dpi_monitor.1;
2528-
conservative_rect.bottom += delta_nudge_to_dpi_monitor.1;
2529-
2530-
if unsafe { MonitorFromRect(&conservative_rect, MONITOR_DEFAULTTONULL) }
2531-
== new_dpi_monitor
2532-
{
2533-
break;
2482+
// Check to see if the new window rect is on the monitor with the new DPI factor.
2483+
// If it isn't, offset the window so that it is.
2484+
let new_dpi_monitor =
2485+
unsafe { MonitorFromWindow(window, MONITOR_DEFAULTTONULL) };
2486+
let conservative_rect_monitor =
2487+
unsafe { MonitorFromRect(&conservative_rect, MONITOR_DEFAULTTONULL) };
2488+
new_outer_rect = if conservative_rect_monitor == new_dpi_monitor {
2489+
conservative_rect
2490+
} else {
2491+
let get_monitor_rect = |monitor| {
2492+
let mut monitor_info = MONITORINFO {
2493+
cbSize: mem::size_of::<MONITORINFO>() as _,
2494+
..unsafe { mem::zeroed() }
2495+
};
2496+
unsafe { GetMonitorInfoW(monitor, &mut monitor_info) };
2497+
monitor_info.rcMonitor
2498+
};
2499+
let wrong_monitor = conservative_rect_monitor;
2500+
let wrong_monitor_rect = get_monitor_rect(wrong_monitor);
2501+
let new_monitor_rect = get_monitor_rect(new_dpi_monitor);
2502+
2503+
// The direction to nudge the window in to get the window onto the monitor with
2504+
// the new DPI factor. We calculate this by seeing which monitor edges are
2505+
// shared and nudging away from the wrong monitor based on those.
2506+
#[allow(clippy::bool_to_int_with_if)]
2507+
let delta_nudge_to_dpi_monitor = (
2508+
if wrong_monitor_rect.left == new_monitor_rect.right {
2509+
-1
2510+
} else if wrong_monitor_rect.right == new_monitor_rect.left {
2511+
1
2512+
} else {
2513+
0
2514+
},
2515+
if wrong_monitor_rect.bottom == new_monitor_rect.top {
2516+
1
2517+
} else if wrong_monitor_rect.top == new_monitor_rect.bottom {
2518+
-1
2519+
} else {
2520+
0
2521+
},
2522+
);
2523+
2524+
let abort_after_iterations = new_monitor_rect.right - new_monitor_rect.left
2525+
+ new_monitor_rect.bottom
2526+
- new_monitor_rect.top;
2527+
for _ in 0..abort_after_iterations {
2528+
conservative_rect.left += delta_nudge_to_dpi_monitor.0;
2529+
conservative_rect.right += delta_nudge_to_dpi_monitor.0;
2530+
conservative_rect.top += delta_nudge_to_dpi_monitor.1;
2531+
conservative_rect.bottom += delta_nudge_to_dpi_monitor.1;
2532+
2533+
if unsafe { MonitorFromRect(&conservative_rect, MONITOR_DEFAULTTONULL) }
2534+
== new_dpi_monitor
2535+
{
2536+
break;
2537+
}
25342538
}
2535-
}
25362539

2537-
conservative_rect
2538-
};
2540+
conservative_rect
2541+
};
2542+
}
2543+
} else {
2544+
// The suggested position is fine w/o adjustment on Windows 11.
2545+
new_outer_rect = suggested_rect
25392546
}
25402547

25412548
unsafe {

src/platform_impl/windows/util.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::iter::once;
33
use std::ops::BitAnd;
44
use std::os::windows::prelude::{OsStrExt, OsStringExt};
55
use std::sync::atomic::{AtomicBool, Ordering};
6+
use std::sync::LazyLock;
67
use std::{io, mem, ptr};
78

89
use windows_sys::core::{HRESULT, PCWSTR};
@@ -26,6 +27,9 @@ use windows_sys::Win32::UI::WindowsAndMessaging::{
2627
use crate::utils::Lazy;
2728
use crate::window::CursorIcon;
2829

30+
pub static WIN_VERSION: LazyLock<windows_version::OsVersion> =
31+
LazyLock::new(windows_version::OsVersion::current);
32+
2933
pub fn encode_wide(string: impl AsRef<OsStr>) -> Vec<u16> {
3034
string.as_ref().encode_wide().chain(once(0)).collect()
3135
}

0 commit comments

Comments
 (0)