From cb7c69932aeb97db4e3960515802cf986ad3b05e Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 5 Dec 2026 02:12:48 -0800 Subject: [PATCH] Windows: a maximize skips the monitor above's taskbar; frame limit scales with DPI With monitors stacked vertically, the upper monitor's auto-hidden bottom taskbar is parked mostly past its edge, so most of it lies on the monitor below. The guard counted it as that monitor's top taskbar and trimmed a maximize there a pixel short of the top. A taskbar that is not wholly on a monitor now counts only when most of it is off that monitor, as an auto-hidden taskbar of its own is. The largest frame a parked maximize may have was a fixed 32 px. The resize border grows with DPI (about 40 px at 500 %), so at high scaling a maximize covering the taskbar was never moved clear of it. The limit is now 32 px at 96 DPI, scaled by the window's DPI. --- apps/vectorcraft/src/window_frame.rs | 72 +++++++++++++++++++++------- 1 file changed, 56 insertions(+), 16 deletions(-) diff --git a/apps/vectorcraft/src/window_frame.rs b/apps/vectorcraft/src/window_frame.rs index ba5d3d298d..fb62a91eac 100644 --- a/apps/vectorcraft/src/window_frame.rs +++ b/apps/vectorcraft/src/window_frame.rs @@ -140,7 +140,8 @@ mod platform { let (rect, monitor, work) = ((r.left, r.top, r.right, r.bottom), (m.left, m.top, m.right, m.bottom), (w.left, w.top, w.right, w.bottom)); // The taskbars are looked up (a few `FindWindowEx` calls) only while the window covers its // whole monitor, which it no longer does once trimmed. - let Some((left, top, right, bottom)) = super::clamped_rect(rect, monitor, work).or_else(|| { + let frame = super::frame_limit(hwnd.GetDpiForWindow()); + let Some((left, top, right, bottom)) = super::clamped_rect(rect, monitor, work, frame).or_else(|| { super::covers(rect, monitor).then(|| super::short_of_autohide_taskbars(rect, monitor, work, &taskbar_edges(monitor))).flatten() }) else { return; @@ -238,21 +239,29 @@ impl Viewports { } } -/// How far a parked maximize reaches past a monitor or work-area edge: the resize border (about -/// 8 pt at 100 %, more with DPI). Past that, a maximized window's rect is not parked but being -/// dragged or snapped, and is left alone. -const FRAME: i32 = 32; +/// How far, in physical pixels at 96 DPI (100 %), a parked maximize reaches past a monitor or +/// work-area edge at most: the resize border is about 8 px at 100 %. Past that, a maximized +/// window's rect is not parked but being dragged or snapped, and is left alone. +const FRAME_AT_96_DPI: i32 = 32; + +/// [`FRAME_AT_96_DPI`] for a window at `dpi`, which the resize border grows with; never less than +/// at 96 DPI, so an unknown DPI (0) counts as 100 %. +#[cfg_attr(not(all(windows, feature = "wgpu")), allow(dead_code))] +fn frame_limit(dpi: u32) -> i32 { + let scaled = i64::from(FRAME_AT_96_DPI) * i64::from(dpi) / 96; + i32::try_from(scaled).unwrap_or(i32::MAX).max(FRAME_AT_96_DPI) +} /// The rect to move a maximize to so it stays inside the work area on every edge, plus the resize /// border it was parked with, or `None` when it already does or `rect` (`left, top, right, /// bottom`, desktop coordinates) is not parked at its maximize place. `monitor` is the monitor's -/// full rect and `work` its work area (the monitor less the taskbar) in the same coordinates. The -/// border is read from the edges that have no taskbar, and every edge must sit that far outside -/// either the monitor (where Windows puts a caption-less maximize) or the work area (where the -/// guard puts it), so a rect it already moved is left alone. Platform-neutral so it is tested -/// everywhere. +/// full rect and `work` its work area (the monitor less the taskbar) in the same coordinates; +/// `frame` is the largest border a parked maximize has ([`frame_limit`]). The border is read from +/// the edges that have no taskbar, and every edge must sit that far outside either the monitor +/// (where Windows puts a caption-less maximize) or the work area (where the guard puts it), so a +/// rect it already moved is left alone. Platform-neutral so it is tested everywhere. #[cfg_attr(not(all(windows, feature = "wgpu")), allow(dead_code))] -fn clamped_rect(rect: (i32, i32, i32, i32), monitor: (i32, i32, i32, i32), work: (i32, i32, i32, i32)) -> Option<(i32, i32, i32, i32)> { +fn clamped_rect(rect: (i32, i32, i32, i32), monitor: (i32, i32, i32, i32), work: (i32, i32, i32, i32), frame: i32) -> Option<(i32, i32, i32, i32)> { let (l, t, r, b) = rect; let (ml, mt, mr, mb) = monitor; let (wl, wt, wr, wb) = work; @@ -269,7 +278,7 @@ fn clamped_rect(rect: (i32, i32, i32, i32), monitor: (i32, i32, i32, i32), work: ] .into_iter() .flatten() - .filter(|n| (0..=FRAME).contains(n)) + .filter(|n| (0..=frame).contains(n)) .min()?; let parked = |monitor: Option, work: Option| monitor == Some(border) || work == Some(border); if !parked(ml.checked_sub(l), wl.checked_sub(l)) @@ -304,9 +313,11 @@ fn covers(rect: Rect, monitor: Rect) -> bool { } /// The edge of `monitor` that a taskbar with the rect `taskbar` sits on: the edge nearest the -/// taskbar's center, or `None` when the taskbar does not overlap the monitor. An auto-hidden -/// taskbar is parked mostly past its edge with a sliver left on the monitor, so its center can lie -/// outside the monitor. +/// taskbar's center, or `None` when the taskbar is not this monitor's. A shown taskbar lies wholly +/// on its monitor. An auto-hidden taskbar is parked mostly past its edge with a sliver left on the +/// monitor, so its center can lie outside the monitor; the part past the edge can lie on a +/// neighboring monitor, which a taskbar that is neither wholly on the monitor nor mostly off it +/// belongs to. #[cfg_attr(not(all(windows, feature = "wgpu")), allow(dead_code))] fn taskbar_edge(taskbar: Rect, monitor: Rect) -> Option { let (tl, tt, tr, tb) = (i64::from(taskbar.0), i64::from(taskbar.1), i64::from(taskbar.2), i64::from(taskbar.3)); @@ -314,6 +325,12 @@ fn taskbar_edge(taskbar: Rect, monitor: Rect) -> Option { if tl >= tr || tt >= tb || ml >= mr || mt >= mb || tr <= ml || tl >= mr || tb <= mt || tt >= mb { return None; } + let inside = tl >= ml && tt >= mt && tr <= mr && tb <= mb; + let area = |l: i64, t: i64, r: i64, b: i64| i128::from(r - l) * i128::from(b - t); + let on_monitor = area(tl.max(ml), tt.max(mt), tr.min(mr), tb.min(mb)); + if !inside && on_monitor * 2 >= area(tl, tt, tr, tb) { + return None; + } // The center and the distances are doubled, so nothing is rounded. let (cx, cy) = (tl + tr, tt + tb); [(Edge::Left, cx - 2 * ml), (Edge::Top, cy - 2 * mt), (Edge::Right, 2 * mr - cx), (Edge::Bottom, 2 * mb - cy)] @@ -346,7 +363,12 @@ fn short_of_autohide_taskbars(rect: Rect, monitor: Rect, work: Rect, taskbars: & #[cfg(test)] mod tests { - use super::{Edge, Viewports, clamped_rect, short_of_autohide_taskbars, taskbar_edge}; + use super::{Edge, Viewports, frame_limit, short_of_autohide_taskbars, taskbar_edge}; + + /// [`super::clamped_rect`] for a window at 96 DPI (100 %). + fn clamped_rect(rect: (i32, i32, i32, i32), monitor: (i32, i32, i32, i32), work: (i32, i32, i32, i32)) -> Option<(i32, i32, i32, i32)> { + super::clamped_rect(rect, monitor, work, frame_limit(96)) + } fn viewport(name: &str) -> egui::ViewportId { egui::ViewportId::from_hash_of(name) @@ -453,6 +475,18 @@ mod tests { } } + #[test] + fn the_frame_limit_grows_with_the_monitor_dpi() { + // At 500 % (480 DPI) the resize border is about 40 px, past the limit at 100 %. + let (monitor, work) = ((0, 0, 3840, 2160), (0, 0, 3840, 2000)); + let rect = (-40, -40, 3880, 2200); + assert_eq!(clamped_rect(rect, monitor, work), None, "100 %"); + assert_eq!(super::clamped_rect(rect, monitor, work, frame_limit(480)), Some((-40, -40, 3880, 2040)), "500 %"); + assert_eq!(super::clamped_rect((-8, -8, 1928 + 40, 1088), (0, 0, 1920, 1080), (0, 0, 1920, 1046), frame_limit(96)), None, "past the limit"); + assert_eq!(frame_limit(0), frame_limit(96), "an unknown DPI counts as 100 %"); + assert!(frame_limit(u32::MAX) > frame_limit(480), "no overflow"); + } + #[test] fn a_maximize_without_a_reserved_taskbar_is_left_alone() { let monitor = (0, 0, 1920, 1080); @@ -503,6 +537,12 @@ mod tests { assert_eq!(taskbar_edge((-60, 0, 2, 1080), monitor), Some(Edge::Left)); assert_eq!(taskbar_edge((1918, 0, 1980, 1080), monitor), Some(Edge::Right)); assert_eq!(taskbar_edge((1920, 1032, 3840, 1080), monitor), None, "other monitor"); + // With monitors stacked, the part of the upper monitor's auto-hidden taskbar parked past its + // bottom edge lies on the monitor below, which it does not belong to. + let below = (0, 1080, 1920, 2160); + assert_eq!(taskbar_edge((0, 1078, 1920, 1126), below), None, "neighbor's auto-hidden taskbar"); + assert_eq!(taskbar_edge((0, 2158, 1920, 2206), below), Some(Edge::Bottom), "its own auto-hidden taskbar"); + assert_eq!(taskbar_edge((0, 1080, 1920, 1128), below), Some(Edge::Top), "its own shown top taskbar"); assert!(taskbar_edge((i32::MIN, i32::MIN, i32::MAX, i32::MAX), monitor).is_some(), "no overflow"); } }