From 42a7f08637205da20e95aec2eede44e59ef7055e Mon Sep 17 00:00:00 2001 From: David Mann Date: Sun, 29 Nov 2026 15:18:17 +0100 Subject: [PATCH] color-ui: an Eyedropper built anew each pass keeps the held sampling ring (photocraft#17931) EyedropperTool::ring reads the ring the button holds and EyedropperTool::held hands it to the next pass's tool, so an app whose sample closure borrows its state, and so rebuilds the tool every pass, paints the ring in a pass with no pointer input too instead of flickering. Co-Authored-By: Claude Opus 5.5 --- crates/color-ui/src/ui/eyedropper.rs | 29 +++++++++++++++++++++++++--- crates/color-ui/src/ui/mod.rs | 2 +- crates/color-ui/tests/it/sampling.rs | 24 +++++++++++++++++++++-- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/crates/color-ui/src/ui/eyedropper.rs b/crates/color-ui/src/ui/eyedropper.rs index 4e17c7b572..0f9d0e508a 100644 --- a/crates/color-ui/src/ui/eyedropper.rs +++ b/crates/color-ui/src/ui/eyedropper.rs @@ -260,6 +260,16 @@ pub struct EyedropperPick { pub background: bool, } +/// The sampling ring while the button is held: its centre on screen and the sampled colour it shows. +#[derive(Clone, Copy, Debug, PartialEq)] +#[non_exhaustive] +pub struct EyedropperRing { + /// The pointer at the sample, on screen. + pub center: Pos2, + /// The sampled colour: the upper half of the ring. + pub color: Srgba, +} + /// The pipette icon's tip, in the icon's own 24-unit box. pub(super) const HOTSPOT: [f32; 2] = [2.0, 22.0]; /// The ring's outer radius in points. @@ -274,7 +284,7 @@ const RING_SEGMENTS: usize = 48; pub struct EyedropperTool<'a> { options: EyedropperOptions, sample: Box Option + 'a>, - held: Option<(Pos2, Srgba)>, + held: Option, picked: Option, foreground: Srgba, } @@ -293,6 +303,19 @@ impl<'a> EyedropperTool<'a> { self } + /// The ring a previous tool held: an app that builds the tool anew each pass hands back what + /// [`ring`](Self::ring) gave the last pass, so a pass with no pointer input still paints the ring. + #[must_use] + pub fn held(mut self, ring: Option) -> Self { + self.held = ring; + self + } + + /// The ring while the button is held over the document; `None` once it is up or the gesture is cancelled. + pub fn ring(&self) -> Option { + self.held + } + /// The newest pick, once. pub fn take(&mut self) -> Option { self.picked.take() @@ -303,7 +326,7 @@ impl<'a> EyedropperTool<'a> { let Point { x, y } = space.view.to_doc(pointer.pos); match (self.sample)([x, y], self.options.size, self.options.source) { Some(color) => { - self.held = Some((pointer.pos, color)); + self.held = Some(EyedropperRing { center: pointer.pos, color }); self.picked = Some(EyedropperPick { color, background: pointer.mods.alt }); } None => self.held = None, @@ -351,7 +374,7 @@ impl CanvasTool for EyedropperTool<'_> { } fn paint(&self, draw: &Draw<'_>, cx: &ToolContext<'_>) { - let (true, Some((center, new)), Some(style)) = (self.options.show_ring, self.held, cx.style()) else { return }; + let (true, Some(EyedropperRing { center, color: new }), Some(style)) = (self.options.show_ring, self.held, cx.style()) else { return }; let tokens = style.tokens; let inner = RING_OUTER_RADIUS - 2.0 * RING_BAND; band(draw, center, (inner, inner + RING_BAND), (PI, TAU), new); diff --git a/crates/color-ui/src/ui/mod.rs b/crates/color-ui/src/ui/mod.rs index c075c29095..d620536e75 100644 --- a/crates/color-ui/src/ui/mod.rs +++ b/crates/color-ui/src/ui/mod.rs @@ -53,7 +53,7 @@ pub use compare::{CompareSwatch, CompareSwatchOutput, CompareSwatchStyle}; pub use disc::{ColorDisc, ColorDiscOutput, ColorDiscStyle, ColorDiscWords, DiscHit, DiscLook, DiscMove, DiscRadius, patch_at}; pub use droppers::{DropperButton, DropperButtonOutput, DropperKind, DropperTool}; pub use eyedropper::{ - EyedropperBar, EyedropperBarOutput, EyedropperOptions, EyedropperPick, EyedropperTool, EyedropperWords, SampleSize, SampleSource, + EyedropperBar, EyedropperBarOutput, EyedropperOptions, EyedropperPick, EyedropperRing, EyedropperTool, EyedropperWords, SampleSize, SampleSource, }; pub use harmony::{ GuideCell, GuideCellAt, HarmonyCommand, HarmonyMenu, HarmonyMenuAction, HarmonyPanel, HarmonyPanelOutput, HarmonyPanelState, HarmonyPicked, diff --git a/crates/color-ui/tests/it/sampling.rs b/crates/color-ui/tests/it/sampling.rs index 53ce25aa92..c0879b36dd 100644 --- a/crates/color-ui/tests/it/sampling.rs +++ b/crates/color-ui/tests/it/sampling.rs @@ -8,8 +8,9 @@ use craft_behavior::CanvasView; use craft_behavior::kurbo::Point; use craft_cms::convert::CmykConverter; use craft_color_ui::ui::{ - CaptureResult, CaptureSource, ColorPicker, ColorPickerLayout, ColorSamplerPass, ColorSamplerTool, ColorSamplers, MAX_SAMPLERS, PickedColor, - SamplerCache, SamplerEvent, SamplerReadouts, ScreenColorPicker, ScreenPick, ScreenPickerWords, StillCapture, + CaptureResult, CaptureSource, ColorPicker, ColorPickerLayout, ColorSamplerPass, ColorSamplerTool, ColorSamplers, EyedropperOptions, + EyedropperTool, MAX_SAMPLERS, PickedColor, SamplerCache, SamplerEvent, SamplerReadouts, ScreenColorPicker, ScreenPick, ScreenPickerWords, + StillCapture, }; use craft_theme::Srgba; use craft_ui::contract::Bind; @@ -74,6 +75,25 @@ fn events(tool: &ToolFixture>) -> Vec { tool.tool_mut().take_events() } +#[trial] +fn the_held_sampling_ring_carries_over_to_the_next_pass_tool() { + // An app whose sample closure borrows its state builds the tool anew each pass: the ring the press held must reach + // the next pass's tool, which gets no pointer input to sample again, or the ring flickers (photocraft#17931). + let red = Srgba::rgb(1.0, 0.0, 0.0); + let tool = EyedropperTool::new(EyedropperOptions::default(), move |_, _, _| Some(red)); + let mut tool = ToolFixture::with_view("Eyedropper", tool, sampler_view, |_, _| {}); + tool.fixture().room(vec2(800.0, 600.0)); + tool.fixture().settle(); + let at = tool.canvas().rect.min + vec2(40.0, 30.0); + tool.press_at(at); + let ring = tool.tool().ring().expect("the held button shows the ring"); + assert_eq!((ring.center, ring.color), (at, red)); + let next = EyedropperTool::new(EyedropperOptions::default(), |_, _, _| None).held(Some(ring)); + assert_eq!(next.ring(), Some(ring)); + tool.release_at(at); + assert_eq!(tool.tool().ring(), None, "the release ends the ring"); +} + #[trial] fn a_new_point_lands_at_the_release_and_a_dragged_point_moves_on_release() { // A new point: nothing at the press, an Add at the release; off the canvas or on Escape, nothing.