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

This commit is contained in:
bflatastic
2026-11-23 21:26:08 +07:00
parent fe2d1d09d7
commit 1c093cc7e0
2 changed files with 37 additions and 6 deletions
+6 -6
View File
@@ -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<Srgba>, 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<SwatchEdit>,
/// 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<Srgba>, bool)>,
edits: Vec<SwatchEdit>,
events: Vec<PresetEvent<SwatchKey>>,
}
@@ -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,
+31
View File
@@ -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.