From 5e4a1501d473ae5a632d784928635ca13f6e4bf1 Mon Sep 17 00:00:00 2001 From: Martin Ball Date: Thu, 19 Nov 2026 00:05:53 +0000 Subject: [PATCH] fu2-dialogs: app batch 2 rulings (S-L362, S-L363) --- crates/ui-widgets/src/choice/swatch_table.rs | 32 +++++++++++-- .../src/choice/swatch_table/stories.rs | 21 ++++++++- .../tests/it/pickers/swatch_table.rs | 46 +++++++++++++++++-- .../api-map/colour-controls-and-panels.toml | 2 +- 4 files changed, 91 insertions(+), 10 deletions(-) diff --git a/crates/ui-widgets/src/choice/swatch_table.rs b/crates/ui-widgets/src/choice/swatch_table.rs index ca3cb20f3..b8617db8a 100644 --- a/crates/ui-widgets/src/choice/swatch_table.rs +++ b/crates/ui-widgets/src/choice/swatch_table.rs @@ -3,13 +3,13 @@ use super::swatch::hexagon; use super::{Swatch, SwatchMetrics, SwatchShape}; use crate::contract::{ - ActivateOn, Bind, BindMut, Collection, CollectionEvent, CollectionProps, Common, Component, ContainerProps, Cx, NoValue, Outcome, Props, Role, - SelectMode, ShowCollection as _, Sizing, + ActivateOn, Bind, BindMut, Collection, CollectionEvent, CollectionProps, Common, Component, ContainerProps, Cx, NoValue, Outcome, Props, + Remember as _, Role, SelectMode, ShowCollection as _, Sizing, }; use crate::list::RowTest; use crate::runtime::env::Label; use crate::style::{ComponentStyle, StyleCx}; -use craft_behavior::{DisplayOrder, ItemLayout, ItemSource, Metrics, NavShape, ScrollPos, Selection, TileLayout}; +use craft_behavior::{DisplayOrder, ItemLayout, ItemSource, Metrics, NavShape, Preview, ScrollPos, Selection, TileLayout}; use craft_theme::Srgba; use egui::{Pos2, Rect, Response, Ui, Vec2}; @@ -60,6 +60,27 @@ pub struct SwatchTableOutput { pub chosen: Vec, /// The entry a double click or Enter asked to edit this pass. pub open: Option, + /// What this pass did to the preview of the cell under the pointer: `Shows` in the pass the pointer moves onto a + /// cell, `Ended` in the pass it leaves the last one. The keyboard lead previews nothing. + pub preview: Preview, +} + +/// The cell the table last reported as previewed, kept in its shown memory, so a pass reports a preview only when the +/// cell under the pointer changes and a second pass of a frame reports nothing. +#[derive(Clone, Copy, Debug, Default)] +struct Previewed(Option); + +impl Previewed { + /// Follows the cell under the pointer this pass and reports what changed. + fn follow(&mut self, hovered: Option) -> Preview { + let preview = match (hovered, self.0) { + (Some(at), shown) if shown != Some(at) => Preview::Shows(at), + (None, Some(_)) => Preview::Ended, + _ => Preview::Unchanged, + }; + self.0 = hovered; + preview + } } impl Outcome for SwatchTableOutput { @@ -303,6 +324,7 @@ impl ItemLayout for SwatchLayout { /// - A click or Space selects the cell; a double click or Enter reports `open` and the Enter is the table's, so a /// dialog around it does not also answer OK. /// - The table is one Tab stop; the arrows move through the cells as a grid. +/// - The cell under the pointer is previewed ([`SwatchTableOutput::preview`]); the selection stays. /// - AccessKit: a list box named by the label; each cell a list box option named by its entry, with the hex colour as /// its value. #[derive(crate::contract::Component, crate::contract::Props)] @@ -413,7 +435,9 @@ impl Component for SwatchTable<'_> { _ => None, }); let changed = response.events.iter().any(|event| matches!(event, CollectionEvent::Selection(_))); - SwatchTableOutput { response: response.response, selected: selection.lead.filter(|_| changed), chosen: selection.items, open } + let hovered = response.shown.iter().find(|row| row.hovered).map(|row| row.key); + let preview = cx.memory::().follow(hovered); + SwatchTableOutput { response: response.response, selected: selection.lead.filter(|_| changed), chosen: selection.items, open, preview } } } diff --git a/crates/ui-widgets/src/choice/swatch_table/stories.rs b/crates/ui-widgets/src/choice/swatch_table/stories.rs index c7556d680..e19ba8843 100644 --- a/crates/ui-widgets/src/choice/swatch_table/stories.rs +++ b/crates/ui-widgets/src/choice/swatch_table/stories.rs @@ -4,7 +4,7 @@ use super::{SwatchArrangement, SwatchTable, SwatchTableOutput}; use crate::contract::Bind; use crate::runtime::env::Label; use crate::testing::{Drive, StoryData, StoryOutput, story}; -use craft_behavior::Selection; +use craft_behavior::{Preview, Selection}; use craft_theme::Srgba; use egui::Ui; @@ -50,6 +50,25 @@ fn swatch_table_multiple(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { with_readout(data, shown) } +/// The readout follows the preview: the cell under the pointer while it shows, the selection once it ends. +#[story(SwatchTable<'static>, covers = [], drive = Drive::Never)] +fn swatch_table_preview(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + let entries = table_entries().into_iter().take(64).collect::>(); + let shown = + SwatchTable::new("Color Table", &entries).selection(Bind::bound(data.value("selected", Selection::single(5usize)))).per_row(16).show_all(ui); + let previewed = data.note("previewed", None::); + match shown.preview { + Preview::Shows(index) => *previewed = Some(index), + Preview::Ended => *previewed = None, + _ => {} + } + let readout = match *previewed { + Some(index) => format!("Previewing: Index {index}"), + None => "Previewing: nothing".to_owned(), + }; + StoryOutput::from(shown).readout(readout) +} + /// 127 entries, the full honeycomb of side 7. #[story(SwatchTable<'static>, covers = [], drive = Drive::Never)] fn swatch_table_honeycomb(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { diff --git a/crates/ui-widgets/tests/it/pickers/swatch_table.rs b/crates/ui-widgets/tests/it/pickers/swatch_table.rs index 75dc4c0d2..287035403 100644 --- a/crates/ui-widgets/tests/it/pickers/swatch_table.rs +++ b/crates/ui-widgets/tests/it/pickers/swatch_table.rs @@ -1,14 +1,16 @@ -//! A honeycomb `SwatchTable`: touching hexagon cells in rows that follow the count, picked by click and moved through by -//! the arrows. +//! `SwatchTable`: a honeycomb's touching hexagon cells in rows that follow the count, picked by click and moved through +//! by the arrows; and the preview of the cell under the pointer. -use std::cell::Cell; +use std::cell::{Cell, RefCell}; use std::rc::Rc; +use craft_behavior::Preview; use craft_theme::Srgba; use craft_ui::choice::{SwatchArrangement, SwatchTable}; use craft_ui::runtime::env::Label; +use craft_ui::testing::pointer::moved; use craft_ui::testing::{Fixture, trial}; -use egui::{Key, Modifiers, Rect, vec2}; +use egui::{Key, Modifiers, Pos2, Rect, vec2}; /// Fifteen cells: the hexagon of side 3 (19 cells) cut short, rows of 3, 4, 5 and a short last row of 3 where the full /// row has 4. @@ -84,3 +86,39 @@ fn honeycomb_cells_touch_without_overlap_and_arrows_follow_neighbours() { assert_eq!(after(&mut fixture, &picked, Key::ArrowUp), Some(0), "Up from the first column stays in it"); assert_eq!(after(&mut fixture, &picked, Key::ArrowDown), Some(3), "Down from the first cell lands on the first cell of the next row"); } + +/// The previews every pass reported since the log was last emptied, the passes that changed nothing left out. +fn reported(log: &RefCell>>) -> Vec> { + log.borrow_mut().drain(..).filter(|preview| *preview != Preview::Unchanged).collect() +} + +#[trial] +fn the_cell_under_the_pointer_is_previewed_once_until_the_pointer_leaves() { + let log = Rc::new(RefCell::new(Vec::new())); + let entries: Vec<(Label, Srgba)> = (0..8).map(|i| (Label::from(format!("Cell {i}")), Srgba::rgb(i as f32 / 8.0, 0.4, 0.6))).collect(); + let mut fixture = Fixture::new({ + let log = Rc::clone(&log); + move |ui| { + let out = SwatchTable::new("Colors", &entries).per_row(4).show_all(ui); + log.borrow_mut().push(out.preview); + } + }); + fixture.room(vec2(400.0, 300.0)); + fixture.settle(); + log.borrow_mut().clear(); + + let cell = fixture.by_label("Cell 2").expect("a cell").rect; + fixture.frame(vec![moved(cell.center())]); + fixture.settle(); + assert_eq!(reported(&log), vec![Preview::Shows(2)],"moving onto a cell previews it, in one pass"); + + fixture.frame(Vec::new()); + fixture.settle(); + assert!(!log.borrow().is_empty(), "the resting frames ran passes"); + assert_eq!(reported(&log), Vec::new(), "a pointer resting on the cell reports nothing more"); + + let last = fixture.by_label("Cell 7").expect("a cell").rect; + fixture.frame(vec![moved(Pos2::new(cell.center().x, last.max.y + 40.0))]); + fixture.settle(); + assert_eq!(reported(&log), vec![Preview::Ended],"leaving the cells ends the preview, once"); +} diff --git a/migration/api-map/colour-controls-and-panels.toml b/migration/api-map/colour-controls-and-panels.toml index c9ac3c7fe..dd1995320 100644 --- a/migration/api-map/colour-controls-and-panels.toml +++ b/migration/api-map/colour-controls-and-panels.toml @@ -1121,7 +1121,7 @@ name = "SwatchTable" old = "" new = "craft_ui::choice::SwatchTable" status = "replaced" -note = "An items component on the collection contract with Swatch cells: `new(label, &[(Label, Srgba)])` with `selection(Bind>)`, `per_row(n)` in place of `columns` and `multiple(bool)`; `show_all(ui)` returns `SwatchTableOutput { response, selected, chosen, open }`. Replaces `SwatchPalette::table`." +note = "An items component on the collection contract with Swatch cells: `new(label, &[(Label, Srgba)])` with `selection(Bind>)`, `per_row(n)` in place of `columns` and `multiple(bool)`; `show_all(ui)` returns `SwatchTableOutput { response, selected, chosen, open, preview }`. Replaces `SwatchPalette::table`." [[type]] name = "SwatchTableResponse"