mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-30 18:27:00 +01:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
a84275d0d4
commit
f6ae74bf48
3 files changed
+26
-20
No files matched your search
@@ -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<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user