From ab8612ef7cf4531f172af0372da12e85c391aaed Mon Sep 17 00:00:00 2001 From: aiongg Date: Tue, 17 Nov 2026 05:11:53 +0000 Subject: [PATCH] Gradient palette shows its presets as small chips: PresetBrowser's PresetTiles::Chips variant, grid headers and tail rows a collection row high --- crates/draw/src/ui/gradient/palette/mod.rs | 13 ++++++--- .../src/patterns/presets/browser.rs | 29 ++++++++++++++----- .../src/patterns/presets/browser/stories.rs | 25 +++++++++++++++- .../ui-patterns/src/patterns/presets/mod.rs | 4 +-- .../ui-patterns/src/patterns/presets/model.rs | 13 +++++++++ crates/ui-widgets/src/tile_grid/grid.rs | 21 +++++++------- crates/ui-widgets/src/tile_grid/layout.rs | 2 ++ 7 files changed, 82 insertions(+), 25 deletions(-) diff --git a/crates/draw/src/ui/gradient/palette/mod.rs b/crates/draw/src/ui/gradient/palette/mod.rs index 4d89e82b0..a95132c31 100644 --- a/crates/draw/src/ui/gradient/palette/mod.rs +++ b/crates/draw/src/ui/gradient/palette/mod.rs @@ -3,7 +3,7 @@ //! the palette supplies the rows and paints each preset's thumbnail as a gradient ramp. use craft_ui::contract::{Bind, NoValue, Outcome, Pattern}; -use craft_ui::patterns::presets::{CellShape, PresetBrowser, PresetEvent, PresetRow, PresetState}; +use craft_ui::patterns::presets::{CellShape, PresetBrowser, PresetEvent, PresetRow, PresetState, PresetTiles}; use craft_ui::runtime::env::{Env, Label}; use egui::{Response, Ui}; @@ -13,7 +13,7 @@ mod stories; use craft_color::ui::gradient::{GradientRamp, GradientStops, Sampler, StopRows, default_sampler}; /// The presets take their content height up to this many small control heights, then scroll. -const MAX_ROWS: f32 = 5.0; +const MAX_ROWS: f32 = 6.0; /// A named group of gradient presets. #[derive(Clone, Debug, PartialEq)] @@ -103,7 +103,8 @@ impl GradientRef { /// left; the gradients are not), Up and Down to the nearest cell in the row above or below /// across group labels, Home and End to the first and last. In List, Up, Down, Home and End /// move and Left and Right do nothing. A focus move scrolls the cell into view. -/// - The presets take their content height up to five small control heights and scroll past it, also in a host +/// - The grid shows the presets as small chips (`PresetTiles::Chips`) under their group names. +/// - The presets take their content height up to six small control heights and scroll past it, also in a host /// that sizes to its content (a popover), where a filling browser would take the whole room. /// - AccessKit: Grid is a `Group` named `label` with `Button` cells; List is a `ListBox` of /// `ListBoxOption`s with `selected` on the chosen one. Cells carry their place and the set's @@ -208,7 +209,11 @@ impl<'a> GradientPalette<'a> { }; let (wide, high) = GradientRamp::CHIP_ASPECT; let most = Env::of(ui).tokens.size.control.sm * MAX_ROWS; - let mut browser = PresetBrowser::new(self.label, self.state, &rows).cell_shape(CellShape::ratio(wide, high)).room_height(most).thumb(thumb); + let mut browser = PresetBrowser::new(self.label, self.state, &rows) + .cell_shape(CellShape::ratio(wide, high)) + .tiles(PresetTiles::Chips) + .room_height(most) + .thumb(thumb); if let Some(selected) = self.selected { browser = browser.selected(selected); } diff --git a/crates/ui-patterns/src/patterns/presets/browser.rs b/crates/ui-patterns/src/patterns/presets/browser.rs index 86d3b8693..d354c5385 100644 --- a/crates/ui-patterns/src/patterns/presets/browser.rs +++ b/crates/ui-patterns/src/patterns/presets/browser.rs @@ -14,14 +14,14 @@ pub use reorder::PresetReorder; use super::event::{PresetEvent, PresetOutput}; use super::footer::{PresetFooter, PresetFooterEvent, PresetFooterOptions}; -use super::model::{CellShape, PresetRow, ThumbInfo, children_end, has_captions}; +use super::model::{CellShape, PresetRow, PresetTiles, ThumbInfo, children_end, has_captions}; use super::state::{DefaultOpen, PresetState, PresetView}; use super::words::PresetWords; use crate::bars::{Bar, BarKind}; use crate::choice::Segmented; use crate::contract::{ Bind, BodyCx, CollectionEvent, DropMark, DropTarget, DropZone, Entry, ListDrag, OpenChange, Pattern, Props, Rename, ReorderMove, ReorderRule, - SelectMode, Show as _, ShowCollection as _, ShowContainer as _, request_rename, + RowHeight, SelectMode, Show as _, ShowCollection as _, ShowContainer as _, request_rename, }; use crate::display::Divider; use crate::layout_components::Stack; @@ -197,6 +197,9 @@ pub struct PresetBrowser<'a, K: Clone + Eq + Hash + Send + Sync + 'static> { /// The shape of a grid tile's picture. #[prop] cell_shape: CellShape, + /// How large the grid view's tiles are; the list view is the same for both. + #[prop] + tiles: PresetTiles, /// The zoom the thumbnails span and how it steps; the footer's slider uses the same. #[prop] thumb_range: TileZoom, @@ -274,6 +277,7 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> PresetBrowser<'a, K> { rows, selected: None, cell_shape: CellShape::Square, + tiles: PresetTiles::Thumbnails, thumb_range: TileZoom::Ladder, default_open: DefaultOpen::All, toolbar: false, @@ -557,6 +561,7 @@ impl PresetBrowser<'_, K> { rows, selected, cell_shape, + tiles: tile_size, thumb_range, default_open, toolbar, @@ -683,16 +688,24 @@ impl PresetBrowser<'_, K> { let tiles = Shown { rows, at: plan.tiles.clone(), open: vec![false; plan.tiles.len()], tree: false, actions: 0 }; let tail: Vec>> = actions.iter().enumerate().map(|(at, label)| TailRow::new(BrowserKey::Action(at), label.clone())).collect(); - let captions = if has_captions(rows) { CaptionPlace::Below } else { CaptionPlace::None }; - let mut grid = TileGrid::>::new(label.clone()) + let aspect = card_aspect(cell_shape); + let grid = TileGrid::>::new(label.clone()) .id(list_id) .select(SelectMode::Single) .arrows_select(false) .selection(Bind::given(selection)) - .caption(captions) - .thumb_size(ThumbSize::Large) - .aspect(card_aspect(cell_shape)) - .zoom(zoom as f32) + .aspect(aspect); + let grid = match tile_size { + PresetTiles::Thumbnails => { + let captions = if has_captions(rows) { CaptionPlace::Below } else { CaptionPlace::None }; + grid.caption(captions).thumb_size(ThumbSize::Large).zoom(zoom as f32) + } + // The zoom is a factor on the picture's width: a wide chip is the small thumbnail high. + PresetTiles::Chips => { + grid.caption(CaptionPlace::None).thumb_size(ThumbSize::Small).zoom(aspect.max(1.0)).row_height(RowHeight::Text) + } + }; + let mut grid = grid .groups(&plan.groups) .tail(&tail) .collapsible() diff --git a/crates/ui-patterns/src/patterns/presets/browser/stories.rs b/crates/ui-patterns/src/patterns/presets/browser/stories.rs index 5344e085b..f561b7c68 100644 --- a/crates/ui-patterns/src/patterns/presets/browser/stories.rs +++ b/crates/ui-patterns/src/patterns/presets/browser/stories.rs @@ -4,7 +4,7 @@ use super::{PresetBrowser, PresetReorder}; use crate::choice::SwatchFace; use crate::contract::Bind; use crate::list::ThumbAspect; -use crate::patterns::presets::{PresetEvent, PresetFooterOptions, PresetRow, PresetState}; +use crate::patterns::presets::{PresetEvent, PresetFooterOptions, PresetRow, PresetState, PresetTiles}; use crate::testing::{Drive, StoryData, StoryOutput, story}; use craft_theme::Srgba; use egui::Ui; @@ -79,3 +79,26 @@ fn preset_browser(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { }; StoryOutput::from(out).readout(readout) } + +/// The chips variant: the same library as small bare chips under text-high group names, as a palette in a popover. +#[story(PresetBrowser<'static, u32>, covers = [], drive = Drive::Never)] +fn preset_browser_chips(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + let rows = library_rows(); + let selected = *data.value("selected", None::); + let mut browser = PresetBrowser::new("Swatches", Bind::bound(data.value("state", PresetState::::new())), &rows).tiles(PresetTiles::Chips); + if let Some(key) = selected { + browser = browser.selected(key); + } + let out = browser.show(ui); + let selected = data.value("selected", None::); + for event in &out.events { + if let PresetEvent::Select { key, .. } | PresetEvent::Activate { key, .. } = event { + *selected = Some(*key); + } + } + let readout = match *selected { + Some(n) => format!("Selected: Preset {n}"), + None => String::from("Nothing selected."), + }; + StoryOutput::from(out).readout(readout) +} diff --git a/crates/ui-patterns/src/patterns/presets/mod.rs b/crates/ui-patterns/src/patterns/presets/mod.rs index ec7888032..ccb51c5cf 100644 --- a/crates/ui-patterns/src/patterns/presets/mod.rs +++ b/crates/ui-patterns/src/patterns/presets/mod.rs @@ -25,8 +25,8 @@ pub use files::{PresetExport, PresetFileEvent, PresetFileFailure, PresetFileRequ pub use footer::{PresetFooter, PresetFooterEvent, PresetFooterOptions, PresetFooterOutput}; pub use manager::{HostedPresetManager, PresetManager, PresetManagerOutput, PresetManagerTaken}; pub use model::{ - CellShape, GroupPath, PresetChange, PresetError, PresetGroup, PresetId, PresetItem, PresetKey, PresetKind, PresetLibrary, PresetRow, Preview, - PreviewPaint, ThumbInfo, children_end, has_captions, nest_allowed, + CellShape, GroupPath, PresetChange, PresetError, PresetGroup, PresetId, PresetItem, PresetKey, PresetKind, PresetLibrary, PresetRow, PresetTiles, + Preview, PreviewPaint, ThumbInfo, children_end, has_captions, nest_allowed, }; pub use named::{NameCheck, NamedPresetCommands, NamedPresetEvent, NamedPresetOutput, NamedPresetRow, NamedPresetStyle}; pub use panel_menu::{PresetCommands, PresetMenuAction, PresetPanelMenu}; diff --git a/crates/ui-patterns/src/patterns/presets/model.rs b/crates/ui-patterns/src/patterns/presets/model.rs index 89571fae3..fe4a270ca 100644 --- a/crates/ui-patterns/src/patterns/presets/model.rs +++ b/crates/ui-patterns/src/patterns/presets/model.rs @@ -45,6 +45,19 @@ impl CellShape { } } +/// How large the grid view's tiles are. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +#[non_exhaustive] +pub enum PresetTiles { + /// Thumbnails at the browser's zoom, with captions when the rows have them, under control-high group headers: a + /// library panel. + #[default] + Thumbnails, + /// Small bare chips, as a palette in a popover shows them: each picture a small thumbnail on its short edge, no + /// captions (the name stays the tooltip and the accessible name), group headers a text row high, and no zoom. + Chips, +} + /// What the thumbnail painter is told about the cell it paints. #[derive(Clone, Copy, Debug)] #[non_exhaustive] diff --git a/crates/ui-widgets/src/tile_grid/grid.rs b/crates/ui-widgets/src/tile_grid/grid.rs index c89da6b50..5258a2cca 100644 --- a/crates/ui-widgets/src/tile_grid/grid.rs +++ b/crates/ui-widgets/src/tile_grid/grid.rs @@ -15,7 +15,7 @@ use super::{CaptionPlace, Tile, TilePartEvent, ZoomStep}; use crate::contract::{ Access, Bind, BindMut, Collection, CollectionEvent, CollectionProps, CollectionResponse, ColumnKind, ColumnSpec, Common, Component, ContainerProps, Cx, DropKind, EditResponse, Focus as _, ListDrag, Meta as _, NoValue, Notch, Outcome, PaintUi, PartsCx, Pointer as _, Props as _, - Role, RowState, SelectMode, Sizing, SpaceAction, SweepApply, SweepStep, ToggleColumn, section_part, + Role, RowHeight, RowState, SelectMode, Sizing, SpaceAction, SweepApply, SweepStep, ToggleColumn, section_part, }; use crate::controls::disclosure::Disclosure; use crate::list::{DropTest, ListRow, ListRowStyle, RowTest, ThumbSize}; @@ -142,18 +142,18 @@ struct GridSizes { thumbs: [f32; 3], /// What a tile adds around its picture. around: AroundPicture, - /// The gaps of the arrangement and the height of a group header (a heading row, as high as the grid's other rows), - /// for captioned tiles and for bare ones. + /// The gaps of the arrangement and the height of a group header (a heading row, as high as the grid's rows: the + /// collection's row height), for captioned tiles and for bare ones. spacing: ArrangeSpacing, bare_spacing: ArrangeSpacing, } impl GridSizes { - fn of(cx: &StyleCx<'_>, card: bool) -> GridSizes { + fn of(cx: &StyleCx<'_>, card: bool, rows: RowHeight) -> GridSizes { let spacing = ArrangeSpacing::new( if card { cx.tokens.space.gap.section } else { cx.tokens.space.gap.stack }, cx.tokens.space.gap.inline, - ListRow::row_height(cx, ThumbSize::None, false), + rows.of(cx), ); GridSizes { card: cx.tokens.size.thumb.card, @@ -394,7 +394,7 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> TileGrid<'a, K> { /// The layout of this pass: `previous` when it was made from the same recipe, else a new arrangement. A card is /// the picture plus what the tile adds around it in every tiling. fn laid_out(&self, previous: Option, source: &dyn ItemSource, width: f32, item: Option, cx: &StyleCx<'_>) -> GridLayout { - let style = GridSizes::of(cx, self.card); + let style = GridSizes::of(cx, self.card, self.items.row_height); let spacing = style.spacing(self.caption); let around = style.around.get(self.caption, self.detail_line); // A card never outgrows the grid: in a column narrower than a card the card takes the column. @@ -405,7 +405,7 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> TileGrid<'a, K> { side = ((free - (columns - 1.0) * gap) / columns - around.size().x).max(1.0); } let aspect = if self.aspect.is_finite() && self.aspect > 0.0 { self.aspect } else { 1.0 }; - let tail_height = ListRow::row_height(cx, ThumbSize::None, false); + let tail_height = self.items.row_height.of(cx); let recipe = self.items.rows_stamp.map(|stamp| Recipe { stamp, width, @@ -414,6 +414,7 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> TileGrid<'a, K> { aspect, justified: self.justified, around, + group_header: spacing.group_header, groups: self.groups.iter().map(|group| (group.start, group.count, group.open)).collect(), tail: (self.tail.len(), tail_height), }); @@ -480,7 +481,7 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> TileGrid<'a, K> { /// The header row of `group`: a heading at the group's depth, with its icon and its detail at the end, and a /// disclosure column the grid owns when the groups open and close. fn header_row(&self, group: &TileGroup, state: RowState) -> ListRow<'static> { - let mut row = ListRow::new(group.title.clone()).heading().node(false).depth(group.depth).state(state); + let mut row = ListRow::new(group.title.clone()).heading().rows(self.items.row_height).node(false).depth(group.depth).state(state); if self.collapsible { row = row.group(group.open).disclosure_by_host(); } @@ -686,7 +687,7 @@ impl Component for TileGrid<'_, K> ui.draw().edge_rule(RuleKind::Divider, rect, RectSide::Top); } let state = RowState { emphasized: lead == Some(Slot::Tail(at)), ..RowState::default() }; - ui.face(section_part(&row.key), ListRow::new(row.label.clone()).node(false).state(state)); + ui.face(section_part(&row.key), ListRow::new(row.label.clone()).rows(self.items.row_height).node(false).state(state)); } } @@ -804,7 +805,7 @@ impl Collection for TileGrid<'_, K if self.card_columns == 0 { return 0.0; } - let style = GridSizes::of(cx, self.card); + let style = GridSizes::of(cx, self.card, self.items.row_height); 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; diff --git a/crates/ui-widgets/src/tile_grid/layout.rs b/crates/ui-widgets/src/tile_grid/layout.rs index 0869d79ee..e61fe6c45 100644 --- a/crates/ui-widgets/src/tile_grid/layout.rs +++ b/crates/ui-widgets/src/tile_grid/layout.rs @@ -512,6 +512,8 @@ pub(super) struct Recipe { pub(super) aspect: f32, pub(super) justified: bool, pub(super) around: Around, + /// A group header's height. + pub(super) group_header: f32, /// Each group's first tile, count and open state. pub(super) groups: Vec<(usize, usize, bool)>, /// How many tail rows follow, and their height.