From c49e10268e75ecf1852e9153889da78847d02c64 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 16:06:58 -0800 Subject: [PATCH] =?UTF-8?q?Add-on=20dialogs:=20a=20draft=20from=20before?= =?UTF-8?q?=20File=20=E2=80=BA=20Revert=20leaves=20the=20restored=20drawin?= =?UTF-8?q?g=20alone?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The shared add-on dialogs checked only the document's uid, which File › Revert keeps while it replaces the drawing. A dialog left open across a revert previewed on and applied to the restored art. They now record the revert count with the uid, stop previewing after a revert, and refuse OK with "The document was reverted. Reopen this dialog to continue.", as Vector Halftone does since #9943. --- crates/ui-egui/src/addons/dialogs.rs | 23 +++++++++++---- crates/ui-egui/src/addons/dialogs/tests.rs | 34 ++++++++++++++++++++++ 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/addons/dialogs.rs b/crates/ui-egui/src/addons/dialogs.rs index 03f69718b9..e58443e259 100644 --- a/crates/ui-egui/src/addons/dialogs.rs +++ b/crates/ui-egui/src/addons/dialogs.rs @@ -42,13 +42,24 @@ pub(crate) struct AddonDialogs { struct Open { id: DialogId, spec: &'static Spec, - document: Option, + /// The opening document's uid and revert count: File › Revert keeps the uid but replaces the drawing. + document: Option<(u64, u64)>, } impl Open { fn on_document(self, app: &VectorcraftApp) -> bool { - self.document == app.session.active().map(|state| state.uid) + self.document == active_document(app) } + + /// Whether the active document is the opening one, reverted since the dialog opened. + fn reverted(self, app: &VectorcraftApp) -> bool { + matches!((self.document, active_document(app)), (Some((uid, reverts)), Some((now, again))) if uid == now && reverts != again) + } +} + +/// The active document's uid and revert count. +fn active_document(app: &VectorcraftApp) -> Option<(u64, u64)> { + app.session.active().map(|state| (state.uid, state.reverts)) } /// Closes the open add-on dialog without an answer and rolls its preview back. @@ -81,7 +92,7 @@ pub(crate) fn open( close_open(app); let id = dialog.id(); host.with(|h| h.open(dialog)); - app.addon_dialogs.open = Some(Open { id, spec, document: app.session.active().map(|state| state.uid) }); + app.addon_dialogs.open = Some(Open { id, spec, document: active_document(app) }); Ok(json!({ "dialog": spec.command })) } @@ -141,8 +152,10 @@ fn pump_commit(app: &mut VectorcraftApp) { app.addon_dialogs.open = None; let preview = mem::take(&mut app.addon_dialogs.preview); // The dialog is closed already, so a refusal can only go to the status line. - if !on_document { - app.status(tl!("The add-on settings apply only to the drawing where the dialog opened.").to_owned()); + if open.reverted(app) { + app.status("The document was reverted. Reopen this dialog to continue."); + } else if !on_document { + app.status("The add-on settings apply only to the drawing where the dialog opened."); } else if let Err(error) = preview.commit(app, spec.command, (spec.params)(&result.fields)) { app.status(error); } diff --git a/crates/ui-egui/src/addons/dialogs/tests.rs b/crates/ui-egui/src/addons/dialogs/tests.rs index b7f648aa1e..679a2e2e34 100644 --- a/crates/ui-egui/src/addons/dialogs/tests.rs +++ b/crates/ui-egui/src/addons/dialogs/tests.rs @@ -340,6 +340,40 @@ fn an_add_on_confirmation_does_not_edit_another_drawing_before_the_pump() { assert_eq!((x0(&app, 1), undo_len(&app)), (100.0, source_undo)); } +/// Saves the two rectangles of [`two_rects`] to a file in the returned directory, so File › Revert has a copy to restore. +fn two_saved_rects(tag: &str) -> (VectorcraftApp, vectorcraft_testkit::TempDir) { + let mut app = two_rects(); + let dir = vectorcraft_testkit::TempDir::new(tag); + app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string()))); + app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string()))); + app.run("file.saveAs", json!({"path": dir.join("Art.vectorcraft")})).unwrap(); + (app, dir) +} + +#[test] +fn an_add_on_draft_from_before_file_revert_leaves_the_restored_drawing_alone() { + let (mut app, _dir) = two_saved_rects("addon-revert"); + open_item(&mut app, "Arrange Tools", "Arrange in Grid…"); + let id = dialog_id(&app); + set(&app, id, "layout", json!("row")); + set(&app, id, "preview", json!(true)); + frame(&mut app); + assert_eq!(x0(&app, 1), 30.0, "previewed before the revert"); + + app.run("file.revert", json!({"confirmed": true})).unwrap(); + app.run("select.all", json!({})).unwrap(); + let restored = app.session.doc().unwrap().doc.clone(); + let undo = undo_len(&app); + frame(&mut app); + assert_eq!(app.session.doc().unwrap().doc, restored, "the draft does not preview on the restored drawing"); + assert!(!app.session.in_interaction()); + assert!(confirm(&app, id)); + frame(&mut app); + assert_eq!(app.session.doc().unwrap().doc, restored, "OK leaves the restored drawing unchanged"); + assert_eq!(undo_len(&app), undo, "OK records no undo step"); + assert_eq!(app.ui.status, "The document was reverted. Reopen this dialog to continue."); +} + #[test] fn an_add_on_cancel_leaves_the_other_drawings_interaction_running() { let mut app = two_drawings();