use gpui_kit::component::{h_flex, v_flex};
use gpui_kit::{
AnyElement, App, Div, ElementId, InteractiveElement as _, IntoElement, ParentElement, Pixels,
RenderOnce, Stateful, Styled as _, TestSupportExt as _, Window, div, px,
};
use super::{FOCUS_RING_PX, HAIRLINE_PX, strip_height, text_edge};
use crate::icon_button::Metrics;
use crate::tokens::Tokens;
#[derive(IntoElement)]
pub struct Pane {
id: ElementId,
tabs: Option<AnyElement>,
toolbar: Option<AnyElement>,
children: Vec<AnyElement>,
}
impl Pane {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
tabs: None,
toolbar: None,
children: Vec::new(),
}
}
pub fn tabs(mut self, tabs: impl IntoElement) -> Self {
self.tabs = Some(tabs.into_any_element());
self
}
pub fn toolbar(mut self, toolbar: impl IntoElement) -> Self {
self.toolbar = Some(toolbar.into_any_element());
self
}
}
impl ParentElement for Pane {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for Pane {
fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
let tokens = Tokens::current(cx);
v_flex()
.id(self.id)
.test_support()
.size_full()
.min_w_0()
.min_h_0()
.bg(tokens.bg)
.child(pane_head(self.tabs, self.toolbar, cx))
.child(
v_flex()
.id("workspace-content")
.test_support()
.flex_1()
.w_full()
.min_w_0()
.min_h_0()
.children(self.children),
)
}
}
pub fn pane_head(tabs: Option<AnyElement>, toolbar: Option<AnyElement>, _cx: &App) -> Div {
v_flex()
.w_full()
.min_w_0()
.flex_shrink_0()
.children(tabs.map(|tabs| TabStrip { tabs }))
.children(toolbar)
}
#[derive(IntoElement)]
struct TabStrip {
tabs: AnyElement,
}
impl RenderOnce for TabStrip {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let tokens = Tokens::current(cx);
h_flex()
.id("workspace-tab-strip")
.relative()
.w_full()
.min_w_0()
.flex_shrink_0()
.h(strip_height(window, cx))
.bg(tokens.sidebar)
.child(
div()
.absolute()
.left_0()
.bottom_0()
.w_full()
.h(px(HAIRLINE_PX))
.bg(tokens.line),
)
.child(self.tabs)
.test_support()
}
}
#[derive(IntoElement)]
pub struct Toolbar {
id: ElementId,
leading: Vec<AnyElement>,
trailing: Vec<AnyElement>,
strip: bool,
}
impl Toolbar {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
leading: Vec::new(),
trailing: Vec::new(),
strip: false,
}
}
pub fn leading(mut self, item: impl IntoElement) -> Self {
self.leading.push(item.into_any_element());
self
}
pub fn trailing(mut self, item: impl IntoElement) -> Self {
self.trailing.push(item.into_any_element());
self
}
pub fn strip(mut self) -> Self {
self.strip = true;
self
}
}
enum Line {
Strip {
height: Pixels,
reserve: Pixels,
gap: Pixels,
},
Wrapping(Pixels),
}
impl Line {
fn frame(&self, frame: Stateful<Div>) -> Stateful<Div> {
match *self {
Self::Strip { height, .. } => frame.items_center().h(height).overflow_hidden(),
Self::Wrapping(least) => frame.items_start().min_h(least).py_1(),
}
}
fn leading(&self, leading: Stateful<Div>, items: usize) -> Stateful<Div> {
match self {
Self::Strip { .. } => self.reserved(ring_clip(leading), items),
Self::Wrapping(_) => leading.flex_wrap(),
}
}
fn trailing(&self, trailing: Stateful<Div>, items: usize) -> Stateful<Div> {
match self {
Self::Strip { .. } => self
.reserved(ring_clip(trailing).min_w_0(), items)
.justify_end(),
Self::Wrapping(_) => trailing.flex_shrink_0(),
}
}
fn reserved(&self, side: Stateful<Div>, items: usize) -> Stateful<Div> {
match *self {
Self::Strip { reserve, gap, .. } if items > 0 => {
let gaps = gap * items.saturating_sub(1) as f32;
side.min_w(reserve + gaps + px(FOCUS_RING_PX + FOCUS_RING_PX))
}
_ => side,
}
}
}
fn ring_clip(side: Stateful<Div>) -> Stateful<Div> {
side.h_full()
.items_center()
.mx(-px(FOCUS_RING_PX))
.px(px(FOCUS_RING_PX))
.overflow_hidden()
}
impl RenderOnce for Toolbar {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let tokens = Tokens::current(cx);
let metrics = Metrics::current(cx);
let gap = metrics.gap();
let line = if self.strip {
Line::Strip {
height: strip_height(window, cx),
reserve: metrics.overflow_width().to_pixels(window.rem_size()),
gap: gap.to_pixels(window.rem_size()),
}
} else {
Line::Wrapping(tokens.toolbar_height)
};
let frame = h_flex()
.id(self.id)
.w_full()
.min_w_0()
.flex_shrink_0()
.gap(gap)
.px(text_edge(&tokens))
.bg(tokens.bg)
.border_b_1()
.border_color(tokens.line);
let (leading_items, trailing_items) = (self.leading.len(), self.trailing.len());
let leading = h_flex()
.id("workspace-toolbar-leading")
.flex_1()
.min_w_0()
.gap(gap)
.children(self.leading);
let trailing = h_flex()
.id("workspace-toolbar-trailing")
.gap(gap)
.children(self.trailing);
line.frame(frame)
.child(line.leading(leading, leading_items).test_support())
.child(line.trailing(trailing, trailing_items).test_support())
.test_support()
}
}