use std::rc::Rc;
use gpui_kit::component::{h_flex, v_flex};
use gpui_kit::prelude::FluentBuilder as _;
use gpui_kit::{
AnyElement, App, ClickEvent, Div, ElementId, FocusHandle, FontWeight, InteractiveElement as _,
IntoElement, ParentElement, RenderOnce, Role, ScrollHandle, SharedString, Stateful,
StatefulInteractiveElement as _, Styled as _, TestSupportExt as _, Window, div, px, relative,
};
use subspace_lucide::IconName;
use super::{header, strip_height, text_edge};
use crate::action::{Intent, icon_button};
use crate::tokens::Tokens;
use crate::typography::heading_font;
#[cfg(test)]
mod fact_tests;
mod tabs;
#[cfg(test)]
mod tabs_tests;
use tabs::InspectorTabs;
pub use tabs::{INSPECTOR_MAX_TABS, InspectorTab};
const FACT_LABEL_WIDTH_PX: f32 = 84.;
const WIDE_FACT_LABEL_WIDTH_PX: f32 = 112.;
const WIDE_FACT_LABEL_MAX_FRACTION: f32 = 0.4;
type ClickHandler = Rc<dyn Fn(&ClickEvent, &mut Window, &mut App)>;
#[derive(IntoElement)]
pub struct Inspector {
id: ElementId,
title: SharedString,
actions: Vec<AnyElement>,
close: Option<(SharedString, ClickHandler)>,
children: Vec<AnyElement>,
footer: Option<AnyElement>,
scroll: Option<ScrollHandle>,
tabs: Option<InspectorTabs>,
}
impl Inspector {
pub fn new(id: impl Into<ElementId>, title: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
title: title.into(),
actions: Vec::new(),
close: None,
children: Vec::new(),
footer: None,
scroll: None,
tabs: None,
}
}
pub fn tabs(
mut self,
tabs: impl IntoIterator<Item = InspectorTab>,
selected: usize,
on_select: impl Fn(usize, &ClickEvent, &mut Window, &mut App) + 'static,
) -> Self {
let tabs: Vec<_> = tabs.into_iter().collect();
self.tabs = (!tabs.is_empty()).then(|| InspectorTabs {
tabs,
selected,
on_select: Rc::new(on_select),
focus: None,
});
self
}
pub fn track_tab(mut self, index: usize, focus: &FocusHandle) -> Self {
if let Some(tabs) = &mut self.tabs {
tabs.focus = Some((index, focus.clone()));
}
self
}
pub fn action(mut self, action: impl IntoElement) -> Self {
self.actions.push(action.into_any_element());
self
}
pub fn on_close(
mut self,
label: impl Into<SharedString>,
on_close: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
) -> Self {
self.close = Some((label.into(), Rc::new(on_close)));
self
}
pub fn footer(mut self, footer: impl IntoElement) -> Self {
self.footer = Some(footer.into_any_element());
self
}
pub fn track_scroll(mut self, scroll: &ScrollHandle) -> Self {
self.scroll = Some(scroll.clone());
self
}
}
impl ParentElement for Inspector {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for Inspector {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let tokens = Tokens::current(cx);
let panel = self.tabs.as_ref().and_then(tabs::panel_label);
let title = heading(self.title, self.tabs, &tokens, cx);
let close = self.close.map(|(label, on_close)| {
icon_button(
"workspace-inspector-close",
Intent::Quiet,
IconName::X,
label,
)
.on_click(move |event, window, cx| on_close(event, window, cx))
});
let height = strip_height(window, cx);
let head = header::strip("workspace-inspector-header", tokens.line, height, &tokens)
.child(title)
.children(self.actions)
.children(close);
let body = v_flex()
.id("workspace-inspector-body")
.test_support()
.flex_1()
.min_h_0()
.min_w_0()
.overflow_y_scroll()
.when_some(self.scroll.as_ref(), |body, scroll| {
body.track_scroll(scroll)
})
.when_some(panel, |body, label| {
body.role(Role::TabPanel).aria_label(label)
})
.gap_6()
.px(text_edge(&tokens))
.py_4()
.children(self.children);
v_flex()
.id(self.id)
.test_support()
.size_full()
.min_w_0()
.bg(tokens.surface)
.border_l_1()
.border_color(tokens.line)
.child(head.test_support())
.child(body)
.children(self.footer.map(|footer| foot(footer, &tokens)))
}
}
fn heading(
title: SharedString,
tabs: Option<InspectorTabs>,
tokens: &Tokens,
cx: &App,
) -> AnyElement {
match tabs {
Some(tabs) => div()
.flex_1()
.min_w_0()
.child(tabs::tab_list(title, tabs, tokens, cx))
.into_any_element(),
None => header::title(
"workspace-inspector-title",
title,
FontWeight::SEMIBOLD,
heading_font(cx),
tokens,
)
.test_support()
.into_any_element(),
}
}
fn foot(footer: AnyElement, tokens: &Tokens) -> Stateful<Div> {
h_flex()
.id("workspace-inspector-footer")
.w_full()
.min_w_0()
.flex_shrink_0()
.flex_wrap()
.gap_2()
.px(text_edge(tokens))
.py_3()
.border_t_1()
.border_color(tokens.line)
.child(footer)
}
pub fn section(title: impl Into<SharedString>, cx: &App) -> Stateful<Div> {
let tokens = Tokens::current(cx);
let title = title.into();
v_flex()
.id(SharedString::from(format!("inspector-section:{title}")))
.w_full()
.min_w_0()
.gap_2()
.child(
div()
.id(SharedString::from(format!(
"inspector-section-title:{title}"
)))
.role(Role::Heading)
.aria_label(title.clone())
.text_size(tokens.caption_size())
.font_weight(FontWeight::SEMIBOLD)
.text_color(tokens.muted)
.child(header::capitals(&title)),
)
}
pub fn fact(label: impl Into<SharedString>, value: impl IntoElement, cx: &App) -> Stateful<Div> {
fact_row(label, value, div().w(px(FACT_LABEL_WIDTH_PX)), cx)
}
pub fn fact_with_wide_label(
label: impl Into<SharedString>,
value: impl IntoElement,
cx: &App,
) -> Stateful<Div> {
let column = div()
.w(px(WIDE_FACT_LABEL_WIDTH_PX))
.max_w(relative(WIDE_FACT_LABEL_MAX_FRACTION));
fact_row(label, value, column, cx)
}
fn fact_row(
label: impl Into<SharedString>,
value: impl IntoElement,
label_column: Div,
cx: &App,
) -> Stateful<Div> {
let tokens = Tokens::current(cx);
let label = label.into();
h_flex()
.id(SharedString::from(format!("inspector-fact:{label}")))
.w_full()
.min_w_0()
.items_start()
.gap_3()
.text_size(tokens.secondary_size())
.child(
label_column
.flex_shrink_0()
.text_color(tokens.muted)
.child(label),
)
.child(
div()
.flex_1()
.min_w_0()
.text_color(tokens.fg_strong)
.child(value),
)
}