PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Menus keep clear of a panel that covers a maximized window (X11) (#3737) (#4380)
FredAppsb507e04f1f78
diff --git a/crates/ui-egui/src/menu_nav_tests.rs b/crates/ui-egui/src/menu_nav_tests.rsindex 2dafc1c5c..1643e2765 100644--- a/crates/ui-egui/src/menu_nav_tests.rs+++ b/crates/ui-egui/src/menu_nav_tests.rs@@ -563,9 +563,61 @@ fn submenus_stay_above_the_taskbar() {}}+/// #3737 on X11 (Linux Mint, 1920 × 1080 px): the window manager maximized the window to the whole+/// monitor without keeping it clear of the bottom panel, so the window's position is known but the+/// panel covers its bottom. At 1× and at a 150% UI scale (1280 × 720 pt).+fn maximized_under_a_panel(scale: f32) -> Harness<'static, PhotocraftApp> {+ let size = egui::vec2(1920.0, 1080.0) / scale;+ let mut h = harness((1920.0, 1080.0, scale));+ let v = h.input_mut().viewports.entry(egui::ViewportId::ROOT).or_default();+ v.monitor_size = Some(size);+ v.inner_rect = Some(egui::Rect::from_min_size(egui::pos2(0.0, 0.0), size));+ v.maximized = Some(true);+ h.run_steps(3);+ h+}++#[test]+fn menus_of_a_maximized_window_stay_above_a_panel_it_runs_under() {+ let mut scrolled = 0;+ for scale in [1.0_f32, 1.5] {+ for menu in LONGEST.iter().copied().chain(["View", "Window"]) {+ let mut h = maximized_under_a_panel(scale);+ let bottom = taskbar_bottom(&h);+ let what = format!("{scale}x {menu}");+ assert!((bottom - (1080.0 / scale - crate::work_area::TASKBAR)).abs() < 1.0, "{what}: visible bottom {bottom}");+ open(&mut h, menu);+ for r in popups(&h) {+ assert!(r.bottom() <= bottom + 0.5, "{what}: popup {r:?} runs under the panel (from {bottom})");+ }+ let last = rows(&h, 0).len() - 1;+ let in_view = |h: &Harness<'static, PhotocraftApp>| {+ let nav = Nav::current(&h.ctx);+ nav.views[0].expand(0.5).contains_rect(nav.rows[0][last].rect) && nav.views[0].bottom() <= bottom + 0.5+ };+ let Some(down) = h.query_by_label("Scroll menu down").map(|n| n.rect()) else {+ assert!(in_view(&h), "{what}: fits, so its last row is in view");+ continue;+ };+ scrolled += 1;+ assert!(down.bottom() <= bottom, "{what}: ▼ at {down:?} is under the panel");+ h.hover_at(down.center());+ for _ in 0..400 {+ h.run_steps(1);+ if in_view(&h) {+ break;+ }+ }+ assert!(in_view(&h), "{what}: ▼ scrolls to the last row, above the panel");+ }+ }+ assert!(scrolled >= 3, "at 150% the long menus overflow above the panel: {scrolled}");+}+#[test]fn a_maximized_window_or_an_unknown_monitor_uses_the_whole_window() {- let mut h = taskbar_harness((1366.0, 728.0), 0.0, 1.0);+ // Maximized inside the work area (the window manager kept it clear of the taskbar).+ let mut h = taskbar_harness((1366.0, 720.0), 0.0, 1.0);h.input_mut().viewports.entry(egui::ViewportId::ROOT).or_default().maximized = Some(true);h.run_steps(2);assert_eq!(crate::work_area::visible_rect(&h.ctx), h.ctx.content_rect());diff --git a/crates/ui-egui/src/work_area.rs b/crates/ui-egui/src/work_area.rsindex 0ac79ea6a..df4bc7a77 100644--- a/crates/ui-egui/src/work_area.rs+++ b/crates/ui-egui/src/work_area.rs@@ -19,11 +19,19 @@ pub const TASKBAR: f32 = 48.0;const MIN_VISIBLE: f32 = 160.0;/// Where the window's content can be seen, in egui points. `monitor` is the monitor's size,-/// `inner` the content's rect on the desktop (both from [`egui::ViewportInfo`]); `fills_screen`-/// is true for a maximized or full-screen window (already inside the work area).-pub fn visible_part(content: Rect, monitor: Option<Vec2>, inner: Option<Rect>, fills_screen: bool) -> Rect {+/// `inner` the content's rect on the desktop (both from [`egui::ViewportInfo`]). `state` says+/// whether the window is maximized or full screen.+pub fn visible_part(content: Rect, monitor: Option<Vec2>, inner: Option<Rect>, state: WindowState) -> Rect {let Some(m) = monitor else { return content };- if fills_screen || !m.x.is_finite() || !m.y.is_finite() || m.x < 1.0 || m.y < 1.0 {+ if state == WindowState::FullScreen || !m.x.is_finite() || !m.y.is_finite() || m.x < 1.0 || m.y < 1.0 {+ return content;+ }+ // A maximized window is usually inside the work area already, but not always (#3737): on X11+ // a window manager that doesn't reserve a panel's space (Cinnamon's Muffin with PhotoCraft's+ // own title bar, a panel without a strut) maximizes it to the whole monitor, and the panel then+ // covers its bottom. Where the window is known, it is checked like any other; only a maximized+ // window with no position (Wayland) is taken to fit.+ if state == WindowState::Maximized && inner.is_none() {return content;}// #3737: Wayland never tells a window where it is (winit's `inner_position` fails), so egui has@@ -49,6 +57,27 @@ pub fn visible_part(content: Rect, monitor: Option<Vec2>, inner: Option<Rect>, ftrusted(content, local)}+/// How the window sits on its monitor.+#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub enum WindowState {+ Normal,+ Maximized,+ /// Full screen covers the taskbar on purpose: all of it can be seen.+ FullScreen,+}++impl WindowState {+ fn of(v: &egui::ViewportInfo) -> Self {+ if v.fullscreen == Some(true) {+ Self::FullScreen+ } else if v.maximized == Some(true) {+ Self::Maximized+ } else {+ Self::Normal+ }+ }+}+/// `content` cut to `usable`, unless that leaves too little to be a sensible guess.fn trusted(content: Rect, usable: Rect) -> Rect {let visible = content.intersect(usable);@@ -63,8 +92,7 @@ pub fn visible_rect(ctx: &egui::Context) -> Rect {let content = ctx.content_rect();ctx.input(|i| {let v = i.viewport();- let fills = v.maximized == Some(true) || v.fullscreen == Some(true);- visible_part(content, v.monitor_size, v.inner_rect, fills)+ visible_part(content, v.monitor_size, v.inner_rect, WindowState::of(v))})}@@ -107,6 +135,7 @@ pub fn fit_window(ctx: &egui::Context) -> bool {#[cfg(test)]mod tests {use super::*;+ use WindowState::{FullScreen, Maximized, Normal};use egui::{Rect, pos2, vec2};const MONITOR: Vec2 = vec2(1366.0, 768.0);@@ -119,29 +148,46 @@ mod tests {fn a_window_taller_than_its_display_is_cut_at_the_taskbar() {// 1440 × 900 at the desktop origin on 1366 × 768: right and bottom are off-screen.let (content, inner) = window(0.0, 0.0, 1440.0, 900.0);- assert_eq!(visible_part(content, Some(MONITOR), Some(inner), false), Rect::from_min_max(pos2(0.0, 0.0), pos2(1366.0, 768.0 - TASKBAR)));+ assert_eq!(visible_part(content, Some(MONITOR), Some(inner), Normal), Rect::from_min_max(pos2(0.0, 0.0), pos2(1366.0, 768.0 - TASKBAR)));// Below a 31 pt title bar, a full-height window reaches under the taskbar.let (content, inner) = window(0.0, 31.0, 1366.0, 737.0);- assert_eq!(visible_part(content, Some(MONITOR), Some(inner), false).bottom(), 768.0 - TASKBAR - 31.0);+ assert_eq!(visible_part(content, Some(MONITOR), Some(inner), Normal).bottom(), 768.0 - TASKBAR - 31.0);// A small window clear of the taskbar is all visible.let (content, inner) = window(100.0, 80.0, 800.0, 500.0);- assert_eq!(visible_part(content, Some(MONITOR), Some(inner), false), content);+ assert_eq!(visible_part(content, Some(MONITOR), Some(inner), Normal), content);// On a second monitor to the right, the same.let (content, inner) = window(1366.0 + 100.0, 0.0, 1440.0, 900.0);- assert_eq!(visible_part(content, Some(MONITOR), Some(inner), false).bottom(), 768.0 - TASKBAR);+ assert_eq!(visible_part(content, Some(MONITOR), Some(inner), Normal).bottom(), 768.0 - TASKBAR);}#[test]fn maximized_unknown_or_nonsense_input_keeps_the_window() {let (content, inner) = window(0.0, 0.0, 1440.0, 900.0);- assert_eq!(visible_part(content, Some(MONITOR), Some(inner), true), content, "maximized: already in the work area");- assert_eq!(visible_part(content, None, Some(inner), false), content);- assert_eq!(visible_part(content, Some(vec2(f32::NAN, 768.0)), Some(inner), false), content);- assert_eq!(visible_part(content, Some(vec2(0.0, 0.0)), Some(inner), false), content);- assert_eq!(visible_part(content, Some(MONITOR), Some(Rect::from_min_size(pos2(f32::INFINITY, 0.0), vec2(10.0, 10.0))), false), content);+ assert_eq!(visible_part(content, Some(MONITOR), Some(inner), FullScreen), content, "full screen covers the taskbar on purpose");+ assert_eq!(visible_part(content, None, Some(inner), Normal), content);+ assert_eq!(visible_part(content, Some(vec2(f32::NAN, 768.0)), Some(inner), Normal), content);+ assert_eq!(visible_part(content, Some(vec2(0.0, 0.0)), Some(inner), Normal), content);+ assert_eq!(visible_part(content, Some(MONITOR), Some(Rect::from_min_size(pos2(f32::INFINITY, 0.0), vec2(10.0, 10.0))), Normal), content);// A guess that would leave almost nothing visible is not trusted.let (content, inner) = window(0.0, 600.0, 1000.0, 300.0);- assert_eq!(visible_part(content, Some(MONITOR), Some(inner), false), content);+ assert_eq!(visible_part(content, Some(MONITOR), Some(inner), Normal), content);+ }++ #[test]+ fn a_maximized_window_under_a_panel_is_still_cut_at_the_taskbar() {+ // #3737, X11 (Linux Mint): the window manager maximized the window to the whole 1920 × 1080+ // monitor without reserving the bottom panel's space, so the panel covers it.+ for scale in [1.0_f32, 1.5] {+ let monitor = vec2(1920.0, 1080.0) / scale;+ let content = Rect::from_min_size(pos2(0.0, 0.0), monitor);+ let bottom = monitor.y - TASKBAR;+ assert_eq!(visible_part(content, Some(monitor), Some(content), Maximized).bottom(), bottom, "at {scale}×");+ // A window manager that reserves the panel: the maximized window already ends above it.+ let fitted = Rect::from_min_size(pos2(0.0, 0.0), vec2(monitor.x, bottom));+ assert_eq!(visible_part(fitted, Some(monitor), Some(fitted), Maximized), fitted, "at {scale}×");+ // Full screen is meant to cover the panel.+ assert_eq!(visible_part(content, Some(monitor), Some(content), FullScreen), content, "at {scale}×");+ }}#[test]@@ -149,13 +195,14 @@ mod tests {// #3737, Wayland: only the monitor's size is known. A window taller than the monitor less// the taskbar can be seen at most down to there, wherever it is.let content = Rect::from_min_size(pos2(0.0, 0.0), vec2(1366.0, 740.0));- assert_eq!(visible_part(content, Some(MONITOR), None, false), Rect::from_min_max(pos2(0.0, 0.0), pos2(1366.0, 768.0 - TASKBAR)));+ assert_eq!(visible_part(content, Some(MONITOR), None, Normal), Rect::from_min_max(pos2(0.0, 0.0), pos2(1366.0, 768.0 - TASKBAR)));let big = Rect::from_min_size(pos2(0.0, 0.0), vec2(1440.0, 900.0));- assert_eq!(visible_part(big, Some(MONITOR), None, false), Rect::from_min_max(pos2(0.0, 0.0), pos2(1366.0, 768.0 - TASKBAR)));+ assert_eq!(visible_part(big, Some(MONITOR), None, Normal), Rect::from_min_max(pos2(0.0, 0.0), pos2(1366.0, 768.0 - TASKBAR)));// One that fits, or a maximized one, is all visible.let small = Rect::from_min_size(pos2(0.0, 0.0), vec2(1000.0, 600.0));- assert_eq!(visible_part(small, Some(MONITOR), None, false), small);- assert_eq!(visible_part(content, Some(MONITOR), None, true), content);+ assert_eq!(visible_part(small, Some(MONITOR), None, Normal), small);+ assert_eq!(visible_part(content, Some(MONITOR), None, Maximized), content);+ assert_eq!(visible_part(content, Some(MONITOR), None, FullScreen), content);}fn frame(ctx: &egui::Context, monitor: Option<Vec2>, outer: Option<Rect>, maximized: Option<bool>) -> Vec<egui::ViewportCommand> {