From 8d91ad712de0f2c71a7d9a7f258b2aacebe77cf9 Mon Sep 17 00:00:00 2001 From: David Mann Date: Tue, 1 Dec 2026 02:37:39 +0100 Subject: [PATCH] Color Replacement: alerts for a mask, adjustment layer, group mask or channel target (#17782) The tool recolours a pixel layer's full colour composite only. A press on a layer mask, an adjustment layer (its mask or its content), a group's mask, an alpha channel or the Quick Mask now opens the reference application's alert with OK, and paint.colorReplacement returns the same text. The target decides before the hidden, locked and 32-bit checks. Measured in RGB documents; other modes and the filter mask keep the generic refusal. Co-Authored-By: Claude Opus 5.5 --- crates/engine-core/src/hidden_target.rs | 37 +++++ crates/engine-paint/src/cmd/brush_cmds.rs | 5 + .../color_replacement_refusal_alert_tests.rs | 9 +- crates/ui-egui/src/color_replacement_tests.rs | 6 +- crates/ui-egui/src/tools/brush_refusal.rs | 10 ++ .../color_replacement_target_alerts_tests.rs | 157 ++++++++++++++++++ 6 files changed, 216 insertions(+), 8 deletions(-) create mode 100644 crates/ui-egui/src/tools/color_replacement_target_alerts_tests.rs diff --git a/crates/engine-core/src/hidden_target.rs b/crates/engine-core/src/hidden_target.rs index 8eaaed25e17..a6f1ba5d8a8 100644 --- a/crates/engine-core/src/hidden_target.rs +++ b/crates/engine-core/src/hidden_target.rs @@ -14,6 +14,14 @@ pub const HIDDEN: &str = "Could not complete your request because the target lay /// The Art History Brush's own alert text (#12638). pub const ART_HISTORY_HIDDEN: &str = "Could not use the art history brush because the target layer is hidden."; +/// The Color Replacement tool's alerts for a target it can't recolour (#17782): a pixel layer's +/// mask, an adjustment layer (its mask or its content), a group's mask, an alpha channel or the +/// Quick Mask. +pub const COLOR_REPLACEMENT_MASK: &str = "Could not use the Color Replacement tool because it only works in full color modes."; +pub const COLOR_REPLACEMENT_ADJUSTMENT: &str = "Could not use the Color Replacement tool because the target is an adjustment layer."; +pub const COLOR_REPLACEMENT_GROUP: &str = "Could not use the Color Replacement tool because the target is a group."; +pub const COLOR_REPLACEMENT_ALPHA: &str = "Could not use the Color Replacement tool because it does not work with alpha channels."; + /// Commands that paint on, or move, the target layer. const GATED: &[&str] = &[ "paint.stroke", @@ -54,6 +62,10 @@ pub fn refusal(s: &Session, id: &str, params: &Value) -> Option<&'static str> { if crate::channel_cmds::is_channel_target(¶ms) { return None; } + // The Color Replacement tool's target refusals come first: a hidden layer's mask gets them (#17782). + if id == "paint.colorReplacement" && color_replacement_target(s, ¶ms).is_some() { + return None; + } let st = s.active()?; let layer = crate::commands::layer_param(s, ¶ms).ok()?; // The Art History Brush's text was measured on a layer's pixels only. @@ -61,3 +73,28 @@ pub fn refusal(s: &Session, id: &str, params: &Value) -> Option<&'static str> { let why = if id == "paint.artHistoryBrush" && pixels { ART_HISTORY_HIDDEN } else { HIDDEN }; (st.doc.layer(layer).is_some() && !shown(&st.doc, layer)).then_some(why) } + +/// Why `paint.colorReplacement` with `params` (its `target` filled in) can't recolour its target +/// (#17782): the tool works on a pixel layer's full colour composite only. The target decides +/// before the colour mode, depth, hidden and locked checks. Measured in RGB documents only: other +/// modes, a smart object's filter mask and the masks of type, shape, fill and smart object layers +/// keep the command's generic refusal. +pub fn color_replacement_target(s: &Session, params: &Value) -> Option<&'static str> { + use crate::channel_cmds::{Target, target_of}; + let st = s.active()?; + if st.doc.mode != photocraft_doc::ColorMode::Rgb { + return None; + } + let target = target_of(params); + if matches!(target, Target::Alpha(_) | Target::QuickMask) { + return Some(COLOR_REPLACEMENT_ALPHA); + } + let layer = st.doc.layer(crate::commands::layer_param(s, params).ok()?)?; + match (&layer.content, target) { + // An adjustment layer's content is sent as its mask (the canvas's `paint_target`). + (photocraft_doc::LayerContent::Adjustment(_), Target::Mask | Target::Pixels) => Some(COLOR_REPLACEMENT_ADJUSTMENT), + (photocraft_doc::LayerContent::Group(_), Target::Mask) => Some(COLOR_REPLACEMENT_GROUP), + (photocraft_doc::LayerContent::Raster(_), Target::Mask) => Some(COLOR_REPLACEMENT_MASK), + _ => None, + } +} diff --git a/crates/engine-paint/src/cmd/brush_cmds.rs b/crates/engine-paint/src/cmd/brush_cmds.rs index d4d88cc2b34..96f20dd1424 100644 --- a/crates/engine-paint/src/cmd/brush_cmds.rs +++ b/crates/engine-paint/src/cmd/brush_cmds.rs @@ -955,6 +955,11 @@ fn mixer_load_pixel(s: &mut Session, p: &Value) -> Result { /// channel or Quick Mask `target` is refused rather than recolouring the layer instead. fn color_replacement_setup(s: &Session, p: &Value) -> Result<(Stroke, ReplaceSettings, photocraft_doc::LayerId)> { let cmd = "paint.colorReplacement"; + // A mask, adjustment layer, group mask or channel target is refused first, with the + // reference application's words (#17782). + if let Some(why) = photocraft_engine_core::hidden_target::color_replacement_target(s, p) { + return Err(EngineError::Other(why.into())); + } // The modes and depths the reference application refuses, with its words (#11923). The live // stroke runs this setup too, so it previews nothing in them. 32-bit Grayscale, CMYK and Lab, // Multichannel and Duotone weren't measured and keep painting. diff --git a/crates/ui-egui/src/color_replacement_refusal_alert_tests.rs b/crates/ui-egui/src/color_replacement_refusal_alert_tests.rs index 26beacdade9..4b166a7c702 100644 --- a/crates/ui-egui/src/color_replacement_refusal_alert_tests.rs +++ b/crates/ui-egui/src/color_replacement_refusal_alert_tests.rs @@ -18,18 +18,15 @@ fn a_hidden_target_layer() { assert_eq!(photocraft_engine::hidden_target::HIDDEN, "Could not complete your request because the target layer is hidden.", "the shared text is unchanged"); } -/// A targeted layer mask on a hidden layer keeps the status-bar refusal: only a pixel target gets the alert (CodeRabbit -/// on #16279). +/// A targeted layer mask on a hidden layer gets the mask alert, not the hidden one: the target decides first (#17782). #[test] -fn a_hidden_layer_with_a_mask_target_keeps_the_status_bar_message() { +fn a_hidden_layer_with_a_mask_target_gets_the_mask_alert() { let mut app = app(Tool::ColorReplacement); app.run("layer.layerMask.revealAll", json!({})).unwrap(); let id = active(&app); app.run("layer.setProps", json!({"layer": id, "visible": false})).unwrap(); app.ui.mask_target = true; - stroke(&mut app); - assert!(alerts(&app).is_empty(), "no alert: {:?}", alerts(&app)); - assert!(app.ui.status_error, "the status bar says it: {:?}", app.ui.status); + refused(&mut app, "Could not use the Color Replacement tool because it only works in full color modes."); } #[test] diff --git a/crates/ui-egui/src/color_replacement_tests.rs b/crates/ui-egui/src/color_replacement_tests.rs index 5c8902178c0..4b709ffc73e 100644 --- a/crates/ui-egui/src/color_replacement_tests.rs +++ b/crates/ui-egui/src/color_replacement_tests.rs @@ -154,7 +154,7 @@ fn live_stroke_at(depth: u32) { assert!(greened(rgba(&app, 20, 20)), "redo reapplies the stroke"); } -/// A targeted layer mask isn't recoloured, and neither is the layer behind it. +/// A targeted layer mask isn't recoloured, and neither is the layer behind it: the press opens the mask alert. #[test] fn a_mask_target_is_refused_without_painting() { let mut app = app(8); @@ -167,7 +167,9 @@ fn a_mask_target_is_refused_without_painting() { tool_event(&mut app, ToolEvent::Down { x: 10.0, y: 20.0, pressure: 1.0 }, m); tool_event(&mut app, ToolEvent::Move { x: 30.0, y: 20.0, pressure: 1.0 }, m); tool_event(&mut app, ToolEvent::Up { x: 30.0, y: 20.0 }, m); - assert!(app.ui.status_error && app.ui.status.contains("layer pixels only"), "{}", app.ui.status); + // The reference application's alert, not a status-bar message (#17782). + let alerts: Vec<_> = app.framework.dialogs.open_dialogs().iter().filter_map(|d| d.fields.as_ref()?.get("message")?.as_str().map(str::to_string)).collect(); + assert_eq!(alerts, ["Could not use the Color Replacement tool because it only works in full color modes."]); assert_eq!(rgba(&app, 20, 20), RED); assert_eq!(app.session.active().unwrap().history.past_len(), steps); } diff --git a/crates/ui-egui/src/tools/brush_refusal.rs b/crates/ui-egui/src/tools/brush_refusal.rs index 6b89be9eff2..8a20a009a56 100644 --- a/crates/ui-egui/src/tools/brush_refusal.rs +++ b/crates/ui-egui/src/tools/brush_refusal.rs @@ -100,6 +100,12 @@ pub fn alert(app: &PhotocraftApp, tool: Tool, hidden: bool) -> Option { if hidden && spot_healing_quiet(app, tool) { return None; } + // The Color Replacement tool's target alerts come before the hidden and locked ones (#17782). + if tool == Tool::ColorReplacement + && let Some(why) = photocraft_engine::hidden_target::color_replacement_target(&app.session, &json!({"target": crate::canvas::paint_target(app)})) + { + return Some(tl!(why).to_owned()); + } let name = tool_name(app, tool)?; // The Art History Brush's (#12638) and the Color Replacement tool's (#11924) hidden alerts were measured on a // layer's pixels only: a hidden mask or channel target keeps the status bar. @@ -222,6 +228,10 @@ mod smudge_refusal_alert_tests; #[path = "../color_replacement_refusal_alert_tests.rs"] mod color_replacement_refusal_alert_tests; +#[cfg(test)] +#[path = "color_replacement_target_alerts_tests.rs"] +mod color_replacement_target_alerts_tests; + #[cfg(test)] #[path = "remove_hidden_layer_tests.rs"] mod remove_hidden_layer_tests; diff --git a/crates/ui-egui/src/tools/color_replacement_target_alerts_tests.rs b/crates/ui-egui/src/tools/color_replacement_target_alerts_tests.rs new file mode 100644 index 00000000000..19fa3a93e1b --- /dev/null +++ b/crates/ui-egui/src/tools/color_replacement_target_alerts_tests.rs @@ -0,0 +1,157 @@ +//! #17782: the Color Replacement tool recolours a pixel layer's full colour composite only. A press on a layer mask, +//! an adjustment layer (its mask or its content), a group's mask, an alpha channel or the Quick Mask opens the +//! reference application's alert with OK and changes nothing; the target decides before the hidden, locked and 32-bit +//! checks. A vector-mask-only layer still paints its pixels. + +use serde_json::json; + +use super::brush_refusal_alert_tests::{active, alerts, app, refused, steps, stroke}; +use super::*; +use crate::control::{ControlRequest, Reply, handle}; + +const MASK: &str = "Could not use the Color Replacement tool because it only works in full color modes."; +const ADJUSTMENT: &str = "Could not use the Color Replacement tool because the target is an adjustment layer."; +const GROUP: &str = "Could not use the Color Replacement tool because the target is a group."; +const ALPHA: &str = "Could not use the Color Replacement tool because it does not work with alpha channels."; + +/// The fixture's painted layer with a Reveal All mask, the mask targeted. +fn masked() -> Box { + let mut app = app(Tool::ColorReplacement); + app.run("layer.layerMask.revealAll", json!({})).unwrap(); + app.ui.mask_target = true; + app.sync_views(); + app +} + +/// A Levels adjustment layer above the painted layer, its Reveal All mask targeted. +fn levels() -> Box { + let mut app = app(Tool::ColorReplacement); + app.run("layer.newAdjustmentLayer.levels", json!({})).unwrap(); + app.sync_views(); + assert!(app.ui.mask_target, "a new adjustment layer targets its mask"); + app +} + +/// The error `paint.colorReplacement` returns for a short stroke with `target`. +fn command_error(app: &mut PhotocraftApp, target: serde_json::Value) -> String { + let before = steps(app); + let err = app.session.execute("paint.colorReplacement", json!({"points": [[20, 32], [44, 32]], "target": target})).unwrap_err().to_string(); + assert_eq!(steps(app), before, "no History state"); + err +} + +#[test] +fn a_layer_mask() { + let mut app = masked(); + refused(&mut app, MASK); + assert!(!app.ui.status_error, "no status-bar error as well: {:?}", app.ui.status); +} + +#[test] +fn a_disabled_layer_mask() { + let mut app = masked(); + let id = active(&app); + app.run("layer.layerMask.enabled", json!({"layer": id, "enabled": false})).unwrap(); + app.ui.mask_target = true; + refused(&mut app, MASK); +} + +/// The target decides first: a hidden or locked layer's mask and a mask in a 32-bit document get the mask text. +#[test] +fn a_mask_on_a_hidden_or_locked_layer_or_in_32_bit() { + let mut app = masked(); + let id = active(&app); + app.run("layer.setProps", json!({"layer": id, "visible": false})).unwrap(); + app.ui.mask_target = true; + refused(&mut app, MASK); + assert_eq!(command_error(&mut app, json!("mask")), MASK); + + let mut app = masked(); + app.run("layer.lockLayers", json!({"all": true})).unwrap(); + app.ui.mask_target = true; + refused(&mut app, MASK); + assert_eq!(command_error(&mut app, json!("mask")), MASK); + + let mut app = masked(); + app.run("image.mode.bits32", json!({})).unwrap(); + app.ui.mask_target = true; + refused(&mut app, MASK); + assert_eq!(command_error(&mut app, json!("mask")), MASK); +} + +#[test] +fn an_adjustment_layer_mask_and_content() { + let mut app = levels(); + refused(&mut app, ADJUSTMENT); + app.run("channel.target.composite", json!({})).unwrap(); + assert!(!app.ui.mask_target, "the content is targeted"); + refused(&mut app, ADJUSTMENT); + assert_eq!(command_error(&mut app, json!("mask")), ADJUSTMENT); + assert_eq!(command_error(&mut app, json!("pixels")), ADJUSTMENT); +} + +#[test] +fn a_hidden_adjustment_layer_mask() { + let mut app = levels(); + let id = active(&app); + app.run("layer.setProps", json!({"layer": id, "visible": false})).unwrap(); + app.ui.mask_target = true; + refused(&mut app, ADJUSTMENT); + assert_eq!(command_error(&mut app, json!("mask")), ADJUSTMENT); +} + +#[test] +fn a_group_mask() { + let mut app = app(Tool::ColorReplacement); + app.run("layer.new.group", json!({})).unwrap(); + app.run("layer.layerMask.revealAll", json!({})).unwrap(); + app.ui.mask_target = true; + app.sync_views(); + refused(&mut app, GROUP); + assert_eq!(command_error(&mut app, json!("mask")), GROUP); +} + +#[test] +fn an_alpha_channel_and_the_quick_mask() { + let mut alpha = app(Tool::ColorReplacement); + alpha.run("channel.new", json!({"name": "Alpha", "fill": "white"})).unwrap(); + alpha.run("channel.target", json!({"channel": {"alpha": 0}})).unwrap(); + refused(&mut alpha, ALPHA); + assert_eq!(command_error(&mut alpha, json!({"channel": 0})), ALPHA); + + let mut quick = app(Tool::ColorReplacement); + quick.run("select.editInQuickMaskMode", json!({})).unwrap(); + quick.sync_views(); + refused(&mut quick, ALPHA); + assert_eq!(command_error(&mut quick, json!("quickMask")), ALPHA); +} + +#[test] +fn the_alert_is_listed_and_confirmed_over_the_control_channel() { + let ctx = egui::Context::default(); + let mut app = masked(); + stroke(&mut app); + let mut call = |method: &str, params: serde_json::Value| { + let (req, _rx) = ControlRequest::new(method, params); + match handle(&mut app, &ctx, &req) { + Reply::Done(v) => v, + _ => panic!("{method}: expected an immediate reply"), + } + }; + let inspect = call("ui.inspect", json!({})); + let dialog = &inspect["result"]["dialogs"][0]; + assert_eq!(dialog["fields"]["message"], MASK, "{inspect}"); + assert_eq!(call("ui.dialog.confirm", json!({"dialog": dialog["id"]}))["ok"], true); + assert!(alerts(&app).is_empty()); +} + +#[test] +fn a_vector_mask_only_layer_still_paints_its_pixels() { + let mut app = app(Tool::ColorReplacement); + app.run("layer.vectorMask.revealAll", json!({})).unwrap(); + app.sync_views(); + let before = steps(&app); + stroke(&mut app); + assert!(alerts(&app).is_empty(), "no alert: {:?}", alerts(&app)); + assert_eq!(steps(&app), before + 1, "the stroke was applied"); +}