diff --git a/crates/ui-patterns/src/patterns/presets/browser.rs b/crates/ui-patterns/src/patterns/presets/browser.rs index 0ebde95c9..12a82e612 100644 --- a/crates/ui-patterns/src/patterns/presets/browser.rs +++ b/crates/ui-patterns/src/patterns/presets/browser.rs @@ -28,7 +28,7 @@ use crate::layout_components::Stack; use crate::list::{List, ListRow, RowInk, RowTest, ThumbSize}; use crate::menu::{Menu, MenuItem}; use crate::notices::{Notice, NoticeLayout}; -use crate::primitives::ContentPainterCx; +use crate::primitives::{ContentPainterCx, IconSpec}; use crate::runtime::env::{Env, Label}; use crate::runtime::memory::Remember as _; use crate::text::TextField; @@ -339,6 +339,11 @@ fn shown_rows( out } +/// The glyph of a group row in both views: an open or a closed folder. +fn folder(open: bool) -> IconId { + IconId::new(if open { "folder_open" } else { "folder" }) +} + /// The grid's tiles and group headers from the shown rows: each header covers the presets that follow it inside its /// group. struct Plan { @@ -353,7 +358,11 @@ fn grid_plan(rows: &[PresetRow], shown: &[usize], open: impl Fn(usi for &at in shown { let row = &rows[at]; if row.is_group() { - let mut group = TileGroup::new(BrowserKey::Preset(row.key.clone()), plan.tiles.len(), 0, row.label.clone()).open(open(at)); + let open = open(at); + let mut group = TileGroup::new(BrowserKey::Preset(row.key.clone()), plan.tiles.len(), 0, row.label.clone()) + .open(open) + .depth(row.depth) + .icon(folder(open)); if let Some(detail) = count(at) { group = group.detail(detail); } @@ -675,7 +684,12 @@ impl PresetBrowser<'_, K> { }; let row = &rows[at]; if row.is_group() { - let mut line = ListRow::new(row.label.clone()).heading().depth(row.depth).group(icx.row.open).rule(ruled(icx.at)); + let mut line = ListRow::new(row.label.clone()) + .heading() + .depth(row.depth) + .group(icx.row.open) + .icon(IconSpec::svg(folder(icx.row.open))) + .rule(ruled(icx.at)); if let Some(detail) = counted(at) { line = line.detail(detail); } diff --git a/crates/ui-widgets/src/list/row.rs b/crates/ui-widgets/src/list/row.rs index d256e04c0..6a5b3957b 100644 --- a/crates/ui-widgets/src/list/row.rs +++ b/crates/ui-widgets/src/list/row.rs @@ -225,6 +225,9 @@ pub struct ListRow<'a> { /// The row is a tree group with children, `open` or closed: it shows the disclosure part. #[prop] pub(super) group: Option, + /// The host declares and paints the disclosure part of this group row (a face has no parts of its own): the row + /// keeps the disclosure column and paints no disclosure. + pub(super) disclosure_by_host: bool, /// The row is an option of a listbox, not an item of a list: inset and rounded, the chosen option outlined. #[style_key] #[prop(flag)] @@ -292,6 +295,7 @@ impl<'a> ListRow<'a> { cell_fields: Vec::new(), depth: None, group: None, + disclosure_by_host: false, option: false, check: None, event: None, @@ -318,6 +322,14 @@ impl<'a> ListRow<'a> { self } + /// The host declares and paints the disclosure part of this group row at [`ListRow::disclosure_at`]; the row keeps + /// the column. For a row the host paints as a face, which declares no parts. + #[must_use] + pub(crate) fn disclosure_by_host(mut self) -> Self { + self.disclosure_by_host = true; + self + } + /// The row state the collection gives the item: the rename editor, the stripe, the app's mark and the press the /// collection holds on the row. #[must_use] diff --git a/crates/ui-widgets/src/list/row/placement.rs b/crates/ui-widgets/src/list/row/placement.rs index 69565b284..90c0a0071 100644 --- a/crates/ui-widgets/src/list/row/placement.rs +++ b/crates/ui-widgets/src/list/row/placement.rs @@ -100,6 +100,13 @@ impl ListRow<'_> { pub fn row_height(cx: &StyleCx<'_>, thumb: ThumbSize, detail: bool) -> f32 { Sizes::of(cx, TypeRole::Body).height(thumb, detail) } + + /// Where this row's disclosure lies when it fills `rect` under `style`, with texts `text_width` wide: the rect at + /// which a host that owns the disclosure part (`disclosure_by_host`) declares it. `None` for a row without one. + pub(crate) fn disclosure_at(&self, style: &ListRowStyle, rect: Rect, rtl: bool, text_width: &dyn Fn(&TextRun<'_>) -> f32) -> Option { + let widths = self.widths(&style.sizes, text_width); + self.place(&style.sizes, &widths, rect, rtl).disclosure + } } /// A trailing item's width where the row has room, the least it narrows to, and whether it drops out for room. diff --git a/crates/ui-widgets/src/list/row/show.rs b/crates/ui-widgets/src/list/row/show.rs index ae710b43e..8de01f50a 100644 --- a/crates/ui-widgets/src/list/row/show.rs +++ b/crates/ui-widgets/src/list/row/show.rs @@ -216,7 +216,7 @@ impl Component for ListRow<'_> { cx.selectable_text(TEXT_CELL.at(*position as u64), &self.cell_run(cx.style, *at)); } } - if let Some(disclosure) = placed.disclosure { + if let Some(disclosure) = placed.disclosure.filter(|_| !self.disclosure_by_host) { let words = cx.words::(); let tip = if self.group == Some(true) { words.collapse_tip.clone() } else { words.expand_tip.clone() }; cx.part(DISCLOSURE, disclosure).sense(Sense::CLICK).pointer_only("Left and Right open and close the row").tooltip(tip); @@ -387,8 +387,10 @@ impl Component for ListRow<'_> { } if let Some(open) = self.group { access.expanded(open); - let pattern = if open { &words.collapse } else { &words.expand }; - access.node(DISCLOSURE, Role::BUTTON).name = Name::Text(named(pattern, label)); + if !self.disclosure_by_host { + let pattern = if open { &words.collapse } else { &words.expand }; + access.node(DISCLOSURE, Role::BUTTON).name = Name::Text(named(pattern, label)); + } } let selected = cx.resolved.selected == Some(true) || cx.env.facts_of(cx.resolved.id).is_some_and(|facts| facts.flags.contains(Flags::SELECTED)); @@ -476,6 +478,7 @@ impl Component for ListRow<'_> { } if let Some(open) = self.group && placed.disclosure.is_some() + && !self.disclosure_by_host { ui.face(DISCLOSURE, Disclosure::new(Bind::given(open)).node(false).triangle(true).label(cx.resolved.label.to_owned())); } diff --git a/crates/ui-widgets/src/tile_grid/grid.rs b/crates/ui-widgets/src/tile_grid/grid.rs index 10a5a6faa..c71fd43a3 100644 --- a/crates/ui-widgets/src/tile_grid/grid.rs +++ b/crates/ui-widgets/src/tile_grid/grid.rs @@ -14,13 +14,13 @@ use super::tile::AroundPicture; 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, NoValue, Notch, Outcome, PaintUi, PartsCx, Pointer as _, Props as _, Role, - RowState, SelectMode, Sizing, SpaceAction, SweepApply, SweepStep, ToggleColumn, + 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, }; use crate::contract::{lead_position, section_part}; use crate::controls::disclosure::Disclosure; -use crate::list::{DropTest, ListRow, RowTest, ThumbSize}; -use crate::primitives::{PaintShape, RectSide}; +use crate::list::{DropTest, ListRow, ListRowStyle, RowTest, ThumbSize}; +use crate::primitives::{IconSpec, PaintShape, RectSide}; use crate::runtime::access::Name; use crate::runtime::commands::FireSource; use crate::runtime::env::Label; @@ -122,7 +122,7 @@ impl Outcome for TileGridOutput { } } -/// The sizes of the grid and the look of the pinned group headers; the headers' own look is the heading row's. +/// The look of the pinned group headers and of the headers themselves, which are heading rows. #[derive(Clone, Debug, PartialEq, ComponentStyle)] #[non_exhaustive] pub struct TileGridStyle { @@ -130,9 +130,13 @@ pub struct TileGridStyle { metrics: Metrics, /// A header pinned over its group's tiles. pinned: StyleColor, - /// The side of the disclosure column and the space beside the header's content. - toggle: f32, - pad: f32, + /// The look of a group header: a heading row, which places the disclosure the grid declares. + header: ListRowStyle, +} + +/// The sizes the grid lays its tiles out with. +#[derive(Clone, Copy, Debug, PartialEq)] +struct GridSizes { /// The picture side of a card at zoom 1 when a grid names no thumbnail size: the card thumbnail. card: f32, /// The picture side of a card at zoom 1 for each thumbnail size. @@ -144,19 +148,14 @@ pub struct TileGridStyle { bare_spacing: ArrangeSpacing, } -impl TileGridStyle { - fn build(cx: &StyleCx<'_>, card: bool) -> TileGridStyle { - let colors = &cx.tokens.color; +impl GridSizes { + fn of(cx: &StyleCx<'_>, card: bool) -> GridSizes { let spacing = ArrangeSpacing::new( if card { cx.tokens.space.gap.section } else { cx.tokens.space.gap.stack }, cx.tokens.space.gap.inline, cx.tokens.size.panel_header, ); - TileGridStyle { - metrics: Metrics::new(), - pinned: cx.color(colors.surface.panel), - toggle: cx.tokens.size.icon.sm, - pad: cx.tokens.space.gap.inline, + GridSizes { card: cx.tokens.size.thumb.card, thumbs: [cx.tokens.size.thumb.sm, cx.tokens.size.thumb.md, cx.tokens.size.thumb.lg], around: Tile::around(cx, card), @@ -241,7 +240,6 @@ pub struct TileGrid<'a, K: Clone + Eq + Hash + Send + Sync + 'static> { #[prop(flag)] detail_line: bool, /// Tiles are framed cards: the picture inset in a raised surface with the name and detail at the reading start. - #[style_key] #[prop(flag)] card: bool, /// The headers over runs of tiles, in display order. @@ -291,6 +289,8 @@ struct Placed { group: usize, rect: Rect, pinned: bool, + /// The header has a disclosure part. + toggle: bool, } impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> TileGrid<'a, K> { @@ -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 = TileGridStyle::build(cx, self.card); + let style = GridSizes::of(cx, self.card); 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. @@ -446,7 +446,7 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> TileGrid<'a, K> { /// The side of a card's picture: the largest tile's (`item`, as the items step measured it) less what a tile adds /// around its picture, with `content_cells`; else the zoomed thumbnail side. - fn picture_side(&self, style: &TileGridStyle, item: Option, around: Around) -> f32 { + fn picture_side(&self, style: &GridSizes, item: Option, around: Around) -> f32 { match item.filter(|_| self.content_cells) { Some(item) => (item.x - around.size().x).max(1.0), None => style.picture_side(self.zoom, self.thumb_size), @@ -477,10 +477,20 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> TileGrid<'a, K> { } } - /// The disclosure column of the header in `rect`, at its reading start. - fn toggle_rect(rect: Rect, style: &TileGridStyle, rtl: bool) -> Rect { - let x = if rtl { rect.max.x - style.pad - style.toggle } else { rect.min.x + style.pad }; - Rect::from_min_size(pos2(x, rect.min.y), vec2(style.toggle, rect.height())) + /// 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); + if self.collapsible { + row = row.group(group.open).disclosure_by_host(); + } + if let Some(icon) = &group.icon { + row = row.icon(IconSpec::svg(icon.clone())); + } + if !group.detail.is_empty() { + row = row.trailing(group.detail.clone()); + } + row } } @@ -490,8 +500,12 @@ impl Component for TileGrid<'_, K> type Style = TileGridStyle; type Output = TileGridOutput; - fn style(cx: &StyleCx<'_>, key: TileGridStyleKey) -> TileGridStyle { - TileGridStyle::build(cx, key.card) + fn style(cx: &StyleCx<'_>, _: TileGridStyleKey) -> TileGridStyle { + TileGridStyle { + metrics: Metrics::new(), + pinned: cx.color(cx.tokens.color.surface.panel), + header: ListRow::style(cx, ListRow::new(Label::default()).heading().style_key()), + } } /// One pointer part per header in view, over the tiles, and a disclosure part in it when headers collapse; one @@ -514,12 +528,14 @@ impl Component for TileGrid<'_, K> if !rect.intersects(clip) { continue; } - self.placed.push(Placed { group, rect, pinned: held }); + let column = self + .header_row(declared, RowState::default()) + .disclosure_at(&cx.style.header, rect, rtl, &|run| cx.layout_at(run, f32::INFINITY).size().x); + self.placed.push(Placed { group, rect, pinned: held, toggle: column.is_some() }); let priority = HitPriority::new(HitLevel::Edge, 0); let sense = Sense::CLICK | Sense::TAKE_FOCUS; cx.part(section_part(&declared.key), rect).sense(sense).priority(priority).over().pointer_only(HEADER_KEYS); - if self.collapsible { - let column = Self::toggle_rect(rect, cx.style, rtl); + if let Some(column) = column { cx.part(TOGGLE.at(group as u64), column).sense(Sense::CLICK).priority(priority).over().pointer_only(HEADER_KEYS); } } @@ -658,13 +674,9 @@ impl Component for TileGrid<'_, K> draw.edge_rule(RuleKind::Divider, rect, RectSide::Bottom); } let lead = RowState { emphasized: lead == Some(Slot::Header(placed.group)), ..RowState::default() }; - let mut heading = ListRow::new(group.title.clone()).heading().node(false).state(lead); - if !group.detail.is_empty() { - heading = heading.trailing(group.detail.clone()); - } - ui.face(section_part(&group.key), heading); - if self.collapsible { - let disclosure = Disclosure::new(Bind::given(group.open)).node(false).label(group.title.clone()); + ui.face(section_part(&group.key), self.header_row(group, lead)); + if placed.toggle { + let disclosure = Disclosure::new(Bind::given(group.open)).node(false).triangle(true).label(group.title.clone()); ui.face(TOGGLE.at(placed.group as u64), disclosure); } } @@ -792,7 +804,7 @@ impl Collection for TileGrid<'_, K if self.card_columns == 0 { return 0.0; } - let style = TileGridStyle::build(cx, self.card); + let style = GridSizes::of(cx, self.card); 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/grid/stories.rs b/crates/ui-widgets/src/tile_grid/grid/stories.rs index 893e84a57..f5aa44780 100644 --- a/crates/ui-widgets/src/tile_grid/grid/stories.rs +++ b/crates/ui-widgets/src/tile_grid/grid/stories.rs @@ -144,7 +144,7 @@ fn tile_grid_groups(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { TileGroup::new(GROUP_KEYS[1], 12, 8, "September 2026").detail("8 photos").open(open[1]), TileGroup::new(GROUP_KEYS[2], 20, 10, "August 2026").detail("10 photos").open(open[2]), ]; - let out = grid(ui, data, TileGrid::new("Library").groups(&groups).collapsible(), 30, 2.0); + let out = grid(ui, data, TileGrid::new("Library").groups(&groups).collapsible(), 30, 1.0); let open = data.value("open", open); for event in &out.events { if let GridEvent::GroupOpen { group, open: wanted } = event { diff --git a/crates/ui-widgets/src/tile_grid/layout.rs b/crates/ui-widgets/src/tile_grid/layout.rs index 5227030ff..3899971bc 100644 --- a/crates/ui-widgets/src/tile_grid/layout.rs +++ b/crates/ui-widgets/src/tile_grid/layout.rs @@ -4,7 +4,7 @@ //! plus what the tile adds around it (`Around`), in every tiling. use crate::runtime::env::Label; -use craft_behavior::{ItemLayout, ItemSource, NavShape, ScrollPos, SizeHint, TileLayout as EqualTiles}; +use craft_behavior::{IconId, ItemLayout, ItemSource, NavShape, ScrollPos, SizeHint, TileLayout as EqualTiles}; use egui::{Pos2, Rect, Vec2, pos2, vec2}; use std::ops::Range; use std::sync::Arc; @@ -32,12 +32,16 @@ pub struct TileGroup { pub detail: String, /// Whether the group shows its tiles; a closed group shows its header only. pub open: bool, + /// The header's level in a tree of groups (a folder in a folder), which indents the header, not its tiles. + pub depth: usize, + /// The glyph before the header's title (a folder). + pub icon: Option, } impl TileGroup { /// An open group of `count` tiles from tile position `start`, titled `title`, whose header row is keyed `key`. pub fn new(key: K, start: usize, count: usize, title: impl Into) -> Self { - TileGroup { key, start, count, title: title.into(), detail: String::new(), open: true } + TileGroup { key, start, count, title: title.into(), detail: String::new(), open: true, depth: 0, icon: None } } /// The detail at the header's trailing end. @@ -53,6 +57,20 @@ impl TileGroup { self.open = open; self } + + /// The header's level in a tree of groups; 0 is the top. + #[must_use] + pub fn depth(mut self, depth: usize) -> Self { + self.depth = depth; + self + } + + /// The glyph before the header's title. + #[must_use] + pub fn icon(mut self, icon: IconId) -> Self { + self.icon = Some(icon); + self + } } /// A full-width row after the tiles and their groups, below a divider: an action such as "Create preset", listed