Skip to main content

ui/
tree.rs

1//! Tree view — nested rows with disclosure, indent guides and arrow keys.
2//!
3//! bezel cannot walk your tree. It has no idea what a node is, and a trait or a
4//! callback to find out would be a data model this library does not want to
5//! own — so the app flattens its own tree into the rows that are visible right
6//! now, which it has to do anyway to render them.
7//!
8//! That is not a compromise, because **a depth-annotated flat list is a
9//! complete navigation model**. Everything a tree does falls out of [`Row`]
10//! with no parent pointers and no tree walk: down and up are neighbouring
11//! indices, a first child is simply the next row, and a parent is the nearest
12//! row above with a smaller depth. [`step`] is that, and nothing else.
13//!
14//! ```ignore
15//! ui::tree::init(cx);   // once, at startup
16//!
17//! // Each frame: flatten what is open, paint it, and let `step` answer the keys.
18//! let rows = self.flatten();                       // Vec<(Row, label)>
19//! tree().children(rows.iter().enumerate().map(|(index, (row, label))| {
20//!     tree_row(&theme, row, self.selected == Some(index), self.cursor == index)
21//!         .id(("row", index))
22//!         .child(label.clone())
23//! }))
24//! ```
25//!
26//! Expansion stays with the app because it *is* app data — a file tree's open
27//! folders often outlive the window — so [`step`] reports an intent, and the app
28//! applies it to the set it owns.
29
30use gpui::{App, KeyBinding, actions, div, prelude::*, px};
31
32use theme::{TextStyle, Theme, Typeset};
33
34use crate::widgets::Layout;
35
36/// One visible row: how deep it sits, and whether it is a branch.
37///
38/// `expanded` is `None` for a leaf — which is a different thing from a closed
39/// branch, and the difference is what stops `right` pretending a file can open.
40#[derive(Clone, Copy, Debug, PartialEq, Eq)]
41pub struct Row {
42    pub depth: usize,
43    pub expanded: Option<bool>,
44}
45
46impl Row {
47    pub fn leaf(depth: usize) -> Self {
48        Self {
49            depth,
50            expanded: None,
51        }
52    }
53
54    pub fn branch(depth: usize, expanded: bool) -> Self {
55        Self {
56            depth,
57            expanded: Some(expanded),
58        }
59    }
60}
61
62#[derive(Clone, Copy, Debug, PartialEq, Eq)]
63pub enum Direction {
64    Up,
65    Down,
66    Left,
67    Right,
68}
69
70/// What a keypress meant. An intent rather than a mutation: only the app can
71/// expand a row, because only the app knows what is under it.
72#[derive(Clone, Copy, Debug, PartialEq, Eq)]
73pub enum Move {
74    To(usize),
75    Expand(usize),
76    Collapse(usize),
77}
78
79/// The row `index` hangs under: the nearest row above it with a smaller depth.
80///
81/// Nearest, not previous — the row directly above is usually a sibling, and
82/// often the last leaf of a sibling's whole subtree. Walking up until the depth
83/// actually drops is what skips all of that.
84pub fn parent_of(rows: &[Row], index: usize) -> Option<usize> {
85    let depth = rows.get(index)?.depth;
86    rows[..index]
87        .iter()
88        .rposition(|candidate| candidate.depth < depth)
89}
90
91/// What an arrow key means at `cursor`, or `None` when it means nothing.
92///
93/// Neither end wraps. A menu wraps because it is a ring of choices; a tree is a
94/// document, and arriving back at the top because you pressed down once too
95/// often loses your place in it.
96pub fn step(rows: &[Row], cursor: usize, direction: Direction) -> Option<Move> {
97    let row = rows.get(cursor)?;
98    match direction {
99        Direction::Up => cursor.checked_sub(1).map(Move::To),
100        Direction::Down => (cursor + 1 < rows.len()).then_some(Move::To(cursor + 1)),
101        // A closed branch opens; an open one steps into it — and its first child
102        // is just the next row, because the list is already in visible order.
103        Direction::Right => match row.expanded {
104            Some(false) => Some(Move::Expand(cursor)),
105            Some(true) => (cursor + 1 < rows.len()).then_some(Move::To(cursor + 1)),
106            None => None,
107        },
108        // The mirror: an open branch closes, everything else goes up a level.
109        Direction::Left => match row.expanded {
110            Some(true) => Some(Move::Collapse(cursor)),
111            _ => parent_of(rows, cursor).map(Move::To),
112        },
113    }
114}
115
116actions!(bezel_tree, [SelectPrevious, SelectNext, Collapse, Expand]);
117
118/// The key context a tree claims. [`tree`] does not set it: the caller puts it,
119/// with focus tracking and a handler per action, on the element holding the
120/// rows.
121pub const KEY_CONTEXT: &str = "Tree";
122
123/// Install the bindings — [`bindings`], bound. Call once, alongside
124/// [`crate::input::init`].
125pub fn init(cx: &mut App) {
126    cx.bind_keys(bindings());
127}
128
129/// The tree's four arrows, as data, so an app can have it without having to
130/// take it — see [`crate::keys`] for layering over it or taking a chord
131/// away.
132///
133/// The handlers are the app's — like [`crate::focus`]
134/// and unlike the menubar, a tree cannot handle them itself, because applying a
135/// [`Move`] means touching the app's own expansion set. What bezel does here is
136/// name the four chords everyone already agrees on, once.
137pub fn bindings() -> Vec<KeyBinding> {
138    let mut bindings = Vec::new();
139    let ctx = Some(KEY_CONTEXT);
140    bindings.extend([
141        KeyBinding::new("up", SelectPrevious, ctx),
142        KeyBinding::new("down", SelectNext, ctx),
143        KeyBinding::new("left", Collapse, ctx),
144        KeyBinding::new("right", Expand, ctx),
145    ]);
146
147    bindings
148}
149
150/// How far a row sits in from either side of the tree.
151const ROW_INSET: f32 = 4.0;
152
153/// How far one level of nesting indents.
154pub const INDENT: f32 = 14.0;
155/// Width of the chevron column, kept by leaves as well so their labels line up
156/// with their siblings' rather than sliding under them.
157const CHEVRON: f32 = 16.0;
158
159/// The container. Rows go in it; scrolling is the caller's, via
160/// [`crate::scroll`].
161pub fn tree() -> gpui::Div {
162    div().flex().flex_col().w_full()
163}
164
165/// One row: its guides, its chevron, and then whatever the caller puts in it.
166///
167/// `selected` is what the app considers chosen; `cursor` is where the keyboard
168/// is. Two tones, because a tree shows both at once.
169pub fn tree_row(theme: &Theme, row: &Row, selected: bool, cursor: bool) -> gpui::Div {
170    let mut frame = div()
171        .flex()
172        .flex_row()
173        .items_center()
174        .mx(px(ROW_INSET))
175        .rounded(px(Theme::control_radius()))
176        .py(px(3.0))
177        .pr(px(8.0))
178        .text_style(TextStyle::Callout)
179        .cursor_pointer();
180    frame = if selected {
181        frame
182            .bg(theme.card_selected_bg())
183            .shadow(theme::glass_selected_shadows())
184            .text_color(theme.text)
185    } else if cursor {
186        frame.bg(theme::wash(0.05)).text_color(theme.text)
187    } else {
188        frame.text_color(theme.text_muted)
189    };
190    frame
191        // One segment per ancestor level, drawn by the row it passes through:
192        // the line is continuous down the page without any element having to
193        // span rows or know its neighbours.
194        .children((0..row.depth).map(|_| {
195            div()
196                .flex_none()
197                .w(px(INDENT))
198                .h(px(18.0))
199                .border_l_1()
200                .border_color(theme.border)
201        }))
202        .child(
203            div()
204                .flex_none()
205                .w(px(CHEVRON))
206                .flex()
207                .items_center()
208                .justify_center()
209                .when_some(row.expanded, |slot, expanded| {
210                    slot.child(theme.disclosure(expanded))
211                }),
212        )
213}