diff --git a/crates/ui-egui/src/canvas/input.rs b/crates/ui-egui/src/canvas/input.rs index a6a0509de5..67391b2e6e 100644 --- a/crates/ui-egui/src/canvas/input.rs +++ b/crates/ui-egui/src/canvas/input.rs @@ -139,8 +139,10 @@ 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: the moves after the release in this frame are hover. - drag_released(app, ui, &f, d, release_pos(ui, button, false).unwrap_or(p)); + // The drag ends where its button went up, with the modifiers held then: the moves and + // modifier changes after the release in this frame do not count. + let up = Pointing { m: release_mods(ui, button).unwrap_or(f.m), ..f }; + drag_released(app, ui, &up, d, release_pos(ui, button, false).unwrap_or(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. @@ -744,6 +746,16 @@ fn release_pos(ui: &Ui, button: egui::PointerButton, last: bool) -> Option }) } +/// The modifiers held when `button` first went up in this frame, the release that ends a drag. +fn release_mods(ui: &Ui, button: egui::PointerButton) -> Option { + ui.input(|i| { + i.events.iter().find_map(|e| match e { + egui::Event::PointerButton { button: b, pressed: false, modifiers, .. } if *b == button => Some(*modifiers), + _ => None, + }) + }) +} + /// Whether Space moves the Zoom tool's marquee in the drag under way ([`zoom_marquee_start`]). fn zoom_space_free_id() -> egui::Id { egui::Id::unique("canvas-zoom-space-free") diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index d5244aa234..cc4660f7bf 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -1421,6 +1421,36 @@ fn shift_clicking_canvas_artboard_label_toggles_multi_selection() { assert_eq!(app.session.tool_options()["selected"], json!([2]), "Plain click selects that artboard alone"); } +/// A tool drag ends with the modifiers held when its button went up. Shift let go after the +/// release in the same frame still constrains the Rectangle tool's last rectangle to a square, and +/// Shift pressed after the release does not constrain it. +#[test] +fn tool_drag_ends_with_the_modifiers_of_its_button_release() { + let draw = |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 from = app.canvas_rect.unwrap().center() - vec2(30.0, 15.0); + let to = from + vec2(60.0, 30.0); + let b = |pos, pressed| egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers: held }; + frame(&mut app, &ctx, vec![egui::Event::ModifiersChanged(held), egui::Event::PointerMoved(from), b(from, true)]); + frame(&mut app, &ctx, vec![egui::Event::PointerMoved(from + vec2(30.0, 15.0))]); + frame(&mut app, &ctx, vec![egui::Event::PointerMoved(to), b(to, false), egui::Event::ModifiersChanged(after)]); + frame(&mut app, &ctx, vec![egui::Event::ModifiersChanged(egui::Modifiers::NONE)]); + let st = app.session.active().unwrap(); + let kids = st.doc.layers[0].children().unwrap(); + assert_eq!(kids.len(), 1, "the drag draws one rectangle"); + let b = kids[0].geometric_bounds().unwrap(); + (b.width(), b.height()) + }; + let (w, h) = draw(egui::Modifiers::SHIFT, egui::Modifiers::NONE); + assert!((w - h).abs() < 1e-6, "Shift held at the release draws a square, got {w} x {h}"); + let (w, h) = draw(egui::Modifiers::NONE, egui::Modifiers::SHIFT); + assert!((w - 2.0 * h).abs() < 1e-6, "Shift pressed after the release does not constrain, got {w} x {h}"); +} + /// A press on an artboard's name starts no drag. Moving with the button still held is not hover, /// so Shift over a gridline intersection does not position the Perspective Selection tool's plane /// until the button is up.