PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Fixed a bug where if you select a mask and hit the delete key, it deletes the image (#2635)
Lyraedanc287ecabc5e9
diff --git a/crates/ui-egui/src/shortcut_dispatch.rs b/crates/ui-egui/src/shortcut_dispatch.rsindex 5a2b17fdc..0245d99f0 100644--- a/crates/ui-egui/src/shortcut_dispatch.rs+++ b/crates/ui-egui/src/shortcut_dispatch.rs@@ -243,8 +243,10 @@ pub fn dispatch_pressed(app: &mut PhotocraftApp, ctx: &egui::Context, focus: Foc/// 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.+/// type or shape layer the key did nothing. A selection, a single channel or Quick Mask keeps Clear.+/// With the active layer's mask targeted the key edits the mask, never the layer's pixels: a+/// selection is filled with the background colour in the mask (Photoshop's Delete on a mask), and+/// with no selection the mask itself is deleted, as Layer › Layer Mask › Delete.fn delete_key_command(app: &PhotocraftApp, id: String) -> String {if id != "edit.clear" {return id;@@ -252,7 +254,11 @@ fn delete_key_command(app: &PhotocraftApp, id: String) -> String {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 {+ if mask && composite {+ let cmd = if st.doc.selection.is_some() { "edit.fillBackground" } else { "layer.layerMask.delete" };+ return cmd.into();+ }+ if st.doc.selection.is_some() || !composite {return id;}"layer.delete".into()diff --git a/crates/ui-egui/src/shortcut_dispatch/tests.rs b/crates/ui-egui/src/shortcut_dispatch/tests.rsindex 3b109bad6..84b87bb76 100644--- a/crates/ui-egui/src/shortcut_dispatch/tests.rs+++ b/crates/ui-egui/src/shortcut_dispatch/tests.rs@@ -458,6 +458,43 @@ fn delete_without_a_selection_deletes_the_selected_layer() {}}+/// With the layer mask targeted, Delete / Backspace edit the mask and never the layer's pixels:+/// a selection fills the mask with the background colour there, and with no selection the mask+/// itself is deleted.+#[test]+fn delete_with_the_mask_targeted_edits_the_mask_not_the_layer() {+ for key in ["Delete", "Backspace"] {+ let mut h = harness();+ put_focus(&mut h, Place::Canvas);+ let s = &mut h.state_mut().session;+ let layer = s.active().unwrap().active_layer.unwrap();+ s.execute("layer.layerMask.revealAll", json!({})).unwrap();+ let sel = s.active().unwrap().doc.selection.as_ref().expect("the harness keeps a selection").content_bounds();+ let (x, y) = ((sel.x0 + sel.x1) / 2, (sel.y0 + sel.y1) / 2);+ h.state_mut().ui.mask_target = true;+ h.run_steps(2);+ let layer_pixels = |h: &Harness<'_, PhotocraftApp>| {+ h.state().session.active().unwrap().doc.layer(layer).and_then(|l| l.surface().cloned()).map(|s| s.read_region(sel))+ };+ let mask_at = |h: &Harness<'_, PhotocraftApp>| {+ h.state().session.active().unwrap().doc.layer(layer).and_then(|l| l.mask.as_ref()).map(|m| m.surface.sample_channel(x, y, 0))+ };+ let before = layer_pixels(&h);+ let bg = h.state().session.tools.background;+ press(&mut h, key);+ assert_eq!(logged(&h), ["edit.fillBackground"], "{key}");+ assert_eq!(mask_at(&h), Some(bg[0]), "{key}: the mask takes the background colour in the selection");+ assert_eq!(layer_pixels(&h), before, "{key}: the layer's pixels are untouched");+ h.state_mut().session.execute("select.deselect", json!({})).unwrap();+ h.run_steps(2);+ press(&mut h, key);+ assert_eq!(logged(&h), ["layer.layerMask.delete"], "{key}");+ assert_eq!(mask_at(&h), None, "{key}: no selection deletes the mask");+ assert!(h.state().session.active().unwrap().doc.layer(layer).is_some(), "{key}: the layer stays");+ assert_eq!(layer_pixels(&h), before, "{key}: the layer's pixels are untouched");+ }+}+/// 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() {