mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 06:48:26 +00:00
ListRow::separator draws a rule between option groups (Select, number presets); List::tree_leaves lines leaves up with groups
This commit is contained in:
@@ -22,7 +22,7 @@ pub use predicate::{DropTest, RowTest, RowText};
|
||||
pub(crate) use row::RowThumb;
|
||||
pub use row::{
|
||||
ListRow, ListRowStyle, RowDragSpot, RowEvent, RowInk, RowOutput, RowStyleCx, ThumbAspect, ThumbPaint, ThumbSize, ThumbTarget, ThumbTargets,
|
||||
TrailingFace, TrailingPart, label_color_label, tree_steps,
|
||||
TrailingFace, TrailingPart, TreeLeaves, label_color_label, tree_steps,
|
||||
};
|
||||
pub use toggle::{ToggleCell, ToggleFace, ToggleState};
|
||||
pub use tree::OpenSet;
|
||||
|
||||
@@ -36,7 +36,7 @@ pub use placement::indent_steps;
|
||||
#[cfg(feature = "testing")]
|
||||
#[doc(hidden)]
|
||||
pub use placement::placement_under_fixed_sizes;
|
||||
pub use placement::tree_steps;
|
||||
pub use placement::{TreeLeaves, tree_steps};
|
||||
pub use style::{ListRowStyle, RowStyleCx};
|
||||
pub(crate) use thumb::RowThumb;
|
||||
pub(super) use thumb::Thumb;
|
||||
@@ -214,6 +214,9 @@ pub struct ListRow<'a> {
|
||||
#[prop(into)]
|
||||
pub(super) accessible_name: Option<String>,
|
||||
pub(super) spacer: bool,
|
||||
/// A spacer that draws a rule across the row: the line between groups of options (`separator`).
|
||||
#[style_key]
|
||||
pub(super) separator: bool,
|
||||
pub(super) cells: Vec<(ColumnKey, Rect)>,
|
||||
/// Each column's alignment and whether its figures are tabular, from `columns(..)`.
|
||||
pub(super) aligns: Vec<(ColumnKey, HAlign, bool)>,
|
||||
@@ -265,6 +268,8 @@ pub struct ListRow<'a> {
|
||||
/// 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,
|
||||
/// How the row's tree lines up its leaves, from the collection (`List::tree_leaves`).
|
||||
pub(super) leaves: TreeLeaves,
|
||||
/// 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)]
|
||||
@@ -322,6 +327,7 @@ impl<'a> ListRow<'a> {
|
||||
progress: None,
|
||||
accessible_name: None,
|
||||
spacer: false,
|
||||
separator: false,
|
||||
cells: Vec::new(),
|
||||
aligns: Vec::new(),
|
||||
muted: Vec::new(),
|
||||
@@ -345,6 +351,7 @@ impl<'a> ListRow<'a> {
|
||||
depth: None,
|
||||
group: None,
|
||||
disclosure_by_host: false,
|
||||
leaves: TreeLeaves::Compact,
|
||||
option: false,
|
||||
command: false,
|
||||
check: None,
|
||||
@@ -370,6 +377,21 @@ impl<'a> ListRow<'a> {
|
||||
row
|
||||
}
|
||||
|
||||
/// A line between groups of rows, as a menu's separator: a spacer (no item, no sense, no AccessKit node; mark its
|
||||
/// position `ItemSource::is_section`) as high as the divider rule and the room around it, drawing the rule.
|
||||
pub fn separator() -> Self {
|
||||
let mut row = Self::spacer();
|
||||
row.separator = true;
|
||||
row
|
||||
}
|
||||
|
||||
/// How the row's tree lines up its leaves: the collection gives it (`List::tree_leaves`).
|
||||
#[must_use]
|
||||
pub(crate) fn leaves(mut self, leaves: TreeLeaves) -> Self {
|
||||
self.leaves = leaves;
|
||||
self
|
||||
}
|
||||
|
||||
/// The rects of the row's columns, which the item context lists (`ItemCx::cells`): toggle cells and text cells
|
||||
/// draw in theirs and the rest of the row lays out in what they leave.
|
||||
#[must_use]
|
||||
|
||||
@@ -268,7 +268,7 @@ impl ListRow<'_> {
|
||||
if self.sub_row {
|
||||
return 0;
|
||||
}
|
||||
self.depth.map_or(0, |depth| tree_steps(depth, self.group.is_some()))
|
||||
self.depth.map_or(0, |depth| self.leaves.steps(depth, self.group.is_some()))
|
||||
}
|
||||
|
||||
/// The widths of the trailing items and the leading text's slot: as `text_width` measures a text, the icon side for
|
||||
@@ -511,6 +511,28 @@ pub fn tree_steps(depth: usize, group: bool) -> usize {
|
||||
if depth > 0 && !group { depth + 1 } else { depth }
|
||||
}
|
||||
|
||||
/// How a tree lines up its leaves with its groups.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
|
||||
#[non_exhaustive]
|
||||
pub enum TreeLeaves {
|
||||
/// A leaf at the top level starts where a group's disclosure does: the Layers panel, whose top-level layers keep
|
||||
/// no disclosure column.
|
||||
#[default]
|
||||
Compact,
|
||||
/// Every leaf keeps the disclosure column, so its label lines up with the labels of the groups beside it: a font
|
||||
/// menu's families with and without styles.
|
||||
Aligned,
|
||||
}
|
||||
|
||||
impl TreeLeaves {
|
||||
/// The indent steps of a tree row at `depth` under this rule: [`tree_steps`], and for `Aligned` one step more for
|
||||
/// a top-level leaf.
|
||||
pub fn steps(self, depth: usize, group: bool) -> usize {
|
||||
let aligned = self == TreeLeaves::Aligned && depth == 0 && !group;
|
||||
tree_steps(depth, group) + usize::from(aligned)
|
||||
}
|
||||
}
|
||||
|
||||
/// The width of a character of a trailing text under fixed sizes.
|
||||
#[cfg(feature = "testing")]
|
||||
const FIXED_ADVANCE: f32 = 10.0;
|
||||
|
||||
@@ -86,7 +86,8 @@ impl Component for ListRow<'_> {
|
||||
let muted = if key.ink == RowInk::Dimmed { text.faint } else { text.muted };
|
||||
ListRowStyle {
|
||||
surface,
|
||||
metrics: Metrics::new().min_size(Vec2::new(0.0, sizes.row)).gap(sizes.gap),
|
||||
// A separator is as high as its rule and the room around it.
|
||||
metrics: Metrics::new().min_size(Vec2::new(0.0, if key.separator { 0.0 } else { sizes.row })).gap(sizes.gap),
|
||||
text: TextLook::new(role, ink(base)),
|
||||
icon: IconLook::new(
|
||||
sizes.icon,
|
||||
@@ -109,6 +110,8 @@ 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,
|
||||
sizes,
|
||||
}
|
||||
}
|
||||
@@ -116,6 +119,9 @@ impl Component for ListRow<'_> {
|
||||
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
|
||||
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);
|
||||
}
|
||||
if self.spacer {
|
||||
return Vec2::ZERO;
|
||||
}
|
||||
@@ -483,6 +489,10 @@ impl Component for ListRow<'_> {
|
||||
}
|
||||
|
||||
fn paint(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) {
|
||||
if self.separator {
|
||||
ui.draw().rule(RuleKind::Divider, cx.rect.left_center(), cx.rect.right_center());
|
||||
return;
|
||||
}
|
||||
if self.spacer {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! The look of a row: ListRowStyle, the fill and ink rules of its flags.
|
||||
|
||||
use super::placement::Sizes;
|
||||
use crate::style::{ComponentStyle, Flags, IconLook, StyleColor, StyleCx, SurfaceLook, TextLook};
|
||||
use crate::style::{ComponentStyle, Flags, IconLook, RuleWidth, StyleColor, StyleCx, SurfaceLook, TextLook};
|
||||
use craft_behavior::Metrics;
|
||||
use craft_theme::{Contrast, Rgba, Stateful, States, contrast_ratio, identifiable_outline, over};
|
||||
|
||||
@@ -45,6 +45,10 @@ 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,
|
||||
pub(super) sizes: Sizes,
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
//! the rename lifecycle shows.
|
||||
|
||||
use super::predicate::{DropTest, RowTest, RowText};
|
||||
use super::{ListRow, RowEvent};
|
||||
use super::{ListRow, RowEvent, TreeLeaves};
|
||||
use crate::contract::{
|
||||
Access, ActivateOn, Bind, BindMut, Collection, CollectionProps, CollectionResponse, ColumnLayout, Common, Component, ContainerProps, Cx,
|
||||
DropKind, EdgePress, EditResponse, HoverLead, LeadOnOpen, ListDrag, Props as _, Role, RowState, SelectMode, Sizing,
|
||||
@@ -53,6 +53,9 @@ pub struct List<'a, K: Clone + Eq + Hash + Send + Sync + 'static> {
|
||||
/// A tree: rows are indented by depth and groups open and close.
|
||||
#[prop]
|
||||
tree: bool,
|
||||
/// How the tree lines up its leaves with its groups: `Compact` unless set.
|
||||
#[prop]
|
||||
tree_leaves: TreeLeaves,
|
||||
/// Which rows may be renamed; a rename request on another row does nothing.
|
||||
#[prop(func = "Fn(&K) -> bool")]
|
||||
can_rename: Option<RowTest<'a, K>>,
|
||||
@@ -96,6 +99,7 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> List<'a, K> {
|
||||
column_layout: None,
|
||||
reveal: None,
|
||||
tree: false,
|
||||
tree_leaves: TreeLeaves::Compact,
|
||||
can_rename: None,
|
||||
rename_draft: None,
|
||||
can_drag: None,
|
||||
@@ -234,7 +238,7 @@ impl<K: Clone + Eq + Hash + Send + Sync + 'static> Collection for List<'_, K> {
|
||||
}
|
||||
|
||||
fn dress<'r>(&self, row: Self::Item<'r>, state: &RowState) -> Self::Item<'r> {
|
||||
let row = row.rows(self.items.row_height).sub_row(state);
|
||||
let row = row.rows(self.items.row_height).sub_row(state).leaves(self.tree_leaves);
|
||||
if self.row_menu { row.row_menu() } else { row }
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ impl<'e, 'l, T> EntryRows<'e, 'l, T> {
|
||||
}
|
||||
|
||||
/// The row of the entry at `at`: its label and icon as an option, checked when it is the `chosen` entry, a caption
|
||||
/// for a heading, a spacer for a separator, the host's picture as the thumbnail and its dress over the rest.
|
||||
/// for a heading, a separator row for a separator, the host's picture as the thumbnail and its dress over the rest.
|
||||
pub(super) fn row<'r>(
|
||||
&self,
|
||||
at: usize,
|
||||
@@ -34,7 +34,7 @@ impl<'e, 'l, T> EntryRows<'e, 'l, T> {
|
||||
let entries: &'e [Entry<T>] = self.entries;
|
||||
let entry = &entries[at];
|
||||
let Some(value) = entry.value() else {
|
||||
return if entry.is_heading() { ListRow::new(self.labels[at].clone()).heading() } else { ListRow::spacer() };
|
||||
return if entry.is_heading() { ListRow::new(self.labels[at].clone()).heading() } else { ListRow::separator() };
|
||||
};
|
||||
let row = ListRow::new(self.labels[at].clone()).option().check(chosen == Some(at)).enabled(entry.is_enabled());
|
||||
let row = match entry.icon_spec() {
|
||||
|
||||
@@ -303,7 +303,7 @@ impl<T: Value + 'static> Component for Select<'_, T> {
|
||||
access.numeric(numeric.step(1.0));
|
||||
}
|
||||
|
||||
/// The listbox: one option row per entry, a spacer for a separator, the picked entry's row selected. Each row the
|
||||
/// The listbox: one option row per entry, a separator row for a separator, the picked entry's row selected. Each row the
|
||||
/// keys move the lead to is proposed, and with a preview each row the pointer moves it to; a pick comes back from
|
||||
/// the list as Enter or a click on a row and commits.
|
||||
fn overlay(&mut self, ui: &mut OverlayUi<'_>, cx: &mut Cx<'_, Self>) {
|
||||
|
||||
@@ -33,7 +33,7 @@ impl<'a, V: NumberValue + 'static> NumberField<'a, V> {
|
||||
.selection(Bind::given(selection))
|
||||
.show_items(ui, &source, &mut |at, _| {
|
||||
if rows[*at].is_separator() {
|
||||
ListRow::spacer()
|
||||
ListRow::separator()
|
||||
} else if rows[*at].is_heading() {
|
||||
ListRow::new(labels[*at].clone()).heading()
|
||||
} else {
|
||||
|
||||
@@ -321,3 +321,32 @@ fn hovering_a_row_moves_the_lead_and_leaves_the_selected_option_held() {
|
||||
fixture.settle();
|
||||
assert_eq!(value(&mut fixture), 3, "the lead followed the pointer, so Enter chooses the row under it");
|
||||
}
|
||||
|
||||
/// One to Four with a separator between Two and Three.
|
||||
fn separated(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let entries = [Entry::new(1u8, "One"), Entry::new(2u8, "Two"), Entry::separator(), Entry::new(3u8, "Three"), Entry::new(4u8, "Four")];
|
||||
Select::new("Pick", data.bind("value", 1u8), &entries).show(ui).into()
|
||||
}
|
||||
|
||||
/// A separator draws a rule between the rows around it, lower than a row, and the keys pass over it (FW-319: it drew
|
||||
/// blank room).
|
||||
#[trial]
|
||||
fn a_separator_in_the_list_draws_a_rule_and_the_keys_pass_over_it() {
|
||||
let mut fixture = Fixture::story(&Story::new("select_separated", separated));
|
||||
fixture.settle();
|
||||
fixture.key(Key::Tab, Modifiers::NONE);
|
||||
fixture.key(Key::ArrowDown, Modifiers::ALT);
|
||||
fixture.settle();
|
||||
let two = fixture.by_label("Two").expect("the Two row").rect;
|
||||
let three = fixture.by_label("Three").expect("the Three row").rect;
|
||||
let gap = three.top() - two.bottom();
|
||||
assert!(gap > 0.0 && gap < two.height(), "the separator is lower than a row: {gap} between rows {} high", two.height());
|
||||
let rule = fixture.shape_bounds().iter().any(|shape| {
|
||||
shape.top() >= two.bottom() - 0.5 && shape.bottom() <= three.top() + 0.5 && shape.width() > two.width() / 2.0 && shape.height() < gap
|
||||
});
|
||||
assert!(rule, "a rule across the list between Two and Three");
|
||||
fixture.key(Key::ArrowDown, Modifiers::NONE);
|
||||
fixture.key(Key::ArrowDown, Modifiers::NONE);
|
||||
fixture.settle();
|
||||
assert_eq!(value(&mut fixture), 3, "Down from Two lands on Three");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user