PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Delete with nothing selected deletes the selected layers, of any kind (#1077) (#1087)
metzmehard2b90697d48cf
diff --git a/crates/ui-egui/src/shortcut_dispatch.rs b/crates/ui-egui/src/shortcut_dispatch.rsindex 9046da42a..14fa6d04b 100644--- a/crates/ui-egui/src/shortcut_dispatch.rs+++ b/crates/ui-egui/src/shortcut_dispatch.rs@@ -189,6 +189,7 @@ pub fn dispatch_pressed(app: &mut PhotocraftApp, ctx: &egui::Context, focus: FocSome(id)});let Some(id) = next else { break };+ let id = delete_key_command(app, id);let owner = key_owner(app, ctx);dispatch(app, ctx, &id);ran = true;@@ -199,6 +200,23 @@ pub fn dispatch_pressed(app: &mut PhotocraftApp, ctx: &egui::Context, focus: Focran}+/// Clear's key (Delete / Backspace) with nothing selected deletes the selected layers, of any+/// kind, as in Photoshop (#1077): Edit › Clear only clears pixel layers, so on an adjustment, fill,+/// type or shape layer the key did nothing. A selection, a targeted layer mask, a single channel+/// or Quick Mask keeps Clear.+fn delete_key_command(app: &PhotocraftApp, id: String) -> String {+ if id != "edit.clear" {+ return id;+ }+ let Some(st) = app.session.active() else { return id };+ let composite = st.channel_view.target == photocraft_engine::channel_cmds::ChannelTarget::Composite && st.doc.quick_mask.is_none();+ let mask = app.ui.mask_target && st.active_layer.and_then(|l| st.doc.layer(l)).is_some_and(|l| l.mask.is_some());+ if st.doc.selection.is_some() || !composite || mask {+ return id;+ }+ "layer.delete".into()+}+/// Why `id` can't run now (the engine's reason when it has one).pub fn disabled_reason(app: &PhotocraftApp, id: &str) -> String {photocraft_engine::commands::find(id).and_then(|_| app.session.disabled_reason_with(id, &app.with_mask_target(id, serde_json::Value::Null))).unwrap_or_else(diff --git a/crates/ui-egui/src/shortcut_dispatch/tests.rs b/crates/ui-egui/src/shortcut_dispatch/tests.rsindex 98768b1ea..62b321e80 100644--- a/crates/ui-egui/src/shortcut_dispatch/tests.rs+++ b/crates/ui-egui/src/shortcut_dispatch/tests.rs@@ -426,3 +426,62 @@ fn a_shortcut_opening_a_dialog_takes_the_frames_later_keys() {assert_eq!(layer_count(&h), 5, "⌘Z didn't undo behind the dialog");assert_eq!(logged(&h), ["image.adjustments.levels"]);}++fn names(h: &Harness<'_, PhotocraftApp>) -> Vec<String> {+ h.state().session.active().unwrap().doc.walk().into_iter().map(|(_, _, l)| l.name.clone()).collect()+}++/// #1077: with nothing selected, Delete (and Backspace) deletes the selected layer whatever its+/// kind, from the canvas or the Layers panel, in one undo step; it used to say Clear needs a+/// pixel layer on an adjustment layer.+#[test]+fn delete_without_a_selection_deletes_the_selected_layer() {+ for place in [Place::Canvas, Place::LayersRow] {+ for key in ["Delete", "Backspace"] {+ let mut h = harness();+ put_focus(&mut h, place);+ let s = &mut h.state_mut().session;+ s.execute("select.deselect", json!({})).unwrap();+ let adj = photocraft_doc::LayerId(s.execute("layer.newAdjustmentLayer.invert", json!({})).unwrap()["layer"].as_u64().unwrap());+ h.run_steps(2);+ let before = names(&h);+ press(&mut h, key);+ assert_eq!(logged(&h), ["layer.delete"], "{place:?} {key}");+ assert!(h.state().session.active().unwrap().doc.layer(adj).is_none(), "{place:?} {key}: the adjustment layer is gone");+ assert_eq!(names(&h).len(), before.len() - 1, "{place:?} {key}: only it");+ press(&mut h, "Cmd+Z");+ assert_eq!(names(&h), before, "{place:?} {key}: one undo brings it back");+ }+ }+}++/// Every selected layer goes, pixel layers too (Photoshop deletes, it doesn't clear the layer).+#[test]+fn delete_without_a_selection_deletes_every_selected_layer() {+ let mut h = harness();+ put_focus(&mut h, Place::Canvas);+ let s = &mut h.state_mut().session;+ s.execute("select.deselect", json!({})).unwrap();+ let paint = s.active().unwrap().active_layer.unwrap();+ let adj = photocraft_doc::LayerId(s.execute("layer.newAdjustmentLayer.invert", json!({})).unwrap()["layer"].as_u64().unwrap());+ s.execute("layer.select", json!({"layer": paint.0})).unwrap();+ s.execute("layer.select", json!({"layer": adj.0, "mode": "add"})).unwrap();+ h.run_steps(2);+ let n = names(&h).len();+ press(&mut h, "Delete");+ let doc = &h.state().session.active().unwrap().doc;+ assert!(doc.layer(paint).is_none() && doc.layer(adj).is_none(), "both selected layers are gone");+ assert_eq!(names(&h).len(), n - 2);+}++/// A selection keeps Edit › Clear: the selected pixels go, the layer stays.+#[test]+fn delete_with_a_selection_clears_it() {+ let mut h = harness();+ put_focus(&mut h, Place::Canvas);+ let before = names(&h);+ assert!(h.state().session.active().unwrap().doc.selection.is_some());+ press(&mut h, "Delete");+ assert_eq!(logged(&h), ["edit.clear"]);+ assert_eq!(names(&h), before, "no layer was deleted");+}