From af596d2ecacfb81dd723c80b27a736ebc6272954 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 30 Nov 2026 19:38:08 -0800 Subject: [PATCH] Tables: header tooltips name only what the column does; About credit columns resize A header cell whose column gives no tooltip showed "Click to sort, drag to move the column. Drag its edge to resize it." on every table, whether or not the column sorts, moves or resizes. The default tooltip now joins one sentence for each action the column offers (sort, move, resize) and shows none when it offers none. The About dialog's Models and Contributors tables now let each column resize by a drag of its header's edge, so a truncated Company or Name column can be widened. --- .../ui-contract/src/contract/items/header.rs | 8 ++- .../ui-contract/src/runtime/words/header.rs | 21 ++++++- .../src/patterns/form_dialogs/about.rs | 4 +- .../tests/it/form_dialogs/about.rs | 56 +++++++++++++++++++ 4 files changed, 82 insertions(+), 7 deletions(-) diff --git a/crates/ui-contract/src/contract/items/header.rs b/crates/ui-contract/src/contract/items/header.rs index b14caee5d9..31c984bffa 100644 --- a/crates/ui-contract/src/contract/items/header.rs +++ b/crates/ui-contract/src/contract/items/header.rs @@ -418,7 +418,7 @@ impl Header { let x = rect.min.x.clamp(view.min.x, view.max.x); Rect::from_x_y_ranges(x..=rect.max.x.clamp(x, view.max.x), rect.y_range()) }; - let help = pcx.words::().tip.clone(); + let words = pcx.words::(); for (at, (spec, width)) in self.specs.iter().zip(widths).enumerate() { let (rect, rest) = flow.take_start(width); flow = rest; @@ -432,7 +432,11 @@ impl Header { } let part = pcx.part(HEADER.at(hash), shown(rect)).sense(sense); let part = if hosted { part } else { part.focusable() }; - part.priority(HitPriority::new(HitLevel::Body, 0)).state_layer().tooltip(spec.tip.clone().unwrap_or_else(|| help.clone())); + let part = part.priority(HitPriority::new(HitLevel::Body, 0)).state_layer(); + // The default tooltip names only what this cell does: a hosted cell neither sorts nor moves. + if let Some(tip) = spec.tip.clone().or_else(|| words.tip(spec.sortable && !hosted, spec.movable && !hosted, spec.resizable)) { + part.tooltip(tip); + } if spec.resizable { let edge = if rtl { Rect::from_min_size(rect.min, Vec2::new(grip, rect.height())) diff --git a/crates/ui-contract/src/runtime/words/header.rs b/crates/ui-contract/src/runtime/words/header.rs index 6dd901c1e2..397c2d2a41 100644 --- a/crates/ui-contract/src/runtime/words/header.rs +++ b/crates/ui-contract/src/runtime/words/header.rs @@ -1,9 +1,24 @@ //! `HeaderWords`: the words of a collection's header row. craft_ui::words! { - /// The words of the header row. + /// The words of the header row. A header whose column gives no tooltip (`ColumnSpec::tip`) shows one sentence for + /// each thing the pointer does to it: `sort` when the column sorts, `move_column` when it moves and `resize` when + /// it resizes, in that order, and no tooltip when it does none of them. pub struct HeaderWords { - /// A header's tooltip when its column gives none (`ColumnSpec::tip`). - tip: "table.headerTip" = "Click to sort, drag to move the column. Drag its edge to resize it."; + sort: "table.headerSort" = "Click to sort."; + move_column: "table.headerMove" = "Drag to move the column."; + resize: "table.headerResize" = "Drag its edge to resize it."; + } +} + +impl HeaderWords { + /// The tooltip of a header cell whose column sorts, moves and resizes as the flags say, or `None` when it does none. + pub(crate) fn tip(&self, sortable: bool, movable: bool, resizable: bool) -> Option { + let parts: Vec<&str> = [(sortable, &self.sort), (movable, &self.move_column), (resizable, &self.resize)] + .into_iter() + .filter(|(on, _)| *on) + .map(|(_, word)| word.as_str()) + .collect(); + (!parts.is_empty()).then(|| parts.join(" ").into()) } } diff --git a/crates/ui-patterns/src/patterns/form_dialogs/about.rs b/crates/ui-patterns/src/patterns/form_dialogs/about.rs index a99d187341..095db3cc05 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/about.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/about.rs @@ -591,7 +591,7 @@ impl Shown<'_> { .iter() .map(|key| { let width = if *key == ContributorSort::Name { ColumnWidth::fill(1.0) } else { ColumnWidth::Content }; - let mut column = ColumnSpec::new(key.column(), key.labels(words).1.clone(), width).sortable(); + let mut column = ColumnSpec::new(key.column(), key.labels(words).1.clone(), width).sortable().resizable(); if key.numeric() { column = column.numeric(); } @@ -650,7 +650,7 @@ impl Shown<'_> { .iter() .map(|column| { let width = if *column == ModelColumn::Company { ColumnWidth::fill(1.0) } else { ColumnWidth::Content }; - let spec = ColumnSpec::new(column.key(), column.head(words), width); + let spec = ColumnSpec::new(column.key(), column.head(words), width).resizable(); if column.numeric() { spec.numeric() } else { spec } }) .collect(); diff --git a/crates/ui-patterns/tests/it/form_dialogs/about.rs b/crates/ui-patterns/tests/it/form_dialogs/about.rs index 2fe0971d51..c58ea28340 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/about.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/about.rs @@ -292,6 +292,62 @@ fn about_tab_is_a_field() { assert_eq!(fields, Some(json!({ "tab": "contributors" })), "the field lists the tab a click picked"); } +/// The header cell of the column `head`. +fn header(fixture: &Fixture, head: &str) -> craft_ui::testing::Target { + fixture.by_role(Role::COLUMN_HEADER).into_iter().find(|cell| fixture.tree().name_of(cell.node) == Some(head)).expect("the table has the column") +} + +/// Drags the end edge of the column `head` by `by` and returns the column's width before and after. +fn drag_edge(fixture: &mut Fixture, head: &str, by: f32) -> (f32, f32) { + let before = header(fixture, head).rect; + fixture.drag_at(egui::Pos2::new(before.max.x - 1.0, before.center().y), Vec2::new(by, 0.0)).idle(10); + (before.width(), header(fixture, head).rect.width()) +} + +/// The text the tooltip of the column `head` paints after the pointer rests on its header. +fn header_tip(fixture: &mut Fixture, head: &str) -> String { + use craft_ui::contract::tooltip::DELAY; + use craft_ui::testing::pointer; + let before: Vec = fixture.texts().iter().map(|run| run.text.clone()).collect(); + let cell = header(fixture, head); + fixture.frame(vec![pointer::moved(cell.rect.center())]); + fixture.advance(DELAY * 2.0).settle(); + let mut fresh: Vec = fixture.texts().iter().map(|run| run.text.clone()).filter(|text| !before.contains(text)).collect(); + fresh.dedup(); + fresh.join(" ") +} + +/// The Models table's columns resize by a drag of a header's edge, and the header's tooltip names only that, since the +/// table neither sorts nor moves its columns. +#[trial] +fn the_models_tables_columns_resize_by_their_header_edge() { + let credits = Credits::new(Vec::new(), vec![ModelCredit::new("Anthropic", "Writer", "2").work(4, 100, 10)], 4); + let mut fixture = Fixture::new(move |ui| { + let _ = AboutDialog::new(&INFO, &credits).tab(Bind::given(AboutTab::Models)).show(ui); + }); + fixture.room(Vec2::new(1400.0, 1000.0)).idle(30); + let (before, after) = drag_edge(&mut fixture, "Company", 60.0); + assert!(after > before + 30.0, "a drag of the Company edge widens the column: {before} to {after}"); + let (before, after) = drag_edge(&mut fixture, "Company", -40.0); + assert!(after < before - 20.0, "a drag back narrows it: {before} to {after}"); + assert_eq!(header_tip(&mut fixture, "Company"), "Drag its edge to resize it.", "the tooltip names only the resize"); +} + +/// The Contributors table's columns resize by a drag of a header's edge, and the header's tooltip names the sort and +/// the resize, without a move the table does not offer. +#[trial] +fn the_contributors_tables_columns_resize_by_their_header_edge() { + let mut fixture = Fixture::new(move |ui| { + let _ = AboutDialog::new(&INFO, &CREDITS).tab(Bind::given(AboutTab::Contributors)).show(ui); + }); + fixture.room(Vec2::new(1400.0, 1000.0)).idle(30); + let table = fixture.by_label("Table").expect("the View control offers Table"); + fixture.click(&table).idle(30); + let (before, after) = drag_edge(&mut fixture, "Name", 60.0); + assert!(after > before + 30.0, "a drag of the Name edge widens the column: {before} to {after}"); + assert_eq!(header_tip(&mut fixture, "Name"), "Click to sort. Drag its edge to resize it.", "the tooltip names the sort and the resize"); +} + /// Guards the app tabs of the About dialog: an app tab is listed after Models, `tab` takes its name and shows its /// body, a click on it reports its name in the field, and a name that is neither built in nor an app tab's is refused. #[trial]