From c287ecabc5e9d427308e0fcefd46da8ab92d468b Mon Sep 17 00:00:00 2001 From: Lyraedan <61066562+Lyraedan@users.noreply.github.com> Date: Sat, 10 Oct 2026 18:33:53 +0100 Subject: [PATCH] Fixed a bug where if you select a mask and hit the delete key, it deletes the image (#2635) * Fixed a bug where if you select a mask and hit the delete key, it deletes the image * Delete on a targeted mask: fill the selection with the background colour; delete the mask only with no selection Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Brandon Thomas Co-authored-by: Claude Opus 5.5 --- crates/ui-egui/src/shortcut_dispatch.rs | 12 ++++-- crates/ui-egui/src/shortcut_dispatch/tests.rs | 37 +++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/shortcut_dispatch.rs b/crates/ui-egui/src/shortcut_dispatch.rs index 5a2b17fdca..0245d99f0b 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.rs index 3b109bad64..84b87bb76d 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() {