Colour picker round 1: slot measured at content width, fixed grids measure natural sizes, row-end eyedropper

- A body-placed footer is measured in unbounded room and drawn across its slot as across an end column.
- A GridFit::Fixed grid sweeps its cells once at their own sizes in unbounded room, also inside a parent's least
  probe: it never squeezes cells to their leasts, and its least width is its width, so parents (the gallery's card
  grids) see it.
- The hex field is six digits wide from the letter column, not the column's width.
- Only Web Colors and the compact hex row put the screen-pick button at the row's end in a fill-width end row.
- The compare block's current half is a Button tinted with the current colour.
- FooterSlot declares its sample; the Full trial finds the strip as the slider after the area.
- DIAG picker prints for the slot and fixed-grid measures.
This commit is contained in:
aiongg
2026-11-17 18:31:26 +00:00
parent 7b745c75bf
commit ceaf91f6e2
7 changed files with 65 additions and 25 deletions
+10 -5
View File
@@ -2,7 +2,7 @@
use craft_behavior::{Metrics, Mixed, Sense};
use craft_theme::Srgba;
use craft_ui::choice::Swatch;
use craft_ui::buttons::Button;
use craft_ui::contract::{Access, Common, Component, Cx, MeasureCx, Outcome, PaintUi, Props as _, Role};
use craft_ui::display::{Shade, Text};
use craft_ui::primitives::{PaintShape, StrokeWidth, TextRun};
@@ -70,8 +70,9 @@ impl Outcome for CompareSwatchOutput {
///
/// - The new half is a picture: AccessKit names the block by its label ("New color") and gives the new colour's hex
/// as its value.
/// - The current half is a colour well named "Restore current color", which is also its tooltip: a click, Enter or
/// Space on it reports [`restore`](CompareSwatchOutput::restore), and the host sets the current colour again.
/// - The current half is a button named "Restore current color", which is also its tooltip, its face filled with the
/// current colour (`Button::tint`): a click, Enter or Space on it reports
/// [`restore`](CompareSwatchOutput::restore), and the host sets the current colour again.
#[must_use = "show the block with `show`"]
#[derive(craft_ui::contract::Component, craft_ui::contract::Props)]
pub struct CompareSwatch<'a> {
@@ -163,8 +164,12 @@ impl Component for CompareSwatch<'_> {
let _ = ui.child("new_caption", above, caption_text(words.new_caption.as_str()));
ui.draw().swatch_well(new, CornerRadius::ZERO, self.new.same().copied());
let restore = words.restore.as_str().to_owned();
let well = Swatch::new(restore.clone(), self.current).cell().tooltip(restore);
self.restore = ui.child("current", current, well).clicked;
let button = Button::new(restore.clone()).painted(Label::from("")).tooltip(restore);
let button = match self.current {
Mixed::Same(color) => button.tint(color.to_rgba()),
Mixed::Mixed => button,
};
self.restore = ui.child("current", current, button).clicked();
let _ = ui.child("current_caption", under, caption_text(words.current_caption.as_str()));
ui.draw().stroke(&PaintShape::Rect(block, CornerRadius::ZERO), StrokeWidth::Thin, style.edge);
// The warnings column: each chip its icon over its swatch, the first level with the block's top.
+3 -2
View File
@@ -74,9 +74,10 @@ impl<'a> Body<'a> {
}
}
}
Row::new().fill_width().align(RowAlign::SpaceBetween).show(ui, |ui, _| {
// The hex field at the row's start; the rest of the row puts the screen-pick button at its end.
Row::new().fill_width().show(ui, |ui, _| {
Field::new(words.hex.as_str().to_owned()).side(HexField::hash_label()).show(ui, |ui, _| self.hex_field(ui, pass, &words, None, got));
self.sample_button(ui, &words, got);
Row::new().fill_width().align(RowAlign::End).show(ui, |ui, _| self.sample_button(ui, &words, got));
});
}
+7 -4
View File
@@ -44,12 +44,14 @@ impl<'a> Body<'a> {
Row::new().gap(Gap::Section).show(ui, |ui, _| {
Stack::new().gap(Gap::Item).show(ui, |ui, _| {
self.area_and_strip(ui, pass, &visible, AreaShape::Square, got);
Row::new().fill_width().align(RowAlign::SpaceBetween).show(ui, |ui, _| {
// The check box at the row's start; the rest of the row puts the screen-pick button at its end, under the
// strip.
Row::new().fill_width().show(ui, |ui, _| {
let check = Checkbox::new(Bind::given(pass.web)).label(words.only_web_colors.as_str().to_owned()).show(ui);
if let Some(on) = check.value() {
got.web_pick = Some(*on);
}
self.sample_button(ui, &words, got);
Row::new().fill_width().align(RowAlign::End).show(ui, |ui, _| self.sample_button(ui, &words, got));
});
});
self.right_half(ui, pass, &visible, &words, got);
@@ -99,7 +101,7 @@ impl<'a> Body<'a> {
/// One field group: a grid of the radio cell and the channel cell (letter, field, unit), one row per channel of
/// each of `models`, an empty row between two models, and in the first group the hex row: "#" in the radio column
/// and the hex field across the channel cells.
/// and the hex field, six digits wide, from the letter column on.
fn field_group(&self, ui: &mut Ui, pass: &Pass<'a>, models: &[&'a dyn ColorModel], choice: ModelChoice<'_>, hex: bool, got: &mut Collected) {
Grid::new(TWO_COLUMNS).fit(GridFit::Fixed).gap(Gap::Row).show(ui, |ui, _| {
for (at, model) in models.iter().enumerate() {
@@ -112,7 +114,8 @@ impl<'a> Body<'a> {
}
if hex {
Text::new(Label::from(HexField::HASH)).shade(Shade::Muted).show(ui);
self.hex_field(ui, pass, choice.words, Some(Sizing::FillWidth), got);
// Six digits wide from the letter column on; the content-sized row keeps it from stretching to the cell.
Row::new().show(ui, |ui, _| self.hex_field(ui, pass, choice.words, Some(Sizing::Content), got));
}
});
}
+6 -4
View File
@@ -161,12 +161,14 @@ impl FullParts {
fn of(fixture: &Fixture, warnings: usize) -> FullParts {
let rect = |label: &str| fixture.by_label(label).unwrap_or_else(|| panic!("the picker shows {label}")).rect;
let text = |text: &str| fixture.texts().iter().filter(|run| run.text == text).map(|run| run.rect).collect::<Vec<_>>();
let strips = fixture.by_role(Role::SLIDER);
assert_eq!(strips.len(), 1, "the Full picker's one slider is its strip");
let area = rect("Saturation and brightness");
// The area's point picker reports its two axes as sliders; the strip is the slider after the area.
let strips: Vec<Rect> = fixture.by_role(Role::SLIDER).into_iter().map(|t| t.rect).filter(|r| r.left() >= area.right() - SLACK).collect();
assert_eq!(strips.len(), 1, "one slider, the strip, stands after the area: {strips:?}");
let tips = ["Out of gamut for printing", "Not a web color"];
FullParts {
area: rect("Saturation and brightness"),
strip: strips[0].rect,
area,
strip: strips[0],
block: rect("New color"),
current: rect("Restore current color"),
new_caption: *text("new").first().expect("the new caption"),
+12 -7
View File
@@ -803,21 +803,23 @@ impl<C: Container> ShapeHooks<C> for BodyHooks<'_> {
.with_owner(owner);
close = close.or(self.end_region(comp, ui, &mut end_cx, &end_frame, &props, regions.end, None));
}
// A footer the body places is measured before the body, as the end region is, in a content column, so the slot
// takes its size; the real run takes that size from the measuring run's record and draws the footer in the
// slot after the body.
// A footer the body places is measured before the body, as the end region is, in a content column of unbounded
// room, so it takes its content's size and the slot takes that; the real run takes the size from the measuring
// run's record and draws the footer in the slot after the body, across the slot as across an end column.
let in_body = footer_in_body(&props);
if in_body && !real && is_open {
let at = Rect::from_min_size(regions.body.min, Vec2::new(regions.body.width(), f32::INFINITY));
let slot_env = in_body.then(|| region_env.derive_local(|scoped| scoped.content_column = true));
if let (true, false, Some(slot_env)) = (is_open, real, slot_env.as_ref()) {
let at = Rect::from_min_size(regions.body.min, Vec2::INFINITY);
let mut bcx = BodyCx::new(body_id, env_view, resolved, at, &mut *cx.core.memory, &mut open, gate.as_mut())
.with_field_message(field_message.clone())
.with_owner(owner);
let mut footer_ui = frame.region(ui, "footer", at);
let installed = Env::install_for(&footer_ui, region_env.derive_local(|scoped| scoped.content_column = true));
let installed = Env::install_for(&footer_ui, slot_env.clone());
comp.footer(&mut footer_ui, &mut bcx);
Env::restore(&ctx, installed);
close = close.or(bcx.close_requested());
self.footer_size = Some(self.region_size(&footer_ui));
eprintln!("DIAG picker footer slot {id:?} measured {:?}", self.footer_size);
}
let footer_slot =
in_body.then(|| FooterSlotOf { owner: id, size: if real { self.measured_footer } else { self.footer_size.unwrap_or(Vec2::ZERO) } });
@@ -1148,7 +1150,10 @@ impl<C: Container> ShapeHooks<C> for BodyHooks<'_> {
if let (Some(_), Some(view)) = (slot, self.view) {
footer_ui.set_clip_rect(view.intersect(footer_ui.clip_rect()));
}
let installed = Env::install_for(&footer_ui, footer_env);
let installed = Env::install_for(&footer_ui, slot.and(slot_env).unwrap_or(footer_env));
if let Some(slot) = slot {
eprintln!("DIAG picker footer slot {id:?} drawn at {slot:?}");
}
comp.footer(&mut footer_ui, &mut bcx);
Env::restore(&ctx, installed);
close = close.or(bcx.close_requested());
@@ -21,6 +21,7 @@ pub struct FooterSlotStyle {
/// their order stay the container's. Under no such container the slot takes no room, so a component that offers the
/// cell shows it whether or not a container fills it.
#[derive(crate::contract::Component)]
#[component(sample = FooterSlot::new)]
pub struct FooterSlot {
#[common]
common: Common,
+26 -3
View File
@@ -1135,8 +1135,9 @@ const STACKED: &[ColumnWidth] = &[ColumnWidth::fill(1.0)];
/// as a probe, every leaf at its least and no column bounded, which gives each column its least, and then once more:
/// each column keeps at least its least, a `Content` column measuring in the room the others leave at theirs. When
/// the leasts do not fit beside each other, that last sweep lays the cells out one under another across the room
/// instead, so no column is cut; a `GridFit::Fixed` grid keeps its columns beside each other at their leasts and
/// overflows the room. The real run places the cells from the widths, leasts and layout the measuring run recorded.
/// instead, so no column is cut. A `GridFit::Fixed` grid sweeps its cells once at their own sizes in unbounded room,
/// whatever the room and the probe, and overflows the room. The real run places the cells from the widths, leasts and
/// layout the measuring run recorded.
fn run_grid<R>(ui: &mut Ui, env: &Env, id: Id, (columns, fit): (&[ColumnWidth], GridFit), gap: Vec2, mut body: impl FnMut(&mut Ui) -> R) -> R {
let ctx = ui.ctx().clone();
let rtl = env.direction.is_rtl();
@@ -1170,6 +1171,28 @@ fn run_grid<R>(ui: &mut Ui, env: &Env, id: Id, (columns, fit): (&[ColumnWidth],
let last = measured_tracks(&ctx, false, id, columns.len());
return child::block(ui, None, layout, |ui| sweep(ui, env, None, columns, &|flow| GridCursor::new(flow, columns, gap, &last)).0).0;
}
// A fixed grid measures its cells at their own sizes in a room unbounded across, never at their leasts, also inside
// a parent's probe for its least: its columns are what its cells take however short the room, so it neither stacks
// nor squeezes them, and its least width is its width. The room then clips or scrolls it.
if fit == GridFit::Fixed {
let natural = env.derive_local(|scoped| {
scoped.unbounded.0 = true;
scoped.least = false;
});
let (inner, measured, ..) =
child::block(ui, None, layout, |ui| sweep(ui, &natural, None, columns, &|flow| GridCursor::unbounded(flow, columns, gap))).0;
eprintln!("DIAG picker fixed grid {id:?} columns {:?} rows {:?}", measured.columns, measured.rows);
for (i, width) in measured.columns.iter().enumerate() {
found(&ctx, true, column_id(id, i), ui.available_rect_before_wrap().size(), Vec2::new(*width, 0.0));
}
for (i, height) in measured.rows.iter().enumerate() {
found(&ctx, true, row_id(id, i), ui.available_rect_before_wrap().size(), Vec2::new(0.0, *height));
}
let room = ui.available_rect_before_wrap().size();
found(&ctx, true, sub(id, "rows"), room, Vec2::new(measured.rows.len() as f32, 0.0));
found(&ctx, true, sub(id, "stacked"), room, Vec2::ZERO);
return inner;
}
// A measuring run under a body sized from its content gives each fill column the width its cells take, as a
// content column: the room it would share is not the body's to claim.
let content_run = env.content.0;
@@ -1241,7 +1264,7 @@ fn run_grid<R>(ui: &mut Ui, env: &Env, id: Id, (columns, fit): (&[ColumnWidth],
})
.collect();
let gaps = gap.x * columns.len().saturating_sub(1) as f32;
let stacked = fit == GridFit::Stack && least.iter().sum::<f32>() + gaps > ui.available_rect_before_wrap().width() + 0.5;
let stacked = least.iter().sum::<f32>() + gaps > ui.available_rect_before_wrap().width() + 0.5;
let (columns, last) = match stacked {
true => (STACKED, GridMeasure::default()),
false => (columns, GridMeasure::default().with_least(least.clone())),