chore: audit components
This commit is contained in:
+175
-148
@@ -18,7 +18,9 @@ type ItemsFn = Box<dyn Fn(&mut Window, &mut App) -> Vec<TreeItem>>;
|
||||
|
||||
/// Tree item.
|
||||
#[derive(Clone)]
|
||||
#[must_use]
|
||||
pub struct TreeItem {
|
||||
key: ElementId,
|
||||
label: SharedString,
|
||||
icon: Option<Icon>,
|
||||
depth: usize,
|
||||
@@ -28,12 +30,14 @@ pub struct TreeItem {
|
||||
}
|
||||
|
||||
impl TreeItem {
|
||||
/// Creates a leaf row with this label at the root depth.
|
||||
pub fn new<L>(label: L) -> Self
|
||||
/// Creates a leaf row shown as `label` at the root depth, identified by `key`.
|
||||
pub fn new<K, L>(key: K, label: L) -> Self
|
||||
where
|
||||
K: Into<ElementId>,
|
||||
L: Into<SharedString>,
|
||||
{
|
||||
Self {
|
||||
key: key.into(),
|
||||
label: label.into(),
|
||||
icon: None,
|
||||
depth: 0,
|
||||
@@ -79,6 +83,7 @@ impl TreeItem {
|
||||
|
||||
/// A tree.
|
||||
#[derive(IntoElement)]
|
||||
#[must_use]
|
||||
pub struct Tree {
|
||||
id: ElementId,
|
||||
selected: Option<usize>,
|
||||
@@ -128,7 +133,7 @@ impl Tree {
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the visible rows, built fresh each render.
|
||||
/// Sets the visible rows; the closure runs during render and must not mutate entities or notify.
|
||||
pub fn items<F>(mut self, items: F) -> Self
|
||||
where
|
||||
F: Fn(&mut Window, &mut App) -> Vec<TreeItem> + 'static,
|
||||
@@ -145,6 +150,45 @@ impl Tree {
|
||||
self.a11y_label = Some(label.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Maps each visible row to its 1-based position-in-set and set size among its siblings.
|
||||
fn sibling_sets(items: &[TreeItem]) -> Vec<(usize, usize)> {
|
||||
let mut sets = vec![(0usize, 0usize); items.len()];
|
||||
// Open sibling runs along the ancestry path, deeper runs above shallower ones.
|
||||
let mut runs: Vec<(usize, Vec<usize>)> = Vec::new();
|
||||
|
||||
for (index, item) in items.iter().enumerate() {
|
||||
while runs
|
||||
.last()
|
||||
.is_some_and(|(run_depth, _)| *run_depth > item.depth)
|
||||
{
|
||||
if let Some((_, pending)) = runs.pop() {
|
||||
Self::stamp_sibling_run(&mut sets, pending);
|
||||
}
|
||||
}
|
||||
|
||||
match runs.last_mut() {
|
||||
// A row at the running depth extends the open sibling run.
|
||||
Some((run_depth, pending)) if *run_depth == item.depth => pending.push(index),
|
||||
_ => runs.push((item.depth, vec![index])),
|
||||
}
|
||||
}
|
||||
|
||||
for (_, pending) in runs {
|
||||
Self::stamp_sibling_run(&mut sets, pending);
|
||||
}
|
||||
|
||||
sets
|
||||
}
|
||||
|
||||
fn stamp_sibling_run(sets: &mut [(usize, usize)], pending: Vec<usize>) {
|
||||
let size_of_set = pending.len();
|
||||
for (position, index) in pending.into_iter().enumerate() {
|
||||
if let Some(set) = sets.get_mut(index) {
|
||||
*set = (position + 1, size_of_set);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RenderOnce for Tree {
|
||||
@@ -169,46 +213,15 @@ impl RenderOnce for Tree {
|
||||
.clone()
|
||||
.unwrap_or_else(|| SharedString::from(self.id.to_string()));
|
||||
|
||||
let sibling_sets: Vec<(usize, usize)> = {
|
||||
let mut sets = vec![(0usize, 0usize); items.len()];
|
||||
let mut start = 0usize;
|
||||
|
||||
while start < items.len() {
|
||||
let depth = items[start].depth;
|
||||
|
||||
let mut end = start + 1;
|
||||
while end < items.len() && items[end].depth > depth {
|
||||
end += 1;
|
||||
}
|
||||
|
||||
let mut position = 0usize;
|
||||
for index in start..end {
|
||||
if items[index].depth == depth {
|
||||
position += 1;
|
||||
sets[index] = (position, 0);
|
||||
}
|
||||
}
|
||||
|
||||
for index in start..end {
|
||||
if items[index].depth == depth {
|
||||
sets[index].1 = position;
|
||||
}
|
||||
}
|
||||
|
||||
start = end;
|
||||
}
|
||||
|
||||
sets
|
||||
};
|
||||
let sibling_sets = Self::sibling_sets(&items);
|
||||
|
||||
let rows: Vec<AnyElement> = items
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, _item)| {
|
||||
.map(|(index, item)| {
|
||||
let (position_in_set, size_of_set) = sibling_sets[index];
|
||||
render_row(
|
||||
item.render_row(
|
||||
cx,
|
||||
&items[index],
|
||||
index,
|
||||
position_in_set,
|
||||
size_of_set,
|
||||
@@ -266,121 +279,135 @@ impl RenderOnce for Tree {
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn render_row(
|
||||
cx: &App,
|
||||
item: &TreeItem,
|
||||
index: usize,
|
||||
position_in_set: usize,
|
||||
size_of_set: usize,
|
||||
selected: bool,
|
||||
on_change: &Option<ChangeHandler>,
|
||||
on_disclosure: &Option<DisclosureHandler>,
|
||||
focus: &FocusHandle,
|
||||
) -> AnyElement {
|
||||
let theme = cx.theme();
|
||||
let label_color = selectable_label_color(cx, selected, item.disabled);
|
||||
let disabled = item.disabled;
|
||||
// One indent step per depth level, on the theme's horizontal padding.
|
||||
let indent = theme.sizes.medium.padding_x * (item.depth as f32 + 1.0);
|
||||
impl TreeItem {
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn render_row(
|
||||
&self,
|
||||
cx: &App,
|
||||
index: usize,
|
||||
position_in_set: usize,
|
||||
size_of_set: usize,
|
||||
selected: bool,
|
||||
on_change: &Option<ChangeHandler>,
|
||||
on_disclosure: &Option<DisclosureHandler>,
|
||||
focus: &FocusHandle,
|
||||
) -> AnyElement {
|
||||
let theme = cx.theme();
|
||||
let label_color = selectable_label_color(cx, selected, self.disabled);
|
||||
let disabled = self.disabled;
|
||||
// usize can exceed f32's exact integer range; bound it via u32 before casting.
|
||||
let depth = u32::try_from(self.depth).unwrap_or(u32::MAX) as f32;
|
||||
// One indent step per depth level, on the theme's horizontal padding.
|
||||
let indent = theme.sizes.medium.padding_x * (depth + 1.0);
|
||||
|
||||
div()
|
||||
.id(("tree-item", index))
|
||||
.flex()
|
||||
.items_center()
|
||||
.gap_2()
|
||||
.pl(indent)
|
||||
.pr_2()
|
||||
.py_1()
|
||||
.rounded(theme.radius.sm)
|
||||
.role(Role::TreeItem)
|
||||
.aria_selected(selected)
|
||||
.aria_level(item.depth + 1)
|
||||
.aria_position_in_set(position_in_set)
|
||||
.aria_size_of_set(size_of_set)
|
||||
.when(item.expandable, |row| row.aria_expanded(item.expanded))
|
||||
.map(|row| paint_selectable_row(cx, row, selected, disabled))
|
||||
.when(!disabled, |row| {
|
||||
row.cursor_pointer().on_click({
|
||||
let on_change = on_change.clone();
|
||||
let focus = focus.clone();
|
||||
move |_, window, cx| {
|
||||
window.focus(&focus, cx);
|
||||
if let Some(on_change) = &on_change {
|
||||
on_change(Some(index), window, cx);
|
||||
div()
|
||||
.id((self.key.clone(), "tree-item"))
|
||||
.flex()
|
||||
.items_center()
|
||||
.gap_2()
|
||||
.pl(indent)
|
||||
.pr_2()
|
||||
.py_1()
|
||||
.rounded(theme.radius.sm)
|
||||
.role(Role::TreeItem)
|
||||
.aria_selected(selected)
|
||||
.aria_level(self.depth + 1)
|
||||
.aria_position_in_set(position_in_set)
|
||||
.aria_size_of_set(size_of_set)
|
||||
.when(self.expandable, |row| row.aria_expanded(self.expanded))
|
||||
.map(|row| paint_selectable_row(cx, row, selected, disabled))
|
||||
.when(!disabled, |row| {
|
||||
row.cursor_pointer().on_click({
|
||||
let on_change = on_change.clone();
|
||||
let focus = focus.clone();
|
||||
move |_, window, cx| {
|
||||
window.focus(&focus, cx);
|
||||
if let Some(on_change) = &on_change {
|
||||
on_change(Some(index), window, cx);
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
.child(render_disclosure(
|
||||
cx,
|
||||
item,
|
||||
index,
|
||||
disabled,
|
||||
selected,
|
||||
on_disclosure,
|
||||
))
|
||||
.when_some(item.icon.clone(), |row, icon| {
|
||||
row.child(icon.size(theme.sizes.medium.icon_size).color(label_color))
|
||||
})
|
||||
.child(
|
||||
div()
|
||||
.flex_1()
|
||||
.text_color(label_color)
|
||||
.child(item.label.clone()),
|
||||
)
|
||||
.into_any_element()
|
||||
}
|
||||
|
||||
/// The disclosure slot.
|
||||
fn render_disclosure(
|
||||
cx: &App,
|
||||
item: &TreeItem,
|
||||
index: usize,
|
||||
disabled: bool,
|
||||
selected: bool,
|
||||
on_disclosure: &Option<DisclosureHandler>,
|
||||
) -> AnyElement {
|
||||
let theme = cx.theme();
|
||||
let glyph_color = if disabled {
|
||||
theme.colors.muted_foreground
|
||||
} else if selected {
|
||||
theme.colors.primary_foreground
|
||||
} else {
|
||||
theme.colors.muted_foreground
|
||||
};
|
||||
|
||||
let cell = div()
|
||||
.flex_none()
|
||||
.size(theme.sizes.medium.icon_size)
|
||||
.flex()
|
||||
.items_center()
|
||||
.justify_center()
|
||||
.child(
|
||||
icon(if item.expanded {
|
||||
IconRole::ChevronDown
|
||||
} else {
|
||||
IconRole::ChevronRight
|
||||
})
|
||||
.size(theme.sizes.medium.dense_icon_size)
|
||||
.color(glyph_color),
|
||||
);
|
||||
|
||||
if item.expandable && !disabled {
|
||||
cell.id(("tree-disclosure", index))
|
||||
.cursor_pointer()
|
||||
.on_click({
|
||||
let on_disclosure = on_disclosure.clone();
|
||||
move |_, window, cx| {
|
||||
if let Some(on_disclosure) = &on_disclosure {
|
||||
on_disclosure(index, window, cx);
|
||||
}
|
||||
// The toggle is not a selection: keep it off the row.
|
||||
cx.stop_propagation();
|
||||
}
|
||||
.child(self.render_disclosure(cx, index, selected, on_disclosure))
|
||||
.when_some(self.icon.clone(), |row, icon| {
|
||||
row.child(icon.size(theme.sizes.medium.icon_size).color(label_color))
|
||||
})
|
||||
.child(
|
||||
div()
|
||||
.flex_1()
|
||||
.text_color(label_color)
|
||||
.child(self.label.clone()),
|
||||
)
|
||||
.into_any_element()
|
||||
} else {
|
||||
cell.into_any_element()
|
||||
}
|
||||
|
||||
fn render_disclosure(
|
||||
&self,
|
||||
cx: &App,
|
||||
index: usize,
|
||||
selected: bool,
|
||||
on_disclosure: &Option<DisclosureHandler>,
|
||||
) -> AnyElement {
|
||||
let theme = cx.theme();
|
||||
let glyph_color = if self.disabled || !selected {
|
||||
theme.colors.muted_foreground
|
||||
} else {
|
||||
theme.colors.primary_foreground
|
||||
};
|
||||
|
||||
let cell = div()
|
||||
.flex_none()
|
||||
.size(theme.sizes.medium.icon_size)
|
||||
.flex()
|
||||
.items_center()
|
||||
.justify_center()
|
||||
.child(
|
||||
icon(if self.expanded {
|
||||
IconRole::ChevronDown
|
||||
} else {
|
||||
IconRole::ChevronRight
|
||||
})
|
||||
.size(theme.sizes.medium.dense_icon_size)
|
||||
.color(glyph_color),
|
||||
);
|
||||
|
||||
if self.expandable && !self.disabled {
|
||||
cell.id((self.key.clone(), "tree-disclosure"))
|
||||
.cursor_pointer()
|
||||
.on_click({
|
||||
let on_disclosure = on_disclosure.clone();
|
||||
move |_, window, cx| {
|
||||
if let Some(on_disclosure) = &on_disclosure {
|
||||
on_disclosure(index, window, cx);
|
||||
}
|
||||
// The toggle is not a selection: keep it off the row.
|
||||
cx.stop_propagation();
|
||||
}
|
||||
})
|
||||
.into_any_element()
|
||||
} else {
|
||||
cell.into_any_element()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn item(depth: usize) -> TreeItem {
|
||||
TreeItem::new("item", "Item").depth(depth)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn flat_rows_number_one_of_three() {
|
||||
let sets = Tree::sibling_sets(&[item(0), item(0), item(0)]);
|
||||
assert_eq!(sets, vec![(1, 3), (2, 3), (3, 3)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nested_rows_number_per_level() {
|
||||
let sets = Tree::sibling_sets(&[item(0), item(1), item(2), item(1), item(0)]);
|
||||
assert_eq!(sets, vec![(1, 2), (1, 2), (1, 1), (2, 2), (2, 2)]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user