Live Corners: a nudge the pixel grid takes back keeps a live rectangle

An anchor nudge on a pixel-aligned rounded rectangle or polygon that the
pixel-grid snap cancels recorded an undo step and left the shape a live
path, because the anchor edit converts the live shape before the snap
runs. The no-change check now treats that conversion as no change when
the converted live shape matches what the same anchors would give
unmoved, so the shape stays a live rectangle and no undo step is
recorded. A change to the uncut outline or the corner settings still
counts as a change.
This commit is contained in:
dotCooCoo committed 2026-12-04 00:56:00 -08:00
1 parent 5eb3adea28
commit e3e11357f4
3 files changed
+55 -4

No files matched your search

+28 -2
View File
@@ -249,16 +249,42 @@ fn snap_keeping_selection(d: &mut Document, id: NodeId, sel: &mut Selection, bef
/// Whether `apply` on a copy of the active document, followed by the pixel-grid snap that
/// [`Session::edit`] makes, leaves the document as it was (a nudge that the snap of a
/// pixel-aligned path takes back). Without a pixel-aligned path among `ids` (or a compound path
/// holding one) the answer is `false` without trying.
/// holding one) the answer is `false` without trying. An anchor edit turns a live rectangle or
/// polygon into a live path with the same outline, radii and kinds; that conversion alone counts
/// as no change, while any change to the uncut outline or the corner settings still counts.
fn snap_undoes(s: &Session, ids: impl IntoIterator<Item = NodeId>, apply: impl FnOnce(&mut Document, &mut Selection) -> Result<()>) -> Result<bool> {
let st = s.doc()?;
if !ids.into_iter().any(|id| st.doc.node(compound_or_self(&st.doc, id)).is_some_and(|n| n.extras().pixel_aligned)) {
let ids: Vec<NodeId> = ids.into_iter().collect();
if !ids.iter().any(|&id| st.doc.node(compound_or_self(&st.doc, id)).is_some_and(|n| n.extras().pixel_aligned)) {
return Ok(false);
}
let mut doc = (*st.doc).clone();
let mut sel = st.selection.clone();
apply(&mut doc, &mut sel)?;
super::refresh_pixel_aligned(&st.doc, &mut doc);
for &id in &ids {
let (Some(NodeKind::Path { path, live, .. }), Some(anchors)) = (st.doc.node(id).map(|n| &n.kind), st.selection.anchors.get(&id)) else {
continue;
};
// A live path keeps its variant through an anchor edit, so only a rectangle or polygon
// needs the comparison below.
if !matches!(live.as_deref(), Some(vectorcraft_doc::LiveShape::Rectangle { .. } | vectorcraft_doc::LiveShape::Polygon { .. })) {
continue;
}
// The path and live shape the same anchors, left where they are, would turn this path
// into. The drawn path is cut from the live shape, and a recut can differ from the stored
// path by rounding, so the live shapes are compared and both fields are restored.
let (mut converted_path, mut converted) = (path.clone(), live.clone());
if vectorcraft_doc::edit_corner_anchors(&mut converted_path, &mut converted, anchors, |_, _| {}).is_none() {
continue;
}
if let Some(NodeKind::Path { path: after_path, live: after, .. }) = doc.node_mut(id).map(|n| &mut n.kind)
&& *after == converted
{
after.clone_from(live);
after_path.clone_from(path);
}
}
Ok(doc == *st.doc)
}
+24
View File
@@ -1446,3 +1446,27 @@ fn a_nudge_the_pixel_grid_takes_back_records_no_undo_step() {
assert_eq!(s.doc().unwrap().doc, doc);
assert_eq!(s.doc().unwrap().history.undo.len(), undo);
}
/// The same nudge on a rounded rectangle that no anchor edit has touched yet records no undo
/// step and leaves it a live rectangle.
#[test]
fn a_nudge_the_pixel_grid_takes_back_keeps_a_live_rectangle() {
for x in [10, 50, 137] {
let mut s = Session::new();
s.execute("file.new", &json!({"width": 400, "height": 400})).unwrap();
let id = NodeId(s.execute("shape.rectangle", &json!({"x": x, "y": 10, "width": 100, "height": 60})).unwrap()["id"].as_u64().unwrap());
s.execute("object.setPixelAligned", &json!({"ids": [id.0]})).unwrap();
run(&mut s, json!({"radius": 10}));
let left: Vec<[usize; 2]> = {
let lc = LiveCorners::of(s.doc().unwrap().doc.node(id).unwrap()).unwrap();
lc.anchors_of(&BTreeSet::from([0, 3])).into_iter().map(|(si, ai)| [si, ai]).collect()
};
s.execute("select.anchors", &json!({"id": id.0, "anchors": left, "mode": "set"})).unwrap();
let doc = s.doc().unwrap().doc.clone();
let undo = s.doc().unwrap().history.undo.len();
s.execute("path.moveAnchors", &json!({"dx": 0.2, "dy": 0})).unwrap();
assert_eq!(s.doc().unwrap().history.undo.len(), undo, "x = {x}");
assert!(matches!(s.doc().unwrap().doc.node(id).unwrap().live_shape(), Some(LiveShape::Rectangle { .. })), "x = {x}");
assert_eq!(s.doc().unwrap().doc, doc, "x = {x}");
}
}
+3 -2
View File
@@ -8938,8 +8938,9 @@ anchor away from `(x, y)`. When the recut changes the anchor layout, all selecte
through their uncut outline anchors and its selected segments are cleared. An unchanged layout keeps the selection.
Anchor edits instead make the shape a plain path when it has no rounded corner, when a live rectangle is sheared or
unevenly scaled, when an arrow-key nudge's selected curved segments bend or hold their anchors, or when a transform of
direct-selected anchors is other than a move. A nudge that changes nothing records no undo step and leaves Live Corners
as they are. These fallbacks edit the drawn anchors rather than recutting the uncut outline.
direct-selected anchors is other than a move. A nudge that changes nothing, including one that the pixel-grid snap of a
pixel-aligned path takes back, records no undo step and leaves Live Corners as they are: a live rectangle stays a live
rectangle. These fallbacks edit the drawn anchors rather than recutting the uncut outline.
`path.setHandle` with `symmetric: true` also places the anchor's other handle opposite the moved one at the same
length, and the anchor keeps its kind. At an open end, where no segment leaves the anchor on the other side,
`symmetric` has no effect and the handle moves as it would without it. Both `symmetric` and `independent` default to false, and a call with both true is