Keep Swatches and Graphic Styles Delete approvals on the opening drawing

This commit is contained in:
dotCooCoo committed 2026-11-26 17:20:44 -08:00
1 parent 5492a5e500
commit 32cd38351d
4 files changed
+42 -7

No files matched your search

+24 -5
View File
@@ -90,12 +90,18 @@ struct Asked {
command: String,
params: Value,
/// The document that must still be active for a document-specific confirmation.
document: Option<u64>,
document: Option<DocumentOwner>,
/// 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<u64>,
}
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<u64>, 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<DocumentOwner>, 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::<Asked>()
{
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);
}
+9 -1
View File
@@ -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]
@@ -62,7 +62,13 @@ pub(super) fn delete(app: &mut VectorcraftApp, names: Vec<String>, 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
@@ -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