tab_bar.rs

  1use gpui::{AnyElement, ScrollHandle, Stateful};
  2use smallvec::SmallVec;
  3
  4use crate::prelude::*;
  5
  6#[derive(IntoElement)]
  7pub struct TabBar {
  8    id: ElementId,
  9    start_children: SmallVec<[AnyElement; 2]>,
 10    children: SmallVec<[AnyElement; 2]>,
 11    end_children: SmallVec<[AnyElement; 2]>,
 12    scroll_handle: Option<ScrollHandle>,
 13}
 14
 15impl TabBar {
 16    pub fn new(id: impl Into<ElementId>) -> Self {
 17        Self {
 18            id: id.into(),
 19            start_children: SmallVec::new(),
 20            children: SmallVec::new(),
 21            end_children: SmallVec::new(),
 22            scroll_handle: None,
 23        }
 24    }
 25
 26    pub fn track_scroll(mut self, scroll_handle: ScrollHandle) -> Self {
 27        self.scroll_handle = Some(scroll_handle);
 28        self
 29    }
 30
 31    pub fn start_children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> {
 32        &mut self.start_children
 33    }
 34
 35    pub fn start_child(mut self, start_child: impl IntoElement) -> Self
 36    where
 37        Self: Sized,
 38    {
 39        self.start_children_mut()
 40            .push(start_child.into_element().into_any());
 41        self
 42    }
 43
 44    pub fn start_children(
 45        mut self,
 46        start_children: impl IntoIterator<Item = impl IntoElement>,
 47    ) -> Self
 48    where
 49        Self: Sized,
 50    {
 51        self.start_children_mut().extend(
 52            start_children
 53                .into_iter()
 54                .map(|child| child.into_any_element()),
 55        );
 56        self
 57    }
 58
 59    pub fn end_children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> {
 60        &mut self.end_children
 61    }
 62
 63    pub fn end_child(mut self, end_child: impl IntoElement) -> Self
 64    where
 65        Self: Sized,
 66    {
 67        self.end_children_mut()
 68            .push(end_child.into_element().into_any());
 69        self
 70    }
 71
 72    pub fn end_children(mut self, end_children: impl IntoIterator<Item = impl IntoElement>) -> Self
 73    where
 74        Self: Sized,
 75    {
 76        self.end_children_mut().extend(
 77            end_children
 78                .into_iter()
 79                .map(|child| child.into_any_element()),
 80        );
 81        self
 82    }
 83}
 84
 85impl ParentElement for TabBar {
 86    fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> {
 87        &mut self.children
 88    }
 89}
 90
 91impl RenderOnce for TabBar {
 92    type Rendered = Stateful<Div>;
 93
 94    fn render(self, cx: &mut WindowContext) -> Self::Rendered {
 95        const HEIGHT_IN_REMS: f32 = 30. / 16.;
 96
 97        div()
 98            .id(self.id)
 99            .z_index(120) // todo!("z-index")
100            .group("tab_bar")
101            .flex()
102            .flex_none()
103            .w_full()
104            .h(rems(HEIGHT_IN_REMS))
105            .bg(cx.theme().colors().tab_bar_background)
106            .child(
107                h_stack()
108                    .flex_none()
109                    .gap_1()
110                    .px_1()
111                    .border_b()
112                    .border_r()
113                    .border_color(cx.theme().colors().border)
114                    .children(self.start_children),
115            )
116            .child(
117                div()
118                    .relative()
119                    .flex_1()
120                    .h_full()
121                    .overflow_hidden_x()
122                    .child(
123                        div()
124                            .absolute()
125                            .top_0()
126                            .left_0()
127                            .z_index(1)
128                            .size_full()
129                            .border_b()
130                            .border_color(cx.theme().colors().border),
131                    )
132                    .child(
133                        h_stack()
134                            .id("tabs")
135                            .z_index(2)
136                            .flex_grow()
137                            .overflow_x_scroll()
138                            .when_some(self.scroll_handle, |cx, scroll_handle| {
139                                cx.track_scroll(&scroll_handle)
140                            })
141                            .children(self.children),
142                    ),
143            )
144            .child(
145                h_stack()
146                    .flex_none()
147                    .gap_1()
148                    .px_1()
149                    .border_b()
150                    .border_l()
151                    .border_color(cx.theme().colors().border)
152                    .children(self.end_children),
153            )
154    }
155}