use super::parts::{child_id, icon_button};
use crate::{
components::{Spinner, SpinnerSize},
icons::CarbonIcon,
motion::{self, Motion},
t3::{
base::Spin,
craft::{mono, sans},
press::{self, Handler, rem},
review::FileStatus,
},
theme::color,
tokens::ColorRole,
};
use gpui_kit::{
AnyElement, App, AppContext as _, CursorStyle, ElementId, Entity, Hsla,
InteractiveElement as _, IntoElement, ParentElement, RenderOnce, SharedString,
StatefulInteractiveElement as _, Styled, Window,
component::{
Sizable as _,
input::{Input, InputState},
},
div,
prelude::FluentBuilder as _,
relative,
};
use std::{collections::HashSet, rc::Rc};
pub fn file_glyph(name: &str, dir: bool, open: bool) -> CarbonIcon {
if dir {
return if open {
CarbonIcon::FolderOpen
} else {
CarbonIcon::Folder
};
}
match name.rsplit('.').next().unwrap_or(name) {
"ts" | "tsx" | "js" | "json" => CarbonIcon::Code,
"md" => CarbonIcon::Document,
"png" | "jpg" => CarbonIcon::Image,
"pdf" => CarbonIcon::DocumentPdf,
"css" => CarbonIcon::ColorPalette,
_ => CarbonIcon::Document,
}
}
pub fn file_glyph_color(dir: bool) -> Hsla {
color(if dir {
ColorRole::TagColorBlue
} else {
ColorRole::IconSecondary
})
}
#[derive(IntoElement, Clone, Debug)]
pub struct FileIcon {
name: SharedString,
dir: bool,
open: bool,
}
impl FileIcon {
pub fn new(name: impl Into<SharedString>) -> Self {
Self {
name: name.into(),
dir: false,
open: false,
}
}
pub fn dir(mut self, dir: bool) -> Self {
self.dir = dir;
self
}
pub fn open(mut self, open: bool) -> Self {
self.open = open;
self
}
}
impl RenderOnce for FileIcon {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
press::icon(
file_glyph(&self.name, self.dir, self.open),
16.,
file_glyph_color(self.dir),
)
.flex_none()
}
}
pub fn path_segments(path: &str) -> Vec<&str> {
path.split('/').filter(|s| !s.is_empty()).collect()
}
#[derive(IntoElement)]
pub struct FileBreadcrumbs {
id: ElementId,
path: SharedString,
label: SharedString,
on_select: Option<super::parts::IndexHandler>,
}
impl FileBreadcrumbs {
pub fn new(id: impl Into<ElementId>, path: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
path: path.into(),
label: "Path".into(),
on_select: None,
}
}
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = label.into();
self
}
pub fn on_select(mut self, f: impl Fn(usize, &mut Window, &mut App) + 'static) -> Self {
self.on_select = Some(Rc::new(f));
self
}
}
impl RenderOnce for FileBreadcrumbs {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let segments = path_segments(&self.path);
let last = segments.len().saturating_sub(1);
let mut parts: Vec<AnyElement> = Vec::new();
for (i, seg) in segments.iter().enumerate() {
if i > 0 {
parts.push(
press::icon(
CarbonIcon::ChevronRight,
12.,
color(ColorRole::IconSecondary),
)
.flex_none()
.into_any_element(),
);
}
let current = i == last;
let seg_id = child_id(&id, "crumb", i);
let (hovered, store) = press::hover((seg_id.clone(), "hover"), window, cx);
let (focus, ring) = press::focus((seg_id.clone(), "focus"), window, cx);
let ink = color(if current || hovered {
ColorRole::TextPrimary
} else {
ColorRole::TextSecondary
});
let on_select = self.on_select.clone();
parts.push(
sans(
gpui_kit::base::Button::new(seg_id)
.track_focus(&focus)
.accessibility_label(SharedString::from(seg.to_string())),
12.,
if current { 600 } else { 400 },
)
.relative()
.flex()
.flex_row()
.items_center()
.gap(rem(4.))
.h(rem(24.))
.px(rem(4.))
.text_color(ink)
.when(hovered, |this| this.bg(color(ColorRole::BackgroundHover)))
.cursor(CursorStyle::PointingHand)
.on_hover(press::track_hover(store))
.when_some(on_select, |this, f| {
this.on_click(move |_, w, cx| f(i, w, cx))
})
.when(!current, |this| {
this.child(press::icon(CarbonIcon::Folder, 14., ink).flex_none())
})
.child(SharedString::from(seg.to_string()))
.when(ring, |this| this.child(press::ring()))
.into_any_element(),
);
}
div()
.id(id)
.flex()
.flex_row()
.flex_wrap()
.items_center()
.gap(rem(2.))
.children(parts)
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct FileNode {
pub path: SharedString,
pub name: SharedString,
pub dir: bool,
pub open: bool,
pub status: Option<FileStatus>,
pub children: Vec<FileNode>,
}
impl FileNode {
pub fn file(path: impl Into<SharedString>, name: impl Into<SharedString>) -> Self {
Self {
path: path.into(),
name: name.into(),
..Default::default()
}
}
pub fn dir(
path: impl Into<SharedString>,
name: impl Into<SharedString>,
children: impl IntoIterator<Item = FileNode>,
) -> Self {
Self {
path: path.into(),
name: name.into(),
dir: true,
children: children.into_iter().collect(),
..Default::default()
}
}
pub fn open(mut self, open: bool) -> Self {
self.open = open;
self
}
pub fn status(mut self, status: FileStatus) -> Self {
self.status = Some(status);
self
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct FileRow<'a> {
pub node: &'a FileNode,
pub depth: usize,
}
pub fn initially_open(tree: &[FileNode]) -> HashSet<SharedString> {
fn walk(nodes: &[FileNode], set: &mut HashSet<SharedString>) {
for n in nodes {
if n.open {
set.insert(n.path.clone());
}
walk(&n.children, set);
}
}
let mut set = HashSet::new();
walk(tree, &mut set);
set
}
pub fn visible_rows<'a>(
tree: &'a [FileNode],
open: &HashSet<SharedString>,
query: &str,
) -> Vec<FileRow<'a>> {
let q = query.to_lowercase();
let mut rows = Vec::new();
fn walk<'a>(
nodes: &'a [FileNode],
depth: usize,
open: &HashSet<SharedString>,
q: &str,
rows: &mut Vec<FileRow<'a>>,
) {
for n in nodes {
if !q.is_empty() && !n.dir && !n.path.to_lowercase().contains(q) {
continue;
}
rows.push(FileRow { node: n, depth });
if n.dir && (open.contains(&n.path) || !q.is_empty()) {
walk(&n.children, depth + 1, open, q, rows);
}
}
}
walk(tree, 0, open, &q, &mut rows);
rows
}
pub fn row_indent(depth: usize) -> f32 {
8. + depth as f32 * 16.
}
#[derive(Clone, Debug)]
pub struct FileBrowserTexts {
pub search: SharedString,
pub refresh: SharedString,
pub loading: SharedString,
pub empty: SharedString,
}
impl Default for FileBrowserTexts {
fn default() -> Self {
Self {
search: "Search files".into(),
refresh: "Refresh files".into(),
loading: "Loading files\u{2026}".into(),
empty: "No matching files.".into(),
}
}
}
struct BrowserState {
query: Entity<InputState>,
open: HashSet<SharedString>,
}
#[derive(IntoElement)]
pub struct FileBrowserPanel {
id: ElementId,
tree: Vec<FileNode>,
selected: Option<SharedString>,
loading: bool,
query: SharedString,
on_select: Option<super::parts::TextHandler>,
on_refresh: Option<Handler>,
texts: FileBrowserTexts,
}
impl FileBrowserPanel {
pub fn new(id: impl Into<ElementId>, tree: impl IntoIterator<Item = FileNode>) -> Self {
Self {
id: id.into(),
tree: tree.into_iter().collect(),
selected: None,
loading: false,
query: SharedString::default(),
on_select: None,
on_refresh: None,
texts: FileBrowserTexts::default(),
}
}
pub fn selected(mut self, path: impl Into<SharedString>) -> Self {
self.selected = Some(path.into());
self
}
pub fn loading(mut self, loading: bool) -> Self {
self.loading = loading;
self
}
pub fn query(mut self, query: impl Into<SharedString>) -> Self {
self.query = query.into();
self
}
pub fn on_select(mut self, f: impl Fn(SharedString, &mut Window, &mut App) + 'static) -> Self {
self.on_select = Some(Rc::new(f));
self
}
pub fn on_refresh(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_refresh = Some(Rc::new(f));
self
}
pub fn texts(mut self, texts: FileBrowserTexts) -> Self {
self.texts = texts;
self
}
}
impl RenderOnce for FileBrowserPanel {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let state = window.use_keyed_state((id.clone(), "state"), cx, {
let query = self.query.clone();
let open = initially_open(&self.tree);
move |window, cx| BrowserState {
query: cx.new(|cx| InputState::new(window, cx).default_value(query)),
open,
}
});
let input = state.read(cx).query.clone();
let query = input.read(cx).value().to_string();
let open = state.read(cx).open.clone();
let secondary = color(ColorRole::IconSecondary);
let search = div()
.relative()
.flex()
.flex_row()
.flex_1()
.min_w_0()
.items_center()
.gap(rem(8.))
.h(rem(40.))
.px(rem(12.))
.child(press::icon(CarbonIcon::Search, 16., secondary).flex_none())
.child(
sans(div(), 14., 400)
.relative()
.flex()
.flex_1()
.min_w_0()
.items_center()
.h_full()
.text_color(color(ColorRole::TextPrimary))
.when(query.is_empty(), |this| {
this.child(
div()
.absolute()
.left_0()
.text_color(color(ColorRole::TextPlaceholder))
.child(self.texts.search.clone()),
)
})
.child(
Input::new(&input)
.appearance(false)
.with_size(gpui_kit::component::Size::XSmall)
.aria_label(self.texts.search.clone())
.p_0()
.h_full()
.w_full()
.text_size(rem(14.))
.line_height(relative(crate::t3::craft::BODY_LINE)),
),
);
let on_refresh = self.on_refresh.clone();
let bar = div()
.flex()
.flex_row()
.items_center()
.gap(rem(4.))
.pr(rem(4.))
.border_b_1()
.border_color(color(ColorRole::BorderSubtle01))
.child(search)
.child(
icon_button(
(id.clone(), "refresh"),
self.texts.refresh.clone(),
CarbonIcon::Renew,
)
.when_some(on_refresh, |b, f| b.on_click(move |_, w, cx| f(w, cx))),
);
let message = |content: AnyElement| {
sans(div(), 14., 400)
.p(rem(16.))
.text_color(color(ColorRole::TextSecondary))
.child(content)
.into_any_element()
};
let body: Vec<AnyElement> = if self.loading {
vec![message(
Spinner::new((id.clone(), "loading"))
.size(SpinnerSize::Inline)
.description(self.texts.loading.clone())
.into_any_element(),
)]
} else {
let rows = visible_rows(&self.tree, &open, &query);
if rows.is_empty() {
vec![message(self.texts.empty.clone().into_any_element())]
} else {
rows.iter()
.map(|row| {
file_row(
&id,
row,
open.contains(&row.node.path),
self.selected.as_ref() == Some(&row.node.path),
&state,
self.on_select.clone(),
window,
cx,
)
})
.collect()
}
};
let tree = crate::components::ScrollArea::new((id.clone(), "tree"))
.axes(crate::components::ScrollAxes::Vertical)
.focusable(false)
.flex_1()
.min_h_0()
.child(div().flex().flex_col().py(rem(4.)).children(body));
div()
.id(id)
.flex()
.flex_col()
.size_full()
.min_h(rem(256.))
.bg(color(ColorRole::Layer01))
.child(bar)
.child(tree)
}
}
#[allow(clippy::too_many_arguments)]
fn file_row(
id: &ElementId,
row: &FileRow<'_>,
is_open: bool,
selected: bool,
state: &Entity<BrowserState>,
on_select: Option<super::parts::TextHandler>,
window: &mut Window,
cx: &mut App,
) -> AnyElement {
let node = row.node;
let row_id = ElementId::from((id.clone(), SharedString::from(format!("row:{}", node.path))));
let (hovered, store) = press::hover((row_id.clone(), "hover"), window, cx);
let (focus, ring) = press::focus((row_id.clone(), "focus"), window, cx);
let bg = if selected {
Some(color(ColorRole::LayerSelected01))
} else if hovered {
Some(color(ColorRole::LayerHover01))
} else {
None
};
let turn = motion::animate(
(row_id.clone(), "chev"),
if is_open {
std::f32::consts::FRAC_PI_2
} else {
0.
},
Motion::TOGGLE,
window,
cx,
);
let lead = if node.dir {
press::icon(
CarbonIcon::ChevronRight,
12.,
color(ColorRole::IconSecondary),
)
.rotate(gpui_kit::radians(turn))
.flex_none()
.into_any_element()
} else {
div().w(rem(12.)).flex_none().into_any_element()
};
let path = node.path.clone();
let dir = node.dir;
let state = state.clone();
let status = node.status.map(|s| {
let (fill, ink) = s.colors();
mono(div(), 11., 600)
.flex_none()
.px(rem(4.))
.bg(fill)
.text_color(ink)
.child(s.letter())
});
sans(
gpui_kit::base::Button::new(row_id)
.track_focus(&focus)
.accessibility_label(node.name.clone()),
14.,
400,
)
.relative()
.flex()
.flex_row()
.items_center()
.justify_start()
.gap(rem(4.))
.w_full()
.h(rem(28.))
.pl(rem(row_indent(row.depth)))
.pr(rem(12.))
.text_color(color(ColorRole::TextPrimary))
.when_some(bg, |this, bg| this.bg(bg))
.cursor(CursorStyle::PointingHand)
.on_hover(press::track_hover(store))
.on_click(move |_, window, cx| {
if dir {
let path = path.clone();
state.update(cx, |s, cx| {
if !s.open.remove(&path) {
s.open.insert(path);
}
cx.notify();
});
} else if let Some(f) = on_select.as_ref() {
f(path.clone(), window, cx);
}
})
.child(lead)
.child(FileIcon::new(node.name.clone()).dir(node.dir).open(is_open))
.child(
div()
.flex_1()
.min_w_0()
.overflow_hidden()
.text_ellipsis()
.whitespace_nowrap()
.text_left()
.child(node.name.clone()),
)
.children(status)
.when(ring, |this| this.child(press::ring()))
.into_any_element()
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum FileSurfaceKind {
#[default]
Loading,
Notice,
Failure,
Binary,
Large,
}
impl FileSurfaceKind {
pub const ALL: [FileSurfaceKind; 5] = [
Self::Loading,
Self::Notice,
Self::Failure,
Self::Binary,
Self::Large,
];
pub const fn name(self) -> &'static str {
match self {
Self::Loading => "loading",
Self::Notice => "notice",
Self::Failure => "failure",
Self::Binary => "binary",
Self::Large => "large",
}
}
pub fn glyph(self) -> Option<(CarbonIcon, ColorRole)> {
match self {
Self::Loading => None,
Self::Notice => Some((CarbonIcon::InformationFilled, ColorRole::IconSecondary)),
Self::Failure => Some((CarbonIcon::ErrorFilled, ColorRole::SupportError)),
Self::Binary => Some((CarbonIcon::Document, ColorRole::IconSecondary)),
Self::Large => Some((CarbonIcon::WarningAltFilled, ColorRole::SupportWarning)),
}
}
}
#[derive(IntoElement)]
pub struct FileSurfaceState {
id: ElementId,
kind: FileSurfaceKind,
title: Option<SharedString>,
loading_title: SharedString,
body: Option<AnyElement>,
actions: Vec<AnyElement>,
}
impl FileSurfaceState {
pub fn new(id: impl Into<ElementId>, kind: FileSurfaceKind) -> Self {
Self {
id: id.into(),
kind,
title: None,
loading_title: "Loading preview\u{2026}".into(),
body: None,
actions: Vec::new(),
}
}
pub fn title(mut self, title: impl Into<SharedString>) -> Self {
self.title = Some(title.into());
self
}
pub fn loading_title(mut self, title: impl Into<SharedString>) -> Self {
self.loading_title = title.into();
self
}
pub fn body(mut self, body: impl IntoElement) -> Self {
self.body = Some(body.into_any_element());
self
}
pub fn action(mut self, action: impl IntoElement) -> Self {
self.actions.push(action.into_any_element());
self
}
}
impl RenderOnce for FileSurfaceState {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let mark = match self.kind.glyph() {
None => Spin::new((self.id.clone(), "spin"))
.size(24.)
.into_any_element(),
Some((glyph, role)) => press::icon(glyph, 24., color(role))
.flex_none()
.into_any_element(),
};
let title = self.title.clone().or_else(|| {
(self.kind == FileSurfaceKind::Loading).then(|| self.loading_title.clone())
});
sans(div(), 14., 400)
.id(self.id)
.flex()
.flex_col()
.items_center()
.justify_center()
.gap(rem(8.))
.min_h(rem(192.))
.p(rem(32.))
.text_center()
.bg(color(ColorRole::Layer01))
.text_color(color(ColorRole::TextPrimary))
.child(mark)
.child(sans(div(), 14., 600).child(title.unwrap_or_default()))
.when_some(self.body, |this, body| {
this.child(
sans(div(), 14., 400)
.max_w(rem(384.))
.line_height(relative(1.42857))
.text_color(color(ColorRole::TextSecondary))
.child(body),
)
})
.when(!self.actions.is_empty(), |this| {
this.child(
div()
.flex()
.flex_row()
.flex_wrap()
.justify_end()
.items_center()
.gap(rem(8.))
.children(self.actions),
)
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn glyphs_follow_ext() {
assert_eq!(file_glyph("Button.tsx", false, false), CarbonIcon::Code);
assert_eq!(file_glyph("tokens.json", false, false), CarbonIcon::Code);
assert_eq!(file_glyph("README.md", false, false), CarbonIcon::Document);
assert_eq!(file_glyph("logo.png", false, false), CarbonIcon::Image);
assert_eq!(file_glyph("a.pdf", false, false), CarbonIcon::DocumentPdf);
assert_eq!(file_glyph("x.css", false, false), CarbonIcon::ColorPalette);
assert_eq!(file_glyph("Makefile", false, false), CarbonIcon::Document);
assert_eq!(file_glyph("src", true, false), CarbonIcon::Folder);
assert_eq!(file_glyph("src", true, true), CarbonIcon::FolderOpen);
}
fn tree() -> Vec<FileNode> {
vec![
FileNode::dir(
"src",
"src",
[
FileNode::dir(
"src/components",
"components",
[FileNode::file("src/components/Button.tsx", "Button.tsx")],
),
FileNode::file("src/tokens.json", "tokens.json").status(FileStatus::Modified),
],
)
.open(true),
FileNode::file("README.md", "README.md"),
]
}
#[test]
fn rows_walk_open_folders_and_filter_files() {
let tree = tree();
let open = initially_open(&tree);
let names = |rows: Vec<FileRow<'_>>| {
rows.iter()
.map(|r| (r.node.name.to_string(), r.depth))
.collect::<Vec<_>>()
};
assert_eq!(
names(visible_rows(&tree, &open, "")),
vec![
("src".into(), 0),
("components".into(), 1),
("tokens.json".into(), 1),
("README.md".into(), 0)
]
);
assert_eq!(
names(visible_rows(&tree, &open, "BUTTON")),
vec![
("src".into(), 0),
("components".into(), 1),
("Button.tsx".into(), 2)
]
);
}
#[test]
fn rows_indent_by_depth_and_crumbs_split_the_path() {
assert_eq!(row_indent(0), 8.);
assert_eq!(row_indent(2), 40.);
assert_eq!(path_segments("/a//b/c.tsx"), vec!["a", "b", "c.tsx"]);
}
}