diff --git a/crates/ui-egui/src/brush_mode_mask_target_tests.rs b/crates/ui-egui/src/brush_mode_mask_target_tests.rs new file mode 100644 index 00000000000..7c29fca6ebc --- /dev/null +++ b/crates/ui-egui/src/brush_mode_mask_target_tests.rs @@ -0,0 +1,93 @@ +//! #17638: the Brush exposes the same modes in its options bar, canvas menu and keyboard cycle. + +use craft::ui::patterns::blend_keys::BlendStep; +use craft::ui::runtime::commands::CommandRef; +use photocraft_doc::BlendMode; +use serde_json::json; + +use super::{PaintMode, mode_entries}; +use crate::brush_mode_menu; +use crate::{PhotocraftApp, state::Tool}; + +const DISABLED: [&str; 8] = ["Behind", "Clear", "Darker Color", "Lighter Color", "Hue", "Saturation", "Color", "Luminosity"]; + +fn brush_on_a_layer_with_mask() -> Box { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 80, "height": 60, "background": "white"})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app.run("layer.layerMask.revealAll", json!({})).unwrap(); + app.run("channel.target", json!({"channel": "composite"})).unwrap(); + app.ui.tool = Tool::Brush; + app +} + +fn dropdown_rows(app: &PhotocraftApp) -> Vec<(&'static str, bool)> { + mode_entries(brush_mode_menu::limits_layer_mask_modes(app, app.ui.tool)) + .iter() + .filter_map(|entry| entry.value().map(|mode: &PaintMode| (mode.text(), entry.is_enabled()))) + .collect() +} + +fn canvas_menu_rows(app: &PhotocraftApp) -> Vec<(String, bool)> { + brush_mode_menu::entries(app, app.ui.tool, false) + .iter() + .filter_map(|entry| entry.value().map(|row| (row.clone(), entry.is_enabled()))) + .filter(|(row, _)| row != "Edit Brush…") + .collect() +} + +#[test] +fn brush_layer_mask_disables_exactly_eight_modes_in_both_menus() { + let mut app = brush_on_a_layer_with_mask(); + let normal = dropdown_rows(&app); + assert!(normal.iter().all(|(_, enabled)| *enabled)); + app.run("channel.target", json!({"channel": "mask"})).unwrap(); + + let dropdown = dropdown_rows(&app); + let menu = canvas_menu_rows(&app); + assert_eq!(dropdown.iter().map(|(name, _)| *name).collect::>(), normal.iter().map(|(name, _)| *name).collect::>()); + assert_eq!(dropdown.iter().filter_map(|(name, enabled)| (!enabled).then_some(*name)).collect::>(), DISABLED); + assert_eq!(menu.iter().map(|(name, enabled)| (name.as_str(), *enabled)).collect::>(), dropdown); + + app.run("channel.target", json!({"channel": "composite"})).unwrap(); + assert_eq!(dropdown_rows(&app), normal); + assert!(canvas_menu_rows(&app).iter().all(|(_, enabled)| *enabled)); +} + +#[test] +fn brush_blend_step_skips_behind_and_clear_only_while_the_layer_mask_is_targeted() { + let mut app = brush_on_a_layer_with_mask(); + app.run("channel.target", json!({"channel": "mask"})).unwrap(); + app.run("tools.setBrush", json!({"brush": {"mode": "Dissolve", "behind": false}})).unwrap(); + let step = |app: &mut PhotocraftApp, direction| { + assert!(crate::blend_keys::fired(app, &crate::blend_keys::test_fired(CommandRef::from(direction)))); + }; + step(&mut app, BlendStep::Next); + assert_eq!(app.session.tools.brush.mode, BlendMode::Darken); + step(&mut app, BlendStep::Previous); + assert_eq!(app.session.tools.brush.mode, BlendMode::Dissolve); + + app.run("channel.target", json!({"channel": "composite"})).unwrap(); + step(&mut app, BlendStep::Next); + assert!(app.session.tools.brush.behind); +} + +#[test] +fn alpha_quick_mask_and_pencil_keep_their_mode_lists() { + let mut app = brush_on_a_layer_with_mask(); + let brush_modes = dropdown_rows(&app); + app.run("channel.new", json!({"name": "Alpha 1"})).unwrap(); + assert_eq!(dropdown_rows(&app), brush_modes); + assert!(canvas_menu_rows(&app).iter().all(|(_, enabled)| *enabled)); + + app.run("channel.target", json!({"channel": "composite"})).unwrap(); + app.run("select.editInQuickMaskMode", json!({"on": true})).unwrap(); + assert_eq!(dropdown_rows(&app), brush_modes); + assert!(canvas_menu_rows(&app).iter().all(|(_, enabled)| *enabled)); + + app.run("select.editInQuickMaskMode", json!({"on": false})).unwrap(); + app.run("channel.target", json!({"channel": "mask"})).unwrap(); + app.ui.tool = Tool::Pencil; + assert_eq!(dropdown_rows(&app), brush_modes); + assert!(canvas_menu_rows(&app).iter().all(|(_, enabled)| *enabled)); +} diff --git a/crates/ui-egui/src/brush_mode_menu.rs b/crates/ui-egui/src/brush_mode_menu.rs index a3a6a25ded5..d504c7726b5 100644 --- a/crates/ui-egui/src/brush_mode_menu.rs +++ b/crates/ui-egui/src/brush_mode_menu.rs @@ -141,8 +141,26 @@ pub(crate) fn labels(tool: Tool) -> Vec<&'static str> { items(tool).into_iter().filter_map(Item::label).collect() } -/// Is a row available? Behind and Clear need transparency: greyed on the Background layer. +/// Whether this Brush target uses the single-channel layer-mask mode list. +pub(crate) fn limits_layer_mask_modes(app: &PhotocraftApp, tool: Tool) -> bool { + tool == Tool::Brush && app.layer_mask_targeted() +} + +/// The modes a layer mask cannot take because they require transparency or colour. +pub(crate) fn allowed_on_layer_mask(item: Item) -> bool { + !matches!( + item, + Item::Behind + | Item::Clear + | Item::Blend(BlendMode::DarkerColor | BlendMode::LighterColor | BlendMode::Hue | BlendMode::Saturation | BlendMode::Color | BlendMode::Luminosity) + ) +} + +/// Is a row available for the current target? Behind and Clear also need a non-Background layer. pub(crate) fn enabled(app: &PhotocraftApp, item: Item) -> bool { + if !allowed_on_layer_mask(item) && limits_layer_mask_modes(app, app.ui.tool) { + return false; + } match item { Item::Separator => false, Item::Behind | Item::Clear => !app diff --git a/crates/ui-egui/src/panels/options_bar/paint.rs b/crates/ui-egui/src/panels/options_bar/paint.rs index e7180aa5a12..8403ae7a676 100644 --- a/crates/ui-egui/src/panels/options_bar/paint.rs +++ b/crates/ui-egui/src/panels/options_bar/paint.rs @@ -76,10 +76,10 @@ fn eraser_mode_name(mode: EraserMode) -> &'static str { /// The Mode list of the Brush and Pencil: the blend modes with Behind and Clear after Dissolve. Unlike the /// Shift+right-click menu and the Pattern Stamp's list, it keeps both on the Background, so the wheel steps through -/// them there too. -fn mode_entries() -> Vec> { - static CACHE: crate::i18n::LangCache<(), Vec>> = crate::i18n::LangCache::new(); - CACHE.get_or_build((), || { +/// them there too. A Brush targeting a layer mask disables the modes that need colour or transparency. +fn mode_entries(layer_mask: bool) -> Vec> { + static CACHE: crate::i18n::LangCache>> = crate::i18n::LangCache::new(); + CACHE.get_or_build(layer_mask, || { let keys: Vec> = brush_mode_menu::choices() .into_iter() .map(|((mode, behind), _)| match (mode, behind) { @@ -88,7 +88,22 @@ fn mode_entries() -> Vec> { (None, false) => BlendKeyEntry::clear(PaintMode::Clear), }) .collect(); - blend_entries(&keys, |entry| Label::from(tl!(entry.key.text()).to_string())) + let entries = blend_entries(&keys, |entry| Label::from(tl!(entry.key.text()).to_string())); + if !layer_mask { + return entries; + } + entries + .into_iter() + .map(|entry| { + let item = match entry.value().copied() { + Some(PaintMode::Blend(mode)) => brush_mode_menu::Item::Blend(mode), + Some(PaintMode::Behind) => brush_mode_menu::Item::Behind, + Some(PaintMode::Clear) => brush_mode_menu::Item::Clear, + _ => return entry, + }; + entry.enabled(brush_mode_menu::allowed_on_layer_mask(item)) + }) + .collect() }) } @@ -250,6 +265,7 @@ fn open_range_alert(app: &mut PhotocraftApp, ui: &egui::Ui, range: &RangeAlert) struct PaintProps<'a> { tool: PaintTool, values: PaintValues, + layer_mask_modes: bool, /// The Eraser's mode; `None` for the other tools. eraser: Option, /// The symmetry menu, for the Brush, Pencil and Eraser. @@ -291,6 +307,7 @@ pub(super) fn members(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) -> let props = PaintProps { tool: paint, values: paint_values(app, tool, &app.session.tools.brush), + layer_mask_modes: brush_mode_menu::limits_layer_mask_modes(app, tool), eraser: (tool == Tool::Eraser).then(|| eraser_mode_of(&app.ui.tool_options.eraser_mode)), symmetry: matches!(tool, Tool::Brush | Tool::Pencil | Tool::Eraser).then(|| crate::symmetry_ui::menu_input(app, ui)), out: Outbox::new(&mut sent), @@ -329,7 +346,7 @@ pub(super) fn members(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) -> /// The paint members' view: `PaintBar` and the symmetry menu. fn paint_view(props: &mut PaintProps<'_>, ui: &mut egui::Ui) { let before = props.values.clone(); - let modes = if matches!(props.tool, PaintTool::Brush | PaintTool::Pencil) { mode_entries() } else { Vec::new() }; + let modes = if matches!(props.tool, PaintTool::Brush | PaintTool::Pencil) { mode_entries(props.layer_mask_modes) } else { Vec::new() }; let mut bar = PaintBar::new(props.tool, &mut props.values).modes(&modes); if let Some(eraser) = props.eraser.as_mut() { bar = bar.eraser_mode(eraser); @@ -367,6 +384,10 @@ fn report(app: &mut PhotocraftApp, result: Result) { } } +#[cfg(test)] +#[path = "../../brush_mode_mask_target_tests.rs"] +mod brush_mode_mask_target_tests; + #[cfg(test)] #[path = "../../mixer_blend_combination_tests.rs"] mod mixer_blend_combination_tests;