From 679e17cab06e5581258e2c31dbdec8908481cf60 Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 09:08:35 +0000 Subject: [PATCH] Table story as the old card: padded report names, Modified and Warnings columns, the first row selected --- crates/ui-widgets/src/table/stories.rs | 85 ++++++++++++++++++-------- 1 file changed, 59 insertions(+), 26 deletions(-) diff --git a/crates/ui-widgets/src/table/stories.rs b/crates/ui-widgets/src/table/stories.rs index a2b6de825..d2be5890a 100644 --- a/crates/ui-widgets/src/table/stories.rs +++ b/crates/ui-widgets/src/table/stories.rs @@ -1,32 +1,46 @@ //! Stories of `Table`: rows with a text in each cell, a sorted column, a toggle column and a tree. use super::{Table, sort_rows}; -use crate::contract::{CollectionEvent, ColumnEvent, ColumnKind, ColumnSort, ColumnSpec, ShowCollection as _, ToggleColumn}; +use crate::contract::{Bind, CollectionEvent, ColumnEvent, ColumnKind, ColumnSort, ColumnSpec, ShowCollection as _, ToggleColumn}; use crate::list::{ListRow, ToggleCell}; use crate::testing::{Drive, StoryData, StoryOutput, story}; -use craft_behavior::{ColumnKey, ColumnWidth, DisplayOrder, ItemSource}; +use craft_behavior::{ColumnKey, ColumnWidth, DisplayOrder, ItemSource, Selection}; +use craft_i18n::{AgeStyle, FileSize, SizeUnit}; use egui::Ui; const NAME: ColumnKey = ColumnKey::new("name"); const PAGES: ColumnKey = ColumnKey::new("pages"); const SIZE: ColumnKey = ColumnKey::new("size"); +const MODIFIED: ColumnKey = ColumnKey::new("modified"); +const WARNINGS: ColumnKey = ColumnKey::new("warnings"); const KEEP: ColumnKey = ColumnKey::new("keep"); +/// Sizes in megabytes with one decimal, as a file list shows them. +const MEGABYTES: FileSize = FileSize::BINARY.smallest(SizeUnit::Mega); + /// A file row of the stories. struct File { name: String, - pages: u32, - kilobytes: u32, + pages: u64, + kilobytes: u64, + /// Seconds since the file changed. + age: u64, + warnings: u64, keep: bool, } fn files(count: usize) -> Vec { (0..count) - .map(|n| File { - name: format!("Report {}.pdf", n + 1), - pages: 3 + (n as u32 * 7) % 40, - kilobytes: 120 + (n as u32 * 53) % 900, - keep: n % 2 == 0, + .map(|n| { + let at = n as u64; + File { + name: format!("Report {:03}.pdf", n + 1), + pages: (at * 7) % 90 + 1, + kilobytes: (at * 937) % 48_000 + 120, + age: (at * 7_919) % (86_400 * 400), + warnings: if n.is_multiple_of(7) { at % 3 + 1 } else { 0 }, + keep: n % 2 == 0, + } }) .collect() } @@ -67,9 +81,11 @@ fn width_of(widths: &Widths, key: &ColumnKey, default: ColumnWidth) -> ColumnWid fn columns(keep: bool, widths: &Widths) -> Vec { let mut columns = vec![ - ColumnSpec::new(NAME, "File name", ColumnWidth::fill(2.0)).sortable().resizable().movable(), - ColumnSpec::new(PAGES, "Pages", width_of(widths, &PAGES, ColumnWidth::Controls(3.0))).numeric().sortable().resizable(), - ColumnSpec::new(SIZE, "Size (KB)", width_of(widths, &SIZE, ColumnWidth::Controls(4.0))).numeric().sortable().resizable().hideable(), + ColumnSpec::new(NAME, "Name", ColumnWidth::fill(2.0)).sortable().resizable().movable(), + ColumnSpec::new(PAGES, "Pages", width_of(widths, &PAGES, ColumnWidth::Controls(2.0))).numeric().sortable().resizable(), + ColumnSpec::new(SIZE, "Size", width_of(widths, &SIZE, ColumnWidth::Controls(3.0))).numeric().sortable().resizable().hideable(), + ColumnSpec::new(MODIFIED, "Modified", width_of(widths, &MODIFIED, ColumnWidth::Controls(3.0))).sortable().resizable().hideable(), + ColumnSpec::new(WARNINGS, "Warnings", width_of(widths, &WARNINGS, ColumnWidth::Controls(3.0))).numeric().sortable().resizable(), ]; if keep { columns.insert(0, ColumnSpec::new(KEEP, "Keep", ColumnWidth::Controls(1.5)).kind(ColumnKind::Toggle(ToggleColumn::sweepable()))); @@ -77,26 +93,43 @@ fn columns(keep: bool, widths: &Widths) -> Vec { columns } -fn table(ui: &mut Ui, data: &mut StoryData, mut rows: Vec, sort: Option, keep: bool, striped: bool) -> StoryOutput { +/// The table of `rows` sorted by `sort`, `selected` the rows picked to begin with. +fn table( + ui: &mut Ui, + data: &mut StoryData, + mut rows: Vec, + sort: Option, + keep: bool, + striped: bool, + selected: Selection, +) -> StoryOutput { let widths = data.value("widths", Widths::new()).clone(); sort_rows(&mut rows, sort.as_ref(), |a, b, column| match column { c if *c == PAGES => a.pages.cmp(&b.pages), c if *c == SIZE => a.kilobytes.cmp(&b.kilobytes), + c if *c == MODIFIED => b.age.cmp(&a.age), + c if *c == WARNINGS => a.warnings.cmp(&b.warnings), _ => a.name.cmp(&b.name), }); - let out = Table::::new("Files", columns(keep, &widths)).striped(striped).sort(sort).empty("No files").show_items( - ui, - &Files(&rows), - &mut |key, icx| { + let out = Table::::new("Files", columns(keep, &widths)) + .striped(striped) + .sort(sort) + .empty("No files") + .selection(Bind::bound(data.value("selection", selected))) + .show_items(ui, &Files(&rows), &mut |key, icx| { let file = &rows[*key]; - let row = ListRow::new(file.name.clone()) + let (units, format) = (icx.env.units(), icx.env.format()); + let mut row = ListRow::new(file.name.clone()) .cells(icx.cells()) .striped(icx.row.striped) - .cell(PAGES, file.pages.to_string()) - .cell(SIZE, file.kilobytes.to_string()); + .cell(PAGES, format.integer(file.pages)) + .cell(SIZE, units.file_size_as(file.kilobytes * 1024, MEGABYTES)) + .cell(MODIFIED, icx.env.relative_age(file.age, AgeStyle::Short)); + if file.warnings > 0 { + row = row.cell(WARNINGS, format.integer(file.warnings)); + } if keep { row.toggle(ToggleCell::check(KEEP, crate::list::ToggleState::of(file.keep), "Keep {row}")) } else { row } - }, - ); + }); let widths = data.value("widths", Widths::new()); for event in &out.events { let CollectionEvent::Column(ColumnEvent::Resized { key, width }) = event else { continue }; @@ -110,20 +143,20 @@ fn table(ui: &mut Ui, data: &mut StoryData, mut rows: Vec, sort: Option, covers = [Default, Many], drive = Drive::Never)] fn table_many(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { - table(ui, data, files(40), Some(ColumnSort::ascending(NAME)), false, true) + table(ui, data, files(40), Some(ColumnSort::ascending(NAME)), false, true, Selection::single(0)) } #[story(Table<'static, usize>, covers = [One], drive = Drive::Never)] fn table_one(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { - table(ui, data, files(1), None, false, false) + table(ui, data, files(1), None, false, false, Selection::default()) } #[story(Table<'static, usize>, covers = [Empty], drive = Drive::Never)] fn table_empty(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { - table(ui, data, Vec::new(), None, false, false) + table(ui, data, Vec::new(), None, false, false, Selection::default()) } #[story(Table<'static, usize>, covers = [], drive = Drive::Never)] fn table_sorted_descending_with_toggles(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { - table(ui, data, files(12), Some(ColumnSort::descending(SIZE)), true, false) + table(ui, data, files(12), Some(ColumnSort::descending(SIZE)), true, false, Selection::default()) }