Quick Pen: a selection change during a press stops it

When another command changes the selection while a press on an anchor or
a segment is down, the release no longer selects an anchor, goes on
drawing from an open end, or moves anything. Choosing another tool during
a drag that came back to where it started now records no undo step.
This commit is contained in:
dotCooCoo committed 2026-11-21 21:51:20 -08:00
1 parent 1410f7500c
commit 357133cef1
3 files changed
+70 -18

No files matched your search

+31
View File
@@ -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
+8 -12
View File
@@ -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<Action> {
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<Action> {
fn deactivate(&mut self, cx: &ToolContext) -> Vec<Action> {
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(),
}
}
+31 -6
View File
@@ -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<Arc<Node>>,
/// The selection when the press went down.
pressed_selection: Selection,
snap: Option<PointSnap>,
pub(super) guides: Vec<Overlay>,
}
@@ -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<Action> {
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<Action> {
let mut out = vec![];