use std::hash::Hash;
use std::rc::Rc;
use gpui::{
AnyElement, ClickEvent, Context, ElementId, FontWeight, KeyDownEvent, MouseButton,
MouseDownEvent, PathBuilder, SharedString, Window, canvas, div, point, prelude::*, px,
};
use crate::controls::WidgetContext;
use crate::keyboard::{self, Key};
use crate::lighting;
use crate::palette::Palette;
use crate::state::{ControlHost, ControlState, MOVE, SWITCH_SLIDE, Tag};
#[derive(Clone, Debug)]
pub struct TreeRow {
pub id: SharedString,
pub label: SharedString,
pub depth: usize,
pub expanded: Option<bool>,
pub detail: Option<SharedString>,
}
impl TreeRow {
pub fn leaf(id: impl Into<SharedString>, label: impl Into<SharedString>, depth: usize) -> Self {
Self {
id: id.into(),
label: label.into(),
depth,
expanded: None,
detail: None,
}
}
pub fn branch(
id: impl Into<SharedString>,
label: impl Into<SharedString>,
depth: usize,
expanded: bool,
) -> Self {
Self {
id: id.into(),
label: label.into(),
depth,
expanded: Some(expanded),
detail: None,
}
}
pub fn detail(mut self, detail: impl Into<SharedString>) -> Self {
self.detail = Some(detail.into());
self
}
}
pub fn flatten_tree<N>(
roots: &[N],
row: &impl Fn(&N, usize) -> TreeRow,
children: &impl Fn(&N) -> &[N],
) -> Vec<TreeRow> {
fn walk<N>(
nodes: &[N],
depth: usize,
row: &impl Fn(&N, usize) -> TreeRow,
children: &impl Fn(&N) -> &[N],
rows: &mut Vec<TreeRow>,
) {
for node in nodes {
let flat = row(node, depth);
let open = flat.expanded == Some(true);
rows.push(flat);
if open {
walk(children(node), depth + 1, row, children, rows);
}
}
}
let mut rows = Vec::new();
walk(roots, 0, row, children, &mut rows);
rows
}
pub const TREE_INDENT: f32 = 14.0;
pub const TREE_ROW_HEIGHT: f32 = 24.0;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum TreeMove {
To(usize),
Expand(bool),
}
pub fn tree_step(rows: &[TreeRow], index: usize, key: Key) -> Option<TreeMove> {
let row = rows.get(index)?;
let last = rows.len().checked_sub(1)?;
match key {
Key::Up => (index > 0).then(|| TreeMove::To(index - 1)),
Key::Down => (index < last).then(|| TreeMove::To(index + 1)),
Key::Home => (index != 0).then_some(TreeMove::To(0)),
Key::End => (index != last).then_some(TreeMove::To(last)),
Key::Right => match row.expanded {
Some(false) => Some(TreeMove::Expand(true)),
Some(true) => rows
.get(index + 1)
.filter(|next| next.depth > row.depth)
.map(|_| TreeMove::To(index + 1)),
None => None,
},
Key::Left => match row.expanded {
Some(true) => Some(TreeMove::Expand(false)),
_ => rows[..index]
.iter()
.rposition(|candidate| candidate.depth < row.depth)
.map(TreeMove::To),
},
_ => None,
}
}
#[allow(clippy::too_many_arguments)]
pub fn tree_row<V: ControlHost>(
id: &'static str,
rows: &[TreeRow],
index: usize,
selected: bool,
ctx: WidgetContext<'_, '_, '_, V>,
on_select: impl Fn(&mut V, SharedString, &mut Window, &mut Context<V>) + 'static,
on_toggle: impl Fn(&mut V, SharedString, bool, &mut Window, &mut Context<V>) + 'static,
) -> impl IntoElement {
let WidgetContext { palette, view, cx } = ctx;
let row = &rows[index];
let state = view.control_state();
let focus = state.focus(id, cx);
let click_focus = focus.clone();
let chevron: gpui::Hsla = crate::color::to_hsla(palette.text_secondary);
let row_id = row.id.clone();
let toggle_id = row.id.clone();
let key_id = row.id.clone();
let expanded = row.expanded;
let key = |what: &'static str| Tag::new((id, what, &row.id));
let on = state.blend(key("selected"), selected, SWITCH_SLIDE);
let turned = expanded.map(|open| state.blend(key("open"), open, SWITCH_SLIDE));
let shown = if state.present(id) {
state.tween_from(key("shown"), 0.0, 1.0, SWITCH_SLIDE)
} else {
state.tween(key("shown"), 1.0, SWITCH_SLIDE)
};
let on_select = Rc::new(on_select);
let key_select = on_select.clone();
let on_toggle = Rc::new(on_toggle);
let key_toggle = on_toggle.clone();
let dbl_toggle = on_toggle.clone();
let moves: Vec<(Key, TreeMove)> = [
Key::Up,
Key::Down,
Key::Left,
Key::Right,
Key::Home,
Key::End,
]
.into_iter()
.filter_map(|key| tree_step(rows, index, key).map(|moved| (key, moved)))
.collect();
let targets: Vec<(Key, TreeMove, Option<SharedString>)> = moves
.into_iter()
.map(|(key, moved)| {
let id = match moved {
TreeMove::To(at) => rows.get(at).map(|row| row.id.clone()),
TreeMove::Expand(_) => None,
};
(key, moved, id)
})
.collect();
let navigate = cx.listener(move |this, event: &KeyDownEvent, window, cx| {
let Some(key) = keyboard::key(event) else {
return;
};
if key == Key::Activate {
cx.stop_propagation();
key_select(this, key_id.clone(), window, cx);
cx.notify();
return;
}
let Some((_, moved, target)) = targets.iter().find(|(candidate, _, _)| *candidate == key)
else {
return;
};
cx.stop_propagation();
match moved {
TreeMove::To(_) => {
if let Some(target) = target.clone() {
key_select(this, target, window, cx);
}
}
TreeMove::Expand(open) => {
key_toggle(this, key_id.clone(), *open, window, cx);
}
}
cx.notify();
});
div()
.id(ElementId::NamedInteger(
format!("{id}-row").into(),
index as u64,
))
.h(px(TREE_ROW_HEIGHT))
.w_full()
.flex_none()
.pl(px(4.0 + row.depth as f32 * TREE_INDENT))
.pr(px(8.0))
.flex()
.items_center()
.gap(px(4.0))
.rounded(px(4.0))
.cursor_pointer()
.opacity(shown)
.text_size(px(12.5))
.text_color(crate::color::lerp(
palette.text_primary,
palette.control_label,
on,
))
.when(on > 0.01, |el| {
el.bg(lighting::lit_at(palette.control_fill, 0.08, on))
})
.when(!selected, |el| {
el.hover(move |style| style.bg(palette.row_hover))
})
.on_click(cx.listener(move |this, event: &ClickEvent, window, cx| {
window.focus(&click_focus, cx);
if event.click_count() == 2
&& let Some(open) = expanded
{
dbl_toggle(this, row_id.clone(), !open, window, cx);
cx.notify();
return;
}
on_select(this, row_id.clone(), window, cx);
cx.notify();
}))
.when(selected, move |el| {
el.relative()
.child(keyboard::ring_for(&focus, 4.0, palette).on_key_down(navigate))
})
.child(
div()
.id(ElementId::NamedInteger(
format!("{id}-chevron").into(),
index as u64,
))
.w(px(12.0))
.h_full()
.flex_none()
.flex()
.items_center()
.justify_center()
.when_some(expanded.zip(turned), |el, (open, turned)| {
el.on_mouse_down(
MouseButton::Left,
cx.listener(move |this, _event: &MouseDownEvent, window, cx| {
on_toggle(this, toggle_id.clone(), !open, window, cx);
cx.notify();
}),
)
.child(
div().w(px(12.0)).h(px(12.0)).flex_none().child(
canvas(
|_bounds, _window, _cx| {},
move |bounds, _state, window, _cx| {
let origin = bounds.origin;
let angle = turned * std::f32::consts::FRAC_PI_2;
let (sin, cos) = angle.sin_cos();
let at = |x: f32, y: f32| {
point(
origin.x + px(6.0 + x * cos - y * sin),
origin.y + px(6.0 + x * sin + y * cos),
)
};
let mut builder = PathBuilder::stroke(px(1.4));
builder.move_to(at(-1.5, -4.0));
builder.line_to(at(2.5, 0.0));
builder.line_to(at(-1.5, 4.0));
if let Ok(path) = builder.build() {
window.paint_path(path, chevron);
}
},
)
.size_full(),
),
)
}),
)
.child(
div()
.flex_1()
.overflow_hidden()
.whitespace_nowrap()
.child(row.label.clone()),
)
.children(row.detail.clone().map(|detail| {
div()
.flex_none()
.text_size(px(11.5))
.text_color(palette.text_secondary)
.child(detail)
}))
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum SortDirection {
Ascending,
Descending,
}
impl SortDirection {
pub fn flipped(self) -> Self {
match self {
SortDirection::Ascending => SortDirection::Descending,
SortDirection::Descending => SortDirection::Ascending,
}
}
}
#[derive(Clone, Debug)]
pub struct Column {
pub id: SharedString,
pub label: SharedString,
pub width: Option<f32>,
pub sortable: bool,
pub numeric: bool,
}
impl Column {
pub fn new(id: impl Into<SharedString>, label: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
label: label.into(),
width: None,
sortable: true,
numeric: false,
}
}
pub fn width(mut self, width: f32) -> Self {
self.width = Some(width);
self
}
pub fn fixed(mut self) -> Self {
self.sortable = false;
self
}
pub fn numeric(mut self) -> Self {
self.numeric = true;
self
}
}
pub const TABLE_ROW_HEIGHT: f32 = 26.0;
const TABLE_CELL_PADDING: f32 = 8.0;
#[derive(Clone, Copy, PartialEq, Debug)]
struct CellLayout {
width: Option<f32>,
align_end: bool,
}
impl CellLayout {
fn of(column: &Column) -> Self {
Self {
width: column.width,
align_end: column.numeric,
}
}
}
fn lay_out_cell<E: Styled + gpui::prelude::FluentBuilder>(el: E, layout: CellLayout) -> E {
el.h_full()
.when_some(layout.width, |el, width| el.w(px(width)).flex_none())
.when(layout.width.is_none(), |el| el.flex_1())
.px(px(TABLE_CELL_PADDING))
.flex()
.items_center()
.when(layout.align_end, |el| el.justify_end())
.whitespace_nowrap()
.overflow_hidden()
}
pub fn table_cell(column: &Column, content: impl IntoElement) -> impl IntoElement {
lay_out_cell(div(), CellLayout::of(column)).child(content)
}
pub fn table_row(
table: &'static str,
key: impl Hash,
slot: usize,
columns: &[Column],
values: impl IntoIterator<Item = impl Into<SharedString>>,
palette: Palette,
state: &ControlState,
) -> gpui::Div {
let place = slot as f32 * TABLE_ROW_HEIGHT;
let offset = state.tween((table, "row", key), place, MOVE) - place;
div()
.relative()
.top(px(offset))
.h(px(TABLE_ROW_HEIGHT))
.flex()
.flex_none()
.items_center()
.children(
columns
.iter()
.zip(values)
.enumerate()
.map(|(index, (column, value))| {
table_cell(
column,
div()
.text_color(if index == 0 {
palette.text_primary
} else {
palette.text_secondary
})
.child(value.into()),
)
}),
)
}
#[allow(clippy::too_many_arguments)]
pub fn table_header<V: ControlHost>(
id: &'static str,
columns: &[Column],
sorted_by: Option<(&str, SortDirection)>,
palette: Palette,
cx: &mut Context<V>,
on_sort: impl Fn(&mut V, SharedString, SortDirection, &mut Window, &mut Context<V>) + 'static,
) -> impl IntoElement {
let on_sort = Rc::new(on_sort);
let mut cells: Vec<AnyElement> = Vec::with_capacity(columns.len());
for (index, column) in columns.iter().enumerate() {
let cx: &mut Context<V> = &mut *cx;
let on_sort = on_sort.clone();
let column_id = column.id.clone();
let active = sorted_by.filter(|(sorted, _)| *sorted == column.id.as_ref());
let sort_ring = ElementId::NamedInteger(format!("{id}-column-ring").into(), index as u64);
let next = match active {
Some((_, direction)) => direction.flipped(),
None => SortDirection::Ascending,
};
let arrow_up = matches!(active, Some((_, SortDirection::Ascending)));
let arrow: gpui::Hsla = crate::color::to_hsla(palette.text_primary);
cells.push(
lay_out_cell(
div().id(ElementId::NamedInteger(
format!("{id}-column").into(),
index as u64,
)),
CellLayout::of(column),
)
.gap(px(4.0))
.text_size(px(11.5))
.font_weight(FontWeight::MEDIUM)
.text_color(if active.is_some() {
palette.text_primary
} else {
palette.text_secondary
})
.when(column.sortable, move |el| {
let key_id = column_id.clone();
let key_sort = on_sort.clone();
el.relative()
.cursor_pointer()
.hover(move |style| style.bg(palette.row_hover))
.on_click(cx.listener(move |this, _event, window, cx| {
on_sort(this, column_id.clone(), next, window, cx);
cx.notify();
}))
.child(
keyboard::ring(sort_ring, 4.0, palette).on_key_down(cx.listener(
move |this, event: &KeyDownEvent, window, cx| {
if keyboard::key(event) == Some(Key::Activate) {
cx.stop_propagation();
key_sort(this, key_id.clone(), next, window, cx);
cx.notify();
}
},
)),
)
})
.child(column.label.clone())
.when(active.is_some(), move |el| {
el.child(
div().w(px(9.0)).h(px(9.0)).flex_none().child(
canvas(
|_bounds, _window, _cx| {},
move |bounds, _state, window, _cx| {
let origin = bounds.origin;
let mut builder = PathBuilder::stroke(px(1.3));
if arrow_up {
builder.move_to(point(origin.x + px(0.5), origin.y + px(6.5)));
builder.line_to(point(origin.x + px(4.5), origin.y + px(2.5)));
builder.line_to(point(origin.x + px(8.5), origin.y + px(6.5)));
} else {
builder.move_to(point(origin.x + px(0.5), origin.y + px(2.5)));
builder.line_to(point(origin.x + px(4.5), origin.y + px(6.5)));
builder.line_to(point(origin.x + px(8.5), origin.y + px(2.5)));
}
if let Ok(path) = builder.build() {
window.paint_path(path, arrow);
}
},
)
.size_full(),
),
)
})
.into_any_element(),
);
}
let mut rule: gpui::Hsla = crate::color::to_hsla(palette.field_border);
rule.alpha = 0.9;
div()
.id(id)
.h(px(TABLE_ROW_HEIGHT))
.w_full()
.flex_none()
.flex()
.items_stretch()
.border_b_1()
.border_color(rule)
.children(cells)
}
#[cfg(test)]
mod tests {
use super::{CellLayout, Column, Key, SortDirection, TreeMove, TreeRow, tree_step};
fn tree() -> Vec<TreeRow> {
vec![
TreeRow::branch("documents", "Documents", 0, true),
TreeRow::leaf("report", "report.pdf", 1),
TreeRow::branch("images", "Images", 1, false),
TreeRow::leaf("readme", "readme.md", 1),
]
}
#[test]
fn a_tree_does_not_wrap() {
assert_eq!(tree_step(&tree(), 0, Key::Up), None);
assert_eq!(tree_step(&tree(), 3, Key::Down), None);
}
#[test]
fn up_and_down_walk_the_flattened_rows() {
assert_eq!(tree_step(&tree(), 1, Key::Down), Some(TreeMove::To(2)));
assert_eq!(tree_step(&tree(), 2, Key::Up), Some(TreeMove::To(1)));
}
#[test]
fn right_opens_a_closed_branch_then_walks_into_it() {
assert_eq!(
tree_step(&tree(), 2, Key::Right),
Some(TreeMove::Expand(true))
);
assert_eq!(tree_step(&tree(), 0, Key::Right), Some(TreeMove::To(1)));
}
#[test]
fn left_closes_an_open_branch_then_walks_out_to_the_parent() {
assert_eq!(
tree_step(&tree(), 0, Key::Left),
Some(TreeMove::Expand(false))
);
assert_eq!(tree_step(&tree(), 1, Key::Left), Some(TreeMove::To(0)));
}
#[test]
fn right_does_nothing_on_a_leaf() {
assert_eq!(tree_step(&tree(), 1, Key::Right), None);
}
#[test]
fn right_on_an_empty_open_branch_stays_put() {
let rows = vec![
TreeRow::branch("empty", "Empty", 0, true),
TreeRow::leaf("sibling", "Sibling", 0),
];
assert_eq!(tree_step(&rows, 0, Key::Right), None);
}
#[test]
fn home_and_end_reach_the_ends() {
assert_eq!(tree_step(&tree(), 2, Key::Home), Some(TreeMove::To(0)));
assert_eq!(tree_step(&tree(), 0, Key::End), Some(TreeMove::To(3)));
assert_eq!(tree_step(&tree(), 0, Key::Home), None);
}
#[test]
fn an_index_off_the_end_moves_nothing() {
assert_eq!(tree_step(&tree(), 99, Key::Down), None);
assert_eq!(tree_step(&[], 0, Key::Down), None);
}
#[test]
fn one_column_gives_one_layout() {
let column = Column::new("size", "Size").width(80.0).numeric();
assert_eq!(CellLayout::of(&column), CellLayout::of(&column));
}
#[test]
fn a_column_without_a_width_shares_the_leftover_space() {
let flexible = Column::new("name", "Name");
assert_eq!(CellLayout::of(&flexible).width, None);
let fixed = Column::new("changed", "Changed").width(110.0);
assert_eq!(CellLayout::of(&fixed).width, Some(110.0));
}
#[test]
fn numeric_columns_align_to_the_right() {
assert!(CellLayout::of(&Column::new("size", "Size").numeric()).align_end);
assert!(!CellLayout::of(&Column::new("name", "Name")).align_end);
}
#[test]
fn sortability_does_not_change_the_grid() {
let sortable = Column::new("size", "Size").width(80.0).numeric();
let fixed = Column::new("size", "Size").width(80.0).numeric().fixed();
assert_eq!(CellLayout::of(&sortable), CellLayout::of(&fixed));
}
#[test]
fn a_sort_direction_flips_both_ways() {
assert_eq!(
SortDirection::Ascending.flipped(),
SortDirection::Descending
);
assert_eq!(
SortDirection::Descending.flipped(),
SortDirection::Ascending
);
}
}