color-ui: StopColor::rect carries the rect of what asked for the colour, the stop's marker or the Color well

This commit is contained in:
bflatastic
2026-11-22 08:36:06 +07:00
parent 7e97036522
commit 82371efbb3
5 changed files with 50 additions and 17 deletions
+3 -3
View File
@@ -316,10 +316,10 @@ impl<'a, P: Clone + Send + Sync + 'static> GradientEditor<'a, P> {
stops.reborrow().write(cx, &event);
edit.events.push(event);
}
color_requested = color_requested.filter(|i| *i < now.color.len());
if let Some(i) = color_requested {
color_requested = color_requested.filter(|(i, _)| *i < now.color.len());
if let Some((i, rect)) = color_requested {
let request = ColorRequest::new(REQUEST_KEY, stop_color(&now, rules.rows, i)).title(label.clone());
output = Some(StopColor::new(i, now.color[i].tag, request));
output = Some(StopColor::new(i, now.color[i].tag, request).at(rect));
}
}
if now_selected != held {
+10 -5
View File
@@ -146,8 +146,8 @@ pub struct GradientBarOutput<P = ()> {
pub edit: EditResponse<GradientStops>,
/// The selection after this pass, when the bar changed it.
pub select: Option<Option<StopRef>>,
/// The colour stop whose colour is asked for: a double click on it, Enter or Space.
pub color_requested: Option<usize>,
/// The colour stop whose colour is asked for (a double click on it, Enter or Space) and its marker's square.
pub color_requested: Option<(usize, Rect)>,
/// Enter or Space on an opacity stop or a midpoint: its first field takes the focus.
pub field_requested: bool,
/// A payload dropped on the bar: where it landed.
@@ -191,7 +191,7 @@ pub struct GradientBar<'a, P: Clone + Send + Sync + 'static = ()> {
#[prop]
selected_marker: Option<StopRef>,
select: Option<Option<StopRef>>,
color_requested: Option<usize>,
color_requested: Option<(usize, Rect)>,
field_requested: bool,
dropped: Option<(StopDrop, P)>,
payload: PhantomData<fn() -> P>,
@@ -370,6 +370,11 @@ impl Geometry {
Rect::from_min_size(pos2(self.x(at) - self.marker / 2.0, top), vec2(self.marker, self.marker))
}
/// The square of colour stop `i`.
fn color_square(&self, stops: &GradientStops, i: usize) -> Rect {
self.body(StopRow::Color, location(stops, StopRow::Color, i))
}
/// What a press takes for marker `m`: a stop within `hit` across, from a diamond's radius away from the ramp's edge
/// to the marker's far edge; a midpoint within `hit` of its diamond.
fn hit_rect(&self, stops: &GradientStops, m: StopRef) -> Rect {
@@ -538,7 +543,7 @@ impl<P: Clone + Send + Sync + 'static> GradientBar<'_, P> {
if press.double_pressed()
&& let StopRef::Color(i) = marker
{
self.color_requested = Some(i);
self.color_requested = Some((i, geo.color_square(stops, i)));
}
}
PressPhase::DragStart { start, pos, .. } if start.button == PointerButton::Primary => {
@@ -647,7 +652,7 @@ impl<P: Clone + Send + Sync + 'static> GradientBar<'_, P> {
let activated = self.selected_marker.is_some_and(|m| matches!(cx.part_activated(part_of(m)), Some(FireSource::Key | FireSource::Access)));
if activated {
match self.selected_marker {
Some(StopRef::Color(i)) => self.color_requested = Some(i),
Some(StopRef::Color(i)) => self.color_requested = Some((i, geo.color_square(&stops, i))),
Some(_) => self.field_requested = true,
None => {}
}
@@ -13,7 +13,7 @@ use craft_ui::contract::{Bind, BodyCx, Props as _, Remember as _, Show as _, Sho
use craft_ui::form::{Field, FieldGroup};
use craft_ui::runtime::env::{Label, UnitKind};
use craft_ui::text::NumberField;
use egui::{Id, Ui};
use egui::{Id, Rect, Ui};
use crate::gradient::stops::{self, StopRules, locations, midpoint};
use crate::gradient::{GradientStops, StopRow, StopRows};
@@ -62,8 +62,8 @@ pub(super) struct Report {
pub events: Vec<EditEvent<GradientStops>>,
/// The selection the fields moved.
pub select: Option<Option<StopRef>>,
/// The colour stop whose colour the host is asked to edit.
pub color_requested: Option<usize>,
/// The colour stop whose colour the host is asked to edit, and the rect of what asked.
pub color_requested: Option<(usize, Rect)>,
pub screen_pick: Option<ScreenPickGesture>,
}
@@ -78,7 +78,7 @@ pub(super) struct Row<'r, 'a> {
pub picker: &'r Option<PickerSetup<'a>>,
pub sampled: Option<Srgba>,
/// The colour request of the bar, when the bar asked for one.
pub requested: Option<usize>,
pub requested: Option<(usize, Rect)>,
}
/// A 0..=1 value in percent, without the binary fraction an `f32` adds (0.3 is 30, not 30.0000012).
@@ -222,7 +222,7 @@ impl Row<'_, '_> {
}
self.popover(ui, cx, now, i, &well.response, report);
} else if well.clicked {
report.color_requested = Some(i);
report.color_requested = Some((i, well.response.rect));
}
}
if rows == StopRows::Combined {
+12 -1
View File
@@ -1,6 +1,7 @@
//! The stop bar's marker vocabulary: [`StopRef`] and [`StopColor`].
use craft_ui::runtime::requests::ColorRequest;
use egui::Rect;
use crate::gradient::StopRow;
@@ -69,11 +70,21 @@ pub struct StopColor {
pub tag: u64,
/// The request, opened at the stop's colour.
pub request: ColorRequest,
/// What asked for the colour (the stop's marker, or the Color well), where the host opens its picker; `None`
/// when the request carries no place.
pub rect: Option<Rect>,
}
impl StopColor {
/// A request to edit colour stop `index`, whose tag is `tag`.
pub fn new(index: usize, tag: u64, request: ColorRequest) -> Self {
Self { index, tag, request }
Self { index, tag, request, rect: None }
}
/// The request raised at `rect`.
#[must_use]
pub fn at(mut self, rect: Rect) -> Self {
self.rect = Some(rect);
self
}
}
+20 -3
View File
@@ -10,7 +10,7 @@ use craft_color_ui::ui::gradient::{ColorStop, GradientEditor, GradientStops, Opa
use craft_theme::Srgba;
use craft_ui::contract::Bind;
use craft_ui::testing::{Fixture, trial};
use egui::{Color32, Event, Modifiers, Shape, TextureId, Vec2, vec2};
use egui::{Color32, Event, Key, Modifiers, Rect, Shape, TextureId, Vec2, vec2};
const RED: Srgba = Srgba::rgb(1.0, 0.0, 0.0);
const BLUE: Srgba = Srgba::rgb(0.0, 0.0, 1.0);
@@ -45,7 +45,7 @@ fn the_default_sampler_bends_each_segment_to_its_midpoint_and_splits_alpha_by_la
#[derive(Default)]
struct Seen {
events: Vec<&'static str>,
requested: Vec<usize>,
requested: Vec<(usize, Option<Rect>)>,
dropped: Vec<StopDrop>,
selected: Option<StopRef>,
}
@@ -82,7 +82,7 @@ fn rig_below(start: GradientStops, rules: StopRules, fields: bool, above: f32) -
let out = GradientEditor::new("Gradient", Bind::bound(&mut stops)).rules(rules.clone()).fields(fields).drop_target::<u32>().show(ui);
let mut seen = report.borrow_mut();
seen.events.extend(out.edit.events.iter().map(kind));
seen.requested.extend(out.color_requested.iter().map(|c| c.index));
seen.requested.extend(out.color_requested.iter().map(|c| (c.index, c.rect)));
seen.dropped.extend(out.dropped.iter().map(|(place, _)| *place));
seen.selected = out.selected;
});
@@ -111,6 +111,23 @@ impl Rig {
}
}
/// The host opens its picker where the request came from: under the stop's marker for Enter on it, at the Color well
/// for a click on the well.
#[trial]
fn a_colour_request_carries_the_rect_of_what_asked() {
let mut rig = rig(three(), StopRules::default(), true);
let last = rig.marker("Color stop 3 of 3");
rig.fixture.click(&last).key(Key::Enter, Modifiers::NONE).idle(1);
let requested = std::mem::take(&mut rig.seen.borrow_mut().requested);
let Some(&(2, Some(rect))) = requested.last() else { panic!("Enter asks for the last stop's colour at a place: {requested:?}") };
assert!(rect.x_range().contains(last.center().x), "under the last stop's marker: {rect:?}, marker {:?}", last.rect);
let well = rig.marker("Color");
rig.fixture.click(&well).idle(1);
let requested = std::mem::take(&mut rig.seen.borrow_mut().requested);
let Some(&(2, Some(rect))) = requested.last() else { panic!("the well asks for the selected stop's colour at a place: {requested:?}") };
assert!(rect.intersects(well.rect), "at the well: {rect:?}, well {:?}", well.rect);
}
#[trial]
fn a_drag_past_a_neighbour_is_one_gesture_and_the_selection_follows_the_stop() {
let mut rig = rig(three(), StopRules::default(), false);