gpui_component/sidebar/
group.rs1use crate::{ActiveTheme, Collapsible, h_flex, sidebar::SidebarItem, v_flex};
2use gpui::ColorExt as _;
3use gpui::{
4 App, ElementId, IntoElement, ParentElement, SharedString, Styled as _, Window, div,
5 prelude::FluentBuilder as _,
6};
7
8#[derive(Clone)]
10pub struct SidebarGroup<E: SidebarItem + 'static> {
11 label: SharedString,
12 collapsed: bool,
13 children: Vec<E>,
14}
15
16impl<E: SidebarItem> SidebarGroup<E> {
17 pub fn new(label: impl Into<SharedString>) -> Self {
19 Self {
20 label: label.into(),
21 collapsed: false,
22 children: Vec::new(),
23 }
24 }
25
26 pub fn child(mut self, child: E) -> Self {
28 self.children.push(child);
29 self
30 }
31
32 pub fn children(mut self, children: impl IntoIterator<Item = E>) -> Self {
36 self.children.extend(children);
37 self
38 }
39}
40
41impl<E: SidebarItem> Collapsible for SidebarGroup<E> {
42 fn is_collapsed(&self) -> bool {
43 self.collapsed
44 }
45
46 fn collapsed(mut self, collapsed: bool) -> Self {
47 self.collapsed = collapsed;
48 self
49 }
50}
51
52impl<E: SidebarItem> SidebarItem for SidebarGroup<E> {
53 fn render(
54 self,
55 id: impl Into<ElementId>,
56 window: &mut Window,
57 cx: &mut App,
58 ) -> impl IntoElement {
59 let id = id.into();
60
61 v_flex()
62 .relative()
63 .when(!self.collapsed, |this| {
64 this.child(
65 h_flex()
66 .flex_shrink_0()
67 .px_2()
68 .rounded(cx.theme().radius)
69 .text_xs()
70 .text_color(cx.theme().sidebar_foreground.opacity(0.7))
71 .h_8()
72 .child(self.label),
73 )
74 })
75 .child(
76 div()
77 .gap_2()
78 .flex_col()
79 .children(self.children.into_iter().enumerate().map(|(ix, child)| {
80 child
81 .collapsed(self.collapsed)
82 .render(format!("{}-{}", id, ix), window, cx)
83 .into_any_element()
84 })),
85 )
86 }
87}