Undo: an anchor drag ends when a command renumbers its path

A command run during a Direct Selection drag of anchors or segments, such as
Add Anchor Point or Reverse Path Direction, can number the path's anchors
anew. The drag kept its old anchor numbers and then moved a different point.
When the command changes a path that holds the drag's selected anchors or
segments in more than a move of the whole path, the drag now ends without a
step of its own, and the command's edit stays. A command that only moves the
path leaves the drag going on the same anchor.
This commit is contained in:
dotCooCoo
2026-11-24 20:22:27 -08:00
parent dd37e3c14b
commit c1859a1f1e
2 changed files with 101 additions and 1 deletions
+40 -1
View File
@@ -342,7 +342,9 @@ impl Session {
/// Drawing showed is applied to it again. The warning or the error of that preview goes to
/// the command's result ([`Session::note_settled_warning`]); a preview that fails leaves the
/// interaction with nothing to commit. An interaction whose document the command closed ends,
/// and a document the command left an interaction of its own on keeps that one.
/// and so does one with anchors or segments selected on a path the command changed in more
/// than a move. A document
/// the command left an interaction of its own on keeps that one.
pub(crate) fn put_back_interaction(&mut self) {
let Some(crate::HeldInteraction { uid, interaction: mut it, shown, state, paint }) = self.held_interaction.take() else { return };
// A selection command loaded the paints of the selection it made into the fill and stroke
@@ -412,6 +414,13 @@ impl Session {
}
return;
}
// The drag's anchors and segments are numbered in the paths it began on. When the command
// changed one of those paths in more than a move, the numbers can name other points, and
// the drag ends without a step.
if renumbered(&it.selection, &it.doc, &st.doc) {
self.clear_deferred_overlay_cache();
return;
}
let began = std::mem::replace(&mut it.doc, st.doc.clone());
// The drag acts on the objects it began with: a selection the command made (a paste, a
// duplicate, Select All) does not change what the drag moves.
@@ -773,6 +782,36 @@ fn drag_targets(params: &mut Value, began: &Document, doc: &Document) -> bool {
!ids.is_empty()
}
/// Whether a path with anchors or segments in `selection` is other in `doc` than in `began`, the
/// document a drag began from, in more than a move: its anchors added, removed, put in another
/// order (Reverse Path Direction) or reshaped, or a subpath opened or closed. A path the command
/// moved as a whole keeps its numbers.
fn renumbered(selection: &Selection, began: &Document, doc: &Document) -> bool {
fn path(d: &Document, id: vectorcraft_doc::NodeId) -> Option<&[vectorcraft_geom::SubPath]> {
d.node(id).and_then(|n| n.path_data()).map(|p| p.subpaths.as_slice())
}
selection.anchors.keys().chain(selection.segments.keys()).any(|&id| match (path(began, id), path(doc, id)) {
(Some(was), Some(now)) => !moved_whole(was, now),
(was, now) => was.is_some() != now.is_some(),
})
}
/// Whether the subpaths `now` are the subpaths `was` moved by one offset: the same number of each,
/// each opened or closed as before, every anchor of the same kind, and every anchor and handle
/// offset alike.
fn moved_whole(was: &[vectorcraft_geom::SubPath], now: &[vectorcraft_geom::SubPath]) -> bool {
if was.len() != now.len() || was.iter().zip(now).any(|(a, b)| a.closed != b.closed || a.anchors.len() != b.anchors.len()) {
return false;
}
let mut pairs = was.iter().zip(now).flat_map(|(a, b)| a.anchors.iter().zip(b.anchors.iter()));
let Some((a0, b0)) = pairs.next() else { return true };
let (dx, dy) = (b0.p.x - a0.p.x, b0.p.y - a0.p.y);
let alike = |a: vectorcraft_geom::Point, b: vectorcraft_geom::Point| {
(b.x - a.x - dx).abs() <= vectorcraft_geom::EPS && (b.y - a.y - dy).abs() <= vectorcraft_geom::EPS
};
std::iter::once((a0, b0)).chain(pairs).all(|(a, b)| a.kind == b.kind && alike(a.p, b.p) && alike(a.h_in, b.h_in) && alike(a.h_out, b.h_out))
}
/// The journal entries that select `selection` again in `doc` after `made`, the selection a
/// command left with `layer` current and `rows` highlighted in the Layers panel ([`object_steps`]).
/// When selecting the objects would make another layer current, `layer.setCurrent` makes `layer`
@@ -722,6 +722,67 @@ fn a_scale_rotate_or_pen_drag_of_an_object_a_command_deleted_ends_without_an_edi
}
}
/// A Direct Selection drag of an anchor, with a command during it that numbers the path's anchors
/// anew (Add Anchor Point before the dragged one, Reverse Path Direction): the command's edit
/// stays, the drag ends without an edit of its own and moves no other anchor of the path.
#[test]
fn an_anchor_drag_after_a_command_renumbers_its_anchors_moves_no_other_anchor() {
let commands = [("path.insertAnchor", json!({"segment": 0, "t": 0.5}), 1), ("path.reverse", json!({}), 0)];
for ((id, params, added), real_time) in commands.iter().flat_map(|c| [(c, true), (c, false)]) {
let at = format!("{id}, real time {real_time}");
let (mut s, target, _) = two_squares(real_time);
s.execute("select.none", &json!({})).unwrap();
s.select_tool("directSelection", view()).unwrap();
let points = |s: &Session| -> Vec<(f64, f64)> {
let doc = &s.doc().unwrap().doc;
let path = doc.node(target).and_then(|n| n.path_data()).expect("the square is a path");
path.anchors().map(|(_, _, a)| (a.p.x, a.p.y)).collect()
};
let before = points(&s);
let &(x, y) = before.last().expect("the square has anchors");
let steps = s.doc().unwrap().history.undo.len();
let mut params = params.clone();
params["id"] = json!(target.0);
drag_with(&mut s, &[(x, y), (x + 10.0, y + 10.0), (x + 30.0, y + 20.0)], |s| {
s.execute(id, &params).unwrap();
});
assert!(!s.in_interaction(), "{at}");
let after = points(&s);
assert_eq!(after.len(), before.len() + added, "{at}: the command's edit stays");
assert!(before.iter().all(|p| after.contains(p)), "{at}: no anchor of the square moved: {before:?} → {after:?}");
assert_eq!(s.doc().unwrap().history.undo.len(), steps + 1, "{at}: only the command is a step");
}
}
/// A Direct Selection drag of an anchor, with a command during it that moves the path: the drag
/// goes on and moves the same anchor of the moved path.
#[test]
fn an_anchor_drag_after_a_command_moves_its_path_moves_the_same_anchor() {
for real_time in [true, false] {
let at = format!("real time {real_time}");
let (mut s, target, _) = two_squares(real_time);
s.execute("select.none", &json!({})).unwrap();
s.select_tool("directSelection", view()).unwrap();
let points = |s: &Session| -> Vec<(f64, f64)> {
let doc = &s.doc().unwrap().doc;
let path = doc.node(target).and_then(|n| n.path_data()).expect("the square is a path");
path.anchors().map(|(_, _, a)| (a.p.x, a.p.y)).collect()
};
let before = points(&s);
let &(x, y) = before.last().expect("the square has anchors");
let steps = s.doc().unwrap().history.undo.len();
drag_with(&mut s, &[(x, y), (x + 10.0, y + 10.0), (x + 30.0, y + 20.0)], |s| {
s.execute("object.move", &json!({"ids": [target.0], "dx": 0, "dy": 50})).unwrap();
});
let after = points(&s);
let last = before.len() - 1;
let moved: Vec<(f64, f64)> =
before.iter().enumerate().map(|(i, &(px, py))| if i == last { (px + 30.0, py + 70.0) } else { (px, py + 50.0) }).collect();
assert_eq!(after, moved, "{at}: the drag moves the same anchor of the moved square");
assert_eq!(s.doc().unwrap().history.undo.len(), steps + 2, "{at}: the move and the drag are a step each");
}
}
/// With the Live Paint Bucket's Selection tool (Cmd+drag), a fill chosen during the drag stays the
/// new-art fill after the release and after Escape, though the drag puts back the bucket's colors
/// it began with.