From 1c093cc7e0fe8f673b312fb1c424a5485d36e25f Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 23 Nov 2026 21:26:08 +0700 Subject: [PATCH] color-ui: a click on any swatch reports SwatchesPanelOutput::used, with its colour only where it holds one: gradient, pattern and None swatches reported nothing, where production applied every swatch --- crates/color-ui/src/ui/swatches/panel.rs | 12 ++++----- crates/color-ui/tests/it/panels.rs | 31 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/crates/color-ui/src/ui/swatches/panel.rs b/crates/color-ui/src/ui/swatches/panel.rs index 6d2336736..3a688ebab 100644 --- a/crates/color-ui/src/ui/swatches/panel.rs +++ b/crates/color-ui/src/ui/swatches/panel.rs @@ -32,9 +32,10 @@ use crate::palette::{SwatchGroup, SwatchNote, SwatchReport}; pub struct SwatchesPanelOutput { /// The panel's response. pub response: Response, - /// A swatch was clicked: its colour (through the document's converter) for the foreground, or for the background - /// when Alt (Ctrl or Cmd too, as PhotoCraft) was held. - pub used: Option<(SwatchAt, Srgba, bool)>, + /// A swatch was clicked: where it is, its colour (through the document's converter) when it holds one, and whether + /// it is for the background (Alt held; Ctrl or Cmd too, as PhotoCraft) rather than the foreground. A gradient, + /// pattern or None swatch reports no colour: the host applies the paint it keeps for the entry. + pub used: Option<(SwatchAt, Option, bool)>, /// The edits the user made this pass, in pass order, for the host to run; the panel never writes the library. pub edits: Vec, /// The browser events the panel does not turn into `used` or `edits`, in the order they happened: `Activate`, @@ -101,7 +102,7 @@ pub struct SwatchesPanel<'a> { struct Settled { before_state: SwatchesState, st: SwatchesState, - used: Option<(SwatchAt, Srgba, bool)>, + used: Option<(SwatchAt, Option, bool)>, edits: Vec, events: Vec>, } @@ -296,9 +297,8 @@ impl<'a> SwatchesPanel<'a> { if let PresetEvent::Select { key: SwatchKey::Swatch(name), mods, .. } = &event && let Some(at) = find_swatch(groups, name) && let Some(entry) = groups.get(at.group).and_then(|g| g.entries.get(at.index)) - && let Some(color) = entry.color() { - used = Some((at, color.to_srgb(cmyk), mods.alt || mods.cmd || mods.ctrl)); + used = Some((at, entry.color().map(|color| color.to_srgb(cmyk)), mods.alt || mods.cmd || mods.ctrl)); } let forwarded = matches!( event, diff --git a/crates/color-ui/tests/it/panels.rs b/crates/color-ui/tests/it/panels.rs index 1984c3189..2eee35a5d 100644 --- a/crates/color-ui/tests/it/panels.rs +++ b/crates/color-ui/tests/it/panels.rs @@ -416,6 +416,37 @@ fn swatches_panel_reports_each_edit_and_leaves_the_library() { assert_eq!(state.rename, Some(SwatchKey::Swatch("Color Swatch 2".to_owned())), "the new swatch opens for renaming"); } +/// Tb-8 (5): a click uses any swatch, as production applied every one: a colour swatch reports its colour, and a +/// gradient, pattern or None swatch reports where it is with no colour, for the host to apply its paint. The panel +/// reported only colour swatches. +#[trial] +fn a_click_uses_every_kind_of_swatch() { + use craft_color_ui::palette::{SwatchEntry, SwatchPaint}; + let groups = vec![SwatchGroup { + name: "Basics".to_owned(), + entries: vec![ + SwatchEntry::new("Red".to_owned(), SwatchColor::Rgb([1.0, 0.0, 0.0])), + SwatchEntry::with_paint("Fade", SwatchPaint::Gradient), + SwatchEntry::with_paint("Dots", SwatchPaint::Pattern), + SwatchEntry::with_paint("None", SwatchPaint::None), + ], + }]; + let used = Rc::new(RefCell::new(Vec::new())); + let seen = Rc::clone(&used); + let mut state = SwatchesState::default(); + let shown = groups.clone(); + let mut fixture = Fixture::new(move |ui| { + let out = SwatchesPanel::new(&shown, Bind::bound(&mut state), RED).show(ui); + seen.borrow_mut().extend(out.used.map(|(at, color, _)| (at.index, color.is_some()))); + }); + fixture.room(Vec2::new(320.0, 400.0)).settle(); + for name in ["Red, #ff0000", "Fade", "Dots", "None"] { + let swatch = fixture.by_label(name).expect("the swatch"); + fixture.click(&swatch).settle(); + } + assert_eq!(*used.borrow(), [(0, true), (1, false), (2, false), (3, false)], "each click uses its swatch, a colour only where it holds one"); +} + /// `count` entries of a colour table, each named by its index. /// A swatch dragged out of the panel carries the host's payload to the host's drop target (the app's paint, which a /// gradient's stops take), and the panel's own drag still moves a swatch onto another group.