mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
Merge pull request #9606 from dotCooCoo/fix/ui-preset-delete-confirmations
Keep Swatches and Graphic Styles Delete approvals on their drawing
This commit is contained in:
4 files changed
+138
-7
No files matched your search
@@ -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);
|
||||
}
|
||||
|
||||
@@ -781,7 +781,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 ----------
|
||||
@@ -1172,6 +1178,58 @@ mod tests {
|
||||
assert!(names(&app).is_empty() && confirm::open_question(&app).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delete_confirmation_keeps_matching_styles_in_another_drawing() {
|
||||
let mut app = app();
|
||||
app.run("graphicStyle.new", json!({"name": "Mine"})).unwrap();
|
||||
let source = app.session.active().unwrap().uid;
|
||||
on_event(&mut app, PresetEvent::Delete("Mine".into()), false);
|
||||
assert!(asks_delete(&app));
|
||||
app.run("file.new", json!({"width": 100, "height": 100})).unwrap();
|
||||
assert_ne!(app.session.active().unwrap().uid, source);
|
||||
app.run("shape.rectangle", json!({"x": 10, "y": 10, "width": 30, "height": 25})).unwrap();
|
||||
app.run("graphicStyle.new", json!({"name": "Mine"})).unwrap();
|
||||
let before = names(&app);
|
||||
let undo = app.session.active().unwrap().history.undo.len();
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delete_confirmation_keeps_the_style_restored_by_revert() {
|
||||
let mut app = app();
|
||||
app.run("graphicStyle.new", json!({"name": "Mine"})).unwrap();
|
||||
let file = std::env::temp_dir().join(format!("vectorcraft-style-delete-revert-{}.vectorcraft", std::process::id()));
|
||||
app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|err| err.to_string())));
|
||||
app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|err| err.to_string())));
|
||||
app.run("file.saveAs", json!({"path": file.to_string_lossy()})).unwrap();
|
||||
let saved = doc(&app);
|
||||
let index = saved.graphic_style_index("Mine").unwrap();
|
||||
let st = app.session.active().unwrap();
|
||||
let (uid, reverts) = (st.uid, st.reverts);
|
||||
|
||||
app.run("paint.setFill", json!({"color": "#b32f5f"})).unwrap();
|
||||
app.run("graphicStyle.redefine", json!({"name": "Mine"})).unwrap();
|
||||
on_event(&mut app, PresetEvent::Delete("Mine".into()), false);
|
||||
assert!(asks_delete(&app));
|
||||
app.run("file.revert", json!({"confirmed": true})).unwrap();
|
||||
let _ = std::fs::remove_file(file);
|
||||
let st = app.session.active().unwrap();
|
||||
assert_eq!((st.uid, st.reverts), (uid, reverts + 1));
|
||||
assert_eq!(doc(&app).graphic_style_index("Mine"), Some(index));
|
||||
let undo = st.history.undo.len();
|
||||
|
||||
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]
|
||||
fn thumbnails_are_versioned_by_look_and_change_when_a_style_changes() {
|
||||
let mut app = app();
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -377,6 +377,54 @@ fn delete_asks_then_removes_the_selection_and_unlinks_art() {
|
||||
assert_eq!(deletable(&app, &["[None]".into(), REGISTRATION.into(), "Grays".into()]), ["Grays"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delete_confirmation_keeps_matching_swatches_in_another_drawing() {
|
||||
let mut app = app();
|
||||
let key = crate::dialogs::confirm::KIND;
|
||||
let source = app.session.active().unwrap().uid;
|
||||
delete(&mut app, vec!["Red".into()], false);
|
||||
assert!(dialog_open(&app, key));
|
||||
app.run("file.new", json!({"width": 200, "height": 200})).unwrap();
|
||||
assert_ne!(app.session.active().unwrap().uid, source);
|
||||
let undo = app.session.active().unwrap().history.undo.len();
|
||||
assert!(doc(&app).swatch("Red").is_some());
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn delete_confirmation_keeps_the_swatch_restored_by_revert() {
|
||||
let mut app = app();
|
||||
let key = crate::dialogs::confirm::KIND;
|
||||
let file = std::env::temp_dir().join(format!("vectorcraft-swatch-delete-revert-{}.vectorcraft", std::process::id()));
|
||||
app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|err| err.to_string())));
|
||||
app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|err| err.to_string())));
|
||||
app.run("file.saveAs", json!({"path": file.to_string_lossy()})).unwrap();
|
||||
let saved_color = doc(&app).swatch("Red").unwrap().paint.color();
|
||||
let st = app.session.active().unwrap();
|
||||
let (uid, reverts) = (st.uid, st.reverts);
|
||||
|
||||
app.run("swatch.edit", json!({"name": "Red", "color": "#00ff00"})).unwrap();
|
||||
assert_ne!(doc(&app).swatch("Red").unwrap().paint.color(), saved_color);
|
||||
delete(&mut app, vec!["Red".into()], false);
|
||||
app.run("file.revert", json!({"confirmed": true})).unwrap();
|
||||
let _ = std::fs::remove_file(file);
|
||||
let st = app.session.active().unwrap();
|
||||
assert_eq!((st.uid, st.reverts), (uid, reverts + 1));
|
||||
assert_eq!(doc(&app).swatch("Red").unwrap().paint.color(), saved_color);
|
||||
let undo = st.history.undo.len();
|
||||
|
||||
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
|
||||
/// `[` can be deleted, the swatch can be edited, and names the document does not hold are left out.
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user