From 93a9ffa3c9735a4cd4999dbbc58757617dd03ebc Mon Sep 17 00:00:00 2001 From: dholzric Date: Tue, 1 Dec 2026 14:06:26 -0600 Subject: [PATCH] Define Brush Preset and Define Pattern: greyed while a layer mask is the target (#18232) With a pixel layer's or a group's layer mask targeted, or an adjustment layer with its mask active, both items stayed enabled and made a brush or pattern from the composite. The reference greys them there. A shared check, pattern_cmds::layer_mask_targeted, now gates edit.definePattern and brush.defineFromSelection; Define Brush Preset passes on the brush command's reason. Co-Authored-By: Claude Opus 5.5 --- crates/engine-core/src/cmd/edit_menu_cmds.rs | 2 +- crates/engine-core/src/cmd/pattern_cmds.rs | 19 ++- crates/engine-paint/src/cmd/brush_cmds.rs | 6 + .../define_brush_mask_target_tests.rs | 126 ++++++++++++++++++ 4 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 crates/engine-paint/src/cmd/brush_cmds/define_brush_mask_target_tests.rs diff --git a/crates/engine-core/src/cmd/edit_menu_cmds.rs b/crates/engine-core/src/cmd/edit_menu_cmds.rs index 3633d630a1..f8ffa52811 100644 --- a/crates/engine-core/src/cmd/edit_menu_cmds.rs +++ b/crates/engine-core/src/cmd/edit_menu_cmds.rs @@ -370,7 +370,7 @@ pub fn specs() -> Vec { ["Edit"], None, r##"{"name":text}"##, - |s| if s.is_enabled("brush.defineFromSelection") { Ok(()) } else { Err("select pixels to define a brush from".into()) }, + |s| s.disabled_reason("brush.defineFromSelection").map_or(Ok(()), Err), define_brush_preset ), spec!( diff --git a/crates/engine-core/src/cmd/pattern_cmds.rs b/crates/engine-core/src/cmd/pattern_cmds.rs index 8a6e108e7e..9f2246fbc2 100644 --- a/crates/engine-core/src/cmd/pattern_cmds.rs +++ b/crates/engine-core/src/cmd/pattern_cmds.rs @@ -456,6 +456,23 @@ fn for_each_band(p: &Pattern, w: u32, h: u32, mut f: impl FnMut(&[[f32; 4]])) { } } +/// A layer mask is the edit target: the active pixel layer's or group's mask is targeted, or the active layer is an +/// adjustment layer with a mask, which is what it edits. Define Pattern and Define Brush Preset are greyed then (#18232). +pub fn layer_mask_targeted(s: &Session) -> bool { + let Some(st) = s.active() else { return false }; + let Some(layer) = st.active_layer.and_then(|id| st.doc.layer(id)).filter(|l| l.mask.is_some()) else { return false }; + match st.channel_view.target { + crate::channel_cmds::ChannelTarget::LayerMask => true, + crate::channel_cmds::ChannelTarget::Composite => st.doc.quick_mask.is_none() && matches!(layer.content, LayerContent::Adjustment(_)), + _ => false, + } +} + +fn can_define(s: &Session) -> std::result::Result<(), String> { + has_doc(s)?; + if layer_mask_targeted(s) { Err("a layer mask is the target".into()) } else { Ok(()) } +} + pub fn specs() -> Vec { vec![ CommandSpec { @@ -464,7 +481,7 @@ pub fn specs() -> Vec { menu: &["Edit"], shortcut: None, params: r##"{"name":text,"rect":[x0,y0,x1,y1]?} → {"pattern":id} (name: default the document's name numbered past the library's names; rect: default the selection bounds or else the canvas; added to the library from the visible composite)"##, - enabled: has_doc, + enabled: can_define, journal: true, run: define, }, diff --git a/crates/engine-paint/src/cmd/brush_cmds.rs b/crates/engine-paint/src/cmd/brush_cmds.rs index e07b0102ed..6cc12942b3 100644 --- a/crates/engine-paint/src/cmd/brush_cmds.rs +++ b/crates/engine-paint/src/cmd/brush_cmds.rs @@ -1166,6 +1166,9 @@ fn presets_set_current(s: &mut Session, p: &Value) -> Result { fn has_selection_and_pixels(s: &Session) -> std::result::Result<(), String> { let d = s.active().ok_or("no document")?; + if crate::pattern_cmds::layer_mask_targeted(s) { + return Err("a layer mask is the target".into()); + } // The area the command reads (selection, else canvas) is limited, not just the painted // extent, like Photoshop (#14275). let area = d.doc.selection.as_ref().map_or_else(|| d.doc.bounds(), |sel| sel.content_bounds().intersect(&d.doc.bounds())); @@ -1706,3 +1709,6 @@ mod filter_mask_stroke_tests; #[cfg(test)] mod define_brush_size_limit_tests; + +#[cfg(test)] +mod define_brush_mask_target_tests; diff --git a/crates/engine-paint/src/cmd/brush_cmds/define_brush_mask_target_tests.rs b/crates/engine-paint/src/cmd/brush_cmds/define_brush_mask_target_tests.rs new file mode 100644 index 0000000000..17c64ac9a2 --- /dev/null +++ b/crates/engine-paint/src/cmd/brush_cmds/define_brush_mask_target_tests.rs @@ -0,0 +1,126 @@ +//! #18232: Define Brush Preset (and the brush command it runs) and Define Pattern are greyed and refused while a layer +//! mask is the edit target: a pixel layer's or a group's mask targeted, or an adjustment layer with its mask. Targeting +//! the composite again, or deleting the adjustment layer's mask, makes them available. Other targets and layer kinds +//! keep today's gates. + +use super::*; + +const COMMANDS: [&str; 3] = ["edit.defineBrushPreset", "brush.defineFromSelection", "edit.definePattern"]; + +/// A white 60 x 40 document with a new layer holding a black rectangle, the layer active. +fn document() -> Session { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 60, "height": 40, "background": "#ffffff"})).unwrap(); + s.execute("layer.new.layer", json!({"name": "Tip"})).unwrap(); + s.edit("BrushSrc", |doc, active| { + doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap().fill_rect(Rect::new(10, 10, 30, 25), &[0.0, 0.0, 0.0, 1.0]); + Ok(()) + }) + .unwrap(); + s +} + +fn target(s: &mut Session, channel: Value) { + s.execute("channel.target", json!({"channel": channel})).unwrap(); +} + +/// The brush, the brush presets, the pattern library and the document, to show that a refusal changes none of them. +fn snapshot(s: &Session) -> (BrushSettings, Vec, usize, usize) { + let patterns = s.patterns.items.len(); + ((*s.tools.brush).clone(), s.tools.presets.to_vec(), patterns, s.active().unwrap().history.past_len()) +} + +fn assert_refused(s: &mut Session, case: &str) { + for command in COMMANDS { + assert!(s.disabled_reason(command).is_some(), "{case}: {command} stays enabled"); + let before = snapshot(s); + assert!(s.execute(command, json!({"name": "Refused"})).is_err(), "{case}: {command} ran"); + assert!(snapshot(s) == before, "{case}: {command} changed the brush, the presets, the patterns or the History"); + } +} + +fn assert_enabled(s: &mut Session, case: &str) { + for command in COMMANDS { + assert_eq!(s.disabled_reason(command), None, "{case}: {command}"); + } +} + +#[test] +fn a_pixel_layers_targeted_mask_greys_them() { + for enabled in [true, false] { + let mut s = document(); + s.execute("layer.layerMask.revealAll", json!({})).unwrap(); + if !enabled { + s.execute("layer.layerMask.enabled", json!({"enabled": false})).unwrap(); + } + target(&mut s, json!("mask")); + assert_refused(&mut s, if enabled { "a pixel layer's mask" } else { "a pixel layer's disabled mask" }); + } +} + +#[test] +fn a_groups_targeted_mask_greys_them() { + let mut s = document(); + s.execute("layer.groupLayers", json!({})).unwrap(); + s.execute("layer.layerMask.revealAll", json!({})).unwrap(); + target(&mut s, json!("mask")); + assert_refused(&mut s, "a group's mask"); +} + +#[test] +fn a_new_adjustment_layer_greys_them_until_its_mask_is_deleted() { + let mut s = document(); + s.execute("layer.newAdjustmentLayer.invert", json!({})).unwrap(); + assert_refused(&mut s, "a new adjustment layer"); + s.execute("layer.layerMask.delete", json!({})).unwrap(); + assert_enabled(&mut s, "the adjustment layer without its mask"); +} + +/// Back on the composite, the items work as before and make the same tip as a document that never targeted a mask. +#[test] +fn targeting_the_composite_again_enables_them_with_the_same_tip() { + let mut plain = document(); + plain.execute("layer.layerMask.revealAll", json!({})).unwrap(); + target(&mut plain, json!("composite")); + plain.execute("brush.defineFromSelection", json!({"name": "Tip"})).unwrap(); + + let mut s = document(); + s.execute("layer.layerMask.revealAll", json!({})).unwrap(); + target(&mut s, json!("mask")); + assert_refused(&mut s, "the mask"); + target(&mut s, json!("composite")); + assert_enabled(&mut s, "the composite again"); + s.execute("brush.defineFromSelection", json!({"name": "Tip"})).unwrap(); + assert!(s.tools.brush.tip == plain.tools.brush.tip, "the tip is the one made without a mask target"); + s.execute("edit.definePattern", json!({"name": "Pattern"})).unwrap(); +} + +/// Not a layer mask target: these keep today's gates. +#[test] +fn other_targets_and_layer_kinds_stay_enabled() { + // A pixel layer with a mask, the composite targeted. + let mut s = document(); + s.execute("layer.layerMask.revealAll", json!({})).unwrap(); + assert_enabled(&mut s, "a masked pixel layer, composite targeted"); + + // An alpha channel, and Quick Mask. + s.execute("channel.new", json!({})).unwrap(); + target(&mut s, json!({"alpha": 0})); + assert_enabled(&mut s, "an alpha channel"); + target(&mut s, json!("composite")); + s.execute("select.editInQuickMaskMode", json!({})).unwrap(); + assert_enabled(&mut s, "Quick Mask"); + + // A locked layer, a group, the Background and a Solid Color fill layer with its mask. + let mut s = document(); + s.execute("layer.setProps", json!({"locks": {"all": true}})).unwrap(); + assert_enabled(&mut s, "a locked layer"); + let mut s = document(); + s.execute("layer.groupLayers", json!({})).unwrap(); + assert_enabled(&mut s, "a group"); + let background = s.active().unwrap().doc.layers[0].id; + s.execute("layer.select", json!({"layer": background.0})).unwrap(); + assert_enabled(&mut s, "the Background"); + s.execute("layer.newFillLayer.solidColor", json!({"color": "#336699"})).unwrap(); + assert_enabled(&mut s, "a Solid Color fill layer"); +}