diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs index 7a5d16dbe7..1c6ae43be8 100644 --- a/crates/ui-egui/src/canvas.rs +++ b/crates/ui-egui/src/canvas.rs @@ -839,10 +839,21 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect: // box is only drawn and driven on the primary canvas. let transform_owns_pointer = primary && app.ui.transform.is_some() && temporary.is_none() && !middle; let hand_pan = tool == Tool::Hand && !transform_owns_pointer; + // A press outside an open canvas tool menu dismisses it (`canvas_tool_menu::show`, after this + // input pass) and the whole gesture is consumed until its release, as in Photoshop: no tool + // press, stroke, drag, pan or zoom step. The menu closes mid-frame (after this input pass), so + // the open-menu hit test covers the dismissal frame, and the flag persists the block for the + // frames after it, until the release clear below the click handling. It gates the Hand and + // flick pans below too: they read `response.dragged()` directly and never see `buttons` + // (CodeRabbit, #9841). + let menu_dismiss_press = app.ui.canvas_tool_menu_dismissed + || crate::canvas_tool_menu::menu_rect_if_open(app, &ctx).is_some_and(|r| { + ui.input(|i| i.pointer.press_origin()).is_some_and(|p| !r.contains(p)) + }); // Preferences › Tools › Enable Flick Panning: a Hand drag let go while moving glides on. let flick_id = egui::Id::unique(("pc-flick-pan", idx, primary, ctx.viewport_id())); let flick_on = app.session.prefs().tools.enable_flick_panning; - if let Some(d) = crate::flick_pan::frame(&ctx, flick_id, hand_pan && response.dragged() && !stroke_dismissal, response.drag_delta(), flick_on) { + if let Some(d) = crate::flick_pan::frame(&ctx, flick_id, hand_pan && response.dragged() && !stroke_dismissal && !menu_dismiss_press, response.drag_delta(), flick_on) { let d = xf.unmap_vec(d) / (view.zoom / ppp); if d.is_finite() { view.zoom_animating = false; @@ -855,7 +866,7 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect: ctx.data_mut(|m| m.get_temp_mut_or_default::(flick_id).stop()); } } - if hand_pan && response.dragged() && !stroke_dismissal { + if hand_pan && response.dragged() && !stroke_dismissal && !menu_dismiss_press { let d = xf.unmap_vec(response.drag_delta()) / (view.zoom / ppp); view.center[0] -= d.x; view.center[1] -= d.y; @@ -889,15 +900,6 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect: crate::paint_mouse::sync_tool_brush(app); let mut buttons = crate::paint_mouse::canvas_buttons(app, &response, tool); - // A press outside an open canvas tool menu dismisses it (`canvas_tool_menu::show`, after - // this input pass) and the whole gesture is consumed until its release, as in Photoshop: - // no tool press, stroke, drag or zoom step. The menu closes mid-frame (after this input - // pass), so the open-menu hit test covers the dismissal frame and the flag persists the - // block for the frames after it, until the release clear below the click handling. - let menu_dismiss_press = app.ui.canvas_tool_menu_dismissed - || crate::canvas_tool_menu::menu_rect_if_open(app, &ctx).is_some_and(|r| { - ui.input(|i| i.pointer.press_origin()).is_some_and(|p| !r.contains(p)) - }); if menu_dismiss_press { // Navigation and drags read `buttons` directly (zoom holds and drags, Rotate View): // starve them. The release click stays armed, so the click handling below consumes diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu/tests.rs b/crates/ui-egui/src/canvas/canvas_tool_menu/tests.rs index 1a1679e6c9..d72f3b1469 100644 --- a/crates/ui-egui/src/canvas/canvas_tool_menu/tests.rs +++ b/crates/ui-egui/src/canvas/canvas_tool_menu/tests.rs @@ -639,6 +639,50 @@ fn a_held_menu_dismissal_press_never_arms_the_zoom_hold() { assert_eq!(h.state().ui.views[0].zoom, zoom, "the dismissal release does not zoom either"); } +/// #9832 (CodeRabbit): while the dismissal press stays down, the temporary Hand (Space) and the +/// selected Hand must not pan either — the consumed gesture is inert until its release, and the +/// pans read `response.dragged()` directly, so they need the gate up front. +#[test] +fn a_held_menu_dismissal_press_cannot_pan_the_view() { + fn key(h: &mut Harness<'static, PhotocraftApp>, k: egui::Key, pressed: bool) { + h.event(egui::Event::Key { key: k, physical_key: None, pressed, repeat: false, modifiers: Modifiers::NONE }); + h.run_steps(1); + } + let mut h = zoom_harness(false); + let (zoom, center0) = (h.state().ui.views[0].zoom, h.state().ui.views[0].center); + let open_at = screen_of(h.state(), [300.0, 100.0]); + right_click(&mut h, open_at, Modifiers::NONE); + assert!(h.state().ui.canvas_tool_menu.is_some(), "right-click opens the view menu"); + // The dismissal press: the menu goes, and the button stays down. + let far = dismissal_point(&h); + h.event(egui::Event::PointerMoved(far)); + h.run_steps(1); + h.event(egui::Event::PointerButton { pos: far, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); + h.run_steps(1); + assert!(h.state().ui.canvas_tool_menu.is_none(), "the outside press dismisses the menu"); + // Hold Space: the temporary Hand arms, but dragging must not pan the view. + key(&mut h, egui::Key::Space, true); + assert_eq!(crate::input::hold_keys::held_tool(h.state(), &h.ctx), Some(crate::input::hold_keys::Temporary::Hand), "Space arms the temporary hand"); + h.event(egui::Event::PointerMoved(far + vec2(120.0, -60.0))); + h.run_steps(2); + h.event(egui::Event::PointerMoved(far + vec2(240.0, -120.0))); + h.run_steps(2); + assert_eq!(h.state().ui.views[0].center, center0, "a held dismissal press must not pan the view"); + assert_eq!(h.state().ui.views[0].zoom, zoom); + // Release both: an ordinary Hand drag pans again, the flag did not stick. + h.event(egui::Event::PointerButton { pos: far, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); + key(&mut h, egui::Key::Space, false); + h.state_mut().ui.tool = Tool::Hand; + h.run_steps(1); + h.event(egui::Event::PointerButton { pos: far, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); + h.run_steps(1); + h.event(egui::Event::PointerMoved(far + vec2(100.0, 50.0))); + h.run_steps(2); + h.event(egui::Event::PointerButton { pos: far, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); + h.run_steps(2); + assert_ne!(h.state().ui.views[0].center, center0, "an ordinary Hand drag pans the view again"); +} + #[test] fn view_menu_zoom_in_keeps_the_clicked_point_under_the_pointer() { let mut app = zoom_app(400, 300);