From 59cb14c0718a6c74f28b11de001d223ac7d45c5c Mon Sep 17 00:00:00 2001 From: David Mann Date: Mon, 30 Nov 2026 04:20:00 +0100 Subject: [PATCH 1/3] WIP: Quick Mask: own foreground/background pair (#17490) From ccd339273f405fdfb76b87c6e268d9c156cbc29a Mon Sep 17 00:00:00 2001 From: David Mann Date: Mon, 30 Nov 2026 04:35:12 +0100 Subject: [PATCH 2/3] Quick Mask: own foreground/background pair (#17490) Quick Mask keeps its own swatch pair, white / black to start, kept across Quick Mask sessions; leaving gives the normal pair back unchanged. The pair follows the active document's Quick Mask state (undo, document switch), is not stored with the preferences, shows grey in the toolbar's colour chips, and Edit > Fill's Foreground / Background take it. Co-Authored-By: Claude Opus 5.5 --- .../src/cmd/channel_cmds/selection.rs | 2 + crates/engine-core/src/doc_state.rs | 40 +++++++++ .../src/quick_mask_swatch_pair_tests.rs | 87 +++++++++++++++++++ crates/engine-core/src/session/documents.rs | 8 ++ crates/engine-core/src/session/execute.rs | 2 + crates/engine/tests/it/main.rs | 1 + .../tests/it/quick_mask_swatch_pair_fill.rs | 24 +++++ crates/ui-egui/src/app/targets.rs | 1 + crates/ui-egui/src/panels/toolbar.rs | 12 ++- crates/ui-egui/src/tool_memory.rs | 10 ++- 10 files changed, 182 insertions(+), 5 deletions(-) create mode 100644 crates/engine-core/src/quick_mask_swatch_pair_tests.rs create mode 100644 crates/engine/tests/it/quick_mask_swatch_pair_fill.rs diff --git a/crates/engine-core/src/cmd/channel_cmds/selection.rs b/crates/engine-core/src/cmd/channel_cmds/selection.rs index f5b1dc3f30..e3ee2b73a0 100644 --- a/crates/engine-core/src/cmd/channel_cmds/selection.rs +++ b/crates/engine-core/src/cmd/channel_cmds/selection.rs @@ -182,5 +182,7 @@ pub(super) fn quick_mask(s: &mut Session, p: &Value) -> Result { Ok(()) })?; } + // Quick Mask has its own swatch pair (#17490). + s.sync_tools_quick_mask(); Ok(json!({ "quickMask": want })) } diff --git a/crates/engine-core/src/doc_state.rs b/crates/engine-core/src/doc_state.rs index 8ac2581f79..8f1ce4140c 100644 --- a/crates/engine-core/src/doc_state.rs +++ b/crates/engine-core/src/doc_state.rs @@ -392,6 +392,11 @@ pub struct ToolState { /// and the alternate pair used while targeting masks. /// Each association also records its RGB stand-in. pub cmyk_roles: CmykRoles, + /// The active document is in Quick Mask ([`ToolState::quick_mask`]). + pub quick_mask_on: bool, + /// The pair Quick Mask put aside, with its CMYK roles: the Quick Mask pair outside Quick + /// Mask, the pixel (or layer mask) pair inside it (#17490). + pub quick_mask_aside: ([[f32; 4]; 2], [Option<([f32; 3], [f32; 4])>; 2]), } /// Native CMYK inks associated with each color role. @@ -483,12 +488,41 @@ impl ToolState { return false; } self.mask_targeted = mask; + // In Quick Mask the pixel or mask pair is the one Quick Mask put aside. + if self.quick_mask_on { + let (pair, roles) = &mut self.quick_mask_aside; + std::mem::swap(&mut self.other_colors, pair); + std::mem::swap(&mut self.cmyk_roles[1], roles); + return true; + } self.cmyk_roles.swap(0, 1); let [fg, bg] = std::mem::replace(&mut self.other_colors, [self.foreground, self.background]); self.foreground = fg; self.background = bg; true } + + /// Enter or leave Quick Mask. As in the reference application, Quick Mask keeps its own + /// app-wide foreground/background pair (white / black until one is picked): entering puts + /// the current pair aside and brings the Quick Mask pair back, leaving restores the pair + /// unchanged (#17490). Not kept between sessions. Returns whether the state changed. + pub fn quick_mask(&mut self, on: bool) -> bool { + if self.quick_mask_on == on { + return false; + } + self.quick_mask_on = on; + let (pair, roles) = &mut self.quick_mask_aside; + std::mem::swap(&mut self.foreground, &mut pair[0]); + std::mem::swap(&mut self.background, &mut pair[1]); + std::mem::swap(&mut self.cmyk_roles[0], roles); + true + } + + /// The current pair and its CMYK roles as they are outside Quick Mask: the pair Quick Mask + /// put aside while it is on, for what stores the swatches. + pub fn outer_pair(&self) -> ([[f32; 4]; 2], [Option<([f32; 3], [f32; 4])>; 2]) { + if self.quick_mask_on { self.quick_mask_aside } else { ([self.foreground, self.background], self.cmyk_roles[0]) } + } } impl Default for ToolState { @@ -512,6 +546,8 @@ impl Default for ToolState { other_colors: Self::MASK_COLORS, cmyk_picks: Vec::new(), cmyk_roles: [[None; 2]; 2], + quick_mask_on: false, + quick_mask_aside: (Self::MASK_COLORS, [None; 2]), } } } @@ -552,6 +588,10 @@ pub fn selected_in(doc: &Document, active: Option, selected: &[LayerId] ids } +#[cfg(test)] +#[path = "quick_mask_swatch_pair_tests.rs"] +mod quick_mask_swatch_pair_tests; + #[cfg(test)] mod selected_tests { use super::selected_in; diff --git a/crates/engine-core/src/quick_mask_swatch_pair_tests.rs b/crates/engine-core/src/quick_mask_swatch_pair_tests.rs new file mode 100644 index 0000000000..ea48a26ee1 --- /dev/null +++ b/crates/engine-core/src/quick_mask_swatch_pair_tests.rs @@ -0,0 +1,87 @@ +//! Quick Mask keeps its own foreground/background pair (#17490): white / black until one is +//! picked, kept across Quick Mask sessions, and leaving gives the normal pair back unchanged. + +use serde_json::json; + +use crate::{Session, ToolState}; + +const RED: [f32; 4] = [200.0 / 255.0, 30.0 / 255.0, 30.0 / 255.0, 1.0]; +const BLUE: [f32; 4] = [30.0 / 255.0, 30.0 / 255.0, 200.0 / 255.0, 1.0]; +const GREEN: [f32; 4] = [0.0, 200.0 / 255.0, 0.0, 1.0]; + +fn quick_mask(s: &mut Session, on: bool) { + s.execute("select.editInQuickMaskMode", json!({ "on": on })).unwrap(); +} + +fn pair(s: &Session) -> [[f32; 4]; 2] { + [s.tools.foreground, s.tools.background] +} + +fn session_with_colors() -> Session { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 8, "height": 8})).unwrap(); + s.execute("tools.setColors", json!({"foreground": RED, "background": BLUE})).unwrap(); + s +} + +#[test] +fn entering_switches_to_a_white_black_pair_and_leaving_restores_the_normal_one() { + let mut s = session_with_colors(); + quick_mask(&mut s, true); + assert_eq!(pair(&s), ToolState::MASK_COLORS); + quick_mask(&mut s, false); + assert_eq!(pair(&s), [RED, BLUE]); +} + +#[test] +fn a_colour_set_in_quick_mask_is_kept_for_the_next_quick_mask_session() { + let mut s = session_with_colors(); + quick_mask(&mut s, true); + s.execute("tools.setColors", json!({"foreground": GREEN})).unwrap(); + quick_mask(&mut s, false); + assert_eq!(pair(&s), [RED, BLUE], "the normal pair is untouched"); + quick_mask(&mut s, true); + assert_eq!(pair(&s), [GREEN, ToolState::MASK_COLORS[1]]); +} + +#[test] +fn undoing_the_enter_step_gives_the_normal_pair_back() { + let mut s = session_with_colors(); + quick_mask(&mut s, true); + s.execute("edit.undo", json!({})).unwrap(); + assert!(s.active().unwrap().doc.quick_mask.is_none()); + assert_eq!(pair(&s), [RED, BLUE]); + s.execute("edit.redo", json!({})).unwrap(); + assert_eq!(pair(&s), ToolState::MASK_COLORS); +} + +#[test] +fn switching_to_a_document_outside_quick_mask_shows_the_normal_pair() { + let mut s = session_with_colors(); + quick_mask(&mut s, true); + s.execute("file.new", json!({"width": 8, "height": 8})).unwrap(); + assert_eq!(pair(&s), [RED, BLUE]); + assert!(s.set_active(0)); + assert_eq!(pair(&s), ToolState::MASK_COLORS); +} + +#[test] +fn a_layer_mask_target_change_inside_quick_mask_keeps_the_pairs_apart() { + let mut s = session_with_colors(); + quick_mask(&mut s, true); + s.execute("tools.setColors", json!({"foreground": GREEN})).unwrap(); + s.tools.target_mask(true); + assert_eq!(s.tools.foreground, GREEN, "the Quick Mask pair stays on the swatches"); + s.tools.target_mask(false); + quick_mask(&mut s, false); + assert_eq!(pair(&s), [RED, BLUE]); + assert_eq!(s.tools.other_colors, ToolState::MASK_COLORS); +} + +#[test] +fn the_stored_pair_is_the_one_outside_quick_mask() { + let mut s = session_with_colors(); + quick_mask(&mut s, true); + s.execute("tools.setColors", json!({"foreground": GREEN})).unwrap(); + assert_eq!(s.tools.outer_pair().0, [RED, BLUE]); +} diff --git a/crates/engine-core/src/session/documents.rs b/crates/engine-core/src/session/documents.rs index 8c358493a9..1b2628fc47 100644 --- a/crates/engine-core/src/session/documents.rs +++ b/crates/engine-core/src/session/documents.rs @@ -134,6 +134,7 @@ impl Session { self.active = Some(index); self.note_active_change(); self.sync_tools_mask_target(); + self.sync_tools_quick_mask(); true } else { false @@ -166,6 +167,13 @@ impl Session { self.tools.target_mask(mask); } + /// Switch the swatches to the Quick Mask pair while the active document is in Quick Mask and + /// back otherwise ([`ToolState::quick_mask`](crate::ToolState::quick_mask)). + pub fn sync_tools_quick_mask(&mut self) { + let on = self.active().is_some_and(|st| st.doc.quick_mask.is_some()); + self.tools.quick_mask(on); + } + /// Move the document at `from` to tab position `to` (clamped to the last), keeping the active /// document active. Returns its new index; `None` when `from` is out of range. pub fn move_document(&mut self, from: usize, to: usize) -> Option { diff --git a/crates/engine-core/src/session/execute.rs b/crates/engine-core/src/session/execute.rs index 7df0e97e86..f22f31e193 100644 --- a/crates/engine-core/src/session/execute.rs +++ b/crates/engine-core/src/session/execute.rs @@ -15,6 +15,8 @@ impl Session { let before = self.active_layer_stamp(); let result = self.dispatch(id, params, false); self.note_layer_change(before); + // Undo, a document switch or a close can enter or leave Quick Mask too. + self.sync_tools_quick_mask(); match result? { jobs::Started::Done(v) => Ok(v), // Not reached: inline dispatch never starts a job. Waiting is still correct. diff --git a/crates/engine/tests/it/main.rs b/crates/engine/tests/it/main.rs index 652e368a46..3a68af2421 100644 --- a/crates/engine/tests/it/main.rs +++ b/crates/engine/tests/it/main.rs @@ -178,6 +178,7 @@ mod pixel_lock_edits; mod place_steps_measure; mod precheck; mod prefs_usage; +mod quick_mask_swatch_pair_fill; mod rasterize_psd_reopen; mod rasterize_selected_layers; mod rendering_preferences; diff --git a/crates/engine/tests/it/quick_mask_swatch_pair_fill.rs b/crates/engine/tests/it/quick_mask_swatch_pair_fill.rs new file mode 100644 index 0000000000..8721a9fafb --- /dev/null +++ b/crates/engine/tests/it/quick_mask_swatch_pair_fill.rs @@ -0,0 +1,24 @@ +//! Edit › Fill's Foreground and Background in Quick Mask take the Quick Mask pair, not the +//! normal pair converted to grey (#17490). + +use photocraft_engine::Session; +use serde_json::json; + +fn quick_mask_value(s: &Session) -> f32 { + let doc = &s.active().unwrap().doc; + doc.quick_mask.as_ref().unwrap().surface.read_region(doc.bounds())[0] +} + +#[test] +fn fill_foreground_and_background_in_quick_mask_use_the_quick_mask_pair() { + let mut s = Session::new(); + s.execute("file.new", json!({"width": 8, "height": 8})).unwrap(); + let (red, blue) = ([200.0 / 255.0, 30.0 / 255.0, 30.0 / 255.0, 1.0], [30.0 / 255.0, 30.0 / 255.0, 200.0 / 255.0, 1.0]); + s.execute("tools.setColors", json!({"foreground": red, "background": blue})).unwrap(); + s.execute("select.editInQuickMaskMode", json!({"on": true})).unwrap(); + // The Quick Mask pair starts white / black. + s.execute("edit.fill", json!({"contents": "background"})).unwrap(); + assert!(quick_mask_value(&s) < 1.0 / 255.0, "background fills black"); + s.execute("edit.fill", json!({"contents": "foreground"})).unwrap(); + assert!(quick_mask_value(&s) > 254.0 / 255.0, "foreground fills white"); +} diff --git a/crates/ui-egui/src/app/targets.rs b/crates/ui-egui/src/app/targets.rs index 51fac77346..de69f24ef0 100644 --- a/crates/ui-egui/src/app/targets.rs +++ b/crates/ui-egui/src/app/targets.rs @@ -197,5 +197,6 @@ impl PhotocraftApp { } let mask = self.layer_mask_targeted() || self.filter_mask_targeted(); self.session.tools.target_mask(mask); + self.session.sync_tools_quick_mask(); } } diff --git a/crates/ui-egui/src/panels/toolbar.rs b/crates/ui-egui/src/panels/toolbar.rs index eace041f50..c2144ddb08 100644 --- a/crates/ui-egui/src/panels/toolbar.rs +++ b/crates/ui-egui/src/panels/toolbar.rs @@ -380,9 +380,17 @@ fn show_drawer( /// column. fn chip_pair<'a>(app: &PhotocraftApp, active: &'a mut Well) -> SwatchPair<'a> { let tools = &app.session.tools; + // In Quick Mask the chips show the Quick Mask pair in grey, the tone it paints the mask with (#17490). + let shown = |c: [f32; 4]| { + if !tools.quick_mask_on { + return c; + } + let g = photocraft_color::convert::rgb_to_gray([c[0], c[1], c[2]]).clamp(0.0, 1.0); + [g, g, g, c[3]] + }; let wells = [ - PairWell::new(tl!("Set foreground color"), crate::panels::color::srgba(tools.foreground)), - PairWell::new(tl!("Set background color"), crate::panels::color::srgba(tools.background)), + PairWell::new(tl!("Set foreground color"), crate::panels::color::srgba(shown(tools.foreground))), + PairWell::new(tl!("Set background color"), crate::panels::color::srgba(shown(tools.background))), ]; SwatchPair::new(tl!("Colors"), wells, Bind::bound(active)) .popover(false) diff --git a/crates/ui-egui/src/tool_memory.rs b/crates/ui-egui/src/tool_memory.rs index 0e8bd97e3c..1f50adfb66 100644 --- a/crates/ui-egui/src/tool_memory.rs +++ b/crates/ui-egui/src/tool_memory.rs @@ -109,17 +109,18 @@ pub fn persist(app: &mut PhotocraftApp, ctx: &egui::Context) { } } -/// Keep pixel and mask swatches separately, even when the app exits with a mask targeted. +/// Keep pixel and mask swatches separately, even when the app exits with a mask targeted. The +/// Quick Mask pair is not kept (#17490): in Quick Mask the pair it put aside is stored. fn colors(app: &PhotocraftApp) -> [[[f32; 4]; 2]; 2] { let t = &app.session.tools; - let current = [t.foreground, t.background]; + let current = t.outer_pair().0; if t.mask_targeted { [t.other_colors, current] } else { [current, t.other_colors] } } /// Associations in persistent order: pixel foreground/background, then mask /// foreground/background, regardless of the currently selected target. fn cmyk_roles(app: &PhotocraftApp) -> CmykRoles { - let roles = app.session.tools.cmyk_roles; + let roles = [app.session.tools.outer_pair().1, app.session.tools.cmyk_roles[1]]; if app.session.tools.mask_targeted { [roles[1], roles[0]] } else { roles } } @@ -196,6 +197,8 @@ pub(crate) fn recover_legacy_cmyk_roles(saved: &Value) -> CmykRoles { roles } pub(crate) fn restore_colors(app: &mut PhotocraftApp) { + // The stored pairs are the ones outside Quick Mask. + let quick_mask = app.session.tools.quick_mask(false); let saved = app.session.prefs().tool_colors.clone(); let mut pairs = colors(app); let [[foreground, background], [mask_foreground, mask_background]] = &mut pairs; @@ -276,6 +279,7 @@ pub(crate) fn restore_colors(app: &mut PhotocraftApp) { app.prefs_rt.tool_cmyk_picks = app.session.tools.cmyk_picks.clone(); app.prefs_rt.tool_cmyk_roles = Some(roles); app.prefs_rt.tool_colors = Some(pairs); + app.session.tools.quick_mask(quick_mask); } /// Remember the tools used last when the list changes, whether or not the selected tool did: a From beb174e2c6563bdc64709d28f020fce3c1f49b01 Mon Sep 17 00:00:00 2001 From: David Mann Date: Mon, 30 Nov 2026 04:47:41 +0100 Subject: [PATCH 3/3] Quick Mask pair: name the CMYK pair type for clippy Co-Authored-By: Claude Opus 5.5 --- crates/engine-core/src/doc_state.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/engine-core/src/doc_state.rs b/crates/engine-core/src/doc_state.rs index 8f1ce4140c..ed9242bbd5 100644 --- a/crates/engine-core/src/doc_state.rs +++ b/crates/engine-core/src/doc_state.rs @@ -396,11 +396,13 @@ pub struct ToolState { pub quick_mask_on: bool, /// The pair Quick Mask put aside, with its CMYK roles: the Quick Mask pair outside Quick /// Mask, the pixel (or layer mask) pair inside it (#17490). - pub quick_mask_aside: ([[f32; 4]; 2], [Option<([f32; 3], [f32; 4])>; 2]), + pub quick_mask_aside: ([[f32; 4]; 2], CmykPair), } /// Native CMYK inks associated with each color role. -pub type CmykRoles = [[Option<([f32; 3], [f32; 4])>; 2]; 2]; +pub type CmykRoles = [CmykPair; 2]; +/// Native CMYK inks of one foreground/background pair. +pub type CmykPair = [Option<([f32; 3], [f32; 4])>; 2]; impl ToolState { /// How many CMYK picks are remembered. pub const CMYK_PICKS: usize = 8; @@ -520,7 +522,7 @@ impl ToolState { /// The current pair and its CMYK roles as they are outside Quick Mask: the pair Quick Mask /// put aside while it is on, for what stores the swatches. - pub fn outer_pair(&self) -> ([[f32; 4]; 2], [Option<([f32; 3], [f32; 4])>; 2]) { + pub fn outer_pair(&self) -> ([[f32; 4]; 2], CmykPair) { if self.quick_mask_on { self.quick_mask_aside } else { ([self.foreground, self.background], self.cmyk_roles[0]) } } }