use std::rc::Rc;
use gpui_kit::component::{ActiveTheme as _, Icon, Sizable as _, h_flex, v_flex};
use gpui_kit::prelude::FluentBuilder as _;
use gpui_kit::{
AnyElement, App, ClickEvent, Div, ElementId, FontWeight, Hsla, InteractiveElement, IntoElement,
ParentElement, RenderOnce, Role, SharedString, Stateful, StatefulInteractiveElement as _,
Styled, TestSupportExt as _, Window, div,
};
use subspace_lucide::SvgIcon;
use super::navigator_header::{self, Head};
use super::row_parts::{
ClickHandler, MenuBuilder, count_mark, fill, row_focus, with_handlers, with_menu,
};
use super::{header, row_order, text_edge};
use crate::row_menu::GroupedMenu;
use crate::tokens::Tokens;
use crate::truncated;
#[derive(IntoElement)]
pub struct Navigator {
title: SharedString,
badge: Option<SharedString>,
actions: Vec<AnyElement>,
children: Vec<AnyElement>,
footer: Option<AnyElement>,
pub(super) title_preview: Option<bool>,
}
impl Navigator {
pub fn new(title: impl Into<SharedString>) -> Self {
Self {
title: title.into(),
badge: None,
actions: Vec::new(),
children: Vec::new(),
footer: None,
title_preview: None,
}
}
pub fn badge(mut self, badge: impl Into<SharedString>) -> Self {
self.badge = Some(badge.into());
self
}
pub fn action(mut self, action: impl IntoElement) -> Self {
self.actions.push(action.into_any_element());
self
}
pub fn footer(mut self, footer: impl IntoElement) -> Self {
self.footer = Some(footer.into_any_element());
self
}
}
impl ParentElement for Navigator {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for Navigator {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
row_order::begin(window, cx);
let tokens = Tokens::current(cx);
let tree_label = self.title.clone();
let show_title = self
.title_preview
.unwrap_or_else(|| super::navigator_title::shown(cx));
let head = navigator_header::head(
Head {
title: self.title,
badge: self.badge,
actions: self.actions,
show_title,
},
window,
cx,
);
v_flex()
.id("workspace-navigator")
.test_support()
.size_full()
.min_w_0()
.bg(tokens.sidebar)
.border_r_1()
.border_color(tokens.sidebar_line)
.children(head)
.child(
v_flex()
.id("workspace-navigator-body")
.test_support()
.role(Role::Tree)
.aria_label(tree_label)
.flex_1()
.min_h_0()
.overflow_y_scroll()
.py_1()
.children(self.children),
)
.children(self.footer.map(|footer| foot(footer, &tokens)))
}
}
fn foot(footer: AnyElement, tokens: &Tokens) -> Stateful<Div> {
h_flex()
.id("workspace-navigator-footer")
.w_full()
.min_w_0()
.flex_shrink_0()
.gap_2()
.px(text_edge(tokens))
.py_2()
.border_t_1()
.border_color(tokens.sidebar_line)
.text_size(tokens.secondary_size())
.text_color(tokens.muted)
.child(footer)
}
pub fn nav_section(label: impl Into<SharedString>, cx: &App) -> Stateful<Div> {
let tokens = Tokens::current(cx);
let label = label.into();
div()
.id(SharedString::from(format!("nav-section:{label}")))
.role(Role::Heading)
.aria_label(label.clone())
.w_full()
.flex_shrink_0()
.px(text_edge(&tokens))
.pt_3()
.pb_1()
.text_size(tokens.caption_size())
.font_weight(FontWeight::SEMIBOLD)
.text_color(tokens.muted)
.child(header::capitals(&label))
}
#[derive(IntoElement)]
pub struct NavRow {
key: SharedString,
label: SharedString,
icon: Option<SvgIcon>,
leading: Option<AnyElement>,
count: Option<SharedString>,
trailing: Option<AnyElement>,
selected: bool,
quiet: bool,
strong: bool,
indent: u8,
on_click: Option<ClickHandler>,
menu: Option<MenuBuilder>,
}
impl NavRow {
pub fn new(key: impl Into<SharedString>, label: impl Into<SharedString>) -> Self {
Self {
key: key.into(),
label: label.into(),
icon: None,
leading: None,
count: None,
trailing: None,
selected: false,
quiet: false,
strong: false,
indent: 0,
on_click: None,
menu: None,
}
}
pub fn icon(mut self, icon: SvgIcon) -> Self {
self.icon = Some(icon);
self
}
pub fn leading(mut self, leading: impl IntoElement) -> Self {
self.leading = Some(leading.into_any_element());
self
}
pub fn count(mut self, count: impl Into<SharedString>) -> Self {
self.count = Some(count.into());
self
}
pub fn trailing(mut self, trailing: impl IntoElement) -> Self {
self.trailing = Some(trailing.into_any_element());
self
}
pub fn selected(mut self, selected: bool) -> Self {
self.selected = selected;
self
}
pub fn quiet(mut self, quiet: bool) -> Self {
self.quiet = quiet;
self
}
pub fn strong(mut self, strong: bool) -> Self {
self.strong = strong;
self
}
pub fn indent(mut self, indent: u8) -> Self {
self.indent = indent;
self
}
pub fn on_click(
mut self,
handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
) -> Self {
self.on_click = Some(Rc::new(handler));
self
}
pub fn menu(
mut self,
commands: impl Fn(&mut Window, &mut App) -> GroupedMenu + 'static,
) -> Self {
self.menu = Some(Rc::new(commands));
self
}
}
impl RenderOnce for NavRow {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let tokens = Tokens::current(cx);
let (focus, rows) = row_focus(&self.key, self.on_click.is_some(), window, cx);
let ring = focus.is_focused(window) && window.last_input_was_keyboard();
let group = SharedString::from(format!("nav-row:{}", self.key));
let title_id = SharedString::from(format!("nav-row-title:{}", self.key));
let colour = self.text_colour(&tokens);
let edge = text_edge(&tokens);
let mono = cx.theme().mono_font_family.clone();
let leading = leading_mark(self.leading, self.icon, &tokens);
let row = h_flex()
.id(ElementId::Name(self.key))
.test_support()
.when(self.on_click.is_some(), |row| row.track_focus(&focus))
.group(group.clone())
.role(Role::TreeItem)
.aria_label(self.label.clone())
.aria_selected(self.selected)
.aria_level(usize::from(self.indent) + 1)
.relative()
.w_full()
.flex_shrink_0()
.h(tokens.row_height)
.gap_2()
.pl(edge + tokens.indent * f32::from(self.indent))
.pr(edge)
.text_size(tokens.row_font)
.text_color(colour)
.when(self.strong, |row| row.font_weight(FontWeight::SEMIBOLD))
.child(fill(group, self.selected, ring, &tokens))
.children(leading)
.child(truncated::text(title_id, self.label).flex_1())
.children(self.count.map(|count| count_mark(count, mono, &tokens)))
.children(self.trailing);
let row = with_handlers(row, focus, rows, self.on_click.clone());
let select = self.on_click.filter(|_| !self.selected);
with_menu(row, self.menu, select)
}
}
impl NavRow {
fn text_colour(&self, tokens: &Tokens) -> Hsla {
if self.selected || self.strong {
tokens.fg_strong
} else if self.quiet {
tokens.muted
} else {
tokens.fg
}
}
}
fn leading_mark(
leading: Option<AnyElement>,
icon: Option<SvgIcon>,
tokens: &Tokens,
) -> Option<AnyElement> {
leading.or_else(|| {
icon.map(|icon| {
Icon::from(icon)
.xsmall()
.flex_shrink_0()
.text_color(tokens.muted)
.into_any_element()
})
})
}