diff --git a/crates/ui-egui/src/dialogs/confirm.rs b/crates/ui-egui/src/dialogs/confirm.rs index c6e5a6780f..bdafe12b9d 100644 --- a/crates/ui-egui/src/dialogs/confirm.rs +++ b/crates/ui-egui/src/dialogs/confirm.rs @@ -90,12 +90,18 @@ struct Asked { command: String, params: Value, /// The document that must still be active for a document-specific confirmation. - document: Option, + 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. automated: bool, } + +struct DocumentOwner { + uid: u64, + reverts: Option, +} + wrapped_dialog!(Question, StandardPrompt, KIND); /// A message with a Close button. @@ -136,12 +142,19 @@ pub fn ask(app: &mut VectorcraftApp, message: &str, detail: &str, command: &str, /// 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) { + if let Some(document) = app.session.active().map(|st| DocumentOwner { uid: st.uid, reverts: None }) { 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) { +/// Ask about items in the active document. Reverting its contents invalidates the approval. +pub(crate) fn ask_in_document_contents(app: &mut VectorcraftApp, message: &str, detail: &str, command: &str, params: Value) { + if let Some(document) = app.session.active().map(|st| DocumentOwner { uid: st.uid, reverts: Some(st.reverts) }) { + 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; @@ -278,8 +291,14 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &Context) { && answer.choice == PromptChoice::Proceed && let Some(Asked { command, params, document, scope, automated }) = answer.take_tag::() { - 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.")); + let refusal = document.and_then(|owner| match app.session.active() { + Some(st) if st.uid == owner.uid => { + owner.reverts.filter(|reverts| *reverts != st.reverts).map(|_| tl!("The document was reverted. Reopen this dialog to continue.")) + } + _ => Some(tl!("This dialog was opened in a different document.")), + }); + if let Some(reason) = refusal { + app.status(reason); } else if let Err(e) = scope.run(|| crate::file_asks::as_automated(app, automated, |app| app.run(&command, params))) { app.status(e); } diff --git a/crates/ui-egui/src/panels/graphic_styles.rs b/crates/ui-egui/src/panels/graphic_styles.rs index 241d545789..62a5bc68d4 100644 --- a/crates/ui-egui/src/panels/graphic_styles.rs +++ b/crates/ui-egui/src/panels/graphic_styles.rs @@ -780,7 +780,13 @@ fn delete(app: &mut VectorcraftApp, names: &[String], now: bool) { [n] => crate::i18n::fmt(tl!("Delete the graphic style “{name}”?"), &[("name", n)]), _ => crate::i18n::tn(names.len() as u64, "Delete this {n} graphic style?", "Delete these {n} graphic styles?"), }; - crate::dialogs::confirm::ask(app, &message, tl!("Objects using them keep their look but are no longer linked."), "graphicStyle.delete", params); + crate::dialogs::confirm::ask_in_document_contents( + app, + &message, + tl!("Objects using them keep their look but are no longer linked."), + "graphicStyle.delete", + params, + ); } // ---------- the panel menu ---------- @@ -1167,6 +1173,7 @@ mod tests { answer_dialog(&mut app, dialog_id(confirm::KIND), &[], true); assert_eq!(names(&app), before, "the other tab keeps its identically named style"); assert_eq!(app.session.active().unwrap().history.undo.len(), undo); + assert_eq!(app.ui.status, "This dialog was opened in a different document."); assert!(app.session.set_active(0)); assert!(doc(&app).graphic_style_index("Mine").is_some(), "the source style still needs a fresh confirmation"); } @@ -1198,6 +1205,7 @@ mod tests { answer_dialog(&mut app, dialog_id(confirm::KIND), &[], true); assert_eq!(doc(&app).graphic_style_index("Mine"), Some(index), "the restored style stays"); assert_eq!(app.session.active().unwrap().history.undo.len(), undo); + assert_eq!(app.ui.status, "The document was reverted. Reopen this dialog to continue."); } #[test] diff --git a/crates/ui-egui/src/panels/swatches/actions.rs b/crates/ui-egui/src/panels/swatches/actions.rs index b865cf2130..13d912b40c 100644 --- a/crates/ui-egui/src/panels/swatches/actions.rs +++ b/crates/ui-egui/src/panels/swatches/actions.rs @@ -62,7 +62,13 @@ pub(super) fn delete(app: &mut VectorcraftApp, names: Vec, now: bool) { [n] => crate::i18n::fmt(tl!("Delete “{name}”?"), &[("name", n)]), _ => crate::i18n::tn(names.len() as u64, "Delete this {n} swatch or group?", "Delete these {n} swatches and groups?"), }; - crate::dialogs::confirm::ask(app, &message, tl!("Art using a deleted global swatch keeps its colour."), "swatch.delete", params); + crate::dialogs::confirm::ask_in_document_contents( + app, + &message, + tl!("Art using a deleted global swatch keeps its colour."), + "swatch.delete", + params, + ); } /// Duplicates the swatches `names` (`swatch.duplicate`, each copy just after its swatch and in its diff --git a/crates/ui-egui/src/panels/swatches/tests.rs b/crates/ui-egui/src/panels/swatches/tests.rs index e86c8a03d9..28ae03a07b 100644 --- a/crates/ui-egui/src/panels/swatches/tests.rs +++ b/crates/ui-egui/src/panels/swatches/tests.rs @@ -392,6 +392,7 @@ fn delete_confirmation_keeps_matching_swatches_in_another_drawing() { confirm(&mut app, key); assert!(doc(&app).swatch("Red").is_some(), "the other tab keeps its identically named swatch"); assert_eq!(app.session.active().unwrap().history.undo.len(), undo); + assert_eq!(app.ui.status, "This dialog was opened in a different document."); assert!(app.session.set_active(0)); assert!(doc(&app).swatch("Red").is_some(), "the source swatch still needs a fresh confirmation"); } @@ -421,6 +422,7 @@ fn delete_confirmation_keeps_the_swatch_restored_by_revert() { confirm(&mut app, key); assert_eq!(doc(&app).swatch("Red").expect("the restored swatch stays").paint.color(), saved_color); assert_eq!(app.session.active().unwrap().history.undo.len(), undo); + assert_eq!(app.ui.status, "The document was reverted. Reopen this dialog to continue."); } /// Only None and Registration are reserved: a user swatch or color group whose name starts with