From 58a9d33f021b0ac5be7c9a4194801608f10cba6d Mon Sep 17 00:00:00 2001 From: Kai Evans Date: Sat, 24 Oct 2026 19:25:56 +0000 Subject: [PATCH] Right-click menus stay open when the opening click's press and release share a frame (#2458) (#2460) A menu ignores presses until it has been on screen for a frame, so the press of the click that opened it can't close it again. Fixes #2458. --- crates/ui-egui/src/context_menu.rs | 8 ++- crates/ui-egui/src/menu_tests.rs | 87 ++++++++++++++++++++++++++++++ crates/ui-egui/src/scenario.rs | 7 +++ 3 files changed, 100 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/context_menu.rs b/crates/ui-egui/src/context_menu.rs index f6b0ed20f..ab3499478 100644 --- a/crates/ui-egui/src/context_menu.rs +++ b/crates/ui-egui/src/context_menu.rs @@ -225,6 +225,8 @@ pub struct MenuState { /// The button that opened the menu, when it toggles it: a press there is not a press outside, /// so its click closes the menu instead of closing and reopening it (#1522). opener: Option, + /// The menu was on screen in an earlier frame. Until then a press is the opening click's own. + shown: bool, } #[derive(Clone, Debug, PartialEq)] @@ -1566,6 +1568,7 @@ pub fn open_for(app: &mut SolveApp, at: Pos2, target: Target) { app.menu.target = target; app.menu.flick = false; app.menu.opener = None; + app.menu.shown = false; app.viewport.context_menu = Some(at); } @@ -1889,10 +1892,11 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) { inside |= sub.response.contains_pointer() || pointer.is_some_and(|p| sub.response.rect.contains(p)); } + let shown = std::mem::replace(&mut app.menu.shown, true); let (primary, secondary, esc) = ctx.input(|i| { ( - i.pointer.button_pressed(egui::PointerButton::Primary), - i.pointer.button_pressed(egui::PointerButton::Secondary), + shown && i.pointer.button_pressed(egui::PointerButton::Primary), + shown && i.pointer.button_pressed(egui::PointerButton::Secondary), i.key_pressed(egui::Key::Escape), ) }); diff --git a/crates/ui-egui/src/menu_tests.rs b/crates/ui-egui/src/menu_tests.rs index 07086602a..338ae6115 100644 --- a/crates/ui-egui/src/menu_tests.rs +++ b/crates/ui-egui/src/menu_tests.rs @@ -1151,3 +1151,90 @@ fn narrow_overflow_submenu_keeps_opening_click_and_accepts_the_next_click() { h.step(&json!({"click":{"handle":"toolbar:solid.extrude"}})).unwrap(); assert!(matches!(h.app.dialog.as_ref().map(|d| &d.kind), Some(crate::dialogs::Kind::Extrude { .. }))); } + +/// A right-click on a browser row opens its menu, also when the press and the release reach the +/// same frame (a trackpad tap, or a click during a slow frame). +mod browser_rows { + use egui::{Event, Modifiers, PointerButton, Pos2, vec2}; + + use crate::context_menu::Target; + use crate::scenario::Harness; + + fn harness() -> Harness { + let mut h = Harness::new(); + h.app.home.open = false; + h.app.ui.show_browser = true; + h.app.session.run_script(&solidcraft_engine::sample::script()).expect("sample"); + h.frames(3); + h + } + + /// What a menu is about, without the browser's scope of a folder. + fn kind(t: &Target) -> String { + match t { + Target::Folder { folder, .. } => format!("folder:{folder}"), + Target::Body { name } => format!("body:{name}"), + other => format!("{other:?}"), + } + } + + fn right(at: Pos2, pressed: bool) -> Event { + Event::PointerButton { pos: at, button: PointerButton::Secondary, pressed, modifiers: Modifiers::NONE } + } + + fn right_click(h: &mut Harness, at: Pos2, one_frame: bool) { + h.frame_with(vec![Event::PointerMoved(at)]); + if one_frame { + h.frame_with(vec![right(at, true), right(at, false)]); + } else { + h.frame_with(vec![right(at, true)]); + h.frame_with(vec![right(at, false)]); + } + h.frames(4); + } + + #[test] + fn a_right_click_on_a_row_opens_its_menu_however_its_events_arrive() { + let mut h = harness(); + let rows = [("row:Origin", "Origin"), ("row:Bodies", "folder:bodies"), ("row:Plate", "body:Plate"), ("row:Sketches", "folder:sketches")]; + for (row, target) in rows { + let Some(centre) = crate::scenario::handle_at(row) else { panic!("{row} is not drawn: {:?}", crate::scenario::handles("row:")) }; + // Pointers sit between whole points (trackpads, HiDPI screens). + for offset in [vec2(0.0, 0.0), vec2(0.25, 0.75), vec2(0.5, 0.5), vec2(0.75, 0.25)] { + for one_frame in [false, true] { + crate::context_menu::close(&mut h.app); + h.frames(2); + right_click(&mut h, centre + offset, one_frame); + let how = if one_frame { "in one frame" } else { "in two frames" }; + assert!(h.app.viewport.context_menu.is_some(), "{row} at {offset:?}, press and release {how}: the menu did not stay open"); + assert_eq!(kind(&h.app.menu.target), target, "{row}"); + } + } + } + } + + #[test] + fn a_click_elsewhere_moves_or_closes_an_open_menu() { + let mut h = harness(); + let half = vec2(0.5, 0.5); + let plate = crate::scenario::handle_at("row:Plate").expect("Plate row"); + let origin = crate::scenario::handle_at("row:Origin").expect("Origin row"); + right_click(&mut h, plate + half, true); + assert!(h.app.viewport.context_menu.is_some()); + assert_eq!(h.app.menu.target, Target::Body { name: "Plate".into() }); + right_click(&mut h, origin + half, true); + assert!(h.app.viewport.context_menu.is_some(), "a right-click on another row opens that row's menu"); + assert_eq!(h.app.menu.target, Target::Origin); + + let empty = h.app.viewport.rect.expect("viewport").right_bottom() - vec2(80.5, 80.5); + right_click(&mut h, empty, true); + assert!(h.app.viewport.context_menu.is_some(), "the viewport's menu opens the same way"); + assert_eq!(h.app.menu.target, Target::Viewport); + + let primary = |pressed| Event::PointerButton { pos: origin, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE }; + h.frame_with(vec![Event::PointerMoved(origin)]); + h.frame_with(vec![primary(true), primary(false)]); + h.frames(2); + assert!(h.app.viewport.context_menu.is_none(), "a click outside the menu closes it"); + } +} diff --git a/crates/ui-egui/src/scenario.rs b/crates/ui-egui/src/scenario.rs index 1e9bb49cc..1f5fa1d8f 100644 --- a/crates/ui-egui/src/scenario.rs +++ b/crates/ui-egui/src/scenario.rs @@ -214,11 +214,18 @@ impl Harness { /// Lay out and draw one frame (feeding queued pointer and key events). pub fn frame(&mut self) { + self.frame_with(Vec::new()); + } + + /// One frame that receives all these events at once (a quick tap, or input queued during a + /// slow frame), before any queued ones. + pub fn frame_with(&mut self, events: Vec) { self.time += 1.0 / 60.0; let mut raw = egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, self.size)), time: Some(self.time), focused: true, + events, ..Default::default() }; self.app.raw_input_hook(&mut raw);