Merge pull request #9605 from dotCooCoo/fix/ui-layer-delete-revert

Protect Layers Delete Selection across File Revert
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-26 21:34:42 -08:00
commit cb8c781b60
2 files changed
+47 -8

No files matched your search

+13 -8
View File
@@ -472,13 +472,14 @@ pub(crate) fn footer(tree: &DocLayers, ui: &mut Ui, out: &mut Vec<(String, Value
}
}
/// `ui.layerDelete {ids?|id?, doc?, confirmed?}`: Delete Selection as the Layers panel runs it (the
/// `ui.layerDelete {ids?|id?, doc?, reverts?, confirmed?}`: Delete Selection as the Layers panel runs it (the
/// trash button, rows dropped on it, the panel menu, Delete and Backspace), on the rows
/// `layer.delete` takes (default: the highlighted rows, else the current layer; in isolation mode a
/// current layer outside the isolated art is an error). When a layer or sublayer among them holds art (an object other than a
/// guide, hidden, locked and nested ones included), it asks first: OK deletes those rows, by id, in
/// the same document (`doc`, its uid, with `confirmed`); with another document active then, nothing
/// is deleted. In isolation mode a row outside the isolated art is an error, and nothing is deleted. Other rows, and rows `layer.delete` refuses (an unknown id, every top-level layer),
/// the same document (`doc`, its uid, and `reverts`, its saved-content generation, with `confirmed`).
/// Switching documents or reverting the drawing invalidates the approval. In isolation mode a row
/// outside the isolated art is an error. Other rows, and rows `layer.delete` refuses (an unknown id, every top-level layer),
/// go to `layer.delete` at once. → `layer.delete`'s result, or null while the question is open
pub fn delete(app: &mut VectorcraftApp, p: &Value) -> Result<Value, String> {
super::unrevealed(app, |app| delete_rows(app, p))
@@ -487,15 +488,19 @@ pub fn delete(app: &mut VectorcraftApp, p: &Value) -> Result<Value, String> {
/// [`delete`], which locates nothing.
fn delete_rows(app: &mut VectorcraftApp, p: &Value) -> Result<Value, String> {
let confirmed = crate::menus::opt_bool(p, "confirmed")?.unwrap_or(false);
let key = "doc";
let doc = match p.get(key) {
None | Some(Value::Null) => None,
Some(v) => Some(v.as_u64().ok_or_else(|| format!("`{key}` must be a whole number of 0 or more"))?),
let opening = |key| match p.get(key) {
None | Some(Value::Null) => Ok(None),
Some(v) => v.as_u64().map(Some).ok_or_else(|| format!("`{key}` must be a whole number of 0 or more")),
};
let doc = opening("doc")?;
let reverts = opening("reverts")?;
let st = app.session.active().ok_or("no document open")?;
if doc.is_some_and(|d| d != st.uid) {
return Err("the active document changed while the question was open; nothing was deleted".into());
}
if reverts.is_some_and(|reverts| reverts != st.reverts) {
return Err(tl!("The document was reverted. Reopen this dialog to continue.").into());
}
let rows: serde_json::Map<String, Value> = ["ids", "id"].iter().filter_map(|k| Some((k.to_string(), p.get(*k)?.clone()))).collect();
// An id the engine refuses: `layer.delete` gives the error, and deletes nothing.
let row = |v: &Value| v.as_u64().map(NodeId).filter(|id| st.doc.node(*id).is_some_and(|n| !n.is_ruler_guide()));
@@ -532,7 +537,7 @@ fn delete_rows(app: &mut VectorcraftApp, p: &Value) -> Result<Value, String> {
[n] if n.is_layer() => tl!("Delete this layer and its artwork?"),
_ => tl!("Delete the selected items and their artwork?"),
};
let params = json!({"ids": ids.iter().map(|i| i.0).collect::<Vec<_>>(), "doc": st.uid, "confirmed": true});
let params = json!({"ids": ids.iter().map(|i| i.0).collect::<Vec<_>>(), "doc": st.uid, "reverts": st.reverts, "confirmed": true});
crate::dialogs::confirm::ask(app, message, "", "ui.layerDelete", params);
Ok(Value::Null)
}
@@ -483,6 +483,40 @@ fn isolation_keeps_a_confirmed_delete_to_the_rows_listed() {
assert!(app.session.active().unwrap().doc.node(sub).is_none());
}
#[test]
fn delete_selection_approval_does_not_remove_restored_layer_art() {
let (app, [_, sub, _, _, art, _]) = nested();
let mut app = crate::testing::windowed(app);
let file = std::env::temp_dir().join(format!("vectorcraft-layer-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 st = app.session.active().unwrap();
let (uid, reverts) = (st.uid, st.reverts);
app.run("layer.setProps", json!({"id": sub.0, "name": "Edited sublayer"})).unwrap();
assert!(app.run("ui.layerDelete", json!({"ids": [sub.0]})).unwrap().is_null());
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!(st.doc.node(sub).unwrap().name.as_deref(), Some("Sub"));
assert!(st.doc.node(art).is_some());
let undo = undo_len(&app);
let confirm = craft::ui::dialog::dialog_id(crate::dialogs::confirm::KIND);
crate::tests::picks::answer_dialog(&mut app, confirm, &[], true);
let st = app.session.active().unwrap();
assert!(st.doc.node(sub).is_some() && st.doc.node(art).is_some(), "old approval cannot delete restored artwork");
assert_eq!(undo_len(&app), undo);
assert_eq!(app.ui.status, "The document was reverted. Reopen this dialog to continue.");
assert!(app.run("ui.layerDelete", json!({"ids": [sub.0]})).unwrap().is_null());
crate::tests::picks::answer_dialog(&mut app, confirm, &[], true);
let st = app.session.active().unwrap();
assert!(st.doc.node(sub).is_none() && st.doc.node(art).is_none(), "a fresh confirmation still deletes the requested layer");
}
/// A document put in isolation mode opens its isolated row at once, and showing the documents again opens nothing.
#[test]
fn isolation_opens_the_isolated_row_of_each_document() {