From 9037b5440e0851984381ed74431e7acf6e32f0c0 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 24 Nov 2026 21:48:25 -0800 Subject: [PATCH 1/3] color-ui: a Recent Colors pick in the FillStroke Color panel keeps the chip's color model The FillStroke Color panel reported every Recent Colors pick as plain sRGB, so a host could not keep a CMYK recent color CMYK. A chip that showed the edited color's sRGB in another model set nothing. - `SwatchPaletteOutput::cell` names the picked cell (`PaletteCell::Swatch` or `PaletteCell::Recent` with its index), so a host can tell apart cells that show the same color. - `ColorPanel::recent_models` takes each recent chip's model values. A pick on a chip that has them reports `PickedColor::in_model` with those values. It sets nothing when the edited color already has those values and shows that color. --- crates/color-ui/src/ui/mod.rs | 2 +- crates/color-ui/src/ui/palette.rs | 32 ++++++++----- crates/color-ui/src/ui/panel.rs | 15 ++++++- crates/color-ui/src/ui/panel/fill_stroke.rs | 27 +++++++++-- crates/color-ui/tests/it/panels.rs | 50 +++++++++++++++++++-- 5 files changed, 106 insertions(+), 20 deletions(-) diff --git a/crates/color-ui/src/ui/mod.rs b/crates/color-ui/src/ui/mod.rs index 54fb837833..a13003b5c9 100644 --- a/crates/color-ui/src/ui/mod.rs +++ b/crates/color-ui/src/ui/mod.rs @@ -68,7 +68,7 @@ pub use paint_chip::{ PaintListEvent, PaintListOptions, PaintListOutput, PaintRow, PaintWords, }; pub use pair::{DropAccepts, PairWell, SwatchPair, SwatchPairOutput, Well}; -pub use palette::{SwatchPalette, SwatchPaletteOutput}; +pub use palette::{PaletteCell, SwatchPalette, SwatchPaletteOutput}; pub use panel::{ ColorPanel, ColorPanelAction, ColorPanelCommand, ColorPanelLayout, ColorPanelMenu, ColorPanelMode, ColorPanelOutput, ColorPanelRamp, ColorPanelShown, ColorPanelState, ColorPanelWords, PanelTarget, TintRow, diff --git a/crates/color-ui/src/ui/palette.rs b/crates/color-ui/src/ui/palette.rs index 000e8ca818..1be2441caf 100644 --- a/crates/color-ui/src/ui/palette.rs +++ b/crates/color-ui/src/ui/palette.rs @@ -35,6 +35,18 @@ pub struct SwatchPaletteOutput { pub pick_color: Option, /// Every swatch click of this pass, also a click on the colour already bound, which files no `edit`. pub picked: Option, + /// The cell `picked` came from, so a host can tell apart cells that show the same color. + pub cell: Option, +} + +/// A cell of a [`SwatchPalette`] by its place in the host's lists. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[non_exhaustive] +pub enum PaletteCell { + /// The swatch at this index of the palette's swatches. + Swatch(usize), + /// The color at this index of the recent row's colors. + Recent(usize), } impl Outcome for SwatchPaletteOutput { @@ -108,8 +120,8 @@ impl PaletteColor<'_> { /// What the body picked in its pass. #[derive(Clone, Copy)] enum Pick { - /// A swatch or a recent colour. - Color(Srgba), + /// A swatch or a recent color, and its cell. + Color(Srgba, PaletteCell), /// The Other… cell. Other, } @@ -208,7 +220,7 @@ impl<'a> SwatchPalette<'a> { // The Other… cell is keyed `swatches.len()`, past the swatches. A click selects on its press and activates // on its release, so only the activation files the request: one request for one click. pick = table.selected.or(table.open).and_then(|at| match swatches.get(at) { - Some((_, picked)) => Some(Pick::Color(*picked)), + Some((_, picked)) => Some(Pick::Color(*picked, PaletteCell::Swatch(at))), None => (other.is_some() && table.open == Some(at) && at == swatches.len()).then_some(Pick::Other), }); } @@ -221,8 +233,8 @@ impl<'a> SwatchPalette<'a> { .activate(ActivateOn::Click) .selection(Bind::bound(&mut current)) .show_all(ui); - if let Some((_, picked)) = row.selected.or(row.open).and_then(|at| recent_cells.get(at)) { - pick = Some(Pick::Color(*picked)); + if let Some((at, (_, picked))) = row.selected.or(row.open).and_then(|at| recent_cells.get(at).map(|cell| (at, cell))) { + pick = Some(Pick::Color(*picked, PaletteCell::Recent(at))); } } Some((before, pick)) @@ -235,21 +247,21 @@ impl<'a> SwatchPalette<'a> { match pick? { Pick::Other => { let key = other.as_ref().map(|(key, _)| key.clone())?; - Some((None, None, Some(ColorRequest::new(key, bound).title(name.clone())))) + Some((None, None, Some(ColorRequest::new(key, bound).title(name.clone())), None)) } - Pick::Color(after) => { + Pick::Color(after, cell) => { let unchanged = bound.is_some_and(|bound| bound.to_rgba() == after.to_rgba()); let edit = (!unchanged).then(|| { color.write(cx, before, after, name.as_str()); let reported = bound.map_or(Mixed::Mixed, Mixed::Same); EditEvent::set(reported, after, Arc::from(name.as_str()), None) }); - Some((Some(after), edit, None)) + Some((Some(after), edit, None, Some(cell))) } } }) .flatten(); - let (picked, edit, pick_color) = settled.unwrap_or((None, None, None)); - SwatchPaletteOutput { response: shown.output, edit, pick_color, picked } + let (picked, edit, pick_color, cell) = settled.unwrap_or((None, None, None, None)); + SwatchPaletteOutput { response: shown.output, edit, pick_color, picked, cell } } } diff --git a/crates/color-ui/src/ui/panel.rs b/crates/color-ui/src/ui/panel.rs index 90c92271b2..a80ac39646 100644 --- a/crates/color-ui/src/ui/panel.rs +++ b/crates/color-ui/src/ui/panel.rs @@ -449,6 +449,7 @@ pub struct ColorPanel<'a> { edited: Option>, shown: Option, recent: &'a [Srgba], + recent_models: &'a [Option], recent_setup: Option, gamut: Option>, } @@ -472,6 +473,7 @@ struct Inputs<'a> { edited: Option<&'a Option>, shown: &'a ColorPanelShown, recent: &'a [Srgba], + recent_models: &'a [Option], recent_setup: Option<&'a RecentSetup>, } @@ -489,6 +491,7 @@ impl<'a> ColorPanel<'a> { edited: None, shown: None, recent: &[], + recent_models: &[], recent_setup: None, gamut: None, } @@ -547,6 +550,15 @@ impl<'a> ColorPanel<'a> { self } + /// The model values of each Recent Colors chip, in the order of [`recent`](Self::recent). A pick on a chip that has + /// them reports them with its color (`PickedColor::in_model`), so a CMYK recent color stays CMYK, and a chip + /// that shows the edited color's sRGB in another model still sets it. A chip with `None`, or past the end of + /// `models`, reports its sRGB. + pub fn recent_models(mut self, models: &'a [Option]) -> Self { + self.recent_models = models; + self + } + /// The payload a Recent Colors chip drags. pub fn recent_payload(mut self, map: impl Fn(usize) -> Option

+ Send + Sync + 'static) -> Self { self.recent_setup = Some(recent_setup(map)); @@ -561,7 +573,7 @@ impl<'a> ColorPanel<'a> { /// Shows the panel and returns this pass's edits and requests. pub fn show(self, ui: &mut Ui) -> ColorPanelOutput { - let ColorPanel { state, colors, cmyk, rgb, dynamic, layout, wells, edited, shown: rows, recent, recent_setup, gamut } = self; + let ColorPanel { state, colors, cmyk, rgb, dynamic, layout, wells, edited, shown: rows, recent, recent_models, recent_setup, gamut } = self; let env = Env::of(ui); let (words, dialog) = (env.words::(), env.words::()); let mut held = Held::new(state); @@ -600,6 +612,7 @@ impl<'a> ColorPanel<'a> { edited: edited.as_ref(), shown: rows.as_ref().unwrap_or(&default_rows), recent, + recent_models, recent_setup: recent_setup.as_ref(), }, now: before, diff --git a/crates/color-ui/src/ui/panel/fill_stroke.rs b/crates/color-ui/src/ui/panel/fill_stroke.rs index a3ff66c160..ed851e4850 100644 --- a/crates/color-ui/src/ui/panel/fill_stroke.rs +++ b/crates/color-ui/src/ui/panel/fill_stroke.rs @@ -3,7 +3,7 @@ //! same `Body` as the Photoshop layout and shares its rows, wells, warnings and ramp helpers. use craft_behavior::{EditEvent, Mixed}; -use craft_theme::Srgba; +use craft_theme::{ModelValues, Srgba}; use craft_ui::choice::{Swatch, SwatchSpacing}; use craft_ui::contract::{Bind, Gap, LabelScheme, Props as _, Scroll, Show as _, ShowContainer as _, Sizing}; use craft_ui::display::Text; @@ -14,7 +14,7 @@ use craft_ui::text::{HexField, TextField}; use egui::Ui; use super::{Body, ColorPanelMode, ColorPanelShown, PanelTarget, TintRow}; -use crate::ui::{ChannelRow, ChannelRule, ChannelUnit, ColorRamp, PickedColor, RampKind, SwatchPalette}; +use crate::ui::{ChannelRow, ChannelRule, ChannelUnit, ColorRamp, PaletteCell, PickedColor, RampKind, SwatchPalette}; impl Body<'_> { /// The layout. The options (rows, quick wells, recent colours) go when the state hides them; the proxy and the @@ -154,7 +154,7 @@ impl Body<'_> { } } - /// The Recent Colors as a spaced palette; a pick sets the colour. + /// The Recent Colors as a spaced palette; a pick sets the color, in the chip's model when the host gave it. // PLAN: Alt on a recent colour or a warning chip should set the other colour; `SwatchPaletteOutput` and // `ColorWarningChipOutput` report no modifiers (color-w1 for the palette, the owner of warning.rs for the chip). fn recent(&mut self, ui: &mut Ui) { @@ -167,13 +167,32 @@ impl Body<'_> { if let Some(setup) = self.inputs.recent_setup { palette = setup(palette); } - if let Some(event) = palette.show(ui).edit + let out = palette.show(ui); + let values = match out.cell { + Some(PaletteCell::Swatch(at)) => self.inputs.recent_models.get(at).copied().flatten(), + _ => None, + }; + if let (Some(color), Some(values)) = (out.picked, values) { + self.set_in_model(color, values); + } else if let Some(event) = out.edit && let Some(color) = event.value() { self.set_color(self.target, *color); } } + /// Sets the edited color to `color` in the model of `values`, keeping its alpha. It sets nothing when the host's + /// color already has those values and shows that color. + fn set_in_model(&mut self, color: Srgba, values: ModelValues) { + let before = self.picked().copied().unwrap_or_else(|| PickedColor::rgb(self.colors[self.target.index()])); + let after = PickedColor::in_model(color.with_alpha(before.color.a), values); + if before.model == after.model && before.color.to_rgba() == after.color.to_rgba() { + return; + } + let name = self.words.title.as_str(); + self.push(self.target, EditEvent::set(Mixed::Same(before), after, name.into(), None)); + } + /// Sets `target`'s colour to `color`, keeping its alpha. fn set_color(&mut self, target: PanelTarget, color: Srgba) { let before = self.colors[target.index()]; diff --git a/crates/color-ui/tests/it/panels.rs b/crates/color-ui/tests/it/panels.rs index 840a36c56a..32fff3ea73 100644 --- a/crates/color-ui/tests/it/panels.rs +++ b/crates/color-ui/tests/it/panels.rs @@ -7,11 +7,11 @@ use std::rc::Rc; use craft_behavior::{EditEvent, Mixed, Selection}; use craft_color_ui::palette::{SwatchColor, SwatchGroup}; use craft_color_ui::ui::{ - ColorPanel, ColorPanelLayout, ColorPanelMode, ColorPanelShown, ColorPanelState, HarmonyWheel, PanelTarget, PickedColor, SwatchEdit, SwatchKey, - SwatchLibrary, SwatchPalette, SwatchesCommand, SwatchesMenu, SwatchesPanel, SwatchesState, SwatchesWords, swatch_rows, + ColorPanel, ColorPanelLayout, ColorPanelMode, ColorPanelShown, ColorPanelState, HarmonyWheel, PaletteCell, PanelTarget, PickedColor, SwatchEdit, + SwatchKey, SwatchLibrary, SwatchPalette, SwatchesCommand, SwatchesMenu, SwatchesPanel, SwatchesState, SwatchesWords, swatch_rows, }; use craft_host::target::Os; -use craft_theme::{ColorModel as _, HsbModel, Srgba}; +use craft_theme::{ColorModel as _, HsbModel, ModelValues, Srgba}; use craft_ui::choice::SwatchTable; use craft_ui::contract::Role; use craft_ui::contract::tooltip::DELAY; @@ -254,6 +254,44 @@ fn the_fill_stroke_spectrums_tooltip_names_the_shown_mode_and_shift_click_cycles assert!(edits.borrow().is_empty(), "a Shift click picks nothing: {:?}", edits.borrow()); } +/// A Recent Colors pick in the FillStroke layout reports the chip's model values with its color, also when the chip +/// shows the edited color's sRGB in another model or without one; a chip whose values the edited color already has +/// sets nothing. +#[trial] +fn a_fill_stroke_recent_pick_keeps_the_chips_model() { + let chip = RECENT[0]; + let cmyk = ModelValues::new("cmyk", &[60.0, 90.0, 0.0, 0.0]); + let pick = |edited: PickedColor| { + let edits: Edits = Default::default(); + let seen = Rc::clone(&edits); + let mut fixture = Fixture::new(move |ui| { + let mut state = ColorPanelState::default(); + let out = ColorPanel::new(Bind::bound(&mut state), [edited.color, BLUE]) + .layout(ColorPanelLayout::FillStroke) + .edited(Some(edited)) + .recent(&RECENT) + .recent_models(&[Some(cmyk)]) + .show(ui); + seen.borrow_mut().extend(out.edits); + }); + fixture.room(COLOR_ROOM).settle(); + let recent = fixture.by_label(&chip.to_hex()).expect("the recent chip is named by its hex"); + fixture.click(&recent).settle(); + edits.take() + }; + let rgb_of = |color: Srgba| ModelValues::new("rgb", &[color.r * 255.0, color.g * 255.0, color.b * 255.0]); + for edited in [PickedColor::in_model(RED, rgb_of(RED)), PickedColor::in_model(chip, rgb_of(chip)), PickedColor::rgb(chip)] { + let edits = pick(edited); + assert!( + matches!(edits.as_slice(), [(PanelTarget::Foreground, EditEvent::Set { after, .. })] + if after.model == Some(cmyk) && after.color.to_rgba() == chip.to_rgba()), + "one Set to the chip in CMYK from {edited:?}: {edits:?}" + ); + } + let edits = pick(PickedColor::in_model(chip, cmyk)); + assert!(edits.is_empty(), "the chip's own values set nothing: {edits:?}"); +} + const SWATCHES: [(&str, [f32; 3]); 6] = [ ("Red", [0.90, 0.28, 0.30]), ("Orange", [0.97, 0.42, 0.08]), @@ -328,6 +366,7 @@ struct PaletteReports { edits: Vec>, requests: Vec, picked: Vec, + cells: Vec, } type Reports = Rc>; @@ -338,6 +377,7 @@ fn collect(reports: &Reports, shown: craft_color_ui::ui::SwatchPaletteOutput) { reports.edits.extend(shown.edit); reports.requests.extend(shown.pick_color); reports.picked.extend(shown.picked); + reports.cells.extend(shown.cell); } fn palette_swatches() -> Vec<(Label, Srgba)> { @@ -394,11 +434,13 @@ fn the_other_cell_files_a_request_at_the_colour_and_a_recent_pick_sets_it() { taken.edits[0] ); assert_eq!(bound.get().to_rgba(), RECENT[0].to_rgba(), "the recent pick is written"); + assert_eq!(taken.cells, [PaletteCell::Recent(0)], "the pick names its cell in the recent row"); let violet = fixture.by_label("Violet").expect("violet"); fixture.click(&violet).settle(); - let _ = std::mem::take(&mut *reports.borrow_mut()); + let taken = std::mem::take(&mut *reports.borrow_mut()); assert_eq!(bound.get().to_rgba(), swatch("Violet").to_rgba()); + assert_eq!(taken.cells, [PaletteCell::Swatch(5)], "the pick names its swatch"); // Past the double-click time, so the repeat is a single click of its own. fixture.advance(1.0); let violet = fixture.by_label("Violet").expect("violet"); From 6c9d4b264cbc8d43e7694b1f400f847af38d08fe Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 20:11:28 -0800 Subject: [PATCH 2/3] color-ui: say what ColorPanel::recent_models does for a chip without values A chip without model values works as without the call: a pick sets its sRGB, and a pick of the color already shown sets nothing. --- crates/color-ui/src/ui/panel.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/color-ui/src/ui/panel.rs b/crates/color-ui/src/ui/panel.rs index 7cace9bb2f..79c18bb671 100644 --- a/crates/color-ui/src/ui/panel.rs +++ b/crates/color-ui/src/ui/panel.rs @@ -554,7 +554,7 @@ impl<'a> ColorPanel<'a> { /// The model values of each Recent Colors chip, in the order of [`recent`](Self::recent). A pick on a chip that has /// them reports them with its color (`PickedColor::in_model`), so a CMYK recent color stays CMYK, and a chip /// that shows the edited color's sRGB in another model still sets it. A chip with `None`, or past the end of - /// `models`, reports its sRGB. + /// `models`, works as without this call: a pick sets its sRGB, and a pick of the color already shown sets nothing. pub fn recent_models(mut self, models: &'a [Option]) -> Self { self.recent_models = models; self From a53930f3e3d0fcf79aa66aa43060db9adf4d5a47 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 02:50:31 -0800 Subject: [PATCH 3/3] color-ui: a Recent Colors pick drops the rows' kept values in the chip's model set_in_model left the rows' kept values in place. Body::values reads them before a host's sRGB-only color, and in HSB a grey or black matches for any hue, so after picking a grey chip the Hue row kept the hue of earlier row edits and the next channel edit started from it. The pick now removes the kept entries for the chip's model before it files the edit. --- crates/color-ui/src/ui/panel/fill_stroke.rs | 2 + crates/color-ui/tests/it/panels.rs | 44 +++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/crates/color-ui/src/ui/panel/fill_stroke.rs b/crates/color-ui/src/ui/panel/fill_stroke.rs index 4c9a518355..1e6dd88dd9 100644 --- a/crates/color-ui/src/ui/panel/fill_stroke.rs +++ b/crates/color-ui/src/ui/panel/fill_stroke.rs @@ -196,6 +196,8 @@ impl Body<'_> { if before.model == after.model && before.color.to_rgba() == after.color.to_rgba() { return; } + // The rows' kept values in the chip's model would win over a host that keeps only sRGB (a grey's HSB hue). + self.kept.values[self.target.index()].retain(|v| v.model != values.model); let name = self.words.title.as_str(); self.push(self.target, EditEvent::set(Mixed::Same(before), after, name.into(), None)); } diff --git a/crates/color-ui/tests/it/panels.rs b/crates/color-ui/tests/it/panels.rs index a2d2b6bc04..ffcc37b8c5 100644 --- a/crates/color-ui/tests/it/panels.rs +++ b/crates/color-ui/tests/it/panels.rs @@ -405,6 +405,50 @@ fn a_fill_stroke_recent_pick_keeps_the_chips_model() { assert!(edits.is_empty(), "the chip's own values set nothing: {edits:?}"); } +/// A Recent Colors pick drops the HSB values the rows kept for a grey: after a host that keeps only sRGB applies a grey +/// chip with HSB values, the Hue row reads the grey afresh instead of the hue of the earlier row edits. +#[trial] +fn a_fill_stroke_recent_pick_drops_the_rows_kept_hue() { + let grey = Srgba::rgb(0.5, 0.5, 0.5); + let chip = [grey]; + let chip_hsb = ModelValues::new("hsb", &[0.0, 0.0, 50.0]); + let host = Rc::new(RefCell::new(grey)); + let color = Rc::clone(&host); + let mut fixture = Fixture::new(move |ui| { + let current = *color.borrow(); + let mut state = ColorPanelState { mode: ColorPanelMode::HsbSliders, ..Default::default() }; + let shown = ColorPanel::new(Bind::bound(&mut state), [current, BLUE]) + .layout(ColorPanelLayout::FillStroke) + .edited(Some(PickedColor::rgb(current))) + .shown(ColorPanelShown::Mode(ColorPanelMode::HsbSliders)) + .recent(&chip) + .recent_models(&[Some(chip_hsb)]) + .show(ui); + for (_, event) in &shown.edits { + if let Some(picked) = event.landed() { + *color.borrow_mut() = picked.color; + } + } + }); + fixture.room(COLOR_ROOM).settle(); + let hue = fixture.by_role(Role::SLIDER).into_iter().next().expect("the hue slider"); + fixture.access(AccessActionKind::Focus, &hue).settle(); + for _ in 0..20 { + fixture.key(Key::ArrowRight, Modifiers::NONE).settle(); + } + let hue_value = |fixture: &mut Fixture| { + let hue = fixture.by_role(Role::SLIDER).into_iter().next().expect("the hue slider"); + fixture.access(AccessActionKind::Focus, &hue).settle(); + let id = fixture.focused().expect("the hue slider takes the focus"); + fixture.tree().value(id).expect("the hue slider reports its value").to_owned() + }; + let stepped = hue_value(&mut fixture); + assert_ne!(stepped, "0", "the steps moved the kept hue of the grey"); + let recent = fixture.by_label(&grey.to_hex()).expect("the recent chip is named by its hex"); + fixture.click(&recent).settle(); + assert_eq!(hue_value(&mut fixture), "0", "the pick drops the kept hue {stepped}"); +} + /// In a host sized by its content (a floating Color panel) the FillStroke spectrum is as tall as its end boxes stacked, /// each at least as tall as it is wide, not a thin band; in a short group it keeps that height and the body scrolls. #[trial]