From f6ae74bf483614de704fb6dce72eac439fa2dbd3 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Fri, 27 Nov 2026 14:20:03 +0000 Subject: [PATCH] A grid's max_rows counts whole visual rows, each as tall as its tallest tile, and min_rows still pads rows it does not lay out Co-Authored-By: Claude Opus 5.5 --- .../ui-contract/src/contract/items/hooks.rs | 43 +++++++++++-------- .../ui-widgets/src/tile_grid/grid/stories.rs | 1 - crates/ui-widgets/tests/it/tile_grid/grid.rs | 2 +- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index 0febacc6d9..aa30187305 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -1133,35 +1133,42 @@ fn rename_input( } /// 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. +/// each one it does not have. A grid's row is a visual row of items side by side, as tall as its tallest item (a +/// tile and its measured caption), from the layout's top, so the rows above it are whole; a run of hidden items is +/// no row. pub(super) fn rows_height(layout: &impl ItemLayout, len: usize, rows: u16, row: f32) -> f32 { + use craft_behavior::NavShape; let rows = usize::from(rows); - match layout.nav() { - craft_behavior::NavShape::WrappedGrid { columns, .. } | craft_behavior::NavShape::BoundedGrid { columns, .. } => { - let count = rows.saturating_mul(columns.max(1)).min(len); - return if count == 0 { 0.0 } else { layout.rect(count - 1, ScrollPos::default()).bottom() }; + let (laid, built) = match layout.nav() { + NavShape::WrappedGrid { columns, .. } | NavShape::BoundedGrid { columns, .. } => { + let columns = columns.max(1); + let count = rows.saturating_mul(columns).min(len); + let laid = count.div_ceil(columns); + let last = laid.saturating_sub(1) * columns..count; + (laid, last.map(|at| layout.rect(at, ScrollPos::default()).max.y).fold(0.0_f32, f32::max)) } - craft_behavior::NavShape::RaggedGrid => { - let (mut at, mut shown, mut bottom) = (0, 0, 0.0_f32); + NavShape::RaggedGrid => { + let (mut at, mut laid, mut built) = (0, 0, 0.0_f32); let mut index = 0; - while at < len && shown < rows { + while at < len && laid < rows { let Some(count) = layout.row_len(index).filter(|count| *count > 0) else { break }; let end = at.saturating_add(count).min(len); - if let Some(first) = (at..end).find(|&at| layout.shown(at)) { - bottom = bottom.max(layout.rect(first, ScrollPos::default()).bottom()); - shown += 1; + if let Some(tallest) = (at..end).filter(|&at| layout.shown(at)).map(|at| layout.rect(at, ScrollPos::default()).max.y).reduce(f32::max) { + built = built.max(tallest); + laid += 1; } at = end; index += 1; } - return bottom; + (laid, built) } - _ => {} - } - let laid = rows.min(len); - let top = if len == 0 { 0.0 } else { layout.rect(0, ScrollPos::default()).min.y }; - let built = if laid == 0 { 0.0 } else { layout.rect(laid - 1, ScrollPos::default()).max.y - top }; - built + (rows - laid) as f32 * row + _ => { + let laid = rows.min(len); + let top = if len == 0 { 0.0 } else { layout.rect(0, ScrollPos::default()).min.y }; + (laid, if laid == 0 { 0.0 } else { layout.rect(laid - 1, ScrollPos::default()).max.y - top }) + } + }; + built + rows.saturating_sub(laid) as f32 * row } impl ItemsHooks<'_, '_, C> { diff --git a/crates/ui-widgets/src/tile_grid/grid/stories.rs b/crates/ui-widgets/src/tile_grid/grid/stories.rs index a70424aeb5..8cf03d690f 100644 --- a/crates/ui-widgets/src/tile_grid/grid/stories.rs +++ b/crates/ui-widgets/src/tile_grid/grid/stories.rs @@ -58,7 +58,6 @@ fn tile_grid_one_column(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { grid(ui, data, TileGrid::new("Library").column_rule(ColumnRule::Exactly(1)), 9, 1.0).into() } -/// A short viewport over multiple columns: the grid's own row geometry caps the height and owns scrolling. #[story(TileGrid<'static, usize>, covers = [], drive = Drive::Never)] fn tile_grid_capped_rows(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { grid(ui, data, TileGrid::new("Library").column_rule(ColumnRule::Exactly(3)).max_rows(2), 24, 1.0).into() diff --git a/crates/ui-widgets/tests/it/tile_grid/grid.rs b/crates/ui-widgets/tests/it/tile_grid/grid.rs index fd31086923..7dd149263d 100644 --- a/crates/ui-widgets/tests/it/tile_grid/grid.rs +++ b/crates/ui-widgets/tests/it/tile_grid/grid.rs @@ -41,7 +41,7 @@ fn a_tile_grid_caps_visual_rows_and_scrolls_to_later_tiles() { .zoom(GridZoom::Fixed(0.25)) .max_rows(2); let grid = if let Some(key) = requested.get() { grid.reveal(key) } else { grid }; - grid.show_items(ui, &source, &mut |key, _| Tile::new(format!("Tile {key}"))); + let _ = grid.show_items(ui, &source, &mut |key, _| Tile::new(format!("Tile {key}"))); Button::new("After grid").show(ui); }); fixture.room(Vec2::new(500.0, 900.0)).settle();