diff --git a/crates/draw/src/ui/brush_settings/sections.rs b/crates/draw/src/ui/brush_settings/sections.rs index da334a98b..46c5f27d7 100644 --- a/crates/draw/src/ui/brush_settings/sections.rs +++ b/crates/draw/src/ui/brush_settings/sections.rs @@ -160,6 +160,8 @@ impl Outcome for BrushSectionListOutput { enum Slot { /// The "Brushes" link row. Link, + /// The line between the link row and the sections (a separator row, so keys and clicks skip it). + Rule, /// Empty room (a spacer row, so keys and clicks skip it) as tall as its gap, counted from 0. Gap(u8), /// The section with this key. @@ -175,7 +177,7 @@ struct Rows<'a, K> { } impl<'a, K: Clone + Eq> Rows<'a, K> { - /// The link row and a gap of `gap`, then the sections with a gap of `gap` after the first row and one of + /// The link row and a line under it, then the sections with a gap of `gap` after the first row and one of /// `switch_gap` before the first switch row. fn new(sections: &'a [BrushSection], link_row: bool, gap: f32, switch_gap: f32) -> Self { let mut slots = Vec::new(); @@ -186,7 +188,7 @@ impl<'a, K: Clone + Eq> Rows<'a, K> { }; if link_row { slots.push(Slot::Link); - room(&mut slots, gap); + slots.push(Slot::Rule); } let first_switch = sections.iter().position(|section| section.kind == BrushSectionKind::Switch); for (at, section) in sections.iter().enumerate() { @@ -216,7 +218,7 @@ impl DisplayOrder> for Rows<'_, K> { } fn is_section(&self, at: usize) -> bool { - matches!(self.slots[at], Slot::Gap(_)) + matches!(self.slots[at], Slot::Gap(_) | Slot::Rule) } fn text(&self, at: usize) -> Option> { @@ -314,7 +316,8 @@ impl<'a, K: Value + Eq + Hash> BrushSectionList<'a, K> { .sizing(Sizing::Content); let out = table.show_items(ui, &rows, &mut |key, icx| match key { Slot::Gap(_) => ListRow::spacer(), - Slot::Link => ListRow::new(words.brushes.clone()).cells(icx.cells()).icon(IconSpec::svg(IconId::new("brush"))).rule(true), + Slot::Rule => ListRow::separator(), + Slot::Link => ListRow::new(words.brushes.clone()).cells(icx.cells()).icon(IconSpec::svg(IconId::new("brush"))), Slot::Section(key) => { let Some(section) = rows.section(key) else { return ListRow::spacer() }; let mut row = @@ -396,7 +399,7 @@ impl<'a, K: Value + Eq + Hash> BrushSectionList<'a, K> { } CollectionEvent::Activate(slot) if !toggled.contains(&slot) => match slot { Slot::Link => events.push(BrushSectionEvent::OpenLink), - Slot::Gap(_) => {} + Slot::Gap(_) | Slot::Rule => {} Slot::Section(key) => { let Some(section) = rows.section(&key) else { continue }; match section.kind { diff --git a/crates/ui-patterns/src/patterns/panels/palette.rs b/crates/ui-patterns/src/patterns/panels/palette.rs index cc37a7a8a..07a2e19c9 100644 --- a/crates/ui-patterns/src/patterns/panels/palette.rs +++ b/crates/ui-patterns/src/patterns/panels/palette.rs @@ -44,13 +44,14 @@ struct Session { announced: Option, } -/// The key of a row of the list: the suggested heading, a command by its position in the app's entries, or the -/// fallback row. +/// The key of a row of the list: the suggested heading, a command by its position in the app's entries, the line that +/// sets the fallback row off from the commands, or the fallback row. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)] enum Line { #[default] Heading, Entry(usize), + Separator, Fallback, } @@ -329,7 +330,7 @@ fn show_rows(ui: &mut Ui, input: &RowsInput<'_>, state: &mut Session, env: &crat match key { Line::Entry(at) => chosen.picked = input.entries.get(at).map(|entry| entry.command().clone()), Line::Fallback => chosen.fallback = Some(found.clone()), - Line::Heading => {} + Line::Heading | Line::Separator => {} } } } @@ -430,6 +431,9 @@ fn layout( } let mut rows: Vec = listing(entries, query, folder, texts, recent).into_iter().map(Line::Entry).collect(); if with_fallback { + if !rows.is_empty() { + rows.push(Line::Separator); + } rows.push(Line::Fallback); } rows @@ -454,8 +458,8 @@ struct RowData { enabled: bool, /// The row heads the rows after it. heading: bool, - /// A rule under the row sets it off from the fallback row that follows. - rule: bool, + /// The row is the line between the commands and the fallback row. + separator: bool, } impl RowData { @@ -465,7 +469,10 @@ impl RowData { if self.heading { return ListRow::new(self.label.clone()).heading(); } - let row = ListRow::new(self.label.clone()).option().command_row().enabled(self.enabled).rule(self.rule); + if self.separator { + return ListRow::separator(); + } + let row = ListRow::new(self.label.clone()).option().command_row().enabled(self.enabled); // The first trailing item is the outermost: the chord ends the row and the path sits before it. let row = match &self.chord { Some(chord) => row.trailing(chord.clone()), @@ -488,11 +495,13 @@ fn row_data( env: &crate::runtime::env::EnvView<'_>, ) -> Vec { let (os, layout) = (env.shell().os, env.keyboard_layout()); + let line = + |label: &str, heading: bool, separator: bool| RowData { label: label.to_owned(), path: None, chord: None, enabled: true, heading, separator }; keys.iter() - .enumerate() - .map(|(index, key)| match key { - Line::Heading => RowData { label: heading.to_owned(), path: None, chord: None, enabled: true, heading: true, rule: false }, - Line::Fallback => RowData { label: fallback.to_owned(), path: None, chord: None, enabled: true, heading: false, rule: false }, + .map(|key| match key { + Line::Heading => line(heading, true, false), + Line::Separator => line("", false, true), + Line::Fallback => line(fallback, false, false), Line::Entry(at) => { let resolved = commands::resolve_beneath(ctx, entries[*at].command().clone()); RowData { @@ -501,7 +510,7 @@ fn row_data( chord: resolved.chord.as_ref().map(|chord| chord.display_for(os, layout)), enabled: resolved.enabled, heading: false, - rule: keys.get(index + 1) == Some(&Line::Fallback), + separator: false, } } }) @@ -524,7 +533,7 @@ impl DisplayOrder for Listed<'_> { } fn is_section(&self, at: usize) -> bool { - self.rows[at].heading + self.rows[at].heading || self.rows[at].separator } fn enabled(&self, at: usize) -> bool { @@ -532,7 +541,7 @@ impl DisplayOrder for Listed<'_> { } fn text(&self, at: usize) -> Option> { - (!self.rows[at].heading).then(|| std::borrow::Cow::Borrowed(self.rows[at].label.as_str())) + (!self.is_section(at)).then(|| std::borrow::Cow::Borrowed(self.rows[at].label.as_str())) } } diff --git a/crates/ui-patterns/src/patterns/presets/browser.rs b/crates/ui-patterns/src/patterns/presets/browser.rs index d343f32d3..7a530147c 100644 --- a/crates/ui-patterns/src/patterns/presets/browser.rs +++ b/crates/ui-patterns/src/patterns/presets/browser.rs @@ -67,19 +67,21 @@ pub enum PresetItemCommands { const LARGE_FROM: f64 = 1.5; const SMALL_UNTIL: f64 = 0.75; -/// The key of a row of the browser's collections: a preset or group of the app's rows, or the action row at an index of -/// `PresetBrowser::actions`. The browser maps it back to the app's key at every edge it reports through. +/// The key of a row of the browser's collections: a preset or group of the app's rows, the action row at an index of +/// `PresetBrowser::actions`, or the line between the presets and the actions. The browser maps it back to the app's key +/// at every edge it reports through. #[derive(Clone, Debug, PartialEq, Eq, Hash)] enum BrowserKey { Preset(K), Action(usize), + Separator, } impl BrowserKey { fn preset(&self) -> Option<&K> { match self { BrowserKey::Preset(key) => Some(key), - BrowserKey::Action(_) => None, + BrowserKey::Action(_) | BrowserKey::Separator => None, } } } @@ -116,7 +118,7 @@ impl DropTarget>> for ZoneDrop<'_, fn app_move(mv: ReorderMove>) -> Option> { let app = |key: BrowserKey| match key { BrowserKey::Preset(key) => Some(key), - BrowserKey::Action(_) => None, + BrowserKey::Action(_) | BrowserKey::Separator => None, }; Some(ReorderMove { key: app(mv.key)?, @@ -305,19 +307,32 @@ struct Shown<'a, K> { } impl Shown<'_, K> { - /// The preset row at position `at`, `None` for an action row. + /// The preset row at position `at`, `None` for the separator or an action row. fn row(&self, at: usize) -> Option<&PresetRow> { self.at.get(at).map(|&row| &self.rows[row]) } + + /// Whether a line sets the action rows off from the presets: both are shown. + fn separated(&self) -> bool { + self.actions > 0 && !self.at.is_empty() + } } impl DisplayOrder> for Shown<'_, K> { fn len(&self) -> usize { - self.at.len() + self.actions + self.at.len() + usize::from(self.separated()) + self.actions } fn key(&self, at: usize) -> BrowserKey { - self.row(at).map_or_else(|| BrowserKey::Action(at - self.at.len()), |row| BrowserKey::Preset(row.key.clone())) + match self.row(at) { + Some(row) => BrowserKey::Preset(row.key.clone()), + None if self.separated() && at == self.at.len() => BrowserKey::Separator, + None => BrowserKey::Action(at - self.at.len() - usize::from(self.separated())), + } + } + + fn is_section(&self, at: usize) -> bool { + self.separated() && at == self.at.len() } fn depth(&self, at: usize) -> usize { @@ -747,7 +762,7 @@ fn list_view(props: &m let accepts = |drag: &ListDrag>, over: Option<&BrowserKey>, insert: usize| { let (Some(rules), Some(drag)) = (reorder, app_drag(drag)) else { return false }; match over { - Some(BrowserKey::Action(_)) => false, + Some(BrowserKey::Action(_) | BrowserKey::Separator) => false, over => rules.allows(source, rows, &drag, over.and_then(BrowserKey::preset), insert), } }; @@ -856,26 +871,23 @@ fn list_view(props: &m list = list.max_rows(rows); } let aspect = cell_shape.aspect(); - // The last preset row draws the divider above the action rows. - let ruled = |at: usize| !actions.is_empty() && at + 1 == shown.len(); - let out = list.show_items(ui, &source_rows, &mut |_, icx| { + let out = list.show_items(ui, &source_rows, &mut |key, icx| { let Some(&at) = source_rows.at.get(icx.at) else { - return ListRow::new(actions[icx.at - shown.len()].clone()); + return match key { + BrowserKey::Action(action) => ListRow::new(actions[*action].clone()), + _ => ListRow::separator(), + }; }; let row = &rows[at]; if row.is_group() { - 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)); + let mut line = + ListRow::new(row.label.clone()).heading().depth(row.depth).group(icx.row.open).icon(IconSpec::svg(folder(icx.row.open))); if let Some(detail) = counted(at) { line = line.detail(detail); } return line; } - let mut line = ListRow::new(row.label.clone()).depth(row.depth).rule(ruled(icx.at)); + let mut line = ListRow::new(row.label.clone()).depth(row.depth); if let Some(caption) = &row.caption { line = line.detail(caption.clone()); } @@ -965,6 +977,7 @@ fn list_view(props: &m action(&mut events, at); } } + Seen::Select(BrowserKey::Separator) | Seen::Activate(BrowserKey::Separator) => {} Seen::Open(change) => { if let BrowserKey::Preset(key) = change.key { events.push(PresetEvent::Open(OpenChange { key, open: change.open, all: change.all })); diff --git a/crates/ui-widgets/src/display/divider.rs b/crates/ui-widgets/src/display/divider.rs index 91495429b..35dfddf52 100644 --- a/crates/ui-widgets/src/display/divider.rs +++ b/crates/ui-widgets/src/display/divider.rs @@ -20,8 +20,27 @@ pub struct DividerStyle { slot: f32, /// The least length of a line, vertical or horizontal: one control height. length: f32, - /// Above and below a horizontal line in a menu: `space.gap.inline`. - separator_pad: f32, + /// A horizontal line in a menu, with its room. + separator: SeparatorRule, +} + +/// A line between groups of items, in a menu (a horizontal [`Divider`]) or a list (`ListRow::separator`): the divider +/// rule with `space.gap.inline` of room above and below it. +#[derive(Clone, Copy, Debug, PartialEq)] +pub(crate) struct SeparatorRule { + width: RuleWidth, + pad: f32, +} + +impl SeparatorRule { + pub(crate) fn of(cx: &StyleCx<'_>) -> Self { + SeparatorRule { width: cx.rule(RuleKind::Divider).width, pad: cx.tokens.space.gap.inline } + } + + /// The height of the line and its room on a display of `pixels_per_point`. + pub(crate) fn height(self, pixels_per_point: f32) -> f32 { + self.width.points(pixels_per_point) + 2.0 * self.pad + } } /// Which way the line runs. @@ -70,7 +89,7 @@ impl Component for Divider { width: cx.rule(RuleKind::Divider).width, slot: cx.tokens.space.gap.item, length: cx.control().height, - separator_pad: cx.tokens.space.gap.inline, + separator: SeparatorRule::of(cx), } } @@ -78,7 +97,7 @@ impl Component for Divider { let width = cx.style.width.points(cx.ui.ctx().pixels_per_point()); let length = cx.style.length; match self.axis { - Axis::Horizontal if cx.env.get::().0 => Vec2::new(length, width + 2.0 * cx.style.separator_pad), + Axis::Horizontal if cx.env.get::().0 => Vec2::new(length, cx.style.separator.height(cx.ui.ctx().pixels_per_point())), Axis::Horizontal => Vec2::new(length, width), Axis::Vertical if cx.env.get::().0 => Vec2::new(cx.style.slot.max(width), length), Axis::Vertical => Vec2::new(width, length), diff --git a/crates/ui-widgets/src/list/row.rs b/crates/ui-widgets/src/list/row.rs index 1bf42c22a..445635205 100644 --- a/crates/ui-widgets/src/list/row.rs +++ b/crates/ui-widgets/src/list/row.rs @@ -178,9 +178,6 @@ pub struct ListRow<'a> { /// or thumbnail. #[prop] pub(super) clipped: bool, - /// A divider rule along the row's bottom edge, in a plain row too. - #[prop] - pub(super) rule: bool, /// The rename editor replaces the label; `state` sets it from the collection's row state. pub(super) renaming: bool, /// Where the collection holds a press on the row, from its row state: the toggle cell it acts on shows it. @@ -315,7 +312,6 @@ impl<'a> ListRow<'a> { label_spans: Vec::new(), highlight: false, clipped: false, - rule: false, renaming: false, pressed: None, menu_open: false, diff --git a/crates/ui-widgets/src/list/row/show.rs b/crates/ui-widgets/src/list/row/show.rs index 411c036d1..62b08a431 100644 --- a/crates/ui-widgets/src/list/row/show.rs +++ b/crates/ui-widgets/src/list/row/show.rs @@ -110,8 +110,7 @@ impl Component for ListRow<'_> { cell_muted: TextLook::new(role, ink(muted)), thumb_frame: c(colors.line.frame), thumb_frame_width: tokens.stroke.thin, - separator: cx.rule(RuleKind::Divider).width, - separator_pad: tokens.space.gap.inline, + separator: crate::display::divider::SeparatorRule::of(cx), sizes, } } @@ -120,7 +119,7 @@ impl Component for ListRow<'_> { self.adopt_facts(&cx.env, cx.resolved.id); let sizes = &cx.style.sizes; if self.separator { - return Vec2::new(0.0, cx.style.separator.points(cx.ui.ctx().pixels_per_point()) + 2.0 * cx.style.separator_pad); + return Vec2::new(0.0, cx.style.separator.height(cx.ui.ctx().pixels_per_point())); } if self.spacer { return Vec2::ZERO; @@ -505,7 +504,7 @@ impl Component for ListRow<'_> { self.label_rect = placed.label; { let draw = ui.draw(); - if self.rule || (self.layer && self.divider_below) { + if self.layer && self.divider_below { draw.edge_rule(RuleKind::Divider, rect, RectSide::Bottom); } if self.layer { diff --git a/crates/ui-widgets/src/list/row/style.rs b/crates/ui-widgets/src/list/row/style.rs index 3dff24b1c..9218a3edb 100644 --- a/crates/ui-widgets/src/list/row/style.rs +++ b/crates/ui-widgets/src/list/row/style.rs @@ -1,7 +1,8 @@ //! The look of a row: ListRowStyle, the fill and ink rules of its flags. use super::placement::Sizes; -use crate::style::{ComponentStyle, Flags, IconLook, RuleWidth, StyleColor, StyleCx, SurfaceLook, TextLook}; +use crate::display::divider::SeparatorRule; +use crate::style::{ComponentStyle, Flags, IconLook, StyleColor, StyleCx, SurfaceLook, TextLook}; use craft_behavior::Metrics; use craft_theme::{Contrast, Rgba, Stateful, States, contrast_ratio, identifiable_outline, over}; @@ -45,10 +46,8 @@ pub struct ListRowStyle { /// The frame at the edge of each thumbnail's slot, around its picture: `line.frame`, `stroke.thin` wide. pub(super) thumb_frame: StyleColor, pub(super) thumb_frame_width: f32, - /// The rule of a separator row: the divider rule's width, and `space.gap.inline` of room above and below it, as a - /// menu's separator has. - pub(super) separator: RuleWidth, - pub(super) separator_pad: f32, + /// The rule of a separator row and its room, as a menu's separator has. + pub(super) separator: SeparatorRule, pub(super) sizes: Sizes, }