Merge pull request #18809

Co-authored-by: autocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com>
This commit is contained in:
davidmann4andautocraft-bot[bot] authored and autocraft-bot[bot] committed 2026-12-02 01:07:07 +00:00
commit 1376634ebd
4 files changed
+185 -2

No files matched your search

+1 -1
View File
@@ -370,7 +370,7 @@ pub fn specs() -> Vec<CommandSpec> {
["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!(
+28 -1
View File
@@ -456,6 +456,33 @@ 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, the active layer is an
/// adjustment layer with a mask, which is what it edits, or every colour channel is hidden with no alpha channel shown, so
/// the mask is the picture (as the desktop's target rule reads it). A shown filter mask is not the layer mask. 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 };
if crate::mask_view_cmds::current(st).is_some_and(|v| v.filter) {
return false;
}
match st.channel_view.target {
crate::channel_cmds::ChannelTarget::LayerMask => true,
crate::channel_cmds::ChannelTarget::Composite if st.doc.quick_mask.is_none() => {
let view = &st.channel_view;
let colors_off = view.visible_colors(crate::channel_cmds::color_count(&st.doc)) == 0;
let alpha_on = (0..st.doc.channels.len()).any(|i| view.alpha_shown(i));
matches!(layer.content, LayerContent::Adjustment(_)) || (colors_off && !alpha_on)
}
_ => 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<CommandSpec> {
vec![
CommandSpec {
@@ -464,7 +491,7 @@ pub fn specs() -> Vec<CommandSpec> {
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,
},
@@ -1166,6 +1166,9 @@ fn presets_set_current(s: &mut Session, p: &Value) -> Result<Value> {
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;
@@ -0,0 +1,150 @@
//! #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<BrushPreset>, 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();
}
/// With every colour channel hidden and no alpha channel shown, the mask is the picture and the target; showing a colour
/// channel or an alpha channel again makes the items available.
#[test]
fn hiding_every_colour_channel_greys_them_on_a_masked_layer() {
let mut s = document();
s.execute("layer.layerMask.revealAll", json!({})).unwrap();
target(&mut s, json!("composite"));
s.execute("channel.setVisible", json!({"channel": "composite", "visible": false})).unwrap();
assert_refused(&mut s, "every colour channel hidden");
s.execute("channel.setVisible", json!({"channel": {"color": 0}, "visible": true})).unwrap();
assert_enabled(&mut s, "one colour channel shown again");
s.execute("channel.new", json!({})).unwrap();
target(&mut s, json!("composite"));
s.execute("channel.setVisible", json!({"channel": "composite", "visible": false})).unwrap();
s.execute("channel.setVisible", json!({"channel": {"alpha": 0}, "visible": true})).unwrap();
assert_enabled(&mut s, "an alpha channel shown");
// Without a mask there is nothing else to target.
let mut s = document();
s.execute("channel.setVisible", json!({"channel": "composite", "visible": false})).unwrap();
assert_enabled(&mut s, "no mask, every colour channel hidden");
}
/// 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");
}