diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index 3922a8e1f0..032aa87927 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -815,6 +815,7 @@ fn undo(s: &mut Session, _: &Value) -> Result { turned.recost_paints(); st.history.redo.push(turned); st.history.trim(); + st.history_restores += 1; st.revision += 1; st.selection.prune(&st.doc); // Guides locked and slices locked or hidden since the step was made stay deselected. @@ -852,6 +853,7 @@ fn redo(s: &mut Session, _: &Value) -> Result { turned.recost_paints(); st.history.undo.push(turned); st.history.trim(); + st.history_restores += 1; st.revision += 1; st.selection.prune(&st.doc); crate::deselect_hidden(&menu, &st.doc, &mut st.selection); diff --git a/crates/engine/src/doc_state.rs b/crates/engine/src/doc_state.rs index 9c80d284ca..9a17cb9c56 100644 --- a/crates/engine/src/doc_state.rs +++ b/crates/engine/src/doc_state.rs @@ -74,6 +74,9 @@ pub struct DocState { /// pasted bitmap) is dropped when this has changed by the time it finishes; edits do not /// change it, and a batch that fails puts it back with the document. pub reverts: u64, + /// Advances when Undo or Redo restores an older document snapshot. The same node ID can name different objects + /// across that boundary. + pub history_restores: u64, /// Preferences › User Interface › Open Documents As Tabs when the document opened: cleared, /// the UI opens it in a floating window of its own. pub opened_as_tab: bool, @@ -419,6 +422,7 @@ impl DocState { recalled_selection: RecalledSelection::default(), uid: NEXT_DOC_UID.fetch_add(1, std::sync::atomic::Ordering::Relaxed), reverts: 0, + history_restores: 0, opened_as_tab: true, mask_view: None, spelling_ignored: Default::default(), diff --git a/crates/ui-egui/src/dialogs/object_mosaic.rs b/crates/ui-egui/src/dialogs/object_mosaic.rs index a32a3ed416..49f355273a 100644 --- a/crates/ui-egui/src/dialogs/object_mosaic.rs +++ b/crates/ui-egui/src/dialogs/object_mosaic.rs @@ -121,6 +121,7 @@ fn label(text: &str) -> Label { struct MosaicSource { document: u64, reverts: u64, + history_restores: u64, image: NodeId, } @@ -134,6 +135,9 @@ impl MosaicSource { if st.reverts != self.reverts { return Err(tl!("The document was reverted. Reopen this dialog to continue.").to_owned()); } + if st.history_restores != self.history_restores { + return Err(tl!("Reopen Create Object Mosaic after Undo or Redo.").to_owned()); + } match selected_image(app) { Some((source, size)) if source == self => Ok(size), None => Err(NO_IMAGE.to_owned()), @@ -147,7 +151,7 @@ fn selected_image(app: &VectorcraftApp) -> Option<(MosaicSource, (f64, f64))> { let st = app.session.active()?; let node = st.selection.objects.iter().filter_map(|id| st.doc.node(*id)).find(|n| matches!(n.kind, NodeKind::Image(_)))?; let b = node.geometric_bounds()?; - Some((MosaicSource { document: st.uid, reverts: st.reverts, image: node.id }, (b.width(), b.height()))) + Some((MosaicSource { document: st.uid, reverts: st.reverts, history_restores: st.history_restores, image: node.id }, (b.width(), b.height()))) } /// Open the dialog on the selected image, with the values `given` holds (the menu item's params, or @@ -941,4 +945,27 @@ mod tests { assert_eq!(mosaic(&app), Some((100.0, 50.0, 4))); assert!(!is_open(&mut app)); } + + #[test] + fn held_mosaic_does_not_delete_a_raster_created_after_undo() { + let mut app = app_with_image(); + let source = image_id(&app); + open(&mut app, &json!({"columns": 2, "rows": 2, "deleteRaster": true})).unwrap(); + + app.run("edit.undo", json!({})).unwrap(); + assert!(app.session.active().unwrap().doc.node(NodeId(source)).is_none_or(|node| !matches!(node.kind, NodeKind::Image(_)))); + app.run("object.rasterize", json!({"ppi": 72})).unwrap(); + assert_eq!(image_id(&app), source, "the new raster repeats the same ID and pixels"); + let undo = app.session.active().unwrap().history.undo.len(); + ok(&mut app); + assert_eq!(mosaic(&app), None, "the old approval cannot delete the new raster"); + assert_eq!(image_id(&app), source); + assert_eq!(app.session.active().unwrap().history.undo.len(), undo); + assert!(is_open(&mut app)); + + open(&mut app, &json!({"columns": 2, "rows": 2, "deleteRaster": true})).unwrap(); + ok(&mut app); + assert_eq!(mosaic(&app), Some((100.0, 50.0, 4))); + assert!(!is_open(&mut app)); + } }