diff --git a/crates/ui-egui/src/canvas/tests.rs b/crates/ui-egui/src/canvas/tests.rs index cb02c42a38..d560fd2b6b 100644 --- a/crates/ui-egui/src/canvas/tests.rs +++ b/crates/ui-egui/src/canvas/tests.rs @@ -718,6 +718,121 @@ fn cmd_with_another_tool_drags_with_the_selection_tool_used_last() { assert_eq!(cmd_drag(&mut app, inside, inside), "selection"); } +/// One frame of the whole app on `ctx` with `events`, in a 1600 × 850 window, as the hosts run it. +fn app_step(app: &mut VectorcraftApp, ctx: &egui::Context, events: Vec) { + let screen = egui::Rect::from_min_size(Pos2::ZERO, vec2(1600.0, 850.0)); + crate::testing::app_frame(app, ctx, egui::RawInput { screen_rect: Some(screen), events, ..Default::default() }); +} + +/// A Rectangle tool on a new 400 × 300 document in the whole app, after three frames, with Smart +/// Guides and Snap to Point off → (the app, its context, the canvas view). +fn rectangle_tool() -> (VectorcraftApp, egui::Context, craft::behavior::CanvasView) { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); + app.ui.view.smart_guides = false; + app.ui.view.snap_to_point = false; + app.select_tool("rectangle"); + let ctx = crate::testing::ui_context(); + for _ in 0..3 { + app_step(&mut app, &ctx, vec![]); + } + let xf = app.canvas_view().unwrap(); + (app, ctx, xf) +} + +/// The bounds of the paths in the active document. +fn drawn_paths(app: &VectorcraftApp) -> Vec { + let mut found = vec![]; + app.session.active().unwrap().doc.walk(|n| { + if n.path_data().is_some() { + found.extend(n.geometric_bounds()); + } + }); + found +} + +/// A primary button event at `pos` with `modifiers` held. +fn primary(pos: Pos2, pressed: bool, modifiers: egui::Modifiers) -> egui::Event { + egui::Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed, modifiers } +} + +/// A press or release of Space. +fn space_key(pressed: bool) -> egui::Event { + egui::Event::Key { key: egui::Key::Space, physical_key: None, pressed, repeat: false, modifiers: egui::Modifiers::NONE } +} + +/// 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 rectangle to a square, Shift +/// pressed after the release does not constrain it, and Alt held only at the release draws it +/// from its center. +#[test] +fn tool_drag_ends_with_the_modifiers_of_its_button_release() { + let draw = |held: egui::Modifiers, after: egui::Modifiers| { + let (mut app, ctx, xf) = rectangle_tool(); + let (from, to) = (xf.to_screen(Point::new(100.0, 100.0)), xf.to_screen(Point::new(160.0, 130.0))); + let none = egui::Modifiers::NONE; + app_step(&mut app, &ctx, vec![egui::Event::PointerMoved(from)]); + app_step(&mut app, &ctx, vec![primary(from, true, none)]); + app_step(&mut app, &ctx, vec![egui::Event::PointerMoved(from.lerp(to, 0.5))]); + app_step( + &mut app, + &ctx, + vec![egui::Event::PointerMoved(to), egui::Event::ModifiersChanged(held), primary(to, false, held), egui::Event::ModifiersChanged(after)], + ); + app_step(&mut app, &ctx, vec![egui::Event::ModifiersChanged(none)]); + let paths = drawn_paths(&app); + assert_eq!(paths.len(), 1, "the drag draws one rectangle"); + paths[0] + }; + let near = |a: f64, b: f64| (a - b).abs() < 1e-3; + let r = draw(egui::Modifiers::SHIFT, egui::Modifiers::NONE); + assert!(near(r.width(), r.height()), "Shift held at the release draws a square: {r:?}"); + let r = draw(egui::Modifiers::NONE, egui::Modifiers::SHIFT); + assert!(near(r.width(), 60.0) && near(r.height(), 30.0), "Shift pressed after the release does not constrain: {r:?}"); + let r = draw(egui::Modifiers::ALT, egui::Modifiers::NONE); + assert!(near(r.width(), 120.0) && near(r.x0, 40.0), "Alt held at the release draws from the center: {r:?}"); +} + +/// Shift pressed only in the frame of a tool drag's release, with the pointer held still since +/// the drag's last frame, still constrains the Rectangle tool's rectangle to a square. +#[test] +fn shift_pressed_at_a_still_release_constrains_the_tool_drag() { + let (mut app, ctx, xf) = rectangle_tool(); + let (from, to) = (xf.to_screen(Point::new(100.0, 100.0)), xf.to_screen(Point::new(160.0, 130.0))); + app_step(&mut app, &ctx, vec![egui::Event::PointerMoved(from), primary(from, true, egui::Modifiers::NONE)]); + app_step(&mut app, &ctx, vec![egui::Event::PointerMoved(from.lerp(to, 0.5))]); + app_step(&mut app, &ctx, vec![egui::Event::PointerMoved(to)]); + app_step(&mut app, &ctx, vec![egui::Event::ModifiersChanged(egui::Modifiers::SHIFT), primary(to, false, egui::Modifiers::SHIFT)]); + app_step(&mut app, &ctx, vec![egui::Event::ModifiersChanged(egui::Modifiers::NONE)]); + let paths = drawn_paths(&app); + assert_eq!(paths.len(), 1, "the drag draws one rectangle"); + assert!((paths[0].width() - paths[0].height()).abs() < 1e-3, "Shift pressed at a still release draws a square: {:?}", paths[0]); +} + +/// The move in the frame of a tool drag's release counts for the drag: the Rectangle tool's last +/// move sizes the rectangle, and with Space held through the release it moves the rectangle +/// instead, at the size the drag had before that move. +#[test] +fn the_move_of_a_releasing_frame_counts_for_the_tool_drag() { + let draw = |space: bool| { + let (mut app, ctx, xf) = rectangle_tool(); + let (from, to) = (xf.to_screen(Point::new(100.0, 100.0)), xf.to_screen(Point::new(160.0, 130.0))); + let none = egui::Modifiers::NONE; + app_step(&mut app, &ctx, vec![egui::Event::PointerMoved(from), primary(from, true, none)]); + app_step(&mut app, &ctx, vec![egui::Event::PointerMoved(from.lerp(to, 0.5))]); + if space { + app_step(&mut app, &ctx, vec![space_key(true)]); + } + app_step(&mut app, &ctx, vec![egui::Event::PointerMoved(to), primary(to, false, none)]); + app_step(&mut app, &ctx, if space { vec![space_key(false)] } else { vec![] }); + let paths = drawn_paths(&app); + assert_eq!(paths.len(), 1, "the drag draws one rectangle"); + paths[0] + }; + assert_eq!(draw(false), Rect::new(100.0, 100.0, 160.0, 130.0), "the last move sizes the rectangle"); + assert_eq!(draw(true), Rect::new(130.0, 115.0, 160.0, 130.0), "with Space held through the release the last move moves it"); +} + /// #6369: only the selection box remains over the temporary halftone, and the original /// selection's outlines come back when Preview is turned off or the dialog is cancelled. #[test] diff --git a/crates/ui-egui/src/canvas/tool_adapter.rs b/crates/ui-egui/src/canvas/tool_adapter.rs index 5bda6d918e..af12608aa5 100644 --- a/crates/ui-egui/src/canvas/tool_adapter.rs +++ b/crates/ui-egui/src/canvas/tool_adapter.rs @@ -150,6 +150,18 @@ impl<'a> SessionTool<'a> { Self { app: RefCell::new(app), view, carrying: Cell::new(false), ctx: ui.ctx().clone(), layer: ui.layer_id() } } + /// `ev` with Shift, Alt, Cmd and Ctrl as they were held when the gesture's button went up in this pass, latched ones + /// included (the pointer frame's `pick_mods`), instead of as they are at the end of the pass: a modifier pressed or + /// let go after the release does not count for it. Space and Backtick stay as the host holds them. Unchanged in a + /// pass where that button did not come up. + fn at_release(&self, pointer: &ToolPointer, mut ev: PointerEvent) -> PointerEvent { + let frame = craft::ui::runtime::pointer::frame(&self.ctx); + if frame.release == Some(pointer.button) { + ev.mods = tool_mods(frame.pick_mods, ev.mods.space); + } + ev + } + /// Whether another window covers the canvas at `pointer`, so art moved there leaves the canvas: a floating /// document's or a floating panel's OS window. fn covered(&self, pointer: &ToolPointer) -> bool { @@ -322,7 +334,8 @@ impl CanvasTool for SessionTool<'_> { let covered = self.covered(pointer); let carried = super::drops::carry_out(&mut self.app(), cx, pointer, covered); if !carried { - let ev = pointer_event(PointerKind::Drag, pointer, cx); + // The moves of the pass that releases the press came before its release, with the keys held then. + let ev = self.at_release(pointer, pointer_event(PointerKind::Drag, pointer, cx)); dispatch(&mut self.app(), &ev, self.view); self.app().canvas.last_drag = Some((pointer.pos, ev)); } @@ -340,21 +353,31 @@ impl CanvasTool for SessionTool<'_> { return; } drop(app); + // The press ends where it went up, with the keys held at its release. + let up = self.at_release(pointer, pointer_event(PointerKind::Up, pointer, cx)); let covered = self.covered(pointer); let carried = super::drops::carried(&self.app(), cx, pointer, covered); self.app().canvas.panes.released_from = carried.as_ref().map(|art| (art.document, art.revision)); - if cx.over_drop_target() || carried.is_some() { + let given_up = cx.over_drop_target() || carried.is_some(); + if given_up { // The art goes to the drop target, or nowhere off the canvas: the move is given up where it started. let _ = self.app().session.cancel_interaction(); } if let Some(art) = carried.filter(|_| !cx.over_drop_target()) { // Released over a pane in the frame the pointer reached it: no target could take it yet. - let op = if pointer.mods.alt { DropOperation::Copy } else { DropOperation::Move }; + let op = if up.mods.alt { DropOperation::Copy } else { DropOperation::Move }; let mut app = self.app(); app.canvas.panes.released = super::drops::released_on_pane(&app, art, pointer.pos, op); } self.app().canvas.grab = None; - self.send(PointerKind::Up, pointer, cx); + // When the pointer moved or the keys changed since the drag's last move, the tool gets a move where the press went + // up first, as it does while held: the last move of the release's pass still sizes the shape, and Shift pressed in + // that pass still draws a square. A move that was given up gets none. + let last = self.app().canvas.last_drag.map(|(_, ev)| ev); + if !given_up && last.is_some_and(|last| (last.pos, last.mods, last.backquote) != (up.pos, up.mods, up.backquote)) { + dispatch(&mut self.app(), &PointerEvent { kind: PointerKind::Drag, ..up }, self.view); + } + dispatch(&mut self.app(), &up, self.view); if self.app().canvas.last_drag.is_some() { // A press that dragged is no click, and the framework counts presses: the next press starts a new run. self.app().ui.trace_pick_clicks = pointer.clicks; @@ -367,7 +390,7 @@ impl CanvasTool for SessionTool<'_> { _ => return, }; let before = self.app().session.tool_id(); - self.send(follow, pointer, cx); + dispatch(&mut self.app(), &PointerEvent { kind: follow, ..up }, self.view); // A double click on type switched a selection tool to the Type tool: the caret goes where the type was // clicked, so typing edits it at once. let switched = follow == PointerKind::DoubleClick && before != "type" && self.app().session.tool_id() == "type";