PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Channels: Delete with an alpha channel targeted fills that channel (#2734) (#2741)
Valentin Kleind5694490f539
diff --git a/crates/engine/src/channel_cmds.rs b/crates/engine/src/channel_cmds.rsindex dc26e16b5..321d0d1a6 100644--- a/crates/engine/src/channel_cmds.rs+++ b/crates/engine/src/channel_cmds.rs@@ -686,6 +686,32 @@ pub(crate) fn clear_color_channel(doc: &mut Document, id: LayerId, k: usize, bacOk(())}+/// The alpha channel or Quick Mask the Channels panel targets, as `"target"` params: Clear+/// (Delete) fills it instead of the layer (#2734).+pub(crate) fn targeted_channel(s: &Session) -> Option<Value> {+ let st = s.active()?;+ let t = match st.channel_view.target {+ ChannelTarget::Alpha(i) if i < st.doc.channels.len() => json!({ "channel": i }),+ ChannelTarget::Composite if st.doc.quick_mask.is_some() => json!("quickMask"),+ _ => return None,+ };+ Some(json!({ "target": t }))+}++/// Clear (Delete) with an alpha channel or the Quick Mask targeted (`target` from+/// [`targeted_channel`]): fill the selected area (or the canvas) of it with the background colour,+/// as gray, as Photoshop does. Partly selected pixels blend.+pub(crate) fn clear_channel(doc: &mut Document, target: &Value, background: [f32; 4]) -> Result<()> {+ let canvas = doc.bounds();+ let sel = doc.selection.clone();+ let area = sel.as_ref().map_or(canvas, |m| m.content_bounds().intersect(&canvas));+ if area.is_empty() {+ return Ok(());+ }+ let (surf, _) = target_surface(doc, None, target)?;+ crate::fill_cmds::fill_color(surf, area, [background[0], background[1], background[2], 1.0], sel.as_ref())+}+/// The active document targets an alpha channel or is in Quick Mask mode, so pixel commands/// work without a pixel layer.pub(crate) fn edits_channel(s: &Session) -> bool {diff --git a/crates/engine/src/channel_cmds/tests.rs b/crates/engine/src/channel_cmds/tests.rsindex 51069c874..9a9b710b3 100644--- a/crates/engine/src/channel_cmds/tests.rs+++ b/crates/engine/src/channel_cmds/tests.rs@@ -547,3 +547,27 @@ fn clear_with_a_colour_channel_targeted_clears_only_that_channel() {assert!(near(&px(&s, 5, 5), &left), "one undoable step");}}++#[test]+fn clear_with_an_alpha_channel_or_quick_mask_targeted_fills_it_not_the_layer() {+ let mut s = session_depth(16);+ let layer = px(&s, 5, 5);+ s.tools.background = [0.0, 0.0, 0.0, 1.0];+ // A white Alpha 1, targeted.+ s.execute("channel.new", json!({"fill": "white"})).unwrap();+ rect(&mut s, 2, 2, 10, 10, "replace");+ s.execute("edit.clear", json!({})).unwrap();+ // The selected part of the channel takes the background colour; the layer stays.+ assert_eq!(chan(&s, 0, 5, 5), 0.0);+ assert_eq!(chan(&s, 0, 15, 5), 1.0, "outside the selection");+ assert_eq!(px(&s, 5, 5), layer, "the layer is untouched");+ s.undo();+ assert_eq!(chan(&s, 0, 5, 5), 1.0, "one undoable step");+ // In Quick Mask mode Delete fills the mask (black = masked), not the layer.+ s.execute("channel.target", json!({"channel": "composite"})).unwrap();+ s.execute("select.editInQuickMaskMode", json!({"on": true})).unwrap();+ s.execute("edit.clear", json!({})).unwrap();+ let q = doc(&s).quick_mask.as_ref().unwrap().surface.sample_channel(5, 5, 0);+ assert_eq!(q, 0.0);+ assert_eq!(px(&s, 5, 5), layer, "the layer is untouched in Quick Mask mode");+}diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rsindex a5928073f..a66ca8ecf 100644--- a/crates/engine/src/commands.rs+++ b/crates/engine/src/commands.rs@@ -397,9 +397,14 @@ fn build() -> Vec<CommandSpec> {has_pixel_or_channel,crate::fill_cmds::fill),- cmd!("edit.clear", "Clear", ["Edit"], Some("Delete"), "{} (a targeted colour channel clears that channel only)", has_pixel_layer, |s, p| {- let id = layer_param(s, p)?;+ cmd!("edit.clear", "Clear", ["Edit"], Some("Delete"), "{} (a targeted channel or Quick Mask is cleared alone)", has_pixel_or_channel, |s, p| {let bg = s.tools.background;+ // A targeted alpha channel or Quick Mask is filled instead (#2734).+ if let Some(t) = crate::channel_cmds::targeted_channel(s) {+ s.edit("Clear", |doc, _| crate::channel_cmds::clear_channel(doc, &t, bg))?;+ return Ok(Value::Null);+ }+ let id = layer_param(s, p)?;// A targeted colour channel clears that channel only (#2720).if let Some(k) = crate::channel_cmds::targeted_color(s) {s.edit("Clear", |doc, _| crate::channel_cmds::clear_color_channel(doc, id, k, bg))?;diff --git a/crates/engine/src/fill_cmds.rs b/crates/engine/src/fill_cmds.rsindex 4d25474f3..d99a29787 100644--- a/crates/engine/src/fill_cmds.rs+++ b/crates/engine/src/fill_cmds.rs@@ -171,6 +171,12 @@ pub(crate) fn composite_over(surf: &mut Surface, src: &Surface, area: Rect, limiblend_into(surf, area, &source, limit, Blend { mode: BlendMode::Normal, opacity: 1.0, keep_alpha: false, restore: false })}+/// `area` of `surf` filled with an opaque `color` (Normal, 100 %), weighted by `sel` when given:+/// Clear on a targeted alpha channel or the Quick Mask (#2734).+pub(crate) fn fill_color(surf: &mut Surface, area: Rect, color: [f32; 4], sel: Option<&Surface>) -> Result<()> {+ blend_into(surf, area, &Source::Color(color), sel, Blend { mode: BlendMode::Normal, opacity: 1.0, keep_alpha: false, restore: false })+}+/// Reuse Edit › Fill's compositing for Edit › Stroke. The stroke's rasterized band/// is already a grayscale coverage mask; opacity, blend modes and alpha locking/// must behave identically for both commands at 8/16/32-bit depth.