diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs index c802eb10..ee2b09fa 100644 --- a/crates/ui-egui/src/widgets.rs +++ b/crates/ui-egui/src/widgets.rs @@ -162,6 +162,8 @@ pub fn nav_icon(ui: &mut Ui, glyph: Glyph, tip: &str, height: f32, busy: bool) - /// How far in from the window's edges a custom title bar's window resizes ([`window_edges`]). /// The title bar's own controls leave this band alone. const RESIZE_EDGE: f32 = 5.0; +/// Width of one caption button, as Windows' own. +const CAPTION_W: f32 = 46.0; /// How wide the resize band along the window's edges is right now: nothing while the window is /// maximized or full screen, when `window_edges` offers no resizing and the edges are the screen's, @@ -261,23 +263,36 @@ enum WindowButton { } /// Minimize, maximize (or restore) and close, `height` tall, for a right-to-left layout: close -/// ends up at the right. +/// ends up at the right. Under a dialog egui ignores the header, so there they are drawn in a +/// layer above the dialog's backdrop, as a system title bar is: the first click on Close closes +/// the window, not the dialog (#853). The details drawer and the lightbox still cover them, since +/// their own controls and the apps' menus can sit in that corner. pub fn window_buttons(ui: &mut Ui, height: f32) { let ctx = ui.ctx().clone(); let maximized = ctx.input(|i| i.viewport().maximized.unwrap_or(false)); + let (rect, _) = ui.allocate_exact_size(vec2(3.0 * CAPTION_W, height), Sense::hover()); + let mut row = egui::UiBuilder::new().max_rect(rect).layout(egui::Layout::left_to_right(egui::Align::Center)); + let mut sense = Sense::click(); + if !ctx.memory(|m| m.is_above_modal_layer(ui.layer_id())) { + row = row.layer_id(egui::LayerId::new(egui::Order::Tooltip, egui::Id::unique("window-buttons"))); + // Pointer only: Tab stays in the dialog. + sense = Sense::CLICK; + } // The first frame after a restore hit-tests the maximized buttons, so an edge press can land on them. let inside = ctx.content_rect().shrink(resize_band(&ctx)); let clicked = |resp: Response| resp.clicked() && resp.interact_pointer_pos().is_none_or(|p| inside.contains(p)); - if clicked(window_button(ui, WindowButton::Close, height)) { - ctx.send_viewport_cmd(egui::ViewportCommand::Close); - } - let size = if maximized { WindowButton::Restore } else { WindowButton::Maximize }; - if clicked(window_button(ui, size, height)) { - ctx.send_viewport_cmd(egui::ViewportCommand::Maximized(!maximized)); - } - if clicked(window_button(ui, WindowButton::Minimize, height)) { - ctx.send_viewport_cmd(egui::ViewportCommand::Minimized(true)); - } + ui.scope_builder(row, |ui| { + if clicked(window_button(ui, WindowButton::Minimize, height, sense)) { + ctx.send_viewport_cmd(egui::ViewportCommand::Minimized(true)); + } + let size = if maximized { WindowButton::Restore } else { WindowButton::Maximize }; + if clicked(window_button(ui, size, height, sense)) { + ctx.send_viewport_cmd(egui::ViewportCommand::Maximized(!maximized)); + } + if clicked(window_button(ui, WindowButton::Close, height, sense)) { + ctx.send_viewport_cmd(egui::ViewportCommand::Close); + } + }); rule(ui, height); } @@ -285,15 +300,15 @@ pub fn window_buttons(ui: &mut Ui, height: f32) { /// command. Hit-testing excludes the top resize band (and, for close, the right-edge band) so /// the window edges stay resizable; when maximized there is no band and close reaches the /// corner. -fn window_button(ui: &mut Ui, button: WindowButton, height: f32) -> Response { +fn window_button(ui: &mut Ui, button: WindowButton, height: f32, sense: Sense) -> Response { let t = Tokens::get(ui.ctx()); - let (rect, _) = ui.allocate_exact_size(vec2(46.0, height), Sense::hover()); + let (rect, _) = ui.allocate_exact_size(vec2(CAPTION_W, height), Sense::hover()); // Clickable below the resize band along the top (and, for close, the right edge); maximized, // there is no band and the close button takes the screen's corner, as Windows' own does. let band = resize_band(ui.ctx()); let right = if button == WindowButton::Close { band } else { 0.0 }; let hit = Rect::from_min_max(rect.min + vec2(0.0, band), rect.max - vec2(right, 0.0)); - let resp = ui.interact(hit, ui.scope_id().with(("window-button", button as u8)), Sense::click()); + let resp = ui.interact(hit, ui.scope_id().with(("window-button", button as u8)), sense); let (fill, ink) = match (button, resp.hovered()) { (WindowButton::Close, true) => (Color32::from_rgb(0xc4, 0x2b, 0x1c), Color32::WHITE), (_, true) => (t.line, t.ink_strong), diff --git a/crates/ui-egui/tests/ui.rs b/crates/ui-egui/tests/ui.rs index 12aa3004..a29e1ebc 100644 --- a/crates/ui-egui/tests/ui.rs +++ b/crates/ui-egui/tests/ui.rs @@ -3104,6 +3104,105 @@ fn custom_title_bar_has_window_buttons() { assert!(closes, "the close button asks the window to close (the shell decides whether that hides it to the tray)"); } +/// With a dialog open, one click on the custom title bar's Close closes the window: the caption +/// buttons sit above the dialog's backdrop, as a system title bar does, so the click isn't spent +/// dismissing the dialog first (#853). The header's own controls beside them still take clicks. +#[test] +fn close_button_closes_with_a_dialog_open() { + let (mut h, _) = harness_with_title_bar(events(), ViewMode::Grid, egui::Theme::Dark, true, 1.0, artcraft_launcher_ui_egui::TitleBar::Custom); + h.run_steps(6); + h.get_by_label("Settings").click(); + h.run_steps(2); + h.get_by_label("Done"); + h.get_by_label("Close").click(); + h.step(); + let closes = h.output().viewport_output.values().any(|v| v.commands.contains(&egui::ViewportCommand::Close)); + assert!(closes, "the first click on Close closes the window"); + h.run_steps(2); + h.get_by_label("Done"); +} + +/// Presses and releases at `pos`, and returns what the window was asked to do. +fn click_at(h: &mut Harness<'_, Option>, pos: egui::Pos2) -> Vec { + let mut asked = Vec::new(); + for pressed in [true, false] { + let input = h.input_mut(); + input.events.push(egui::Event::PointerMoved(pos)); + input.events.push(egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: egui::Modifiers::NONE }); + h.step(); + asked.extend(h.output().viewport_output.values().flat_map(|v| v.commands.iter().cloned())); + } + h.run_steps(2); + asked +} + +/// Above a dialog the caption buttons are for the pointer alone: Tab stays in the dialog, and a +/// click just beside them is still the backdrop's (#853). +#[test] +fn the_lifted_caption_buttons_leave_tab_and_the_backdrop_to_the_dialog() { + let (mut h, _) = harness_with_title_bar(events(), ViewMode::Grid, egui::Theme::Dark, true, 1.0, artcraft_launcher_ui_egui::TitleBar::Custom); + h.run_steps(6); + let tab = |h: &mut Harness<'_, Option>| -> String { + h.key_press(egui::Key::Tab); + h.run_steps(2); + let focused = h.root().query_all(egui_kittest::kittest::by()).find(|n| n.accesskit_node().is_focused()); + focused.map_or(String::new(), |n| n.accesskit_node().label().unwrap_or_default().to_string()) + }; + let caption = ["Minimize", "Maximize", "Close"]; + let reached: Vec = (0..4).map(|_| tab(&mut h)).filter(|l| caption.contains(&l.as_str())).collect(); + assert_eq!(reached, caption, "without a dialog Tab reaches them in the order they're drawn"); + h.get_by_label("Settings").click(); + h.run_steps(3); + assert_eq!(tab(&mut h), "Language", "the first Tab goes to the dialog"); + for _ in 0..40 { + let focused = tab(&mut h); + assert!(!caption.contains(&focused.as_str()), "Tab left the dialog for {focused}"); + } + let close = h.get_by_label("Close").rect(); + let asked = click_at(&mut h, egui::pos2(close.center().x, close.bottom() + 3.0)); + assert!(asked.is_empty(), "below Close: {asked:?}"); + assert!(h.query_by_label("Done").is_none(), "a click below Close dismisses the dialog"); + h.get_by_label("Settings").click(); + h.run_steps(3); + let minimize = h.get_by_label("Minimize").rect(); + let asked = click_at(&mut h, egui::pos2(minimize.left() - 3.0, minimize.center().y)); + assert!(asked.is_empty(), "left of Minimize: {asked:?}"); + assert!(h.query_by_label("Done").is_none(), "a click left of Minimize dismisses the dialog"); +} + +/// The details drawer covers the header, and the window's Close stays under it. +#[test] +fn the_details_drawer_keeps_the_caption_buttons_under_it() { + let (mut h, _) = harness_with_title_bar(events(), ViewMode::Grid, egui::Theme::Dark, true, 1.0, artcraft_launcher_ui_egui::TitleBar::Custom); + h.run_steps(8); + let close = h.get_by_label("Close").rect().center(); + h.get_by_label("PhotoCraft details").click(); + h.run_steps(10); + let asked = click_at(&mut h, close); + assert!(!asked.contains(&egui::ViewportCommand::Close), "the click reached the window's Close: {asked:?}"); +} + +/// The enlarged picture's own Close sits under the custom title bar's Close. With no dialog open +/// the caption buttons stay in the header, under the picture, so that click closes the picture +/// and not the window. +#[test] +fn the_enlarged_pictures_close_closes_the_picture_not_the_window() { + let (mut h, _) = harness_with_title_bar(events(), ViewMode::Grid, egui::Theme::Dark, true, 1.0, artcraft_launcher_ui_egui::TitleBar::Custom); + h.run_steps(8); + h.get_by_label("PhotoCraft details").click(); + h.run_steps(10); + h.get_by_label("Enlarge picture").click(); + h.run_steps(4); + // The drawer's Close is lower down; the picture's is at the top right, under the title bar. + let picture = h.query_all_by_label("Close (Esc)").map(|n| n.rect()).min_by(|a, b| a.top().total_cmp(&b.top())).unwrap(); + let spot = picture.intersect(h.get_by_label("Close").rect()); + assert!(spot.is_positive(), "the two buttons don't overlap: {picture:?}"); + let asked = click_at(&mut h, spot.center()); + assert!(!asked.contains(&egui::ViewportCommand::Close), "the click reached the window's Close: {asked:?}"); + h.run_steps(4); + assert_eq!(h.query_all_by_label("Close (Esc)").count(), 1, "the enlarged picture is still open"); +} + /// Maximized, the window's top-right pixel is the screen's corner: there is no resize band there, /// so it belongs to the close button, as it does with Windows' own caption buttons (#374). #[test] @@ -3248,6 +3347,21 @@ fn a_press_on_the_window_edge_only_resizes() { let asked = press(&mut h, egui::pos2(590.0, 1198.0)); assert!(asked.iter().any(|c| matches!(c, Asked::BeginResize(_))), "{asked:?}"); assert!(settings_open(&h), "the dialog closed on a resize"); + // Above the dialog the caption buttons have a layer of their own (#853); the band over them still only resizes. + for label in ["Minimize", "Maximize", "Close"] { + let x = h.get_by_label(label).rect().center().x; + let asked = press(&mut h, egui::pos2(x, 2.0)); + assert!(asked.iter().any(|c| matches!(c, Asked::BeginResize(_))), "{label} over the dialog: {asked:?}"); + assert!(!asked.iter().any(|c| matches!(c, Asked::Minimized(_) | Asked::Maximized(_) | Asked::Close)), "{label} over the dialog: {asked:?}"); + } + for label in ["Minimize", "Restore", "Close"] { + let asked = press_on_restore(&mut h, label, 4.5); + assert!( + !asked.iter().any(|c| matches!(c, Asked::Minimized(_) | Asked::Maximized(_) | Asked::Close)), + "{label} restored over the dialog: {asked:?}" + ); + } + assert!(settings_open(&h), "the dialog closed on a resize"); } #[test]