Merge pull request #10010 from dotCooCoo/fix/addon-dialogs-revert

Add-on dialogs: a draft from before File › Revert leaves the restored drawing alone
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-28 17:29:07 -08:00
commit 01f24fa4e4
2 files changed
+52 -5

No files matched your search

+18 -5
View File
@@ -42,13 +42,24 @@ pub(crate) struct AddonDialogs {
struct Open {
id: DialogId,
spec: &'static Spec,
document: Option<u64>,
/// 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);
}
@@ -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();