One separator between option groups: ListRow::rule goes, the preset browser, command palette and brush settings use ListRow::separator, whose height comes from the Divider's SeparatorRule

This commit is contained in:
aiongg
2026-11-22 06:57:34 +00:00
parent 54f5950a26
commit 8a546e58f1
7 changed files with 92 additions and 54 deletions
@@ -160,6 +160,8 @@ impl<K> Outcome for BrushSectionListOutput<K> {
enum Slot<K> {
/// 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<K>], 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<K: Clone + Eq> DisplayOrder<Slot<K>> 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<Cow<'_, str>> {
@@ -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 {
@@ -44,13 +44,14 @@ struct Session {
announced: Option<usize>,
}
/// 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<Line> = 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<RowData> {
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<Line> 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<Line> for Listed<'_> {
}
fn text(&self, at: usize) -> Option<std::borrow::Cow<'_, str>> {
(!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()))
}
}
@@ -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<K> {
Preset(K),
Action(usize),
Separator,
}
impl<K> BrowserKey<K> {
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<K: Clone + PartialEq> DropTarget<ListDrag<BrowserKey<K>>> for ZoneDrop<'_,
fn app_move<K>(mv: ReorderMove<BrowserKey<K>>) -> Option<ReorderMove<K>> {
let app = |key: BrowserKey<K>| 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<K> 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<K>> {
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<K: Clone> DisplayOrder<BrowserKey<K>> 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<K> {
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<K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static>(props: &m
let accepts = |drag: &ListDrag<BrowserKey<K>>, over: Option<&BrowserKey<K>>, 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<K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static>(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<K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static>(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 }));
+23 -4
View File
@@ -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::<InMenu>().0 => Vec2::new(length, width + 2.0 * cx.style.separator_pad),
Axis::Horizontal if cx.env.get::<InMenu>().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::<InBar>().0 => Vec2::new(cx.style.slot.max(width), length),
Axis::Vertical => Vec2::new(width, length),
-4
View File
@@ -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,
+3 -4
View File
@@ -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 {
+4 -5
View File
@@ -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,
}