diff --git a/crates/engine/src/tests/quickpen.rs b/crates/engine/src/tests/quickpen.rs index 39cc07a530..e9181b03d7 100644 --- a/crates/engine/src/tests/quickpen.rs +++ b/crates/engine/src/tests/quickpen.rs @@ -676,6 +676,37 @@ fn a_drag_back_to_its_start_records_no_undo_step() { assert_eq!(points(&s), [pt(100.0, 100.0), pt(200.0, 100.0), pt(300.0, 100.0)], "press at {at:?}"); assert_eq!(undo_steps(&s), undo, "press at {at:?}"); } + // Choosing another tool during such a drag records none either. + let v = view(); + s.pointer(&PointerEvent::new(PointerKind::Down, 200.0, 100.0), v).unwrap(); + s.pointer(&PointerEvent::new(PointerKind::Drag, 200.0, 150.0), v).unwrap(); + s.pointer(&PointerEvent::new(PointerKind::Drag, 200.0, 100.0), v).unwrap(); + s.select_tool("selection", v).unwrap(); + assert_eq!(points(&s), [pt(100.0, 100.0), pt(200.0, 100.0), pt(300.0, 100.0)]); + assert_eq!(undo_steps(&s), undo); +} + +/// When another command changes the selection while the press is down, neither a click nor a drag +/// selects or moves the anchor the press found. +#[test] +fn a_selection_change_during_an_edit_press_stops_it() { + let mut s = session(); + let id = selected_quick_pen_path(&mut s, (100.0, 100.0), json!([{"x": 200, "y": 100}, {"x": 300, "y": 100}])); + let v = view(); + for drag in [false, true] { + s.execute("select.set", &json!({"ids": [id.0]})).unwrap(); + let undo = undo_steps(&s); + s.pointer(&PointerEvent::new(PointerKind::Down, 200.0, 100.0), v).unwrap(); + s.execute("select.none", &json!({})).unwrap(); + let y = if drag { 150.0 } else { 100.0 }; + if drag { + s.pointer(&PointerEvent::new(PointerKind::Drag, 200.0, y), v).unwrap(); + } + s.pointer(&PointerEvent::new(PointerKind::Up, 200.0, y), v).unwrap(); + assert!(s.doc().unwrap().selection.objects.is_empty(), "drag: {drag}"); + assert_eq!(points(&s), [pt(100.0, 100.0), pt(200.0, 100.0), pt(300.0, 100.0)], "drag: {drag}"); + assert_eq!(undo_steps(&s), undo, "drag: {drag}"); + } } /// When another command edits the path while the press is down, neither a drag nor a release past diff --git a/crates/tools/src/quickpen.rs b/crates/tools/src/quickpen.rs index bdb80c2a5b..62819f793f 100644 --- a/crates/tools/src/quickpen.rs +++ b/crates/tools/src/quickpen.rs @@ -499,17 +499,14 @@ impl QuickPenTool { /// go past the drag distance after shorter drag events, moves what it holds to `p` and ends /// its undo step. A click on an end of an open path goes on drawing from it, a click on another /// anchor selects it alone (with Shift, adds it to the selected anchors or takes it out), and a - /// click on a segment starts a new path there. A click whose path another command edited while - /// the press was down does nothing. + /// click on a segment starts a new path there. A click whose path or selection another command + /// changed while the press was down does nothing. fn release_edit(&mut self, cx: &ToolContext, mut e: Edit, p: Point) -> Vec { if e.began || p.distance(e.start) >= cx.tol(DRAG_PX) { let mut out = e.drag(cx, p); - // A drag that could not begin (its path changed under the press) has no step to end. - // One that came back to where it started records none, unless another command's - // edit during the drag is in the step. - if e.began { - out.push(if e.moved || e.interrupted { Action::Commit } else { Action::Cancel }); - } + // A drag that could not begin (its path or the selection changed under the press) has + // no step to end. + out.extend(e.finish(cx)); return out; } if !e.unchanged(cx) { @@ -734,14 +731,13 @@ impl Tool for QuickPenTool { } } - fn deactivate(&mut self, _cx: &ToolContext) -> Vec { + fn deactivate(&mut self, cx: &ToolContext) -> Vec { self.drawing = None; self.snap.clear(); - let moved = self.edit.take().is_some_and(|e| e.began); + let moved = self.edit.take().and_then(|mut e| e.finish(cx)); match self.press.take() { Some(pr) if pr.begun => vec![Action::Commit], - _ if moved => vec![Action::Commit], - _ => vec![], + _ => moved.into_iter().collect(), } } diff --git a/crates/tools/src/quickpen/edit.rs b/crates/tools/src/quickpen/edit.rs index 8ba8f970ce..2620118b91 100644 --- a/crates/tools/src/quickpen/edit.rs +++ b/crates/tools/src/quickpen/edit.rs @@ -93,6 +93,8 @@ pub(super) struct Edit { pub(super) interrupted: bool, /// The grabbed path as the document had it when the press went down. pressed: Option>, + /// The selection when the press went down. + pressed_selection: Selection, snap: Option, pub(super) guides: Vec, } @@ -100,7 +102,20 @@ pub(super) struct Edit { impl Edit { pub(super) fn new(cx: &ToolContext, grab: Grab, start: Point, shift: bool) -> Self { let pressed = shared_node(cx.doc, grab.id()).cloned(); - Self { grab, start, shift, began: false, moved: false, revision: None, interrupted: false, pressed, snap: None, guides: vec![] } + let pressed_selection = cx.selection.clone(); + Self { + grab, + start, + shift, + began: false, + moved: false, + revision: None, + interrupted: false, + pressed, + pressed_selection, + snap: None, + guides: vec![], + } } /// Whether the drag began and still previews: no other command changed the document or the @@ -112,18 +127,28 @@ impl Edit { self.began && !self.interrupted } - /// Whether the grabbed path is as the document had it when the press went down. + /// What ends a drag that began when the press is let go: a commit, or a cancel when the drag + /// came back to where it started and no other command's edit is in its undo step. + pub(super) fn finish(&mut self, cx: &ToolContext) -> Option { + if !self.began { + return None; + } + Some(if self.open(cx) && !self.moved { Action::Cancel } else { Action::Commit }) + } + + /// Whether the grabbed path and the selection are as they were when the press went down. pub(super) fn unchanged(&self, cx: &ToolContext) -> bool { - match (cx.doc.node(self.grab.id()), self.pressed.as_ref()) { + let path = match (cx.doc.node(self.grab.id()), self.pressed.as_ref()) { (Some(now), Some(then)) => same_node(now, then), _ => false, - } + }; + path && *cx.selection == self.pressed_selection } /// Moves what the press holds by the drag to `p`. The first move selects the anchor, or the /// segment, unless it is already selected, and begins the undo step; a drag that starts on - /// one of several selected anchors moves them all. When the grabbed path changed after the - /// press went down, the drag does not begin, and after another command's edit during the + /// one of several selected anchors moves them all. When the grabbed path or the selection + /// changed after the press went down, the drag does not begin, and after another command's edit during the /// drag it moves nothing more. pub(super) fn drag(&mut self, cx: &ToolContext, p: Point) -> Vec { let mut out = vec![];