diff --git a/crates/ui-egui/src/dialogs/live_shape_prompt.rs b/crates/ui-egui/src/dialogs/live_shape_prompt.rs index 8f05693489..b963516bc4 100644 --- a/crates/ui-egui/src/dialogs/live_shape_prompt.rs +++ b/crates/ui-egui/src/dialogs/live_shape_prompt.rs @@ -32,6 +32,7 @@ fn is_live(app: &PhotocraftApp, r: PathRef) -> bool { /// or "Don't show again" was ticked. pub fn ask(app: &mut PhotocraftApp, r: PathRef, params: &Value) -> bool { let PathRef::Layer(layer) = r else { return false }; + let Some(document) = app.session.active().map(|st| st.doc.id.0) else { return false }; if !is_live(app, r) || dont_show_again::dismissed(app, PREF) { return false; } @@ -43,6 +44,8 @@ pub fn ask(app: &mut PhotocraftApp, r: PathRef, params: &Value) -> bool { f.insert("__command".into(), json!("path.deleteAnchor")); f.insert("__label".into(), json!(tl!("Delete Anchor Point"))); f.insert("layer".into(), json!(layer)); + // The layer id means something only in this document (another may become active meanwhile). + f.insert("__document".into(), json!(document)); f.insert("params".into(), params.clone()); f.insert("__dontShowAgain".into(), json!(PREF)); f.insert("skip".into(), json!(false)); @@ -59,6 +62,10 @@ pub fn body(ui: &mut egui::Ui, fields: &mut Map) { pub fn confirm(app: &mut PhotocraftApp, fields: &Map) -> Result { let layer = fields.get("layer").and_then(Value::as_u64).ok_or(tl!("No anchor to delete"))?; let params = fields.get("params").cloned().ok_or(tl!("No anchor to delete"))?; + let document = fields.get("__document").and_then(Value::as_u64); + if document.is_none() || app.session.active().map(|st| st.doc.id.0) != document { + return Err(tl!("No anchor to delete").into()); + } let result = app.run("path.deleteAnchor", params)?; // The anchors shift: a Direct Selection on this path would point at the wrong ones. let r = PathRef::Layer(layer); diff --git a/crates/ui-egui/src/tools/anchor_tools/delete_anchor_live_shape_tests.rs b/crates/ui-egui/src/tools/anchor_tools/delete_anchor_live_shape_tests.rs index a4d448f57c..015a4a7e04 100644 --- a/crates/ui-egui/src/tools/anchor_tools/delete_anchor_live_shape_tests.rs +++ b/crates/ui-egui/src/tools/anchor_tools/delete_anchor_live_shape_tests.rs @@ -82,6 +82,28 @@ fn yes_deletes_the_anchor_and_the_layer_is_no_longer_live() { assert_eq!(app.session.active().unwrap().history.undo_label(), Some("Delete Anchor Point")); } +/// The question belongs to the document it was asked in: Yes after another document became active +/// is refused and changes nothing there (layer ids are unique in a session, so this guards the +/// binding the way the Color Picker's `__document` does), and the question's own document keeps +/// its shape. +#[test] +fn yes_after_another_document_became_active_deletes_nothing() { + let (mut app, shape) = live(Tool::DeleteAnchorPoint); + click(&mut app, CORNER); + let id = question(&app).expect("the question"); + let first = app.session.active_index(); + app.session.execute("file.new", json!({"width": 800, "height": 600})).unwrap(); + app.sync_views(); + let other = app.run("shape.create", json!({"kind": "rect", "rect": [450, 100, 200, 150], "fill": "#3070c0"})).unwrap()["layer"].as_u64().unwrap(); + assert_ne!(app.session.active_index(), first, "the new document is active"); + let (before, n) = (shape_path(&mut app, other), steps(&app)); + assert!(crate::dialogs::confirm(&mut app, id).is_err()); + assert_eq!((shape_path(&mut app, other), steps(&app)), (before, n)); + assert!(app.session.set_active(first.unwrap())); + app.sync_views(); + assert_eq!(corners(&mut app, shape), 4, "the first document's shape keeps its corner"); +} + #[test] fn no_leaves_the_shape_its_parameters_and_history_unchanged() { let (mut app, shape) = live(Tool::DeleteAnchorPoint);