mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 02:09:00 +00:00
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
This commit is contained in:
@@ -229,7 +229,7 @@ pub struct TileGrid<'a, K: Clone + Eq + Hash + Send + Sync + 'static> {
|
|||||||
#[prop(flag)]
|
#[prop(flag)]
|
||||||
content_cells: bool,
|
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
|
/// 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]
|
#[prop]
|
||||||
card_columns: usize,
|
card_columns: usize,
|
||||||
/// Where every tile's caption goes; `CaptionPlace::None` keeps no room for one.
|
/// Where every tile's caption goes; `CaptionPlace::None` keeps no room for one.
|
||||||
@@ -864,14 +864,13 @@ impl<K: Clone + Eq + Hash + Send + Sync + 'static> Collection for TileGrid<'_, K
|
|||||||
|
|
||||||
/// The last layout again while the width, the rows stamp and everything the arrangement reads are the same.
|
/// The last layout again while the width, the rows stamp and everything the arrangement reads are the same.
|
||||||
fn ideal_width(&self, item: Option<Vec2>, _rows: usize, cx: &StyleCx<'_>) -> f32 {
|
fn ideal_width(&self, item: Option<Vec2>, _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 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 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 around = style.around.get(self.caption, self.detail_line);
|
||||||
let card = style.card(self.picture_side(&style, item, around), aspect, around).x;
|
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`.
|
// Half a point over, so the division that counts the columns back never rounds below `columns`.
|
||||||
let spacing = style.spacing(self.caption);
|
let spacing = style.spacing(self.caption);
|
||||||
columns * card + (columns - 1.0) * spacing.gap + 2.0 * spacing.inset + 0.5
|
columns * card + (columns - 1.0) * spacing.gap + 2.0 * spacing.inset + 0.5
|
||||||
|
|||||||
@@ -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).
|
//! 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_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::testing::Fixture;
|
||||||
use craft_ui::tile_grid::{Around, ArrangeSpacing, GridEvent, GroupHeads, Tile, TileGrid, TileGroup, Tiling, arrange};
|
use craft_ui::tile_grid::{Around, ArrangeSpacing, GridEvent, GroupHeads, Tile, TileGrid, TileGroup, Tiling, arrange};
|
||||||
use egui::Vec2;
|
use egui::Vec2;
|
||||||
use std::cell::RefCell;
|
use std::cell::{Cell, RefCell};
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
const WIDTH: f32 = 400.0;
|
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 { .. }));
|
.any(|event| matches!(event, GridEvent::GroupOpen { .. } | GridEvent::GroupPicked { .. } | GridEvent::GroupMenu { .. }));
|
||||||
assert!(!grouped, "a click on a rule reports no group event: {:?}", events.borrow());
|
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<u64> = (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");
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user