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 b5d35ac5c0..7da075f1aa 100644 --- a/crates/color-ui/src/ui/palette.rs +++ b/crates/color-ui/src/ui/palette.rs @@ -38,6 +38,18 @@ pub struct SwatchPaletteOutput { /// The picked entry's index in the palette, including a swatch with the bound colour or a duplicate colour. /// The recent row and the Other… cell do not report a swatch index. pub picked_swatch: 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 { @@ -112,8 +124,8 @@ impl PaletteColor<'_> { /// What the body picked in its pass. #[derive(Clone, Copy)] enum Pick { - /// A swatch or a recent colour. - Color { color: Srgba, swatch: Option }, + /// A swatch or a recent color, and its cell. + Color(Srgba, PaletteCell), /// The Other… cell. Other, } @@ -235,7 +247,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 = picked_cell(&table, ui).and_then(|at| match swatches.get(at) { - Some((_, picked)) => Some(Pick::Color { color: *picked, swatch: Some(at) }), + Some((_, picked)) => Some(Pick::Color(*picked, PaletteCell::Swatch(at))), None => (other.is_some() && table.open == Some(at) && at == swatches.len()).then_some(Pick::Other), }); } @@ -249,8 +261,8 @@ impl<'a> SwatchPalette<'a> { .space(SpaceAction::ActivateLead) .selection(Bind::bound(&mut current)) .show_all(ui); - if let Some((_, picked)) = picked_cell(&row, ui).and_then(|at| recent_cells.get(at)) { - pick = Some(Pick::Color { color: *picked, swatch: None }); + if let Some((at, (_, picked))) = picked_cell(&row, ui).and_then(|at| recent_cells.get(at).map(|cell| (at, cell))) { + pick = Some(Pick::Color(*picked, PaletteCell::Recent(at))); } } Some((before, pick)) @@ -265,19 +277,23 @@ impl<'a> SwatchPalette<'a> { let key = other.as_ref().map(|(key, _)| key.clone())?; Some((None, None, Some(ColorRequest::new(key, bound).title(name.clone())), None)) } - Pick::Color { color: after, swatch } => { + 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, swatch)) + Some((Some(after), edit, None, Some(cell))) } } }) .flatten(); - let (picked, edit, pick_color, picked_swatch) = settled.unwrap_or((None, None, None, None)); - SwatchPaletteOutput { response: shown.output, edit, pick_color, picked, picked_swatch } + let (picked, edit, pick_color, cell) = settled.unwrap_or((None, None, None, None)); + let picked_swatch = match cell { + Some(PaletteCell::Swatch(at)) => Some(at), + _ => None, + }; + SwatchPaletteOutput { response: shown.output, edit, pick_color, picked, picked_swatch, cell } } } diff --git a/crates/color-ui/src/ui/panel.rs b/crates/color-ui/src/ui/panel.rs index 1363d82c47..dc32f707b1 100644 --- a/crates/color-ui/src/ui/panel.rs +++ b/crates/color-ui/src/ui/panel.rs @@ -452,6 +452,7 @@ pub struct ColorPanel<'a> { edited: Option>, shown: Option, recent: Option<&'a [Srgba]>, + recent_models: &'a [Option], recent_setup: Option, gamut: Option>, } @@ -476,6 +477,7 @@ struct Inputs<'a> { edited: Option<&'a Option>, shown: &'a ColorPanelShown, recent: Option<&'a [Srgba]>, + recent_models: &'a [Option], recent_setup: Option<&'a RecentSetup>, } @@ -494,6 +496,7 @@ impl<'a> ColorPanel<'a> { edited: None, shown: None, recent: None, + recent_models: &[], recent_setup: None, gamut: None, } @@ -563,6 +566,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`, 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 + } + /// 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)); @@ -577,7 +589,8 @@ 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, buttons, edited, shown: rows, recent, recent_setup, gamut } = self; + let ColorPanel { state, colors, cmyk, rgb, dynamic, layout, wells, buttons, 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); @@ -617,6 +630,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 183844b574..1e6dd88dd9 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::{Shade, Text}; @@ -15,7 +15,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 @@ -156,8 +156,8 @@ impl Body<'_> { } } - /// The Recent Colors caption over a spaced palette; a pick sets the color. The caption shows while the host's list - /// is empty, where the first applied color will go. + /// The Recent Colors caption over a spaced palette; a pick sets the color, in the chip's model when the host gave it. + /// The caption shows while the host's list is empty, where the first applied color will go. // 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) { @@ -174,13 +174,34 @@ 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; + } + // 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)); + } + /// 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 6efa4a64a3..ffcc37b8c5 100644 --- a/crates/color-ui/tests/it/panels.rs +++ b/crates/color-ui/tests/it/panels.rs @@ -8,11 +8,11 @@ use craft_behavior::{EditEvent, Mixed, Selection}; use craft_color_ui::palette::{SwatchColor, SwatchGroup}; use craft_color_ui::ui::{ ColorPanel, ColorPanelLayout, ColorPanelMode, ColorPanelShown, ColorPanelState, HarmonyPanel, HarmonyPanelState, HarmonyPicked, HarmonyWheel, - PanelTarget, PickedColor, SwatchEdit, SwatchKey, SwatchLibrary, SwatchPalette, SwatchesCommand, SwatchesMenu, SwatchesPanel, SwatchesState, - SwatchesWords, swatch_rows, + 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::buttons::Button; use craft_ui::choice::SwatchTable; use craft_ui::contract::Role; @@ -367,6 +367,88 @@ 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:?}"); +} + +/// 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] @@ -471,6 +553,7 @@ struct PaletteReports { edits: Vec>, requests: Vec, picked: Vec, + cells: Vec, swatches: Vec, } @@ -482,6 +565,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); reports.swatches.extend(shown.picked_swatch); } @@ -539,11 +623,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");