From dfd8a1f27a504d933fed6d36fc531e21ae405846 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 00:34:45 +0000 Subject: [PATCH] One step-until-changed for quantised colour steps craft_behavior::nav::step_until_changed steps again while the value is the same one to the control. ColorStrip uses it in place of its own loop; PointPicker takes a `same` test and runs every relative move through it. ColorArea steps a quantised field by the channel's step until the colour changes, as the old area did. --- crates/behavior/src/nav/value.rs | 15 ++++++ crates/color/src/ui/area.rs | 24 ++++++---- crates/color/src/ui/strip.rs | 18 ++----- crates/color/tests/it/color_parts.rs | 31 ++++++++++-- crates/ui-widgets/src/value_drag/mod.rs | 2 +- .../ui-widgets/src/value_drag/point_picker.rs | 47 +++++++++++++++---- 6 files changed, 102 insertions(+), 35 deletions(-) diff --git a/crates/behavior/src/nav/value.rs b/crates/behavior/src/nav/value.rs index 101804502..66f6eb67e 100644 --- a/crates/behavior/src/nav/value.rs +++ b/crates/behavior/src/nav/value.rs @@ -96,6 +96,21 @@ pub fn step_value(steps: &ValueSteps, axes: ValueAxes, mv: NavMove, mods: MoveMo StepResult::Set((steps.value + delta).clamp(steps.min, steps.max)) } +/// The value `step` reaches from `from`, stepped again while a step still moves it and `same` says the value is the +/// same one as `from` to the control: a control that reports quantised values (a web-safe colour) steps on to the +/// next value that differs, or as far as it can go. +pub fn step_until_changed(from: T, step: impl Fn(T) -> T, same: impl Fn(T) -> bool) -> T { + let mut next = from; + loop { + let after = step(next); + let moved = after != next; + next = after; + if !(moved && same(next)) { + return next; + } + } +} + /// Maps a wheel event to the new value of a single-value control: one `notch` per event, up for positive scroll. /// Vertical scroll counts first; horizontal scroll counts when there is none (a vertical-only control ignores it), /// flipped under right-to-left text. Shift scales the notch by `large / small` and Alt by `fine / small`; both diff --git a/crates/color/src/ui/area.rs b/crates/color/src/ui/area.rs index 8f8300cf5..842866d53 100644 --- a/crates/color/src/ui/area.rs +++ b/crates/color/src/ui/area.rs @@ -45,8 +45,9 @@ pub struct ColorArea<'a> { mode: AreaMode, values: Bind<'a, ModelValues>, /// Puts the colour the area reports on this many levels (at least two) per red, green and blue channel, whatever - /// the mode: a web-safe field has 6. The pointer takes the nearest such colour and a key steps one level of the - /// channel's range. The field shows flat cells of the snapped colours. + /// the mode: a web-safe field has 6. The pointer takes the nearest such colour to the point on the channels' + /// steps, and a key or wheel step goes on by the channel's step until the colour is a different one. The field + /// shows flat cells of the snapped colours. #[prop] quantize: Option, /// The names of the across and down components in the value text; the model's channel names without them. @@ -80,15 +81,18 @@ impl<'a> ColorArea<'a> { }; let (x, y) = (channel_range(model, across), channel_range(model, down)); let y = *y.end()..=*y.start(); - let steps = [across, down].map(|channel| match self.levels() { - Some(levels) => { - let range = channel_range(model, channel); - (range.end() - range.start()) / (levels - 1) as f64 - } - None => channel_step(model, channel), - }); + let steps = [across, down].map(|channel| channel_step(model, channel)); let (mode, levels) = (self.mode, self.levels()); - let picker = PointPicker::new(Bind::given(point), x, y).steps(steps).mark(PointMark::Ring); + let mut picker = PointPicker::new(Bind::given(point), x, y).steps(steps).mark(PointMark::Ring); + if let Some(levels) = levels { + let color = move |point: [f64; 2]| { + let mut values = base; + values.values[across] = settled(model, across, point[0] as f32); + values.values[down] = settled(model, down, point[1] as f32); + quantized(model, &values, levels) + }; + picker = picker.same(move |from, to| color(from) == color(to)); + } if field { picker.backdrop(move |painter: &ContentPainterCx, plot: Rect| paint_field(painter, plot, mode, &base, levels)) } else { picker } } } diff --git a/crates/color/src/ui/strip.rs b/crates/color/src/ui/strip.rs index 82ed3b823..9ab0f04fc 100644 --- a/crates/color/src/ui/strip.rs +++ b/crates/color/src/ui/strip.rs @@ -2,7 +2,7 @@ use super::channels::{QUANTIZE_CELLS, channel_range, channel_step, quantized, settled, snapped}; use crate::AreaMode; -use craft_behavior::nav::{LARGE_STEPS, StepResult, ValueAxes, ValueSteps, step_value}; +use craft_behavior::nav::{LARGE_STEPS, StepResult, ValueAxes, ValueSteps, step_until_changed, step_value}; use craft_behavior::{Axis, Metrics, NavMove, PartKey, Sense, ValueDrag}; use craft_theme::ModelValues; use craft_ui::contract::{ @@ -109,18 +109,10 @@ impl<'a> ColorStrip<'a> { StepResult::Set(next) => next, _ => from, }; - let mut next = f64::from(values.values[channel]); - let from = self.levels().map(|levels| quantized(model, values, levels)); - loop { - let after = once(next); - let moved = after != next; - next = after; - let unchanged = from.is_some_and(|from| quantized(model, &self.with(values, next), self.levels().unwrap_or(2)) == from); - if !(moved && unchanged) { - break; - } - } - self.with(values, next) + let levels = self.levels(); + let from = levels.map(|levels| quantized(model, values, levels)); + let same = |next: f64| levels.zip(from).is_some_and(|(levels, from)| quantized(model, &self.with(values, next), levels) == from); + self.with(values, step_until_changed(f64::from(values.values[channel]), once, same)) } /// The colour of the strip at `value` of its component. diff --git a/crates/color/tests/it/color_parts.rs b/crates/color/tests/it/color_parts.rs index 5bdea6973..edac297f8 100644 --- a/crates/color/tests/it/color_parts.rs +++ b/crates/color/tests/it/color_parts.rs @@ -1,8 +1,11 @@ //! The colour area, strip, ramp, disc and wheel: the colour maths and the edits they report. -use craft_color::ui::{WheelGeometry, hue_at, triangle_corners, triangle_point, triangle_sv}; -use craft_ui::testing::trial; -use egui::{Pos2, Rect, pos2, vec2}; +use craft_color::AreaMode; +use craft_color::ui::{ColorArea, WheelGeometry, hue_at, triangle_corners, triangle_point, triangle_sv}; +use craft_theme::{ColorModel, HsbModel, ModelValues, Srgba}; +use craft_ui::contract::{Bind, Show as _}; +use craft_ui::testing::{Fixture, Story, StoryData, StoryOutput, trial}; +use egui::{Key, Modifiers, Pos2, Rect, Ui, pos2, vec2}; fn within(a: f32, b: f32) -> bool { (a - b).abs() < 1e-3 @@ -24,3 +27,25 @@ fn the_wheel_geometry_round_trips() { let geometry = WheelGeometry::new(Rect::from_min_size(Pos2::ZERO, vec2(200.0, 200.0)), 4.0, (8.0, 24.0), 0.09, 1.0); assert!(geometry.inner < geometry.outer && geometry.triangle < geometry.inner); } + +fn web_safe_area(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + let values = data.value("values", HsbModel.from_srgb(Srgba::rgb(0.4, 0.6, 0.8))); + ColorArea::new("Saturation and brightness", AreaMode::Hue, Bind::bound(values)).quantize(6).show(ui).into() +} + +/// A key on a quantising area steps by the channel's step and goes on until the colour is a different one, so each +/// press reports a new web-safe colour. +#[trial] +fn each_key_on_a_web_safe_area_reaches_another_colour() { + let mut fixture = Fixture::story(&Story::new("web safe", web_safe_area)); + fixture.settle(); + let color = |fixture: &Fixture| HsbModel.to_srgb(&fixture.data().value("values", ModelValues::new("hsb", &[])).values); + fixture.key(Key::Tab, Modifiers::NONE); + fixture.idle(2); + for key in [Key::ArrowRight, Key::ArrowRight, Key::ArrowDown, Key::ArrowDown] { + let before = color(&fixture); + fixture.key(key, Modifiers::NONE); + fixture.idle(2); + assert_ne!(color(&fixture), before, "{key:?} reaches another colour"); + } +} diff --git a/crates/ui-widgets/src/value_drag/mod.rs b/crates/ui-widgets/src/value_drag/mod.rs index 070ede6c4..fec2aba73 100644 --- a/crates/ui-widgets/src/value_drag/mod.rs +++ b/crates/ui-widgets/src/value_drag/mod.rs @@ -9,7 +9,7 @@ pub(crate) mod track; mod words; pub use angle_dial::{AngleDial, AngleDialStyle, AngleOrientation}; -pub use point_picker::{Backdrop, PointMark, PointPicker, PointPickerStyle, PointPickerWords}; +pub use point_picker::{Backdrop, PointMark, PointPicker, PointPickerStyle, PointPickerWords, SamePoint}; pub use range_slider::{RangeSlider, RangeSliderStyle}; pub use slider::{Slider, SliderStyle, SliderThumb}; pub use thumb_row::{ diff --git a/crates/ui-widgets/src/value_drag/point_picker.rs b/crates/ui-widgets/src/value_drag/point_picker.rs index 1c724b6b5..0b7aee082 100644 --- a/crates/ui-widgets/src/value_drag/point_picker.rs +++ b/crates/ui-widgets/src/value_drag/point_picker.rs @@ -11,7 +11,7 @@ use crate::primitives::{ContentPainterCx, PaintShape}; use crate::runtime::access::Name; use crate::runtime::focus::{ActivateKeys, KeepKeys, KeyUse}; use crate::style::{ComponentStyle, FocusLook, StyleCx}; -use craft_behavior::nav::{LARGE_STEPS, STEP_SHARE, StepResult, ValueAxes, ValueSteps, snap_to_step, step_value}; +use craft_behavior::nav::{LARGE_STEPS, STEP_SHARE, StepResult, ValueAxes, ValueSteps, snap_to_step, step_until_changed, step_value}; use craft_behavior::{ AccessAction, Axis, CursorIcon, HitLevel, HitPriority, Metrics, Mixed, MoveMods, NavMove, PartKey, Sense, ValueDrag, Wheel, decimals_for_step, wheel_step, @@ -70,6 +70,18 @@ pub enum PointMark { Ring, } +/// Whether two points are the same value to the caller: `PointPicker::same`. +pub struct SamePoint<'a>(SameFn<'a>); + +type SameFn<'a> = Box bool + 'a>; + +impl<'a> SamePoint<'a> { + /// The test that runs `same`. + pub fn new(same: impl Fn([f64; 2], [f64; 2]) -> bool + 'a) -> Self { + SamePoint(Box::new(same)) + } +} + /// Paints the image under the point into the plot rect through the content painter: `PointPicker::backdrop`. pub struct Backdrop<'a>(BackdropFn<'a>); @@ -109,6 +121,12 @@ pub struct PointPicker<'a> { /// Paints the image under the point; without one the well shows a checkerboard. #[prop(func = "FnOnce(&ContentPainterCx, Rect)")] backdrop: Option>, + /// Whether two points are the same value to the caller (two points of a quantised colour field that give the same + /// colour). A relative move (an arrow, Page Up or Down, a wheel notch, an assistive step) that lands on a point + /// the same as the one it started from moves on until the point differs or the axis can go no further. Without + /// it, every distinct point differs. + #[prop(func = "Fn([f64; 2], [f64; 2]) -> bool")] + same: Option>, /// How the point is marked. #[prop] mark: PointMark, @@ -136,6 +154,7 @@ impl<'a> PointPicker<'a> { steps: None, aspect: None, backdrop: None, + same: None, mark: PointMark::Cross, } .ltr_axis() @@ -215,6 +234,16 @@ impl<'a> PointPicker<'a> { } } + /// `axis` of `from` after `step`, stepped again while the point is the same as `from` to the caller (`same`). + fn moved(&self, from: [f64; 2], axis: usize, step: impl Fn(f64) -> f64) -> f64 { + let Some(SamePoint(same)) = &self.same else { return step(from[axis]) }; + step_until_changed(from[axis], step, |value| { + let mut to = from; + to[axis] = value; + same(from, to) + }) + } + /// The axis a wheel turn steps and its new value: vertical scroll steps y and horizontal scroll x. Scrolling up /// moves the point up, as the Up key does, and right moves it right. fn wheeled(&self, wheel: &Wheel, placed: [f64; 2]) -> Option<(usize, f64)> { @@ -227,10 +256,12 @@ impl<'a> PointPicker<'a> { }; let mut turned = *wheel; turned.scroll = scroll; - match wheel_step(&self.value_steps(axis, placed[axis]), ValueAxes::Both, &turned, false) { - StepResult::Set(next) => Some((axis, self.settle(axis, next, self.grid(axis, wheel.mods.alt && !wheel.mods.shift)))), + let notch = |from: f64| match wheel_step(&self.value_steps(axis, from), ValueAxes::Both, &turned, false) { + StepResult::Set(next) => Some(self.settle(axis, next, self.grid(axis, wheel.mods.alt && !wheel.mods.shift))), _ => None, - } + }; + notch(placed[axis])?; + Some((axis, self.moved(placed, axis, |from| notch(from).unwrap_or(from)))) } /// The image fitted in `well`, else the well. @@ -355,8 +386,8 @@ impl Component for PointPicker<'_> { // While a drag runs, keys, the wheel and screen-reader requests change nothing; they are still taken. while let Some((mv, mods)) = cx.intents().take_move() { let Some((axis, toward, reach)) = key_move(mv).filter(|_| !running) else { continue }; - let from = self.placed(&now)[axis]; - now = with_axis(&now, axis, self.stepped(axis, from, toward, reach, mods)); + let next = self.moved(self.placed(&now), axis, |from| self.stepped(axis, from, toward, reach, mods)); + now = with_axis(&now, axis, next); cx.set(now); } if let Some(wheel) = cx.take_wheel() @@ -380,8 +411,8 @@ impl Component for PointPicker<'_> { _ => continue, }; if !running { - let from = self.placed(&now)[axis]; - now = with_axis(&now, axis, self.settle(axis, from + steps * self.small(axis), self.grid(axis, false))); + let next = self.moved(self.placed(&now), axis, |from| self.settle(axis, from + steps * self.small(axis), self.grid(axis, false))); + now = with_axis(&now, axis, next); cx.set(now); } }