fix(ui): toggle color swatches when selecting or deselecting a layer mask (#2560)

* fix(ui): toggle color swatches when selecting or deselecting a layer mask (#2420)

- Call sync_mask_targets() when handling ui.maskTarget / ui.vectorMaskTarget
  clicks in the Layers panel, Channels panel, and control channel
- Automatically target the layer mask when creating new adjustment or fill layers
- In tools.defaultColors, reset to mask default colors (white/black) when a
  mask is currently targeted
- Add unit test in engine and UI harness test in mask_thumbs_ui

* rustfmt

* Rasterize prompt test: target the fill layer itself, now that a new fill layer targets its mask

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: huudinh1401 <huudinh1401@gmail.com>
Co-authored-by: Brandon Thomas <echelon@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
authored and GitHub committed 2026-10-10 13:55:43 -04:00
1 parent ff9663bc9a
commit 865c8a15f8
8 files changed
+94 -4

No files matched your search

+7 -2
View File
@@ -891,8 +891,13 @@ fn build() -> Vec<CommandSpec> {
Ok(Value::Null)
}),
cmd!("tools.defaultColors", "Default Foreground and Background Colors", [], Some("D"), "{}", always, |s, _| {
s.tools.foreground = [0.0, 0.0, 0.0, 1.0];
s.tools.background = [1.0, 1.0, 1.0, 1.0];
if s.tools.mask_targeted {
s.tools.foreground = crate::ToolState::MASK_COLORS[0];
s.tools.background = crate::ToolState::MASK_COLORS[1];
} else {
s.tools.foreground = [0.0, 0.0, 0.0, 1.0];
s.tools.background = [1.0, 1.0, 1.0, 1.0];
}
Ok(Value::Null)
}),
// Queries (not journaled)
+18
View File
@@ -629,6 +629,24 @@ fn fill_layers_and_colors() {
assert!(l < r);
}
#[test]
fn default_colors_with_mask_targeted_resets_to_mask_colors() {
let mut s = Session::new();
s.tools.foreground = [0.2, 0.4, 0.6, 1.0];
s.tools.background = [0.8, 0.6, 0.4, 1.0];
s.tools.target_mask(true);
s.execute("tools.setColors", json!({"foreground": "#112233", "background": "#445566"})).unwrap();
s.execute("tools.defaultColors", json!({})).unwrap();
assert_eq!(s.tools.foreground, [1.0, 1.0, 1.0, 1.0]);
assert_eq!(s.tools.background, [0.0, 0.0, 0.0, 1.0]);
s.tools.target_mask(false);
assert_eq!(s.tools.foreground, [0.2, 0.4, 0.6, 1.0]);
assert_eq!(s.tools.background, [0.8, 0.6, 0.4, 1.0]);
s.execute("tools.defaultColors", json!({})).unwrap();
assert_eq!(s.tools.foreground, [0.0, 0.0, 0.0, 1.0]);
assert_eq!(s.tools.background, [1.0, 1.0, 1.0, 1.0]);
}
#[test]
fn fill_layer_pixel_rewrites_refresh_the_effect_maps() {
// A pixel-only rewrite of a fill layer's cache (what Image › Transform and friends do)
+1
View File
@@ -293,6 +293,7 @@ pub fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
ctx.data_mut(|d| d.insert_temp(thumbs_id(), drawn));
if let Some(on) = mask_click {
app.ui.mask_target = on;
app.sync_mask_targets();
}
for (id, p) in actions {
if id == "ui.renameChannel" {
+2
View File
@@ -540,10 +540,12 @@ fn dispatch(app: &mut PhotocraftApp, ctx: &egui::Context, req: &ControlRequest)
if let Some(m) = mask_target {
app.ui.mask_target = m;
app.ui.vector_mask_target &= !m;
app.sync_mask_targets();
}
if let Some(m) = vector_mask_target {
app.ui.vector_mask_target = m;
app.ui.mask_target &= !m;
app.sync_mask_targets();
}
// Selection tools' options-bar mode: 0 New, 1 Add, 2 Subtract, 3 Intersect.
if let Some(m) = selection_mode {
+3 -2
View File
@@ -839,7 +839,8 @@ impl PhotocraftApp {
} else {
jobs_ui::run(self, id, params)
};
if r.is_ok() && ADDS_LAYER_MASK.contains(&id) {
let creates_adjustment_or_fill = id.starts_with("layer.newAdjustmentLayer.") || id.starts_with("layer.newFillLayer.");
if r.is_ok() && (ADDS_LAYER_MASK.contains(&id) || creates_adjustment_or_fill) {
// Adding a layer mask targets it, as in Photoshop (#2166).
self.ui.mask_target = true;
self.ui.vector_mask_target = false;
@@ -1502,7 +1503,7 @@ impl PhotocraftApp {
/// Viewing a layer mask (#196) targets it; a vector-mask target needs a vector mask on the
/// active layer (a shape layer's path is its content, not a mask). Targeting a mask or the
/// pixels brings back that target's foreground/background pair, as in Photoshop (#2166).
fn sync_mask_targets(&mut self) {
pub(crate) fn sync_mask_targets(&mut self) {
if let Some(st) = self.session.active() {
if photocraft_engine::mask_view_cmds::current(st).is_some() {
self.ui.mask_target = true;
@@ -551,3 +551,60 @@ fn command_i_inverts_the_targeted_mask() {
let mask = &st.doc.layer(st.active_layer.unwrap()).unwrap().mask.as_ref().unwrap().surface;
assert_eq!(mask.sample_channel(10, 10, 0), 0.0, "reveal all → hide all");
}
/// #2420: Selecting a layer mask in the Layers panel toggles the color swatches
/// to the mask's colors (white/black by default), and deselecting toggles back.
#[test]
fn selecting_mask_thumbnail_toggles_swatches_to_mask_colors_and_back() {
const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0];
const BLUE: [f32; 4] = [0.0, 0.0, 1.0, 1.0];
const WHITE: [f32; 4] = [1.0, 1.0, 1.0, 1.0];
const BLACK: [f32; 4] = [0.0, 0.0, 0.0, 1.0];
let (mut s, masked, _) = dotted();
s.execute("tools.setColors", json!({"foreground": "#ff0000", "background": "#0000ff"})).unwrap();
let mut h = harness(s, 0, 1.0, 290.0);
// Initial state: layer pixels targeted, red foreground, blue background.
assert!(!h.state().ui.mask_target);
assert_eq!(h.state().session.tools.foreground, RED);
assert_eq!(h.state().session.tools.background, BLUE);
// 1. Click mask thumbnail in Layers panel -> targets mask, swatches toggle to mask colors.
let r_mask = mask_rect(&h, masked, MaskKind::Pixel);
click_with(&mut h, r_mask.center(), Modifiers::NONE);
assert!(h.state().ui.mask_target, "mask thumbnail clicked: mask is targeted");
assert_eq!(h.state().session.tools.foreground, WHITE);
assert_eq!(h.state().session.tools.background, BLACK);
// 2. Click layer thumbnail -> deselects mask, swatches toggle back to pixel colors.
let r_layer = layer_thumb_center(&h, masked);
click_with(&mut h, r_layer, Modifiers::NONE);
assert!(!h.state().ui.mask_target, "layer thumbnail clicked: pixels are targeted");
assert_eq!(h.state().session.tools.foreground, RED);
assert_eq!(h.state().session.tools.background, BLUE);
// 3. Click mask thumbnail again -> swatches toggle back to mask colors.
click_with(&mut h, r_mask.center(), Modifiers::NONE);
assert!(h.state().ui.mask_target);
assert_eq!(h.state().session.tools.foreground, WHITE);
assert_eq!(h.state().session.tools.background, BLACK);
// 4. Changing mask colors preserves them when switching away and back.
h.state_mut().run("tools.setColors", json!({"foreground": "#808080"})).unwrap();
let grey = h.state().session.tools.foreground;
assert_eq!(grey, [128.0 / 255.0, 128.0 / 255.0, 128.0 / 255.0, 1.0]);
click_with(&mut h, r_layer, Modifiers::NONE);
assert_eq!(h.state().session.tools.foreground, RED, "pixel colors restored");
click_with(&mut h, r_mask.center(), Modifiers::NONE);
assert_eq!(h.state().session.tools.foreground, grey, "mask colors restored");
// 5. Creating a new adjustment layer targets its mask and toggles to mask colors.
click_with(&mut h, r_layer, Modifiers::NONE);
assert_eq!(h.state().session.tools.foreground, RED);
h.state_mut().run("layer.newAdjustmentLayer.curves", json!({})).unwrap();
assert!(h.state().ui.mask_target, "new adjustment layer targets its mask");
assert_eq!(h.state().session.tools.foreground, grey);
}
+2
View File
@@ -2043,11 +2043,13 @@ fn layers(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
if id == "ui.maskTarget" {
app.ui.mask_target = p.as_bool().unwrap_or(false);
app.ui.vector_mask_target = false;
app.sync_mask_targets();
continue;
}
if id == "ui.vectorMaskTarget" {
app.ui.vector_mask_target = p.as_bool().unwrap_or(false);
app.ui.mask_target = false;
app.sync_mask_targets();
continue;
}
if p.is_null() {
@@ -20,6 +20,10 @@ fn app_with(kind: Kind) -> PhotocraftApp {
Kind::Fill => app.run("layer.newFillLayer.solidColor", json!({"color": "#00ff00"})).map(|_| ()),
}
.unwrap();
// A new fill layer targets its mask (Photoshop); painting its content means targeting the
// layer itself, as a click on its thumbnail does.
app.ui.mask_target = false;
app.sync_mask_targets();
app.run("tools.setColors", json!({"foreground": "#0000ff"})).unwrap();
app
}