Colour picker round 4: Grid::cells(CellAlign) sets cells at their row's top; the Full picker's halves and right half use it

- CellAlign { Center, Top } on BodyLayout::Grid and Grid::cells, with the grid_cells_top story: a cell shorter than its
  row stands in its middle (default) or at its top.
- Full picker: the two halves are a fixed, top-aligned grid, so the left half keeps its width in any room and the
  Only Web Colors row's button stays under the strip; the right half is top-aligned, so a shorter second field group
  starts level with the first and the dialog's buttons start level with the compare block.
This commit is contained in:
aiongg
2026-11-17 18:59:32 +00:00
parent 8376d6467a
commit 949be1f915
6 changed files with 80 additions and 14 deletions
+7 -5
View File
@@ -6,7 +6,7 @@ use craft_ui::choice::{Checkbox, Radio, RadioGroup};
use craft_ui::contract::{Bind, FieldLayout, FooterSlot, Gap, GlyphLabel, LabelKind, Props as _, Show as _, ShowContainer as _, SideLabel, Sizing};
use craft_ui::display::{Shade, Text};
use craft_ui::form::Field;
use craft_ui::layout::{GridFit, RowAlign, empty_cell};
use craft_ui::layout::{CellAlign, GridFit, RowAlign, empty_cell};
use craft_ui::layout_components::{Grid, Row, Stack};
use craft_ui::runtime::env::{Env, Label, UnitKind};
use craft_ui::text::{HexField, NumberField, UnitPlacement};
@@ -17,8 +17,8 @@ use super::compact::Body;
use super::{Collected, ColorPicker, ColorPickerWords, Pass, PickedColor};
use crate::ui::{AreaMode, AreaShape, CompareSwatch};
/// Two columns that never stack: the right half (compare block and actions over the two field groups), and each field
/// group (the radio cell, then the channel's letter, field and unit).
/// Two columns that never stack: the two halves of the picker, the right half (compare block and actions over the two
/// field groups), and each field group (the radio cell, then the channel's letter, field and unit).
const TWO_COLUMNS: &[ColumnWidth] = &[ColumnWidth::Content, ColumnWidth::Content];
/// The models the picker shows and the area modes it offers.
@@ -41,7 +41,9 @@ impl<'a> Body<'a> {
pub(super) fn full(&self, ui: &mut Ui, pass: &Pass<'a>, got: &mut Collected) {
let words = Env::of(ui).words::<ColorPickerWords>();
let visible: Vec<&dyn ColorModel> = self.models.iter().copied().chain([pass.mode.model()]).collect();
Row::new().gap(Gap::Section).show(ui, |ui, _| {
// The two halves in a grid that never stacks or squeezes, so each keeps its width in any room: the Only Web Colors
// row spans the area and the strip, its button under the strip's end.
Grid::new(TWO_COLUMNS).fit(GridFit::Fixed).cells(CellAlign::Top).gap(Gap::Section).show(ui, |ui, _| {
Stack::new().gap(Gap::Item).show(ui, |ui, _| {
self.area_and_strip(ui, pass, &visible, AreaShape::Square, got);
// The check box at the row's start; the rest of the row puts the screen-pick button at its end, under the
@@ -68,7 +70,7 @@ impl<'a> Body<'a> {
let group = RadioGroup::new(words.field_shows.as_str().to_owned(), Bind::given(pass.mode)).nav(NavShape::Spatial);
let picked = group
.show(ui, |ui, _| {
Grid::new(TWO_COLUMNS).fit(GridFit::Fixed).gap(Gap::Section).show(ui, |ui, _| {
Grid::new(TWO_COLUMNS).fit(GridFit::Fixed).cells(CellAlign::Top).gap(Gap::Section).show(ui, |ui, _| {
self.compare_block(ui, pass, words, got);
let _ = FooterSlot::new().show(ui);
self.field_group(ui, pass, &first, choice, true, got);
+42 -6
View File
@@ -34,9 +34,23 @@ pub enum BodyLayout {
columns: Cow<'static, [ColumnWidth]>,
/// What the grid does when its columns do not fit beside each other.
fit: GridFit,
/// Where a cell shorter than its row stands in it.
cells: CellAlign,
},
}
/// Where a `Grid` sets a cell that is shorter than its row.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum CellAlign {
/// In the row's middle: a form's label beside a taller control.
#[default]
Center,
/// At the row's top: panels of different heights side by side whose first lines stand level (the Color Picker's
/// field groups, its buttons beside its compare block).
Top,
}
/// What a `Grid` does when the least widths of its columns do not fit beside each other in its room.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
@@ -79,13 +93,21 @@ impl BodyLayout {
/// Children row-major over `columns`: a `'static` slice, or columns built in the pass.
pub fn grid(columns: impl Into<Cow<'static, [ColumnWidth]>>) -> Self {
BodyLayout::Grid { columns: columns.into(), fit: GridFit::Stack }
BodyLayout::Grid { columns: columns.into(), fit: GridFit::Stack, cells: CellAlign::Center }
}
/// A `Grid` with `fit`; any other layout is unchanged.
pub fn fit(self, fit: GridFit) -> Self {
match self {
BodyLayout::Grid { columns, .. } => BodyLayout::Grid { columns, fit },
BodyLayout::Grid { columns, cells, .. } => BodyLayout::Grid { columns, fit, cells },
other => other,
}
}
/// A `Grid` with `cells`; any other layout is unchanged.
pub fn cells(self, cells: CellAlign) -> Self {
match self {
BodyLayout::Grid { columns, fit, .. } => BodyLayout::Grid { columns, fit, cells },
other => other,
}
}
@@ -971,7 +993,7 @@ pub(crate) fn place_in_cell(ui: &mut Ui, size: Vec2, child: InFlow) -> Rect {
Some(_) => (above, (rect.height() - above - size.y).max(0.0)),
None => {
let spare = (rect.height() - size.y).max(0.0);
(spare / 2.0, spare / 2.0)
if env.grid_top { (0.0, spare) } else { (spare / 2.0, spare / 2.0) }
}
};
Rect::from_min_max(egui::pos2(rect.min.x, rect.min.y + above), egui::pos2(rect.max.x, rect.max.y - below))
@@ -1138,7 +1160,14 @@ const STACKED: &[ColumnWidth] = &[ColumnWidth::fill(1.0)];
/// 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 {
fn run_grid<R>(
ui: &mut Ui,
env: &Env,
id: Id,
(columns, fit, cells): (&[ColumnWidth], GridFit, CellAlign),
gap: Vec2,
mut body: impl FnMut(&mut Ui) -> R,
) -> R {
let ctx = ui.ctx().clone();
let rtl = env.direction.is_rtl();
let layout = child::column_layout(rtl);
@@ -1150,7 +1179,13 @@ fn run_grid<R>(ui: &mut Ui, env: &Env, id: Id, (columns, fit): (&[ColumnWidth],
let cursor = make(Flow::new(ui.available_rect_before_wrap(), rtl));
let count = columns.len();
pass_state::with::<Cursors, _>(&ctx, |cursors| cursors.0.insert(id, Cursor::Grid(cursor)));
let installed = Env::install_for(ui, scoped.derive_local(|scoped| scoped.grid = Some(id)));
let installed = Env::install_for(
ui,
scoped.derive_local(|scoped| {
scoped.grid = Some(id);
scoped.grid_top = cells == CellAlign::Top;
}),
);
let (inner, unsettled) = ColumnGroup::grid(ui, id, count, seed, &mut body);
Env::restore(&ctx, installed);
let cursor = match pass_state::with::<Cursors, _>(&ctx, |cursors| cursors.0.remove(&id)) {
@@ -1448,6 +1483,7 @@ pub(crate) fn run_body_measured<R>(
ui,
env.derive_local(|scoped| {
scoped.grid = Some(id);
scoped.grid_top = false;
scoped.row_line = Some(line);
scoped.first_line = None;
}),
@@ -1523,6 +1559,6 @@ pub(crate) fn run_body_measured<R>(
}
(inner, (*align != RowAlign::Start).then(|| took.width()))
}
BodyLayout::Grid { columns, fit } => (run_grid(ui, env, id, (&columns[..], *fit), gap, body), None),
BodyLayout::Grid { columns, fit, cells } => (run_grid(ui, env, id, (&columns[..], *fit, *cells), gap, body), None),
}
}
+1 -1
View File
@@ -7,7 +7,7 @@ mod label;
mod slot;
pub use arrange::{Placed, arrange};
pub use body::{BodyLayout, GridFit, RowAlign, ShrinkOrder, ShrinkUnit, empty_cell, shrink_widths};
pub use body::{BodyLayout, CellAlign, GridFit, RowAlign, ShrinkOrder, ShrinkUnit, empty_cell, shrink_widths};
pub use column_group::ColumnGroup;
#[doc(hidden)]
pub use column_group::ambient_width;
+4
View File
@@ -116,6 +116,8 @@ pub struct Scoped {
pub transform: Option<CanvasTransform>,
/// The `Grid` or spread `Row` body whose cursor places the children in flow.
pub(crate) grid: Option<Id>,
/// The `Grid` body of `grid` sets its cells at the top of their row (`CellAlign::Top`), not in its middle.
pub(crate) grid_top: bool,
/// The body that encloses the scope is a content-sized column: it takes its width from its widest child that is
/// not fill-width, so a fill-width child takes the width the others settle on and never sets it. Such a column
/// exists only where the width comes from content: under a content-sized mount, a row or a grid.
@@ -370,6 +372,7 @@ impl Scoped {
/// Ends the layout room of the enclosing body: its flow grid and content column.
pub(crate) fn start_room(&mut self) {
self.grid = None;
self.grid_top = false;
self.content_column = false;
self.given_column = false;
self.content = (false, false);
@@ -493,6 +496,7 @@ impl Env {
roving: None,
transform: None,
grid: None,
grid_top: false,
content_column: false,
given_column: false,
content: (false, false),
@@ -5,7 +5,7 @@ use crate::contract::{
Access, BodyCx, Common, Component, Container, ContainerProps, Cx, EndRegion, FooterPlace, FooterRegion, Gap, HeaderRegion, LabelScheme, Pad,
Role, Scroll, Sizing, ThemeScope,
};
use crate::layout::{BodyLayout, GridFit, RowAlign};
use crate::layout::{BodyLayout, CellAlign, GridFit, RowAlign};
use crate::style::{ComponentStyle, StyleCx};
use craft_behavior::{ColumnWidth, Insets, Metrics, Sense};
use egui::{Response, Ui};
@@ -328,4 +328,11 @@ impl Grid {
self.props.layout = std::mem::replace(&mut self.props.layout, BodyLayout::Stack).fit(fit);
self
}
/// Where a cell shorter than its row stands in it: [`CellAlign::Center`] (the default) in the row's middle,
/// [`CellAlign::Top`] at its top.
pub fn cells(mut self, cells: CellAlign) -> Self {
self.props.layout = std::mem::replace(&mut self.props.layout, BodyLayout::Stack).cells(cells);
self
}
}
@@ -2,7 +2,7 @@
use super::{Grid, Row, Stack};
use crate::contract::{Gap, Pad, ShowContainer as _};
use crate::layout::{GridFit, empty_cell};
use crate::layout::{CellAlign, GridFit, empty_cell};
use crate::story_kit::sample::{outlined, words};
use crate::testing::{StoryData, StoryOutput, story};
use craft_behavior::ColumnWidth;
@@ -57,3 +57,20 @@ fn grid_fixed(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
.into()
})
}
static TOP: [ColumnWidth; 2] = [ColumnWidth::Content, ColumnWidth::Content];
/// A grid whose cells stand at the top of their row: a short column of words beside a taller one starts level with it.
#[story(Grid, covers = [])]
fn grid_cells_top(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
outlined(ui, |ui| {
Grid::new(&TOP)
.cells(CellAlign::Top)
.pad(Pad::Regular)
.show(ui, |ui, _| {
Stack::new().show(ui, |ui, _| words(ui, 0..1));
Stack::new().show(ui, |ui, _| words(ui, 0..4));
})
.into()
})
}