Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

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

buivanvu94865c8a15f848
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs
index 848cdffd3..35974fc23 100644
--- a/crates/engine/src/commands.rs
+++ b/crates/engine/src/commands.rs
@@ -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)
diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs
index 3827ad9a3..cb997bf29 100644
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@ -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)
diff --git a/crates/ui-egui/src/channels_panel.rs b/crates/ui-egui/src/channels_panel.rs
index ba1051cdd..26c6a412f 100644
--- a/crates/ui-egui/src/channels_panel.rs
+++ b/crates/ui-egui/src/channels_panel.rs
@@ -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" {
diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs
index 2e78698df..889426960 100644
--- a/crates/ui-egui/src/control.rs
+++ b/crates/ui-egui/src/control.rs
@@ -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 {
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 2b98aada7..e5e3c43b1 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -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;
diff --git a/crates/ui-egui/src/mask_thumbs_ui/tests.rs b/crates/ui-egui/src/mask_thumbs_ui/tests.rs
index f92f5546a..3fd0904eb 100644
--- a/crates/ui-egui/src/mask_thumbs_ui/tests.rs
+++ b/crates/ui-egui/src/mask_thumbs_ui/tests.rs
@@ -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);
+}
diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs
index 5c3b200d0..ccebc5a81 100644
--- a/crates/ui-egui/src/panels.rs
+++ b/crates/ui-egui/src/panels.rs
@@ -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() {
diff --git a/crates/ui-egui/src/rasterize_prompt_tests.rs b/crates/ui-egui/src/rasterize_prompt_tests.rs
index 108397cc3..8eb2dfa58 100644
--- a/crates/ui-egui/src/rasterize_prompt_tests.rs
+++ b/crates/ui-egui/src/rasterize_prompt_tests.rs
@@ -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
}