diff --git a/crates/ui-egui/src/panels/blend.rs b/crates/ui-egui/src/panels/blend.rs index b9f3526efe..6a69792e79 100644 --- a/crates/ui-egui/src/panels/blend.rs +++ b/crates/ui-egui/src/panels/blend.rs @@ -109,36 +109,38 @@ fn percent_slider(app: &mut VectorcraftApp, ui: &mut Ui, target: &EditTarget, sl }); } -/// The Steps, Distance, Ramp and Shift draft key: the document uid, its File › Revert count, and -/// each selected object id paired with its nearest blend id, or None when it resolves to no blend. -/// Symbol-part selections resolve to no blend, as the Blend commands require. A field drops an -/// unfinished value when this key changes, even when the new target shows the same value. -/// [`settings`] reuses this key until the document or selection changes and shares it with its fields. -pub(super) type EditTarget = Option<(u64, u64, Arc)>>)>; +/// The Steps, Distance, Ramp and Shift draft key: the document uid, how many times File › Revert, +/// Undo or Redo and other rollbacks replaced its content (after which a node id can name another +/// object), as [`crate::panels::SelectionTarget`] counts them, and each selected object id paired +/// with its nearest blend id, or None when it resolves to no blend. Symbol-part selections resolve +/// to no blend, as the Blend commands require. A field drops an unfinished value when this key +/// changes, even when the new target shows the same value. [`settings`] reuses this key until the +/// document or selection changes and shares it with its fields. +pub(super) type EditTarget = Option<((u64, u64, u64, u64), Arc)>>)>; /// The resolved objects also hold the selection's ids, so checking the cache copies no selection. struct TargetCache { - uid: u64, + document: (u64, u64, u64, u64), revision: u64, - reverts: u64, symbol_part: Option, objects: Arc)>>, } -/// The active document's uid and revert count, with each selected id and its resolved blend id. -/// Resolution uses the same nearest-node finder and symbol-part guard as the Blend commands. -/// Unchanged document revisions and selections reuse the resolved objects between passes. +/// The active document's uid with its revert, history restore and rollback counts, and each +/// selected id with its resolved blend id. Resolution uses the same nearest-node finder and +/// symbol-part guard as the Blend commands. Unchanged document revisions and selections reuse the +/// resolved objects between passes. pub(super) fn edit_target(app: &VectorcraftApp, ctx: &egui::Context) -> EditTarget { let st = app.session.active()?; + let document = (st.uid, st.reverts, st.history_restores, st.rollbacks); let key = egui::Id::unique("blend-edit-target"); if let Some(held) = ctx.data(|d| d.get_temp::>(key)) - && held.uid == st.uid + && held.document == document && held.revision == st.revision - && held.reverts == st.reverts && held.symbol_part == st.selection.symbol_part && held.objects.iter().map(|(id, _)| id).eq(st.selection.objects.iter()) { - return Some((st.uid, st.reverts, held.objects.clone())); + return Some((document, held.objects.clone())); } let mut find = st.doc.nearest_finder(); let objects: Vec<(NodeId, Option)> = st @@ -155,16 +157,10 @@ pub(super) fn edit_target(app: &VectorcraftApp, ctx: &egui::Context) -> EditTarg ctx.data_mut(|d| { d.insert_temp( key, - Arc::new(TargetCache { - uid: st.uid, - revision: st.revision, - reverts: st.reverts, - symbol_part: st.selection.symbol_part.clone(), - objects: objects.clone(), - }), + Arc::new(TargetCache { document, revision: st.revision, symbol_part: st.selection.symbol_part.clone(), objects: objects.clone() }), ); }); - Some((st.uid, st.reverts, objects)) + Some((document, objects)) } /// Spacing and Orientation, as `object.blend.info` reports them; a change runs diff --git a/crates/ui-egui/src/panels/tests_blend.rs b/crates/ui-egui/src/panels/tests_blend.rs index b43eb606ad..69270e83c8 100644 --- a/crates/ui-egui/src/panels/tests_blend.rs +++ b/crates/ui-egui/src/panels/tests_blend.rs @@ -92,7 +92,7 @@ fn the_edit_target_is_reused_until_the_document_or_selection_changes() { app.run("select.set", json!({"ids": [ids[0].0, blend.0, ids[1].0]})).unwrap(); (ids, blend) }; - let resolved = |app: &VectorcraftApp| edit_target(app, &ctx).unwrap().2; + let resolved = |app: &VectorcraftApp| edit_target(app, &ctx).unwrap().1; let (ids, blend) = art(&mut app); let first = resolved(&app); assert_eq!(*first, vec![(ids[0], Some(blend)), (blend, Some(blend)), (ids[1], Some(blend))]); @@ -149,14 +149,17 @@ fn the_edit_target_is_reused_until_the_document_or_selection_changes() { assert!(!Arc::ptr_eq(&whole, &reverted), "the key is resolved for the new document state after reverting"); assert!(Arc::ptr_eq(&reverted, &resolved(&app))); - // Another document uses the same ids, revision, revert count and selection. + // Another document uses the same ids, revision, revert, history restore and rollback counts and selection. let st = app.session.active().unwrap(); - let (revision, reverts, selection) = (st.revision, st.reverts, st.selection.objects.clone()); + let (revision, reverts, restores, rollbacks, selection) = + (st.revision, st.reverts, st.history_restores, st.rollbacks, st.selection.objects.clone()); let (other_ids, other_blend) = art(&mut app); assert_eq!((other_ids, other_blend), (ids, blend)); let st = app.session.active_mut().unwrap(); st.revision = revision; st.reverts = reverts; + st.history_restores = restores; + st.rollbacks = rollbacks; st.selection.objects = selection; let other_document = resolved(&app); assert!(!Arc::ptr_eq(&reverted, &other_document), "the key is resolved for the new document state in another document"); @@ -806,3 +809,108 @@ fn an_unfinished_steps_value_is_dropped_by_file_revert() { p.key(Key::Enter, Modifiers::NONE); assert_eq!(spec(&p.app(), id).spacing, BlendSpacing::Steps(1), "Revert drops the unfinished value"); } + +#[test] +fn an_unfinished_steps_value_does_not_reach_a_blend_that_takes_a_canceled_groups_blend_id() { + use craft::ui::contract::Role; + use egui::{Key, Modifiers}; + let blend = |app: &mut VectorcraftApp| { + let ids: Vec = + [0, 200].map(|x| app.run("shape.rectangle", json!({"x": x, "y": 0, "width": 20, "height": 20})).unwrap()["id"].clone()).into(); + app.run("select.set", json!({"ids": ids})).unwrap(); + let id = app.run("object.blend.make", json!({"spacing": "steps", "steps": 5})).unwrap()["id"].clone(); + app.run("select.set", json!({"ids": [id]})).unwrap(); + NodeId(id.as_u64().unwrap()) + }; + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.run("file.new", json!({"width": 300, "height": 300})).unwrap(); + app.run("edit.undoGroup.begin", json!({})).unwrap(); + let first = blend(&mut app); + let mut p = Panel::new(app, show); + p.click_role("Steps:", Role::SPIN_BUTTON); + p.key(Key::A, Modifiers::COMMAND); + p.type_text("3+"); + + // Canceling the group removes the blend; the same art made again gives a blend with its id and steps. + p.app().run("edit.undoGroup.end", json!({"cancel": true})).unwrap(); + let second = blend(&mut p.app()); + assert_eq!(second, first, "the new blend takes the canceled group's blend's node ID"); + p.frame(); + p.type_text("1"); + p.key(Key::Enter, Modifiers::NONE); + assert_eq!(spec(&p.app(), second).spacing, BlendSpacing::Steps(1), "the new blend takes 1 step, not 3+1"); +} + +#[test] +fn an_unfinished_steps_value_does_not_reach_a_blend_that_takes_a_canceled_previews_blend_id() { + use craft::ui::contract::Role; + use egui::{Key, Modifiers}; + let mut app = app(); + let params = json!({"spacing": "steps", "steps": 5}); + app.session.begin_interaction("Blend").unwrap(); + let first = NodeId(app.session.preview("object.blend.make", ¶ms).unwrap()["id"].as_u64().unwrap()); + let mut p = Panel::new(app, show); + p.click_role("Steps:", Role::SPIN_BUTTON); + p.key(Key::A, Modifiers::COMMAND); + p.type_text("3+"); + + // Canceling the preview restores the selected rectangles and frees the blend's node ID without a history restore. + { + let mut app = p.app(); + let st = app.session.active().unwrap(); + let (rollbacks, restores) = (st.rollbacks, st.history_restores); + app.session.cancel_interaction().unwrap(); + let st = app.session.active().unwrap(); + assert!(st.rollbacks > rollbacks, "canceling the preview advances the rollback count"); + assert_eq!(st.history_restores, restores, "canceling the preview leaves the history restore count unchanged"); + } + let second = NodeId(p.app().run("object.blend.make", params).unwrap()["id"].as_u64().unwrap()); + assert_eq!(second, first, "the new blend takes the canceled preview's blend's node ID"); + p.app().run("select.set", json!({"ids": [second.0]})).unwrap(); + p.frame(); + p.type_text("1"); + p.key(Key::Enter, Modifiers::NONE); + assert_eq!(spec(&p.app(), second).spacing, BlendSpacing::Steps(1), "the new blend takes 1 step, not 3+1"); +} + +#[test] +fn an_unfinished_steps_ramp_or_shift_value_is_dropped_by_undo_and_redo() { + use craft::ui::contract::Role; + use egui::{Key, Modifiers}; + let (mut app, id) = pathed(); + let reset = json!({"spacing": "steps", "steps": 5, "ramp": 30, "colorShift": 30}); + app.run("object.blend.options", reset.clone()).unwrap(); + let mut p = Panel::new(app, show); + for name in ["Steps:", "Ramp:", "Shift:"] { + let value = |s: &BlendSpec| match name { + "Steps:" => match s.spacing { + BlendSpacing::Steps(n) => f64::from(n), + _ => f64::NAN, + }, + "Ramp:" => s.ease.ramp * 100.0, + _ => s.color_shift, + }; + for cmd in ["edit.undo", "edit.redo"] { + // A change elsewhere for Undo or Redo to replace, leaving the blend as it is. + p.app().run("shape.rectangle", json!({"x": 100, "y": 200, "width": 20, "height": 20})).unwrap(); + if cmd == "edit.redo" { + p.app().run("edit.undo", json!({})).unwrap(); + } + p.app().run("select.set", json!({"ids": [id.0]})).unwrap(); + p.frame(); + p.click_role(name, Role::SPIN_BUTTON); + p.key(Key::A, Modifiers::COMMAND); + p.type_text("3+"); + + // The restored document has the same blend and, selected again, the same selection. + p.app().run(cmd, json!({})).unwrap(); + p.app().run("select.set", json!({"ids": [id.0]})).unwrap(); + p.frame(); + p.type_text("1"); + p.key(Key::Enter, Modifiers::NONE); + let at = value(&spec(&p.app(), id)); + assert!((at - 1.0).abs() < 1e-9, "{name}: {cmd} drops the unfinished value, so the blend takes 1, not 3+1: {at}"); + p.app().run("object.blend.options", reset.clone()).unwrap(); + } + } +}