From ba86552f6e9ed6a3394f66edee4b373aed678d8a Mon Sep 17 00:00:00 2001 From: dholzric Date: Tue, 1 Dec 2026 18:54:33 -0600 Subject: [PATCH] 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 --- ...gradient_mask_target_greyed_modes_tests.rs | 64 +++++++++++++++++++ crates/ui-egui/src/tools/gradient_ui/bar.rs | 35 ++++++++-- 2 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 crates/ui-egui/src/gradient_mask_target_greyed_modes_tests.rs diff --git a/crates/ui-egui/src/gradient_mask_target_greyed_modes_tests.rs b/crates/ui-egui/src/gradient_mask_target_greyed_modes_tests.rs new file mode 100644 index 0000000000..5e033dc469 --- /dev/null +++ b/crates/ui-egui/src/gradient_mask_target_greyed_modes_tests.rs @@ -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 { + 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> { + mode_entries(app).iter().filter(|e| !e.is_enabled()).filter_map(|e| e.value().copied()).collect() +} + +fn expected_on_a_mask() -> Vec> { + let mut v: Vec> = vec![None]; + v.extend(GREYED_ON_A_MASK.map(Some)); + v.sort_by_key(|m| format!("{m:?}")); + v +} + +fn sorted(mut v: Vec>) -> Vec> { + 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)); +} diff --git a/crates/ui-egui/src/tools/gradient_ui/bar.rs b/crates/ui-egui/src/tools/gradient_ui/bar.rs index 7102722973..a971b1de93 100644 --- a/crates/ui-egui/src/tools/gradient_ui/bar.rs +++ b/crates/ui-egui/src/tools/gradient_ui/bar.rs @@ -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>> { - static CACHE: crate::i18n::LangCache>>> = 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>> { + static CACHE: crate::i18n::LangCache<(bool, bool), Vec>>> = crate::i18n::LangCache::new(); + CACHE.get_or_build((behind_enabled, on_mask), || { let mut keys: Vec>> = crate::blend_keys::tool_modes(Tool::Gradient) .unwrap_or_default() .into_iter() @@ -38,11 +44,23 @@ fn blend_modes(behind_enabled: bool) -> Vec>> { 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>> { + // 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::(); let styles: Vec> = 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;