From ebf9254522733e67419cab41cf4a1c0404d152e5 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 23 Nov 2026 12:18:27 +0700 Subject: [PATCH] ui-widgets: a tile grid that takes its width from its content asks for at least one card, so a measuring run lays it out at the width it shows in and a stack sized from it holds every row and what follows --- crates/ui-widgets/src/tile_grid/grid.rs | 9 ++--- crates/ui-widgets/tests/it/tile_grid/grid.rs | 39 +++++++++++++++++++- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/crates/ui-widgets/src/tile_grid/grid.rs b/crates/ui-widgets/src/tile_grid/grid.rs index 1f345d1ef4..1ef762323f 100644 --- a/crates/ui-widgets/src/tile_grid/grid.rs +++ b/crates/ui-widgets/src/tile_grid/grid.rs @@ -229,7 +229,7 @@ pub struct TileGrid<'a, K: Clone + Eq + Hash + Send + Sync + 'static> { #[prop(flag)] content_cells: bool, /// The columns of cards a grid that takes its width from its content asks for: its width holds this many cards and - /// their gaps. 0 asks for none, so the content decides. + /// their gaps. 0 asks for one card, and the content decides the rest. #[prop] card_columns: usize, /// Where every tile's caption goes; `CaptionPlace::None` keeps no room for one. @@ -864,14 +864,13 @@ impl Collection for TileGrid<'_, K /// The last layout again while the width, the rows stamp and everything the arrangement reads are the same. fn ideal_width(&self, item: Option, _rows: usize, cx: &StyleCx<'_>) -> f32 { - if self.card_columns == 0 { - return 0.0; - } let style = GridSizes::of(cx, self.card, self.card_text, self.items.row_height, self.regular_card_padding); let aspect = if self.aspect.is_finite() && self.aspect > 0.0 { self.aspect } else { 1.0 }; let around = style.around.get(self.caption, self.detail_line); let card = style.card(self.picture_side(&style, item, around), aspect, around).x; - let columns = self.card_columns as f32; + // At least one card: in a narrower column the cards shrink, and the grid would measure shorter than it shows + // in the wider column it is given. + let columns = self.card_columns.max(1) as f32; // Half a point over, so the division that counts the columns back never rounds below `columns`. let spacing = style.spacing(self.caption); columns * card + (columns - 1.0) * spacing.gap + 2.0 * spacing.inset + 0.5 diff --git a/crates/ui-widgets/tests/it/tile_grid/grid.rs b/crates/ui-widgets/tests/it/tile_grid/grid.rs index 0e160b71db..f3c0c6a0f4 100644 --- a/crates/ui-widgets/tests/it/tile_grid/grid.rs +++ b/crates/ui-widgets/tests/it/tile_grid/grid.rs @@ -1,11 +1,15 @@ //! The tile grid's layout: justified rows fill the width with whole cards (picture and what the tile adds around it). use craft_behavior::keyed; -use craft_ui::contract::ShowCollection as _; +use craft_ui::buttons::Button; +use craft_ui::contract::{Show as _, ShowCollection as _, ShowContainer as _}; +use craft_ui::display::Text; +use craft_ui::layout_components::{Row, Stack}; +use craft_ui::list::{ThumbAspect, ToggleState}; use craft_ui::testing::Fixture; use craft_ui::tile_grid::{Around, ArrangeSpacing, GridEvent, GroupHeads, Tile, TileGrid, TileGroup, Tiling, arrange}; use egui::Vec2; -use std::cell::RefCell; +use std::cell::{Cell, RefCell}; use std::rc::Rc; const WIDTH: f32 = 400.0; @@ -78,3 +82,34 @@ fn rule_group_heads_take_no_title_no_tab_stop_and_never_close() { .any(|event| matches!(event, GridEvent::GroupOpen { .. } | GridEvent::GroupPicked { .. } | GridEvent::GroupMenu { .. })); assert!(!grouped, "a click on a rule reports no group event: {:?}", events.borrow()); } + +/// A grid in a stack beside another takes its rows' height in the row's measuring run, so the stack holds the whole +/// grid and what follows it: a button under the grid takes clicks. +#[craft_ui::testing::trial] +fn a_grid_in_a_measured_stack_keeps_its_rows_and_what_follows_them() { + let clicked = Rc::new(Cell::new(false)); + let held = clicked.clone(); + let mut fixture = Fixture::new(move |ui| { + let keys: Vec = (0..3).collect(); + let source = keyed(&keys, |key: &u64| *key); + Row::new().show(ui, |ui, _| { + Stack::new().show(ui, |ui, _| { + let _ = TileGrid::new("Boards").checks(true).max_rows(3).show_items(ui, &source, &mut |key, _| { + Tile::new(format!("Board {key}")).check(ToggleState::Off).thumb(ThumbAspect::SQUARE, |_, _| {}) + }); + if Button::new("Clear").show(ui).clicked() { + held.set(true); + } + }); + Stack::new().show(ui, |ui, _| { + Text::new("Options").show(ui); + }); + }); + }); + fixture.room(Vec2::new(800.0, 600.0)); + fixture.settle(); + let (tile, clear) = (fixture.by_label("Board 2").expect("tile").rect, fixture.by_label("Clear").expect("button")); + assert!(clear.rect.top() >= tile.bottom(), "the button follows the grid's rows: {tile:?} {:?}", clear.rect); + fixture.click(&clear).settle(); + assert!(clicked.get(), "the button under the grid takes a click"); +}