diff --git a/crates/ui-egui/src/dialogs/confirm.rs b/crates/ui-egui/src/dialogs/confirm.rs index 72fb9a40a6..c6e5a6780f 100644 --- a/crates/ui-egui/src/dialogs/confirm.rs +++ b/crates/ui-egui/src/dialogs/confirm.rs @@ -89,6 +89,8 @@ pub(crate) struct Question(StandardPrompt); struct Asked { command: String, params: Value, + /// The document that must still be active for a document-specific confirmation. + document: Option, /// The automation roots in force when asked: the command runs under them. scope: ScopeHandle, /// Automation asked ([`VectorcraftApp::automation_asked`]): the command runs as automation. @@ -129,6 +131,17 @@ pub(crate) struct ReplaceFiles { /// Ask `message` (with an optional `detail` line), both in the UI language; OK runs `command` /// with `params`. pub fn ask(app: &mut VectorcraftApp, message: &str, detail: &str, command: &str, params: Value) { + ask_for(app, None, message, detail, command, params); +} + +/// Ask about the active document. OK is valid only while that document is active. +pub(crate) fn ask_in_document(app: &mut VectorcraftApp, message: &str, detail: &str, command: &str, params: Value) { + if let Some(document) = app.session.active().map(|st| st.uid) { + ask_for(app, Some(document), message, detail, command, params); + } +} + +fn ask_for(app: &mut VectorcraftApp, document: Option, message: &str, detail: &str, command: &str, params: Value) { let Some(dialogs) = app.dialogs() else { app.status(format!("{message} (no window to ask in)")); return; @@ -137,7 +150,7 @@ pub fn ask(app: &mut VectorcraftApp, message: &str, detail: &str, command: &str, if !detail.is_empty() { prompt = prompt.paragraph(detail.to_string()); } - let asked = Asked { command: command.to_string(), params, scope: ScopeHandle::capture(), automated: app.automation_asked() }; + let asked = Asked { command: command.to_string(), params, document, scope: ScopeHandle::capture(), automated: app.automation_asked() }; let prompt = prompt.button(PromptChoice::Proceed, tl!("OK")).button(PromptChoice::Cancel, tl!("Cancel")).tag(asked); dialogs.with(|host| host.open(Question(prompt))); } @@ -263,10 +276,13 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &Context) { }; if let Some(DialogResult::Ok(mut answer)) = dialogs.with(|host| host.take::()) && answer.choice == PromptChoice::Proceed - && let Some(Asked { command, params, scope, automated }) = answer.take_tag::() - && let Err(e) = scope.run(|| crate::file_asks::as_automated(app, automated, |app| app.run(&command, params))) + && let Some(Asked { command, params, document, scope, automated }) = answer.take_tag::() { - app.status(e); + if document.is_some_and(|document| app.session.active().is_none_or(|st| st.uid != document)) { + app.status(tl!("This dialog was opened in a different document.")); + } else if let Err(e) = scope.run(|| crate::file_asks::as_automated(app, automated, |app| app.run(&command, params))) { + app.status(e); + } } // The host opens queued messages in `enqueue` order, so the one closed is the oldest. while dialogs.with(|host| host.take::()).is_some() { diff --git a/crates/ui-egui/src/dialogs/package.rs b/crates/ui-egui/src/dialogs/package.rs index 61397b4076..7fe45b0635 100644 --- a/crates/ui-egui/src/dialogs/package.rs +++ b/crates/ui-egui/src/dialogs/package.rs @@ -20,7 +20,7 @@ use craft::ui::runtime::requests::PathKind; use serde_json::{Value, json}; use vectorcraft_engine::EngineError; -use super::confirm::ask; +use super::confirm::{ask, ask_in_document}; use crate::background::{self, Writer}; use crate::{VectorcraftApp, file_asks, io}; @@ -46,7 +46,7 @@ fn downloads(app: &VectorcraftApp) -> bool { pub fn open(app: &mut VectorcraftApp) -> Result { let st = app.session.active().ok_or("no document")?; let Some(path) = st.path.clone() else { - ask( + ask_in_document( app, tl!("Save the document first?"), tl!("Package collects a saved document with its linked files and fonts."), diff --git a/crates/ui-egui/src/dialogs/tests_package.rs b/crates/ui-egui/src/dialogs/tests_package.rs index e0d0e87e72..99c968218b 100644 --- a/crates/ui-egui/src/dialogs/tests_package.rs +++ b/crates/ui-egui/src/dialogs/tests_package.rs @@ -609,3 +609,28 @@ fn package_without_a_location_says_so() { assert!(matches!(held_state(&app), Some(HeldOk::Refused(_))), "OK is refused with its reason"); assert!(with_package(&app, |_| ()).is_some(), "stays open"); } + +#[test] +fn package_save_first_does_not_save_a_different_drawing() { + let mut app = windowed(); + let picker = crate::tests::picks::scripted(&mut app); + app.run("file.new", json!({"width": 200, "height": 200})).unwrap(); + app.run("file.new", json!({"width": 300, "height": 300})).unwrap(); + app.run("document.activate", json!({"index": 0})).unwrap(); + app.run("ui.packageDialog", json!({})).unwrap(); + assert_eq!(confirm::open_question(&app).unwrap().2, "file.saveAs"); + + app.run("document.activate", json!({"index": 1})).unwrap(); + answer_question(&mut app); + let ctx = app.ui_ctx.clone().unwrap(); + crate::tests::picks::poll(&mut app, &ctx); + + assert!(picker.requests().is_empty(), "Package in drawing A must never start Save As for drawing B"); + assert_eq!(app.ui.status, "This dialog was opened in a different document."); + + app.run("document.activate", json!({"index": 0})).unwrap(); + app.run("ui.packageDialog", json!({})).unwrap(); + answer_question(&mut app); + crate::tests::picks::poll(&mut app, &ctx); + assert_eq!(picker.requests().len(), 1, "approval in the opening drawing starts Save As"); +} diff --git a/crates/ui-egui/src/io/open.rs b/crates/ui-egui/src/io/open.rs index 75d9bd9b65..98464ba721 100644 --- a/crates/ui-egui/src/io/open.rs +++ b/crates/ui-egui/src/io/open.rs @@ -689,7 +689,13 @@ pub fn ask_revert(app: &mut VectorcraftApp) -> Result { (c.enabled)(&app.session)?; let name = app.session.active().map(|d| d.title()).unwrap_or_default(); let message = crate::i18n::fmt(tl!("Revert to the saved version of “{name}”?"), &[("name", &name)]); - dialogs::confirm::ask(app, &message, tl!("Changes made since it was last saved will be lost."), "file.revert", json!({ "confirmed": true })); + dialogs::confirm::ask_in_document( + app, + &message, + tl!("Changes made since it was last saved will be lost."), + "file.revert", + json!({ "confirmed": true }), + ); Ok(json!({ "pending": dialogs::confirm::KIND })) } diff --git a/crates/ui-egui/src/tests/save.rs b/crates/ui-egui/src/tests/save.rs index c5a2815463..e92f4bbdf9 100644 --- a/crates/ui-egui/src/tests/save.rs +++ b/crates/ui-egui/src/tests/save.rs @@ -494,3 +494,24 @@ fn file_save_writes_opened_eps_back_without_another_save_as_dialog() { assert_eq!(app.session.active().unwrap().path.as_deref(), Some(path.as_str())); std::fs::remove_dir_all(d).unwrap(); } + +#[test] +fn revert_confirmation_never_discards_another_drawings_changes() { + let d = dir("revert-drawing-switch"); + let mut app = app(); + for name in ["first.vectorcraft", "second.vectorcraft"] { + app.run("file.new", json!({"width": 100, "height": 100})).unwrap(); + let path = d.join(name).to_string_lossy().to_string(); + app.run("file.saveAs", json!({"path": path})).unwrap(); + rect(&mut app); + } + app.run("document.activate", json!({"index": 0})).unwrap(); + app.run("file.revert", json!({})).unwrap(); + assert!(confirm::open_question(&app).unwrap().0.contains("first")); + app.run("document.activate", json!({"index": 1})).unwrap(); + let count = objects(&app); + answer_top(&mut app, true); + let result = (objects(&app), app.session.active().unwrap().is_dirty()); + let _ = std::fs::remove_dir_all(d); + assert_eq!(result, (count, true), "the second drawing must retain its unsaved edits"); +}