From eab5b32afad0a0f14d8a9351cc3aea6f27fc898e Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 10:39:32 +0000 Subject: [PATCH] Collection row height: CollectionProps::row_height picks control-high or text rows, one resolver for the item layouts, the min_rows fill and ListRow's least height, which each ListRow collection sets in dress --- crates/ui-contract/src/contract/collection.rs | 3 +- .../src/contract/collection/props.rs | 36 ++++++++++++++++++- .../ui-contract/src/contract/items/hooks.rs | 6 ++-- crates/ui-contract/src/contract/mod.rs | 2 +- crates/ui-macros/src/props.rs | 7 +++- crates/ui-widgets/src/list/events.rs | 9 +++-- crates/ui-widgets/src/list/findings.rs | 8 +++-- crates/ui-widgets/src/list/row.rs | 13 ++++++- crates/ui-widgets/src/list/row/placement.rs | 16 ++++----- crates/ui-widgets/src/list/row/show.rs | 2 +- crates/ui-widgets/src/list/undo.rs | 8 +++-- crates/ui-widgets/src/list/view.rs | 3 +- crates/ui-widgets/src/table/view.rs | 11 ++++-- 13 files changed, 97 insertions(+), 27 deletions(-) diff --git a/crates/ui-contract/src/contract/collection.rs b/crates/ui-contract/src/contract/collection.rs index ac4861d7f..67490f49b 100644 --- a/crates/ui-contract/src/contract/collection.rs +++ b/crates/ui-contract/src/contract/collection.rs @@ -12,7 +12,8 @@ pub use events::{ CollectionEvent, ColumnEvent, ColumnRequest, ContextMenuRequest, HeaderMenuRequest, ListDrag, OpenChange, Rename, ReorderMove, ToggleEdit, }; pub use props::{ - ActivateOn, CollectionProps, DragPayload, ItemKeys, LeadOnOpen, LeadSource, ReorderRule, RowPressed, RowState, SelectMode, SpaceAction, + ActivateOn, CollectionProps, DragPayload, ItemKeys, LeadOnOpen, LeadSource, ReorderRule, RowHeight, RowPressed, RowState, SelectMode, + SpaceAction, }; pub(crate) use response::Gestures; pub(crate) use response::ItemsOut; diff --git a/crates/ui-contract/src/contract/collection/props.rs b/crates/ui-contract/src/contract/collection/props.rs index 10de014fb..07554cf45 100644 --- a/crates/ui-contract/src/contract/collection/props.rs +++ b/crates/ui-contract/src/contract/collection/props.rs @@ -4,6 +4,7 @@ use super::columns::{ColumnSort, ColumnSpec}; use super::events::ListDrag; use crate::contract::Notch; use crate::runtime::env::{Counted, Label}; +use crate::style::StyleCx; use craft_behavior::{Axis, ColumnKey}; use std::any::Any; use std::sync::Arc; @@ -117,6 +118,29 @@ pub enum LeadOnOpen { First, } +/// How tall a collection's rows are. Read-only display tables (a metadata table) take text rows; lists the user picks +/// from or navigates keep control-high rows, a pointer target as tall as a control. A row with a thumbnail or a +/// detail line is as tall as those need either way. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +#[non_exhaustive] +pub enum RowHeight { + /// A control high: `size.control` at the collection's control size. + #[default] + Control, + /// One text line and a tight gap: `size.row.text`. + Text, +} + +impl RowHeight { + /// The height of one row under `cx`. + pub fn of(self, cx: &StyleCx<'_>) -> f32 { + match self { + RowHeight::Control => cx.control().height, + RowHeight::Text => cx.tokens.size.row.text, + } + } +} + /// What a collection reads from the app: plain data. A new builder's name is added to the forwarder list in /// `crates/ui-macros/src/props.rs`, so the components that embed these props forward it. #[derive(Clone, Debug)] @@ -191,12 +215,14 @@ pub struct CollectionProps { /// the lead scrolls the page to keep the row in view. For short collections only; a long one keeps its own /// scroll and is shown outside a scrolling body (16.3). pub natural_height: bool, - /// The least number of rows the collection is tall: rows it does not have count the control height, so an empty + /// The least number of rows the collection is tall: rows it does not have count the row height, so an empty /// collection stays a target of its size. pub min_rows: u16, /// The most rows the collection shows: past them it is as tall as its first `max_rows` rows and scrolls them. It /// owns that scroll inside a scrolling body too, since its height is bounded and the wheel passes on at its ends. pub max_rows: Option, + /// How tall the rows are. + pub row_height: RowHeight, /// A Cmd/Ctrl click on an item, on its own area or a part that picks it, is a command on the item, not a pick: the /// selection stays and the release reports `CollectionEvent::CommandClick`. pub command_click: bool, @@ -287,6 +313,7 @@ impl CollectionProps { natural_height: false, min_rows: 0, max_rows: None, + row_height: RowHeight::Control, command_click: false, notch: None, overflow: Axis::Y, @@ -507,6 +534,13 @@ impl CollectionProps { self } + /// The rows are `height` tall (see [`RowHeight`]). + #[must_use] + pub const fn row_height(mut self, height: RowHeight) -> Self { + self.row_height = height; + self + } + /// A Cmd/Ctrl click on an item is a command, not a pick (see [`CollectionProps::command_click`]). #[must_use] pub const fn command_click(mut self, on: bool) -> Self { diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index 0bfaf9a21..116ac9f40 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -228,7 +228,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { let content = layout.content_size(); self.content = Vec2::new(content.x as f32, content.y as f32); let props = comp.items(); - let row = scx.control().height; + let row = props.row_height.of(scx); let floor = rows_height(&layout, self.source.len(), props.min_rows, row); let cap = props.max_rows.map(|rows| rows_height(&layout, self.source.len(), rows, row).max(floor)); self.columns = layout.column_count() as u32; @@ -904,8 +904,8 @@ impl ItemsHooks<'_, '_, C> { } } -/// The height of the first `rows` rows of `layout` over `len` items: the rows it lays out, and the control height `row` -/// for each one it does not have. +/// The height of the first `rows` rows of `layout` over `len` items: the rows it lays out, and the row height `row` for +/// each one it does not have. fn rows_height(layout: &impl ItemLayout, len: usize, rows: u16, row: f32) -> f32 { let rows = usize::from(rows); let laid = rows.min(len); diff --git a/crates/ui-contract/src/contract/mod.rs b/crates/ui-contract/src/contract/mod.rs index a6e9385d5..25dbd3cf6 100644 --- a/crates/ui-contract/src/contract/mod.rs +++ b/crates/ui-contract/src/contract/mod.rs @@ -51,7 +51,7 @@ pub use body::pad_of; pub use collection::{ ActivateOn, CollectionEvent, CollectionProps, CollectionResponse, ColumnAlign, ColumnEvent, ColumnKind, ColumnRequest, ColumnSort, ColumnSpec, ContextMenuRequest, DragPayload, DropAt, DropKind, DropPayload, DropPlace, HeaderMenuRequest, ItemKeys, LeadOnOpen, LeadSource, ListDrag, - OpenChange, PendingDrop, Rename, ReorderMove, ReorderRule, RowPressed, RowState, SelectMode, ShownRow, SpaceAction, ToggleColumn, ToggleEdit, + OpenChange, PendingDrop, Rename, ReorderMove, ReorderRule, RowHeight, RowPressed, RowState, SelectMode, ShownRow, SpaceAction, ToggleColumn, ToggleEdit, }; #[doc(hidden)] pub use container::InBar; diff --git a/crates/ui-macros/src/props.rs b/crates/ui-macros/src/props.rs index a79d2442a..16aebc853 100644 --- a/crates/ui-macros/src/props.rs +++ b/crates/ui-macros/src/props.rs @@ -189,8 +189,13 @@ const COLLECTION: &[Forwarded] = &[ ("deletable", "bool", "Delete and Backspace at the lead report the items to delete."), ("drag_count", "::craft_ui::runtime::env::Counted", "The drag badge reads this count word for the dragged items, one included."), ("natural_height", "bool", "The collection takes its full content height and owns no scroll: for a short collection inside a scrolling page."), - ("min_rows", "u16", "The collection is at least this many rows tall; rows it does not have count the control height."), + ("min_rows", "u16", "The collection is at least this many rows tall; rows it does not have count the row height."), ("max_rows", "u16", "The collection shows at most this many rows and scrolls the rest, inside a scrolling body too."), + ( + "row_height", + "::craft_ui::contract::RowHeight", + "How tall the rows are: text rows for a read-only display table, control-high rows for a list the user picks from.", + ), ("notch", "::craft_ui::contract::Notch", "The distance one wheel notch scrolls the rows."), ("command_click", "bool", "A Cmd/Ctrl click on an item is a command (`CollectionEvent::CommandClick`), not a pick."), ( diff --git a/crates/ui-widgets/src/list/events.rs b/crates/ui-widgets/src/list/events.rs index b384a660d..2c42137a4 100644 --- a/crates/ui-widgets/src/list/events.rs +++ b/crates/ui-widgets/src/list/events.rs @@ -4,7 +4,8 @@ use super::view_words::EventWords; use super::{ListRow, RowEvent}; use crate::contract::{ - Bind, BindMut, Collection, CollectionProps, CollectionResponse, Common, Component, ContainerProps, Cx, Props as _, Role, SelectMode, Sizing, + Bind, BindMut, Collection, CollectionProps, CollectionResponse, Common, Component, ContainerProps, Cx, Props as _, Role, RowState, SelectMode, + Sizing, }; use crate::primitives::IconSpec; use crate::runtime::env::{Label, WordSet as _}; @@ -247,7 +248,11 @@ impl Collection for EventList<'_> { } fn item_layout(&self, source: &dyn ItemSource, width: f32, cx: &StyleCx<'_>) -> SpanLayout { - SpanLayout::sized(source, width, cx.control().height) + SpanLayout::sized(source, width, self.items.row_height.of(cx)) + } + + fn dress<'r>(&self, row: Self::Item<'r>, _: &RowState) -> Self::Item<'r> { + row.rows(self.items.row_height) } } diff --git a/crates/ui-widgets/src/list/findings.rs b/crates/ui-widgets/src/list/findings.rs index c5125d652..b790fdd90 100644 --- a/crates/ui-widgets/src/list/findings.rs +++ b/crates/ui-widgets/src/list/findings.rs @@ -6,7 +6,7 @@ use super::words::ListWords; use super::{ListRow, RowEvent}; use crate::contract::{ ActivateOn, Bind, BindMut, Collection, CollectionProps, CollectionResponse, Common, Component, ContainerProps, Cx, MeasureCx, PaintUi, - Props as _, Role, SelectMode, Sizing, Styled as _, WordSetCx as _, + Props as _, Role, RowState, SelectMode, Sizing, Styled as _, WordSetCx as _, }; use crate::primitives::{IconSpec, TextRun, glyph}; use crate::runtime::env::{Label, WordSet as _}; @@ -208,7 +208,11 @@ impl Collection for FindingsList<'_> { } fn item_layout(&self, source: &dyn ItemSource, width: f32, cx: &StyleCx<'_>) -> SpanLayout { - SpanLayout::sized(source, width, cx.control().height) + SpanLayout::sized(source, width, self.items.row_height.of(cx)) + } + + fn dress<'r>(&self, row: Self::Item<'r>, _: &RowState) -> Self::Item<'r> { + row.rows(self.items.row_height) } } diff --git a/crates/ui-widgets/src/list/row.rs b/crates/ui-widgets/src/list/row.rs index c33e0efa5..69287e658 100644 --- a/crates/ui-widgets/src/list/row.rs +++ b/crates/ui-widgets/src/list/row.rs @@ -9,7 +9,7 @@ use super::toggle::ToggleCell; use super::words::ListWords; -use crate::contract::{Common, OpenChange, RowPressed, RowState, Show}; +use crate::contract::{Common, OpenChange, RowHeight, RowPressed, RowState, Show}; use crate::primitives::{ContentPainterCx, HAlign, IconSpec}; use crate::runtime::env::Label; use craft_behavior::ColumnKey; @@ -133,6 +133,9 @@ pub struct ListRow<'a> { #[style_key] #[prop] pub(super) role: Option, + /// How tall the row is: its collection sets it from `CollectionProps::row_height` in `Collection::dress`. + #[style_key] + pub(super) rows: RowHeight, /// The label in italics. #[prop] pub(super) italic: bool, @@ -257,6 +260,7 @@ impl<'a> ListRow<'a> { layer: false, striped: false, role: None, + rows: RowHeight::Control, italic: false, highlight: false, clipped: false, @@ -329,6 +333,13 @@ impl<'a> ListRow<'a> { self } + /// The row is `rows` tall: the height its collection's rows take. + #[must_use] + pub(crate) fn rows(mut self, rows: RowHeight) -> Self { + self.rows = rows; + self + } + /// The row state the collection gives the item: the rename editor, the stripe, the app's mark and the press the /// collection holds on the row. #[must_use] diff --git a/crates/ui-widgets/src/list/row/placement.rs b/crates/ui-widgets/src/list/row/placement.rs index 2f3abd927..3a552c0e5 100644 --- a/crates/ui-widgets/src/list/row/placement.rs +++ b/crates/ui-widgets/src/list/row/placement.rs @@ -5,7 +5,7 @@ use super::show::named; use super::style::ListRowStyle; use super::thumb::{Thumb, ThumbSize}; use super::trailing::{TrailingItem, trailing_run}; -use crate::contract::{ColumnAlign, PartsCx, WordSetCx as _}; +use crate::contract::{ColumnAlign, PartsCx, RowHeight, WordSetCx as _}; use crate::layout::{ShrinkOrder, ShrinkUnit, shrink_widths}; use crate::list::toggle::ToggleState; use crate::list::words::ListWords; @@ -44,11 +44,11 @@ pub(super) struct Sizes { } impl Sizes { - pub(super) fn of(cx: &StyleCx<'_>, role: TypeRole) -> Sizes { + pub(super) fn of(cx: &StyleCx<'_>, role: TypeRole, rows: RowHeight) -> Sizes { let (control, tokens) = (cx.control(), cx.tokens); let thumbs = &tokens.size.thumb; Sizes { - row: control.height, + row: rows.of(cx), icon: control.icon, pad: control.pad_x, gap: control.gap, @@ -76,8 +76,8 @@ impl Sizes { thumb.pick(self.thumbs) } - /// The height of a row with a thumbnail of `thumb` and, if `detail`, a detail line: the control height, the - /// thumbnail with its padding (at least `thumb_row`), or the two lines of text, whichever is tallest. + /// The height of a row with a thumbnail of `thumb` and, if `detail`, a detail line: the row height, the thumbnail + /// with its padding (at least `thumb_row`), or the two lines of text, whichever is tallest. pub(super) fn height(&self, thumb: ThumbSize, detail: bool) -> f32 { let base = match self.side(thumb) { 0.0 => self.row, @@ -95,10 +95,10 @@ pub(super) fn halign(align: ColumnAlign) -> HAlign { } impl ListRow<'_> { - /// The height of a row with a thumbnail of `thumb` and, if `detail`, a detail line, for the item layout of a - /// list of such rows. + /// The height of a control-high row with a thumbnail of `thumb` and, if `detail`, a detail line, for the item + /// layout of a list of such rows. pub fn row_height(cx: &StyleCx<'_>, thumb: ThumbSize, detail: bool) -> f32 { - Sizes::of(cx, TypeRole::Body).height(thumb, detail) + Sizes::of(cx, TypeRole::Body, RowHeight::Control).height(thumb, detail) } /// Where this row's disclosure lies when it fills `rect` under `style`, with texts `text_width` wide: the rect at diff --git a/crates/ui-widgets/src/list/row/show.rs b/crates/ui-widgets/src/list/row/show.rs index 1fba012fc..aa95b2025 100644 --- a/crates/ui-widgets/src/list/row/show.rs +++ b/crates/ui-widgets/src/list/row/show.rs @@ -80,7 +80,7 @@ impl Component for ListRow<'_> { }; let weight = key.strong || key.heading; let role = key.role.unwrap_or(if weight { TypeRole::BodyStrong } else { TypeRole::Body }); - let sizes = Sizes::of(cx, role); + let sizes = Sizes::of(cx, role, key.rows); let muted = if key.ink == RowInk::Dimmed { text.faint } else { text.muted }; ListRowStyle { surface, diff --git a/crates/ui-widgets/src/list/undo.rs b/crates/ui-widgets/src/list/undo.rs index a38618958..938a7b272 100644 --- a/crates/ui-widgets/src/list/undo.rs +++ b/crates/ui-widgets/src/list/undo.rs @@ -5,7 +5,7 @@ use super::view_words::UndoWords; use super::{ListRow, RowEvent}; use crate::contract::{ ActivateOn, Bind, BindMut, Collection, CollectionEvent, CollectionProps, CollectionResponse, Common, Component, ContainerProps, Cx, Props as _, - Role, SelectMode, Sizing, + Role, RowState, SelectMode, Sizing, }; use crate::runtime::env::Label; use crate::style::{ComponentStyle, StyleCx}; @@ -178,6 +178,10 @@ impl Collection for UndoList<'_> { } fn item_layout(&self, source: &dyn ItemSource, width: f32, cx: &StyleCx<'_>) -> SpanLayout { - SpanLayout::new(source, width, cx.control().height) + SpanLayout::new(source, width, self.items.row_height.of(cx)) + } + + fn dress<'r>(&self, row: Self::Item<'r>, _: &RowState) -> Self::Item<'r> { + row.rows(self.items.row_height) } } diff --git a/crates/ui-widgets/src/list/view.rs b/crates/ui-widgets/src/list/view.rs index 6e4bef658..ccb6592d5 100644 --- a/crates/ui-widgets/src/list/view.rs +++ b/crates/ui-widgets/src/list/view.rs @@ -208,11 +208,12 @@ impl Collection for List<'_, K> { } fn item_layout(&self, source: &dyn ItemSource, width: f32, cx: &StyleCx<'_>) -> SpanLayout { - let layout = SpanLayout::sized(source, width, cx.control().height); + let layout = SpanLayout::sized(source, width, self.items.row_height.of(cx)); if self.tree { layout.tree() } else { layout } } fn dress<'r>(&self, row: Self::Item<'r>, _: &RowState) -> Self::Item<'r> { + let row = row.rows(self.items.row_height); if self.row_menu { row.row_menu() } else { row } } diff --git a/crates/ui-widgets/src/table/view.rs b/crates/ui-widgets/src/table/view.rs index deb06daae..d92e9d70d 100644 --- a/crates/ui-widgets/src/table/view.rs +++ b/crates/ui-widgets/src/table/view.rs @@ -6,7 +6,8 @@ use super::words::TableWords; use crate::contract::overlay::Anchor; use crate::contract::{ Bind, BindMut, Collection, CollectionProps, CollectionResponse, ColumnCommands, ColumnRequest, ColumnSpec, Common, Component, ContainerProps, Cx, - DropKind, EditResponse, ListDrag, OverlayUi, Overlays as _, Props as _, Role, SelectMode, Show as _, ShowContainer as _, Sizing, WordSetCx as _, + DropKind, EditResponse, ListDrag, OverlayUi, Overlays as _, Props as _, Role, RowState, SelectMode, Show as _, ShowContainer as _, Sizing, + WordSetCx as _, }; use crate::display::Divider; use crate::list::{DropTest, ListRow, RowEvent, RowTest, RowText}; @@ -192,12 +193,16 @@ impl Collection for Table<'_, K> { self.reveal.as_ref() } - /// Column widths come from the specs and the header's memory; a row is a control high. + /// Column widths come from the specs and the header's memory; a row is as tall as `CollectionProps::row_height`. fn item_layout(&self, source: &dyn ItemSource, width: f32, cx: &StyleCx<'_>) -> SpanLayout { - let layout = SpanLayout::sized(source, width, cx.control().height); + let layout = SpanLayout::sized(source, width, self.items.row_height.of(cx)); if self.tree { layout.tree() } else { layout } } + fn dress<'r>(&self, row: Self::Item<'r>, _: &RowState) -> Self::Item<'r> { + row.rows(self.items.row_height) + } + fn rename_draft(&self, key: &K) -> Option { self.rename_draft.as_ref().and_then(|draft| draft.text(key)) }