PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Fix multi-layer Delete and Move drags on a locked Background (#4109)
Vikram Bhaskaran4ff2dc33cacc
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 804744d39..17de8820e 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -4629,10 +4629,29 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers)let target = app.ui.tool_options.move_target.clone();let mode = if mods.shift { "add" } else { "replace" };let before = app.session.active().map(|st| st.selected_layers());+ // A locked layer under the pointer (the Background) can't move. Photoshop draws+ // the layer marquee instead of reporting the lock (#4058). Probe without+ // selecting so a locked hit doesn't become the selection first.+ let probe = app.run("layer.pickAt", json!({"x": x, "y": y, "target": target, "select": false}));+ let locked_hit = probe.as_ref().ok().and_then(|v| v["layer"].as_u64()).is_some_and(|id| {+ app.session.active().is_some_and(|st| {+ let id = photocraft_doc::LayerId(id);+ let locks = st.doc.effective_locks(id);+ locks.position || locks.all || st.doc.is_background(id)+ })+ });+ if locked_hit || (probe.as_ref().is_ok_and(|v| v["layer"].is_null()) && crate::move_box::empty_at(app, [x, y])) {+ if locked_hit {+ // A click still selects the locked layer, as in Photoshop; a drag's+ // marquee then replaces that selection on release.+ let _ = app.run("layer.pickAt", json!({"x": x, "y": y, "target": target, "mode": mode}));+ }+ crate::move_box::begin(app, [x, y], mods.shift);+ return;+ }let picked = app.run("layer.pickAt", json!({"x": x, "y": y, "target": target, "mode": mode}));// No pixels there at all (an empty spot, or off the canvas): drag a marquee that- // selects the layers it touches instead (move_box.rs). A fully locked layer's- // pixels still say it is locked once the pointer moves (`move_lock`).+ // selects the layers it touches instead (move_box.rs).if picked.is_ok_and(|v| v["layer"].is_null()) && crate::move_box::empty_at(app, [x, y]) {crate::move_box::begin(app, [x, y], mods.shift);return;diff --git a/crates/ui-egui/src/move_box.rs b/crates/ui-egui/src/move_box.rsindex f826992a3..ccbfd9cb4 100644--- a/crates/ui-egui/src/move_box.rs+++ b/crates/ui-egui/src/move_box.rs@@ -114,4 +114,24 @@ mod tests {sel.sort_unstable_by_key(|l| l.0);assert_eq!(sel, ids);}++ /// #4058: a drag that starts on the locked Background draws the layer marquee. It does not+ /// open the move-lock dialog, and releasing selects the unlocked layer the box touches.+ #[test]+ fn a_drag_on_the_locked_background_selects_instead_of_erroring() {+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({"width": 80, "height": 60})).unwrap();+ app.sync_views();+ app.run("layer.new.layer", json!({})).unwrap();+ app.run("select.rect", json!({"x": 40, "y": 8, "width": 16, "height": 16})).unwrap();+ app.run("edit.fill", json!({"color": "#ff0000"})).unwrap();+ app.run("select.deselect", json!({})).unwrap();+ let paint = app.session.active().unwrap().active_layer.unwrap();+ app.ui.tool = Tool::Move;+ app.ui.tool_options.move_auto_select = true;+ drag(&mut app, [8.0, 8.0], [50.0, 20.0], egui::Modifiers::NONE);+ assert!(app.ui.dialogs.iter().all(|d| d.kind != crate::state::DialogKind::Error), "no lock dialog");+ assert!(app.layer_box.is_none());+ assert_eq!(app.session.active().unwrap().selected_layers(), vec![paint]);+ }}