diff --git a/crates/ui-egui/src/canvas/input.rs b/crates/ui-egui/src/canvas/input.rs index 5be38cd994..b996462561 100644 --- a/crates/ui-egui/src/canvas/input.rs +++ b/crates/ui-egui/src/canvas/input.rs @@ -121,6 +121,13 @@ pub(super) fn handle_input(app: &mut VectorcraftApp, ui: &Ui, resp: &egui::Respo ui.data_mut(|dd| dd.insert_temp(name_press_id(), (i, press_shift))); } else { begin_drag(app, ui, &f, p); + // Pressed and released in this frame (a quick click or tap): the press ends now, with + // the modifiers, Space and position of its release, not those of the next frame. + if !pointer.primary_down() + && let Some(d) = ui.data(|dd| dd.get_temp::(drag_id())) + { + end_drag(app, ui, &f, d, egui::PointerButton::Primary, p); + } } } else if drag.is_none() && resp.hovered() @@ -140,12 +147,7 @@ pub(super) fn handle_input(app: &mut VectorcraftApp, ui: &Ui, resp: &egui::Respo if held { drag_held(app, ui, &f, d, p); } else { - // The drag ends where its button went up, with the modifiers and Space held then: the - // moves and key changes after the release in this frame do not count. - let m = release_mods(ui, button).unwrap_or(f.m); - let space = space_lends_view_tools(app, m, key_at_release(ui, egui::Key::Space, button, f.space_before)); - let up = Pointing { m, space, ..f }; - drag_released(app, ui, &up, d, release_pos(ui, button, false).unwrap_or(p)); + end_drag(app, ui, &f, d, button, p); } } else if let Some(p) = hover_tool // A press on an artboard's name starts no drag: the moves while it is held are not hover. @@ -594,6 +596,16 @@ fn dispatch_frame_moves(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, backquo } } +/// A gesture whose `button` went up ends where it went up, with the modifiers and Space held then: +/// the moves and key changes after the release in this frame do not count. Without a release in +/// this frame, it ends at `p` with the modifiers and Space held now. +fn end_drag(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, d: Drag, button: egui::PointerButton, p: Pos2) { + let m = release_mods(ui, button).unwrap_or(f.m); + let space = space_lends_view_tools(app, m, key_at_release(ui, egui::Key::Space, button, f.space_before)); + let up = Pointing { m, space, ..*f }; + drag_released(app, ui, &up, d, release_pos(ui, button, false).unwrap_or(p)); +} + /// A gesture released at `p`: the view settles, or the tool gets its up. fn drag_released(app: &mut VectorcraftApp, ui: &Ui, f: &Pointing, d: Drag, p: Pos2) { let Pointing { resp, rect, m, space, xf, view, .. } = *f; diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index 73b2d37547..a72dd436ff 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -1476,6 +1476,39 @@ fn shift_pressed_at_a_still_release_constrains_the_tool_drag() { assert!((w - h).abs() < 1e-6, "Shift pressed at a still release draws a square, got {w} x {h}"); } +/// A click whose press and release come in one frame ends with the modifiers held at its release, +/// not those of the next frame: Alt pressed before the release and let go after it in that frame +/// opens the Rectangle dialog centered on the click, and Alt pressed after the release does not. +#[test] +fn a_click_within_one_frame_ends_with_the_modifiers_of_its_release() { + let click = |held: egui::Modifiers, after: egui::Modifiers| { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + app.select_tool("rectangle"); + let ctx = egui::Context::default(); + frame(&mut app, &ctx, vec![]); + let at = app.canvas_rect.unwrap().center(); + let b = |pressed, modifiers| egui::Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed, modifiers }; + frame( + &mut app, + &ctx, + vec![ + egui::Event::PointerMoved(at), + b(true, egui::Modifiers::NONE), + egui::Event::ModifiersChanged(held), + b(false, held), + egui::Event::ModifiersChanged(after), + ], + ); + frame(&mut app, &ctx, vec![egui::Event::ModifiersChanged(egui::Modifiers::NONE)]); + let d = app.ui.dialog.as_ref().expect("the click opens the Rectangle dialog"); + assert_eq!(d.kind, "rectangle"); + d.fields.get("alt").cloned() + }; + assert_eq!(click(egui::Modifiers::ALT, egui::Modifiers::NONE), Some(json!(true)), "Alt held at the release centers the rectangle"); + assert_eq!(click(egui::Modifiers::NONE, egui::Modifiers::ALT), Some(json!(false)), "Alt pressed after the release does not"); +} + /// Space pressed after a tool drag's release, in the same frame, does not count for the drag: the /// Rectangle tool's last move still resizes the rectangle instead of moving it. #[test]