docking: a restored native window opens in the monitor's work area (#1270)

This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-16 21:36:37 -05:00
1 parent 5018f14e14
commit b554424c08
5 files changed
+139 -43

No files matched your search

+6 -1
View File
@@ -108,6 +108,11 @@ pub struct Monitor {
pub rect: [f32; 4],
/// Physical pixels per logical point.
pub scale: f32,
/// The part of `rect` windows may use, without the taskbar, Dock or menu bar:
/// `[x, y, width, height]` on the desktop. Restored native windows keep their bar in it.
/// None, or an area outside `rect`: all of `rect`.
#[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "Option::is_none"))]
pub work_area: Option<[f32; 4]>,
}
/// A window id no other window of this process has drawn, from a random start so ids saved
@@ -583,7 +588,7 @@ pub(super) fn valid_rect(rect: [f32; 4]) -> bool {
/// A floating window's saved monitor and monitor size, each where given.
fn valid_monitor(monitor: Option<&Monitor>, monitor_size: Option<[f32; 2]>) -> bool {
monitor.is_none_or(|m| valid_rect(m.rect) && m.scale.is_finite() && m.scale > 0.0)
monitor.is_none_or(|m| valid_rect(m.rect) && m.scale.is_finite() && m.scale > 0.0 && m.work_area.is_none_or(valid_rect))
&& monitor_size.is_none_or(|[w, h]| valid_rect([0.0, 0.0, w, h]))
}
+5 -1
View File
@@ -452,8 +452,12 @@ fn dock_validation_rejects_two_docks_per_side_nested_docks_empty_columns_and_bad
native.floating[0].native = Some([0.0, 0.0, -1.0, 10.0]);
assert_eq!(native.validate(), Err(DockError::InvalidGeometry));
let mut monitor = docked();
monitor.floating[0].monitor = Some(super::Monitor { name: None, rect: [0.0, 0.0, 1920.0, 1080.0], scale: f32::NAN });
monitor.floating[0].monitor = Some(super::Monitor { name: None, rect: [0.0, 0.0, 1920.0, 1080.0], scale: f32::NAN, work_area: None });
assert_eq!(monitor.validate(), Err(DockError::InvalidGeometry));
let mut work_area = docked();
work_area.floating[0].monitor =
Some(super::Monitor { name: None, rect: [0.0, 0.0, 1920.0, 1080.0], scale: 1.0, work_area: Some([0.0, 0.0, f32::NAN, 1040.0]) });
assert_eq!(work_area.validate(), Err(DockError::InvalidGeometry));
let mut monitor_size = docked();
monitor_size.floating[0].monitor_size = Some([f32::NAN, 1080.0]);
assert_eq!(monitor_size.validate(), Err(DockError::InvalidGeometry));
+25 -8
View File
@@ -334,13 +334,14 @@ fn a_window_keeps_its_id_and_os_window_when_its_first_panel_moves_out() {
#[test]
fn a_moved_window_reports_its_place_and_monitor() {
let mut d = driver(layout(true));
d.monitors = vec![Monitor { name: Some("left".into()), rect: [0.0, 0.0, 1920.0, 1080.0], scale: 1.0 }];
let left = Monitor { name: Some("left".into()), rect: [0.0, 0.0, 1920.0, 1080.0], scale: 1.0, work_area: Some([0.0, 0.0, 1920.0, 1040.0]) };
d.monitors = vec![left.clone()];
let viewport = d.viewport(window(&d, 5).id);
d.host().windows.get_mut(&viewport).unwrap().rect = Rect::from_min_size(pos2(700.0, 200.0), vec2(240.0, 300.0));
let out = d.step(vec![]);
assert!(out.actions.contains(&Action::SetNative { panel: 5, rect: Some([700.0, 200.0, 240.0, 300.0]) }), "{:?}", out.actions);
assert_eq!(window(&d, 5).rect, [600.0, 150.0, 240.0, 300.0], "the in-window rect follows");
assert_eq!(window(&d, 5).monitor.as_ref().and_then(|m| m.name.as_deref()), Some("left"));
assert_eq!(window(&d, 5).monitor, Some(left));
// Without a monitor list, the size egui reports.
let mut d = driver(layout(true));
@@ -351,8 +352,8 @@ fn a_moved_window_reports_its_place_and_monitor() {
#[test]
fn a_restored_window_opens_on_a_connected_monitor() {
let left = Monitor { name: Some("left".into()), rect: [0.0, 0.0, 1920.0, 1080.0], scale: 1.0 };
let right = Monitor { name: Some("right".into()), rect: [1920.0, 0.0, 2560.0, 1440.0], scale: 2.0 };
let left = Monitor { name: Some("left".into()), rect: [0.0, 0.0, 1920.0, 1080.0], scale: 1.0, work_area: None };
let right = Monitor { name: Some("right".into()), rect: [1920.0, 0.0, 2560.0, 1440.0], scale: 2.0, work_area: None };
let mut saved = layout(true);
saved.floating[0].native = Some([3000.0, 100.0, 240.0, 300.0]);
saved.floating[0].monitor = Some(right.clone());
@@ -366,12 +367,23 @@ fn a_restored_window_opens_on_a_connected_monitor() {
// Unplugged: the main window's monitor, at the same offset.
let mut d = DockDriver::new(Layout::default(), Content::default()).with_native_host(ORIGIN);
d.monitors = vec![left];
d.layout = saved;
d.monitors = vec![left.clone()];
d.layout = saved.clone();
d.settle();
assert_eq!(os_window(&d, 5).unwrap().0.min, pos2(1080.0, 100.0));
assert_eq!(window(&d, 5).native, Some([1080.0, 100.0, 240.0, 300.0]));
assert_eq!(window(&d, 5).monitor.as_ref().and_then(|m| m.name.as_deref()), Some("left"));
// Its bar under the taskbar: up into the monitor's work area.
let left = Monitor { work_area: Some([0.0, 0.0, 1920.0, 1040.0]), ..left };
saved.floating[0].native = Some([500.0, 1050.0, 240.0, 300.0]);
saved.floating[0].monitor = Some(left.clone());
let mut d = DockDriver::new(Layout::default(), Content::default()).with_native_host(ORIGIN);
d.monitors = vec![left];
d.layout = saved;
d.settle();
assert_eq!(os_window(&d, 5).unwrap().0.min, pos2(500.0, 740.0));
assert_eq!(window(&d, 5).native, Some([500.0, 740.0, 240.0, 300.0]));
}
/// The last `InnerSize`, `MinInnerSize` and `MaxInnerSize` sent to `viewport`.
@@ -722,7 +734,8 @@ fn unused_keys_in_a_native_window_reach_the_app() {
#[test]
fn a_native_window_saves_its_id_place_and_monitor() {
let mut layout = layout(true);
let monitor = Monitor { name: Some("DP-1".into()), rect: [-1920.0, 0.0, 1920.0, 1080.0], scale: 1.5 };
let monitor =
Monitor { name: Some("DP-1".into()), rect: [-1920.0, 0.0, 1920.0, 1080.0], scale: 1.5, work_area: Some([-1920.0, 25.0, 1920.0, 1055.0]) };
layout.floating[0].native = Some([-1500.0, 100.0, 240.0, 300.0]);
layout.floating[0].monitor = Some(monitor);
layout.floating.push(Floating {
@@ -734,6 +747,10 @@ fn a_native_window_saves_its_id_place_and_monitor() {
let back: Layout<u32> = serde_json::from_str(&json).unwrap();
assert_eq!(back, layout);
assert_eq!(back.floating.iter().map(|w| w.id).collect::<Vec<_>>(), layout.floating.iter().map(|w| w.id).collect::<Vec<_>>());
// A monitor saved without a work area reads as one without, and writes none.
let old: Monitor = serde_json::from_str(r#"{"name": "DP-1", "rect": [0, 0, 1920, 1080], "scale": 1}"#).unwrap();
assert_eq!(old.work_area, None);
assert!(!serde_json::to_string(&old).unwrap().contains("work_area"));
// Old layouts get fresh ids, and copies of one id unique ones.
let old = r#"{"root": null, "floating": [{"panels": [5], "active": 0, "rect": [0, 0, 100, 100]}, {"panels": [6], "active": 0, "rect": [0, 0, 100, 100]}]}"#;
@@ -1244,7 +1261,7 @@ fn a_changed_layout_moves_the_os_window() {
assert_eq!(window(&d, 5).native, Some([700.0, 200.0, 260.0, 320.0]));
// Off every monitor: onto the nearest, and the layout follows.
d.monitors = vec![Monitor { name: Some("left".into()), rect: [0.0, 0.0, 1920.0, 1080.0], scale: 1.0 }];
d.monitors = vec![Monitor { name: Some("left".into()), rect: [0.0, 0.0, 1920.0, 1080.0], scale: 1.0, work_area: None }];
d.layout.floating[0].native = Some([5000.0, 200.0, 260.0, 320.0]);
d.settle();
assert_eq!(d.host().windows[&viewport].rect.min, pos2(1660.0, 200.0));
+101 -32
View File
@@ -37,6 +37,15 @@ fn to_rect([x, y, w, h]: [f32; 4]) -> Rect {
Rect::from_min_size(pos2(x, y), Vec2::new(w, h))
}
/// The part of `monitor` windows may use: its work area where that lies on it, else all of it.
fn area(monitor: &Monitor) -> [f32; 4] {
let work = monitor.work_area.filter(|work| work.iter().all(|v| v.is_finite()));
match work.map(|work| to_rect(work).intersect(to_rect(monitor.rect))) {
Some(on) if on.is_finite() && on.width() > 0.0 && on.height() > 0.0 => [on.left(), on.top(), on.width(), on.height()],
_ => monitor.rect,
}
}
/// The monitor holding the center of `rect`, else the one nearest to it. None for an empty
/// list.
pub(super) fn monitor_of(monitors: &[Monitor], rect: [f32; 4]) -> Option<&Monitor> {
@@ -47,24 +56,27 @@ pub(super) fn monitor_of(monitors: &[Monitor], rect: [f32; 4]) -> Option<&Monito
.or_else(|| monitors().min_by(|a, b| to_rect(a.rect).distance_sq_to_pos(center).total_cmp(&to_rect(b.rect).distance_sq_to_pos(center))))
}
/// Whether enough of the bar along the top of `rect` lies on one of `monitors` to grab it.
fn reachable(rect: [f32; 4], monitors: &[Monitor]) -> bool {
/// Whether enough of the bar along the top of `rect` lies in `area` to grab it.
fn reachable_in(rect: [f32; 4], area: [f32; 4]) -> bool {
let [x, y, w, _] = rect;
let bar = to_rect([x, y, w, BAR_HEIGHT]);
monitors.iter().filter(|m| usable(m)).any(|m| {
let on = bar.intersect(to_rect(m.rect));
on.width() >= REACHABLE[0].min(w) && on.height() >= REACHABLE[1]
})
let on = to_rect([x, y, w, BAR_HEIGHT]).intersect(to_rect(area));
on.width() >= REACHABLE[0].min(w) && on.height() >= REACHABLE[1]
}
/// `rect` made safe, and moved wholly onto the nearest of `monitors` when too little of its
/// bar lies on any to grab it. Without a monitor list, only made safe.
/// Whether enough of the bar along the top of `rect` lies in the work area of one of
/// `monitors` to grab it.
fn reachable(rect: [f32; 4], monitors: &[Monitor]) -> bool {
monitors.iter().filter(|m| usable(m)).any(|m| reachable_in(rect, area(m)))
}
/// `rect` made safe, and moved wholly into the work area of the nearest of `monitors` when
/// too little of its bar lies in any to grab it. Without a monitor list, only made safe.
pub(super) fn kept_reachable(rect: [f32; 4], monitors: &[Monitor]) -> [f32; 4] {
let rect = sanitized(rect);
if !monitors.iter().any(usable) || reachable(rect, monitors) {
return rect;
}
monitor_of(monitors, rect).map_or(rect, |m| clamp_onto(rect, m.rect))
monitor_of(monitors, rect).map_or(rect, |m| clamp_onto(rect, area(m)))
}
/// `rect` moved, and shrunk if it must be, to lie wholly on `area`.
@@ -78,13 +90,18 @@ fn clamp_onto(rect: [f32; 4], area: [f32; 4]) -> [f32; 4] {
/// Where a native window from a saved layout opens.
///
/// - Enough of its bar lies in the work area of a connected monitor to grab it: the saved
/// rect, whatever monitor it was saved on.
/// - Its saved monitor (`monitor`), found in `monitors` by name or else by rect, is connected:
/// the saved rect, moved in proportion if that monitor's rect changed, and onto it if too
/// little of its bar lies on a monitor to grab it.
/// - It is not: the main window's monitor, at the same offset from its origin.
/// the saved rect, moved in proportion if that monitor's rect changed, and into its work
/// area.
/// - It is not: the main window's monitor, at the same offset from its origin, and into its
/// work area.
/// - No monitor list: the main window's monitor as egui reports it, by its size alone. Its
/// origin is taken as the multiple of that size nearest below the main window's center,
/// which holds for a row or grid of monitors of one size.
/// which holds for a row or grid of monitors of one size. When that area does not hold the
/// main window, the guess is wrong: a window whose bar lies on the main window stays, and
/// any other moves onto the main window, no larger than the monitor.
///
/// A window that moves to another monitor ends up wholly on it.
pub(super) fn restore(saved: [f32; 4], monitor: Option<&Monitor>, monitors: &[Monitor], main: Option<Rect>, monitor_size: Option<Vec2>) -> [f32; 4] {
@@ -94,7 +111,22 @@ pub(super) fn restore(saved: [f32; 4], monitor: Option<&Monitor>, monitors: &[Mo
let (Some(main), Some(size)) = (main, monitor_size.filter(|s| s.is_finite() && s.x >= 1.0 && s.y >= 1.0)) else { return saved };
let center = to_rect(main).center();
let origin = Pos2::new((center.x / size.x).floor() * size.x, (center.y / size.y).floor() * size.y);
return clamp_onto(saved, [origin.x, origin.y, size.x, size.y]);
let guess = [origin.x, origin.y, size.x, size.y];
if to_rect(guess).contains_rect(to_rect(main)) {
return clamp_onto(saved, guess);
}
if reachable_in(saved, main) {
return saved;
}
let [x, y, w, h] = saved;
let [mx, my, mw, mh] = main;
let (w, h) = (w.min(size.x), h.min(size.y));
// As in `clamp_onto`, but the window shrinks only to the monitor's size, not the main
// window's.
return sanitized([x.min(mx + mw - w).max(mx), y.min(my + mh - h).max(my), w, h]);
}
if reachable(saved, monitors) {
return saved;
}
let main_monitor = main.and_then(|main| monitor_of(monitors, main)).or_else(|| monitors.iter().find(|m| usable(m)));
let connected = monitor.and_then(|old| {
@@ -103,33 +135,21 @@ pub(super) fn restore(saved: [f32; 4], monitor: Option<&Monitor>, monitors: &[Mo
});
let [x, y, w, h] = saved;
match (connected, monitor) {
(Some((old, now)), _) if old.rect == now.rect => {
if reachable(saved, monitors) {
saved
} else {
clamp_onto(saved, now.rect)
}
}
(Some((old, now)), _) if old.rect == now.rect => clamp_onto(saved, area(now)),
(Some((old, now)), _) => {
let [ox, oy, ow, oh] = old.rect;
let [nx, ny, nw, nh] = now.rect;
let (sx, sy) = if usable(old) { (nw / ow, nh / oh) } else { (1.0, 1.0) };
clamp_onto([nx + (x - ox) * sx, ny + (y - oy) * sy, w, h], now.rect)
clamp_onto([nx + (x - ox) * sx, ny + (y - oy) * sy, w, h], area(now))
}
(None, Some(old)) => {
let Some(target) = main_monitor else { return saved };
let [ox, oy, ..] = old.rect;
let [tx, ty, ..] = target.rect;
let (dx, dy) = if usable(old) { (x - ox, y - oy) } else { (0.0, 0.0) };
clamp_onto([tx + dx, ty + dy, w, h], target.rect)
}
// No saved monitor: the window stays where its bar can be grabbed.
(None, None) => {
if reachable(saved, monitors) {
return saved;
}
main_monitor.map_or(saved, |target| clamp_onto(saved, target.rect))
clamp_onto([tx + dx, ty + dy, w, h], area(target))
}
(None, None) => main_monitor.map_or(saved, |target| clamp_onto(saved, area(target))),
}
}
@@ -139,7 +159,7 @@ mod tests {
use egui::vec2;
fn monitor(name: &str, rect: [f32; 4], scale: f32) -> Monitor {
Monitor { name: Some(name.into()), rect, scale }
Monitor { name: Some(name.into()), rect, scale, work_area: None }
}
fn main_at(x: f32, y: f32) -> Option<Rect> {
@@ -238,6 +258,55 @@ mod tests {
assert!((x - area[0]).abs() < 0.01, "{x}");
}
#[test]
fn a_bar_under_the_taskbar_moves_into_the_work_area() {
let work = Monitor { work_area: Some([0.0, 0.0, 1920.0, 1040.0]), ..monitor("left", LEFT, 1.0) };
let monitors = [work.clone()];
let under = [100.0, 1045.0, 400.0, 300.0];
assert_eq!(restore(under, Some(&work), &monitors, main_at(100.0, 100.0), None), [100.0, 740.0, 400.0, 300.0]);
assert_eq!(restore(under, None, &monitors, main_at(100.0, 100.0), None), [100.0, 740.0, 400.0, 300.0]);
assert_eq!(kept_reachable(under, &monitors), [100.0, 740.0, 400.0, 300.0]);
// A bar in the work area stays, the body below it too.
let low = [100.0, 1000.0, 400.0, 300.0];
assert_eq!(restore(low, Some(&work), &monitors, main_at(100.0, 100.0), None), low);
// Unplugged: the main window's monitor, into its work area.
let gone = monitor("gone", RIGHT, 2.0);
assert_eq!(restore([2000.0, 1300.0, 400.0, 300.0], Some(&gone), &monitors, main_at(100.0, 100.0), None), [80.0, 740.0, 400.0, 300.0]);
// A work area that is not finite or lies off the monitor: all of the monitor.
for area in [[f32::NAN, 0.0, 1920.0, 1040.0], [3000.0, 0.0, 100.0, 100.0], [0.0, 0.0, -5.0, 1040.0]] {
let monitors = [Monitor { work_area: Some(area), ..monitor("left", LEFT, 1.0) }];
assert_eq!(kept_reachable(under, &monitors), under, "{area:?}");
assert_eq!(kept_reachable([100.0, 1075.0, 400.0, 300.0], &monitors), [100.0, 780.0, 400.0, 300.0], "{area:?}");
}
// Part of the work area off the monitor: only the part on it.
let monitors = [Monitor { work_area: Some([-100.0, 0.0, 2020.0, 1040.0]), ..monitor("left", LEFT, 1.0) }];
assert_eq!(kept_reachable([-300.0, 100.0, 300.0, 300.0], &monitors), [0.0, 100.0, 300.0, 300.0]);
}
#[test]
fn a_visible_window_stays_when_its_monitor_moves() {
let old = monitor("left", LEFT, 1.0);
let monitors = [monitor("left", [100.0, 0.0, 1920.0, 1080.0], 1.0)];
let saved = [200.0, 200.0, 400.0, 300.0];
assert_eq!(restore(saved, Some(&old), &monitors, main_at(100.0, 100.0), None), saved);
// Off the moved monitor: moved with it.
assert_eq!(restore([2100.0, 200.0, 400.0, 300.0], Some(&old), &monitors, main_at(100.0, 100.0), None), [1620.0, 200.0, 400.0, 300.0]);
}
#[test]
fn without_a_monitor_list_a_wrong_guess_puts_the_window_on_the_main_window() {
// The real monitor is [2500, 0, 1000, 800]; the multiple of its size below the main
// window's center is 2000, which does not hold the main window.
let main = Some(Rect::from_min_size(pos2(2600.0, 100.0), vec2(600.0, 400.0)));
let size = Some(vec2(1000.0, 800.0));
assert_eq!(restore([0.0, 100.0, 400.0, 300.0], None, &[], main, size), [2600.0, 100.0, 400.0, 300.0]);
// Its bar on the main window: it stays.
let on = [3000.0, 300.0, 400.0, 300.0];
assert_eq!(restore(on, None, &[], main, size), on);
// Larger than the main window: at its top-left corner, no larger than the monitor.
assert_eq!(restore([0.0, 0.0, 900.0, 1000.0], None, &[], main, size), [2600.0, 100.0, 900.0, 800.0]);
}
#[test]
fn a_window_without_a_saved_monitor_stays_on_screen() {
let monitors = [monitor("left", LEFT, 1.0), monitor("right", RIGHT, 2.0)];
+2 -1
View File
@@ -1137,7 +1137,8 @@ impl<'a, P: Clone + Eq + Hash + Debug + Send + Sync + 'static> DockArea<'a, P> {
/// The connected monitors (feature `native-windows`). A native floating window stores the
/// one it is on, and a restored layout puts each window back on its monitor, or on the
/// main window's when that monitor is gone. egui 0.36 reports no monitors; without a list
/// main window's when that monitor is gone, with its bar in the monitor's
/// [`Monitor::work_area`] where one is given. egui 0.36 reports no monitors; without a list
/// windows store the monitor size egui reports and restore onto the main window's monitor.
/// eframe 0.36 keeps winit's event loop, and with it `available_monitors`, to itself, so an
/// eframe application builds the list from the platform's own display API.