A project's files as a folder tree. Folders fold open and closed, and clicking a file selects it.
Loading GPUI…
use std::collections::{HashMap, HashSet};
use std::f32::consts::FRAC_PI_2;
use std::time::Duration;
use gpui::{
div, prelude::*, px, radians, Animation, AnimationExt, AnyElement, App, Context, ElementId,
Transformation, Window,
};
use crate::icons::{icon, CHEVRON_RIGHT, FILE, FOLDER};
use crate::theme::ActiveTheme;
/// Tailwind's `transition-transform`: 150ms on cubic-bezier(0.4, 0, 0.2, 1).
const TURN: Duration = Duration::from_millis(150);
enum Node {
File { name: &'static str, path: &'static str },
Folder { name: &'static str, path: &'static str, children: &'static [Node] },
}
const FILES: &[Node] = &[
Node::Folder {
name: "app",
path: "app",
children: &[
Node::File { name: "layout.tsx", path: "app/layout.tsx" },
Node::File { name: "page.tsx", path: "app/page.tsx" },
],
},
Node::Folder {
name: "components",
path: "components",
children: &[
Node::Folder {
name: "ui",
path: "components/ui",
children: &[Node::File { name: "button.tsx", path: "components/ui/button.tsx" }],
},
Node::File { name: "login-form.tsx", path: "components/login-form.tsx" },
],
},
];
pub struct FileTreePattern {
/// Every folder starts open.
closed: HashSet<&'static str>,
selected: &'static str,
/// How often each folder was toggled. A chevron turns only after a toggle, and the count
/// keys its animation so every toggle starts a fresh one.
toggles: HashMap<&'static str, usize>,
}
impl Default for FileTreePattern {
fn default() -> Self {
Self {
closed: HashSet::new(),
selected: "components/ui/button.tsx",
toggles: HashMap::new(),
}
}
}
impl Render for FileTreePattern {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
div()
.w(px(320.0))
.flex()
.flex_col()
.py_6()
.children(FILES.iter().map(|node| self.node(node, cx)))
}
}
impl FileTreePattern {
fn node(&self, node: &'static Node, cx: &mut Context<Self>) -> AnyElement {
match node {
Node::Folder { name, path, children } => {
let path = *path;
let open = !self.closed.contains(path);
let toggles = self.toggles.get(path).copied().unwrap_or(0);
let angle = |open: bool| if open { FRAC_PI_2 } else { 0.0 };
let chevron = icon(CHEVRON_RIGHT).text_color(cx.theme().foreground);
let chevron = if toggles == 0 {
chevron.with_transformation(Transformation::rotate(radians(angle(open)))).into_any_element()
} else {
let (from, to) = (angle(!open), angle(open));
chevron
.with_animation(
ElementId::Name(format!("{path}-turn-{toggles}").into()),
Animation::new(TURN).with_easing(cubic_bezier(0.4, 0.0, 0.2, 1.0)),
move |this, t| {
this.with_transformation(Transformation::rotate(radians(from + (to - from) * t)))
},
)
.into_any_element()
};
let trigger = row(ElementId::Name(path.into()), false, cx)
.on_click(cx.listener(move |this, _, _, cx| {
if !this.closed.remove(path) {
this.closed.insert(path);
}
*this.toggles.entry(path).or_default() += 1;
cx.notify();
}))
.child(chevron)
.child(icon(FOLDER).text_color(cx.theme().foreground))
.child(*name);
// The panel shows and hides at once; only the chevron animates.
div()
.flex()
.flex_col()
.child(trigger)
.when(open, |this| {
this.child(
div()
.ml_4()
.flex()
.flex_col()
.children(children.iter().map(|child| self.node(child, cx))),
)
})
.into_any_element()
}
Node::File { name, path } => {
let path = *path;
let selected = self.selected == path;
let theme = cx.theme();
let color = if selected { theme.accent_foreground } else { theme.foreground };
row(ElementId::Name(path.into()), selected, cx)
.on_click(cx.listener(move |this, _, _, cx| {
this.selected = path;
cx.notify();
}))
.child(icon(FILE).text_color(color))
.child(*name)
.into_any_element()
}
}
}
}
/// shadcn's ghost button at size sm, stretched and left-aligned: a 28px pill with 16px icons
/// 4px apart.
fn row(id: ElementId, selected: bool, cx: &App) -> gpui::Stateful<gpui::Div> {
let theme = cx.theme();
// shadcn's ghost hover: muted, at half strength in dark mode.
let hover = if theme.background.l > 0.5 { theme.muted } else { theme.muted.opacity(0.5) };
div()
.id(id)
.flex()
.flex_none()
.items_center()
.gap_1()
.w_full()
.h(px(28.0))
// 12px of padding inside the button's transparent 1px border.
.px(px(13.0))
.rounded_full()
.text_sm()
.line_height(px(20.0))
.text_color(if selected { theme.accent_foreground } else { theme.foreground })
.cursor_pointer()
.when(selected, |this| this.bg(theme.accent))
.hover(move |this| this.bg(hover))
}
/// A CSS `cubic-bezier()` timing function, solved for x with Newton's method.
fn cubic_bezier(x1: f32, y1: f32, x2: f32, y2: f32) -> impl Fn(f32) -> f32 {
move |t| {
let at = |a: f32, b: f32, s: f32| 3.0 * a * s * (1.0 - s).powi(2) + 3.0 * b * s * s * (1.0 - s) + s.powi(3);
let slope = |a: f32, b: f32, s: f32| 3.0 * a * (1.0 - s).powi(2) + 6.0 * (b - a) * s * (1.0 - s) + 3.0 * (1.0 - b) * s * s;
let mut s = t.clamp(0.0, 1.0);
for _ in 0..8 {
let dx = slope(x1, x2, s);
if dx.abs() < 1e-6 {
break;
}
s = (s - (at(x1, x2, s) - t) / dx).clamp(0.0, 1.0);
}
at(y1, y2, s)
}
}Installation
Add the components
$ cargo hyuskui add iconsCopy the pattern
Paste the code into
src/ai/file_tree.rsand declare the module. The demo plays scripted data on a timer; drive it from your agent's events instead.src/ai/file_tree.rs use std::collections::{HashMap, HashSet}; use std::f32::consts::FRAC_PI_2; use std::time::Duration; use gpui::{ div, prelude::*, px, radians, Animation, AnimationExt, AnyElement, App, Context, ElementId, Transformation, Window, }; use crate::icons::{icon, CHEVRON_RIGHT, FILE, FOLDER}; use crate::theme::ActiveTheme; /// Tailwind's `transition-transform`: 150ms on cubic-bezier(0.4, 0, 0.2, 1). const TURN: Duration = Duration::from_millis(150); enum Node { File { name: &'static str, path: &'static str }, Folder { name: &'static str, path: &'static str, children: &'static [Node] }, } const FILES: &[Node] = &[ Node::Folder { name: "app", path: "app", children: &[ Node::File { name: "layout.tsx", path: "app/layout.tsx" }, Node::File { name: "page.tsx", path: "app/page.tsx" }, ], }, Node::Folder { name: "components", path: "components", children: &[ Node::Folder { name: "ui", path: "components/ui", children: &[Node::File { name: "button.tsx", path: "components/ui/button.tsx" }], }, Node::File { name: "login-form.tsx", path: "components/login-form.tsx" }, ], }, ]; pub struct FileTreePattern { /// Every folder starts open. closed: HashSet<&'static str>, selected: &'static str, /// How often each folder was toggled. A chevron turns only after a toggle, and the count /// keys its animation so every toggle starts a fresh one. toggles: HashMap<&'static str, usize>, } impl Default for FileTreePattern { fn default() -> Self { Self { closed: HashSet::new(), selected: "components/ui/button.tsx", toggles: HashMap::new(), } } } impl Render for FileTreePattern { fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { div() .w(px(320.0)) .flex() .flex_col() .py_6() .children(FILES.iter().map(|node| self.node(node, cx))) } } impl FileTreePattern { fn node(&self, node: &'static Node, cx: &mut Context<Self>) -> AnyElement { match node { Node::Folder { name, path, children } => { let path = *path; let open = !self.closed.contains(path); let toggles = self.toggles.get(path).copied().unwrap_or(0); let angle = |open: bool| if open { FRAC_PI_2 } else { 0.0 }; let chevron = icon(CHEVRON_RIGHT).text_color(cx.theme().foreground); let chevron = if toggles == 0 { chevron.with_transformation(Transformation::rotate(radians(angle(open)))).into_any_element() } else { let (from, to) = (angle(!open), angle(open)); chevron .with_animation( ElementId::Name(format!("{path}-turn-{toggles}").into()), Animation::new(TURN).with_easing(cubic_bezier(0.4, 0.0, 0.2, 1.0)), move |this, t| { this.with_transformation(Transformation::rotate(radians(from + (to - from) * t))) }, ) .into_any_element() }; let trigger = row(ElementId::Name(path.into()), false, cx) .on_click(cx.listener(move |this, _, _, cx| { if !this.closed.remove(path) { this.closed.insert(path); } *this.toggles.entry(path).or_default() += 1; cx.notify(); })) .child(chevron) .child(icon(FOLDER).text_color(cx.theme().foreground)) .child(*name); // The panel shows and hides at once; only the chevron animates. div() .flex() .flex_col() .child(trigger) .when(open, |this| { this.child( div() .ml_4() .flex() .flex_col() .children(children.iter().map(|child| self.node(child, cx))), ) }) .into_any_element() } Node::File { name, path } => { let path = *path; let selected = self.selected == path; let theme = cx.theme(); let color = if selected { theme.accent_foreground } else { theme.foreground }; row(ElementId::Name(path.into()), selected, cx) .on_click(cx.listener(move |this, _, _, cx| { this.selected = path; cx.notify(); })) .child(icon(FILE).text_color(color)) .child(*name) .into_any_element() } } } } /// shadcn's ghost button at size sm, stretched and left-aligned: a 28px pill with 16px icons /// 4px apart. fn row(id: ElementId, selected: bool, cx: &App) -> gpui::Stateful<gpui::Div> { let theme = cx.theme(); // shadcn's ghost hover: muted, at half strength in dark mode. let hover = if theme.background.l > 0.5 { theme.muted } else { theme.muted.opacity(0.5) }; div() .id(id) .flex() .flex_none() .items_center() .gap_1() .w_full() .h(px(28.0)) // 12px of padding inside the button's transparent 1px border. .px(px(13.0)) .rounded_full() .text_sm() .line_height(px(20.0)) .text_color(if selected { theme.accent_foreground } else { theme.foreground }) .cursor_pointer() .when(selected, |this| this.bg(theme.accent)) .hover(move |this| this.bg(hover)) } /// A CSS `cubic-bezier()` timing function, solved for x with Newton's method. fn cubic_bezier(x1: f32, y1: f32, x2: f32, y2: f32) -> impl Fn(f32) -> f32 { move |t| { let at = |a: f32, b: f32, s: f32| 3.0 * a * s * (1.0 - s).powi(2) + 3.0 * b * s * s * (1.0 - s) + s.powi(3); let slope = |a: f32, b: f32, s: f32| 3.0 * a * (1.0 - s).powi(2) + 6.0 * (b - a) * s * (1.0 - s) + 3.0 * (1.0 - b) * s * s; let mut s = t.clamp(0.0, 1.0); for _ in 0..8 { let dx = slope(x1, x2, s); if dx.abs() < 1e-6 { break; } s = (s - (at(x1, x2, s) - t) / dx).clamp(0.0, 1.0); } at(y1, y2, s) } }