diff --git a/crates/engine-core/src/cmd/channel_cmds/selection.rs b/crates/engine-core/src/cmd/channel_cmds/selection.rs index 7a87df36b7..971d6c2ce1 100644 --- a/crates/engine-core/src/cmd/channel_cmds/selection.rs +++ b/crates/engine-core/src/cmd/channel_cmds/selection.rs @@ -186,5 +186,7 @@ pub(super) fn quick_mask(s: &mut Session, p: &Value) -> Result { return Ok(json!({ "quickMask": false, "noPixels": true })); } } + // 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 26f8b0d932..836c5837b6 100644 --- a/crates/engine-core/src/doc_state.rs +++ b/crates/engine-core/src/doc_state.rs @@ -407,10 +407,17 @@ 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], 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; @@ -498,12 +505,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], CmykPair) { + if self.quick_mask_on { self.quick_mask_aside } else { ([self.foreground, self.background], self.cmyk_roles[0]) } + } } impl Default for ToolState { @@ -527,6 +563,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]), } } } @@ -567,6 +605,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 c3739cbe54..f9df84f003 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 c4d4390e25..74e8fba6dd 100644 --- a/crates/engine/tests/it/main.rs +++ b/crates/engine/tests/it/main.rs @@ -180,6 +180,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 1fbedb9ca9..bd38d30765 100644 --- a/crates/ui-egui/src/app/targets.rs +++ b/crates/ui-egui/src/app/targets.rs @@ -210,5 +210,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 df5fe3d80d..e3cf32a56c 100644 --- a/crates/ui-egui/src/panels/toolbar.rs +++ b/crates/ui-egui/src/panels/toolbar.rs @@ -394,9 +394,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