From cd08707be80d46802881856ae210e87e1771f8ab Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 5 Dec 2026 06:06:15 -0800 Subject: [PATCH] Undo: canceled undo groups and scopes count only as history restores Since #10882 a canceled undo group, and a canceled undo scope that put back an earlier snapshot, advance history_restores, which every edit target that reads rollbacks also hashes. They no longer advance rollbacks as well, and the two engine tests for that are removed; history_mem.rs covers the count. The Corner Radius tests now cancel an interaction whose preview drew the shape, which only rollbacks counts, so they still fail without rollbacks in corner_target. --- crates/engine/src/doc_state.rs | 2 +- crates/engine/src/session/undo.rs | 5 -- crates/engine/src/tests/journal.rs | 60 ------------------- .../src/panels/transform/shape/tests.rs | 34 ++++++----- 4 files changed, 19 insertions(+), 82 deletions(-) diff --git a/crates/engine/src/doc_state.rs b/crates/engine/src/doc_state.rs index 8b27c87f30..c1da77255b 100644 --- a/crates/engine/src/doc_state.rs +++ b/crates/engine/src/doc_state.rs @@ -121,7 +121,7 @@ pub struct DocState { /// Advances when Undo, Redo or a canceled undo group restores an older document snapshot. The same node ID can name different objects /// across that boundary. pub history_restores: u64, - /// Advances when a canceled undo group, undo scope or interaction, Exit Symbol Editing Mode taking + /// Advances when a canceled interaction, Exit Symbol Editing Mode taking /// back the entering step, Discard Empty Type taking back the step that made the text, or a command /// that panicked puts back an older document snapshot. A canceled plug-in run advances it /// when its calls changed the document. A preview advances it when it discards an earlier preview diff --git a/crates/engine/src/session/undo.rs b/crates/engine/src/session/undo.rs index 5d224e2a5e..910fbda9f9 100644 --- a/crates/engine/src/session/undo.rs +++ b/crates/engine/src/session/undo.rs @@ -125,7 +125,6 @@ impl Session { st.drop_stale_variables_highlight(); st.history_restores += 1; st.revision += 1; - st.rollbacks += 1; // The journal drops only the canceled groups' own entries and keeps the ones made in other documents meanwhile. journal.extend(g.journal); steps.extend(g.steps); @@ -275,7 +274,6 @@ impl Session { } Some(_) => return, } - let changed = !Arc::ptr_eq(&st.doc, &doc); let undone = std::mem::replace(&mut st.doc, doc); st.selection = selection; st.restore_isolation(scope.isolation, &undone); @@ -287,9 +285,6 @@ impl Session { st.history_restores += 1; } st.revision += 1; - if changed { - st.rollbacks += 1; - } if colors_changed { self.paint = scope.paint; } diff --git a/crates/engine/src/tests/journal.rs b/crates/engine/src/tests/journal.rs index 4031910307..ac2cf6c903 100644 --- a/crates/engine/src/tests/journal.rs +++ b/crates/engine/src/tests/journal.rs @@ -829,31 +829,6 @@ fn canceling_undo_groups_in_two_documents_drops_the_edits_of_both_from_the_journ assert_eq!(ids.last(), Some(&"edit.undoGroup.end")); } -/// A canceled undo group puts back an older snapshot, after which a new object can take a removed object's ID, so -/// it advances `rollbacks`; a group that ends without cancel leaves it as it is. Neither changes `history_restores`. -#[test] -fn undo_group_cancellation_advances_rollbacks_only_when_restoring() { - let mut s = Session::new(); - s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); - for cancel in [true, false] { - let restores = s.doc().unwrap().history_restores; - let rollbacks = s.doc().unwrap().rollbacks; - let before = s.doc().unwrap().doc.node_count(); - s.execute("edit.undoGroup.begin", &json!({})).unwrap(); - s.execute("shape.rectangle", &rect_step(10)["params"]).unwrap(); - assert!(s.doc().unwrap().doc.node_count() > before); - s.execute("edit.undoGroup.end", &json!({"cancel": cancel})).unwrap(); - assert_eq!(s.doc().unwrap().history_restores, restores); - if cancel { - assert!(s.doc().unwrap().rollbacks > rollbacks, "a canceled group advances the rollback count"); - assert_eq!(s.doc().unwrap().doc.node_count(), before); - } else { - assert_eq!(s.doc().unwrap().rollbacks, rollbacks); - assert!(s.doc().unwrap().doc.node_count() > before); - } - } -} - /// Exit Symbol Editing Mode with nothing changed takes back the entering step, which puts back an older snapshot, so /// it advances `rollbacks`; leaving after an edit adds a step and leaves it as it is. #[test] @@ -1492,41 +1467,6 @@ fn an_undo_scope_in_an_open_group_takes_back_only_its_own_edits() { assert_eq!(objects(&s), 0, "one undo takes back the group's edits"); } -/// A canceled undo scope that joined an open group advances `rollbacks` only when it restores an older snapshot; -/// a scope that makes no edit or ends without cancel leaves it as it is. Neither changes `history_restores`. -#[test] -fn joined_undo_scope_cancellation_advances_rollbacks_only_when_restoring() { - let mut s = Session::new(); - s.execute("file.new", &json!({"width": 400, "height": 300})).unwrap(); - s.begin_undo_group(); - for cancel in [true, false] { - let restores = s.doc().unwrap().history_restores; - let rollbacks = s.doc().unwrap().rollbacks; - let before = s.doc().unwrap().doc.node_count(); - let scope = s.begin_undo_scope().unwrap(); - s.execute("shape.rectangle", &rect_step(10)["params"]).unwrap(); - assert!(s.doc().unwrap().doc.node_count() > before); - s.end_undo_scope(scope, cancel); - assert_eq!(s.doc().unwrap().history_restores, restores); - if cancel { - assert!(s.doc().unwrap().rollbacks > rollbacks, "a canceled scope advances the rollback count"); - assert_eq!(s.doc().unwrap().doc.node_count(), before); - } else { - assert_eq!(s.doc().unwrap().rollbacks, rollbacks); - assert!(s.doc().unwrap().doc.node_count() > before); - } - assert!(s.doc().unwrap().undo_group.is_some()); - } - s.execute("shape.rectangle", &rect_step(10)["params"]).unwrap(); - let restores = s.doc().unwrap().history_restores; - let rollbacks = s.doc().unwrap().rollbacks; - let scope = s.begin_undo_scope().unwrap(); - s.end_undo_scope(scope, true); - assert_eq!(s.doc().unwrap().rollbacks, rollbacks, "canceling a scope with no edits preserves the rollback count"); - assert_eq!(s.doc().unwrap().history_restores, restores); - s.end_undo_group(false); -} - /// A canceled undo scope that joined an open group forgets the journal entries it cut: when a command /// in another document takes the same entry number, a later cancel of the group keeps that entry, and /// the journal replays to the same documents. diff --git a/crates/ui-egui/src/panels/transform/shape/tests.rs b/crates/ui-egui/src/panels/transform/shape/tests.rs index b2f5a4c147..67f4578ba8 100644 --- a/crates/ui-egui/src/panels/transform/shape/tests.rs +++ b/crates/ui-egui/src/panels/transform/shape/tests.rs @@ -145,21 +145,23 @@ fn an_unfinished_angle_does_not_reach_a_shape_that_takes_a_canceled_groups_shape assert_eq!(p.value_role("Angle", Role::SPIN_BUTTON).as_deref(), Some("7°"), "the ellipse takes 7°, not the unfinished 3+ and 7"); } -/// A canceled undo group removes the shape a Corner Radius value was typed for, and a new shape made by `cmd` with -/// `args` takes its node ID. The value is dropped when that shape is selected in the panel `draw` draws. -fn check_corner_radius_after_a_canceled_group(draw: fn(&mut VectorcraftApp, &mut Ui), cmd: &str, args: Value, panel: &str) { +/// A canceled interaction removes the shape its preview made and a Corner Radius value was typed for, and a new shape +/// made by `cmd` with `args` takes its node ID. The value is dropped when that shape is selected in the panel `draw` +/// draws. +fn check_corner_radius_after_a_canceled_preview(draw: fn(&mut VectorcraftApp, &mut Ui), cmd: &str, args: Value, panel: &str) { let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); app.run("file.new", json!({"width": 400, "height": 400})).unwrap(); - app.run("edit.undoGroup.begin", json!({})).unwrap(); - let first = app.run(cmd, args.clone()).unwrap()["id"].clone(); - app.run("select.set", json!({"ids": [first]})).unwrap(); + app.session.begin_interaction("Shape").unwrap(); + let first = app.session.preview(cmd, &args).unwrap()["id"].clone(); + let selected = app.session.active().map(|st| st.selection.objects.clone()).unwrap_or_default(); + assert_eq!(json!(selected.iter().map(|id| id.0).collect::>()), json!([first]), "{panel} {cmd}: the preview selects its shape"); let mut p = Panel::new(app, draw); p.click_role("Corner Radius", Role::SPIN_BUTTON); p.key(Key::A, Modifiers::COMMAND); p.type_text("3+"); - p.app().run("edit.undoGroup.end", json!({"cancel": true})).unwrap(); + p.app().session.cancel_interaction().unwrap(); let second = p.app().run(cmd, args).unwrap()["id"].clone(); - assert_eq!(second, first, "{panel} {cmd}: the new shape takes the canceled group's shape's node ID"); + assert_eq!(second, first, "{panel} {cmd}: the new shape takes the canceled preview's shape's node ID"); p.app().run("select.set", json!({"ids": [second]})).unwrap(); p.frame(); let shown = p.value_role("Corner Radius", Role::SPIN_BUTTON); @@ -172,27 +174,27 @@ fn check_corner_radius_after_a_canceled_group(draw: fn(&mut VectorcraftApp, &mut } #[test] -fn an_unfinished_rectangle_corner_radius_does_not_reach_a_shape_that_takes_a_canceled_groups_shapes_id() { +fn an_unfinished_rectangle_corner_radius_does_not_reach_a_shape_that_takes_a_canceled_previews_shapes_id() { let rect = json!({"x": 0, "y": 0, "width": 30, "height": 30}); - check_corner_radius_after_a_canceled_group(crate::panels::transform::show, "shape.rectangle", rect, "Transform"); + check_corner_radius_after_a_canceled_preview(crate::panels::transform::show, "shape.rectangle", rect, "Transform"); } #[test] -fn an_unfinished_polygon_corner_radius_does_not_reach_a_shape_that_takes_a_canceled_groups_shapes_id() { +fn an_unfinished_polygon_corner_radius_does_not_reach_a_shape_that_takes_a_canceled_previews_shapes_id() { let polygon = json!({"cx": 60, "cy": 60, "radius": 40, "sides": 5}); - check_corner_radius_after_a_canceled_group(crate::panels::transform::show, "shape.polygon", polygon, "Transform"); + check_corner_radius_after_a_canceled_preview(crate::panels::transform::show, "shape.polygon", polygon, "Transform"); } #[test] -fn an_unfinished_rectangle_corner_radius_in_the_properties_panel_does_not_reach_a_shape_that_takes_a_canceled_groups_shapes_id() { +fn an_unfinished_rectangle_corner_radius_in_the_properties_panel_does_not_reach_a_shape_that_takes_a_canceled_previews_shapes_id() { let rect = json!({"x": 0, "y": 0, "width": 30, "height": 30}); - check_corner_radius_after_a_canceled_group(crate::panels::properties::show, "shape.rectangle", rect, "Properties"); + check_corner_radius_after_a_canceled_preview(crate::panels::properties::show, "shape.rectangle", rect, "Properties"); } #[test] -fn an_unfinished_polygon_corner_radius_in_the_properties_panel_does_not_reach_a_shape_that_takes_a_canceled_groups_shapes_id() { +fn an_unfinished_polygon_corner_radius_in_the_properties_panel_does_not_reach_a_shape_that_takes_a_canceled_previews_shapes_id() { let polygon = json!({"cx": 60, "cy": 60, "radius": 40, "sides": 5}); - check_corner_radius_after_a_canceled_group(crate::panels::properties::show, "shape.polygon", polygon, "Properties"); + check_corner_radius_after_a_canceled_preview(crate::panels::properties::show, "shape.polygon", polygon, "Properties"); } /// Exit Symbol Editing Mode with nothing changed takes back the entering step, which removes the editing copy a value