Gradient: grey the colour modes on a layer mask in the Classic Mode list (#18634)

With a layer mask targeted, Darker Color, Lighter Color, Hue,
Saturation, Color and Luminosity are greyed besides Behind.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
dholzricandClaude Opus 5.5 committed 2026-12-01 18:54:33 -06:00
1 parent ff7c499d2c
commit ba86552f6e
2 files changed
+92 -7

No files matched your search

@@ -0,0 +1,64 @@
//! #18634: with a layer mask targeted, the Classic gradient's Mode list greys Behind, Darker Color, Lighter Color, Hue,
//! Saturation, Color and Luminosity, the modes that need colour or transparency; every other entry stays enabled. With
//! the layer's pixels targeted again, this rule greys nothing.
use photocraft_doc::BlendMode;
use serde_json::json;
use super::mode_entries;
use crate::PhotocraftApp;
use crate::state::Tool;
const GREYED_ON_A_MASK: [BlendMode; 6] =
[BlendMode::DarkerColor, BlendMode::LighterColor, BlendMode::Hue, BlendMode::Saturation, BlendMode::Color, BlendMode::Luminosity];
fn app() -> Box<PhotocraftApp> {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({"width": 80, "height": 60, "background": "white"})).unwrap();
app.ui.tool = Tool::Gradient;
app.ui.tool_options.gradient_classic = true;
app
}
/// The greyed entries of the Mode list: `None` is Behind.
fn greyed(app: &PhotocraftApp) -> Vec<Option<BlendMode>> {
mode_entries(app).iter().filter(|e| !e.is_enabled()).filter_map(|e| e.value().copied()).collect()
}
fn expected_on_a_mask() -> Vec<Option<BlendMode>> {
let mut v: Vec<Option<BlendMode>> = vec![None];
v.extend(GREYED_ON_A_MASK.map(Some));
v.sort_by_key(|m| format!("{m:?}"));
v
}
fn sorted(mut v: Vec<Option<BlendMode>>) -> Vec<Option<BlendMode>> {
v.sort_by_key(|m| format!("{m:?}"));
v
}
#[test]
fn a_targeted_layer_mask_greys_the_seven_modes() {
let cases: [(&str, &[(&str, serde_json::Value)]); 3] = [
("a pixel layer's mask", &[("layer.new.layer", json!({})), ("layer.layerMask.revealAll", json!({})), ("channel.target", json!({"channel": "mask"}))]),
("a solid colour fill layer's mask", &[("layer.newFillLayer.solidColor", json!({"color": "#336699"})), ("channel.target", json!({"channel": "mask"}))]),
("a Curves adjustment layer's mask", &[("layer.newAdjustmentLayer.curves", json!({})), ("channel.target", json!({"channel": "mask"}))]),
];
for (case, steps) in cases {
let mut app = app();
for (id, params) in steps {
app.run(id, params.clone()).unwrap_or_else(|e| panic!("{case}: {id}: {e}"));
}
assert_eq!(sorted(greyed(&app)), expected_on_a_mask(), "{case}");
}
}
#[test]
fn the_layers_pixels_targeted_again_grey_none_of_them() {
let mut app = app();
app.run("layer.new.layer", json!({})).unwrap();
app.run("layer.layerMask.revealAll", json!({})).unwrap();
app.run("channel.target", json!({"channel": "mask"})).unwrap();
app.run("channel.target", json!({"channel": "composite"})).unwrap();
assert!(greyed(&app).is_empty(), "{:?}", greyed(&app));
}
+28 -7
View File
@@ -22,12 +22,18 @@ use super::{fill_layer_active, options_changed, stops};
use crate::state::Tool;
use crate::{PhotocraftApp, panel_docking};
/// The modes a layer mask has no use for: they need colour (#18634). The reference greys them, with Behind, while a
/// layer mask is the target.
const COLOUR_MODES: [BlendMode; 6] =
[BlendMode::DarkerColor, BlendMode::LighterColor, BlendMode::Hue, BlendMode::Saturation, BlendMode::Color, BlendMode::Luminosity];
/// The modes the Classic gradient lists, with a rule above each group: `None` is Behind, third
/// after Normal and Dissolve, and greyed where `behind_enabled` is false (the Background, as the
/// Paint Bucket's). The list has no Clear (#12011).
fn blend_modes(behind_enabled: bool) -> Vec<Entry<Option<BlendMode>>> {
static CACHE: crate::i18n::LangCache<bool, Vec<Entry<Option<BlendMode>>>> = crate::i18n::LangCache::new();
CACHE.get_or_build(behind_enabled, || {
/// Paint Bucket's). The list has no Clear (#12011). With `on_mask` (a layer mask targeted) the
/// [`COLOUR_MODES`] are greyed too.
fn blend_modes(behind_enabled: bool, on_mask: bool) -> Vec<Entry<Option<BlendMode>>> {
static CACHE: crate::i18n::LangCache<(bool, bool), Vec<Entry<Option<BlendMode>>>> = crate::i18n::LangCache::new();
CACHE.get_or_build((behind_enabled, on_mask), || {
let mut keys: Vec<BlendKeyEntry<Option<BlendMode>>> = crate::blend_keys::tool_modes(Tool::Gradient)
.unwrap_or_default()
.into_iter()
@@ -38,11 +44,23 @@ fn blend_modes(behind_enabled: bool) -> Vec<Entry<Option<BlendMode>>> {
keys.insert(at, BlendKeyEntry::behind(None));
blend_entries(&keys, |entry| Label::from(tl!(entry.key.map_or("Behind", BlendMode::label)).to_string()))
.into_iter()
.map(|entry| if entry.value() == Some(&None) { entry.enabled(behind_enabled) } else { entry })
.map(|entry| match entry.value().copied() {
Some(None) => entry.enabled(behind_enabled),
Some(Some(mode)) if on_mask && COLOUR_MODES.contains(&mode) => entry.enabled(false),
_ => entry,
})
.collect()
})
}
/// The Classic gradient's Mode list for the current target.
pub(crate) fn mode_entries(app: &PhotocraftApp) -> Vec<Entry<Option<BlendMode>>> {
// Behind paints under a layer's pixels: greyed on a mask, a channel or Quick Mask, as the engine refuses it there.
let target = crate::canvas::paint_target(app);
let pixels = target == serde_json::json!("pixels");
blend_modes(pixels && crate::brush_mode_menu::enabled(app, crate::brush_mode_menu::Item::Behind), target == serde_json::json!("mask"))
}
/// `add` captioned `label` as a field of the bar.
fn labelled(ui: &mut egui::Ui, label: &str, mut add: impl FnMut(&mut egui::Ui)) {
Field::new(label.to_owned()).show(ui, |ui, _| add(ui));
@@ -60,9 +78,8 @@ pub(crate) fn bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
let words = Env::of(ui).words::<GradientFillWords>();
let styles: Vec<Entry<GradientStyle>> = GradientStyle::ALL.iter().map(|style| Entry::new(*style, style.label(&words)).icon(style.icon())).collect();
let modes = [Entry::new(false, tl!("Gradient").to_owned()), Entry::new(true, tl!("Classic gradient").to_owned())];
// Behind paints under a layer's pixels: greyed on a mask, a channel or Quick Mask, as the engine refuses it there.
let pixels = crate::canvas::paint_target(app) == serde_json::json!("pixels");
let blends = blend_modes(pixels && crate::brush_mode_menu::enabled(app, crate::brush_mode_menu::Item::Behind));
let blends = mode_entries(app);
let open_editor;
let presets;
{
@@ -185,3 +202,7 @@ fn preset_picker(app: &mut PhotocraftApp, ui: &mut egui::Ui, at: egui::Pos2) {
keep_popup(ui, PresetPopup { open: false, ..popup });
}
}
#[cfg(test)]
#[path = "../../gradient_mask_target_greyed_modes_tests.rs"]
mod gradient_mask_target_greyed_modes_tests;