gpui_component/sidebar/
group.rs1use std::rc::Rc;
2
3use crate::{ActiveTheme, Collapsible, h_flex, sidebar::SidebarItem, v_flex};
4use gpui::{
5 AnyElement, App, ElementId, IntoElement, ParentElement, SharedString, Styled as _, Window, div,
6 prelude::FluentBuilder as _,
7};
8use gpui_base::LogicalStyledExt as _;
9
10#[derive(Clone)]
12pub struct SidebarGroup<E: SidebarItem + 'static> {
13 label: SharedString,
14 suffix: Option<Rc<dyn Fn(&mut Window, &mut App) -> AnyElement + 'static>>,
15 collapsed: bool,
16 children: Vec<E>,
17}
18
19impl<E: SidebarItem> SidebarGroup<E> {
20 pub fn new(label: impl Into<SharedString>) -> Self {
22 Self {
23 label: label.into(),
24 suffix: None,
25 collapsed: false,
26 children: Vec::new(),
27 }
28 }
29
30 pub fn suffix<F, S>(mut self, builder: F) -> Self
36 where
37 F: Fn(&mut Window, &mut App) -> S + 'static,
38 S: IntoElement,
39 {
40 self.suffix = Some(Rc::new(move |window, cx| {
41 builder(window, cx).into_any_element()
42 }));
43 self
44 }
45
46 pub fn child(mut self, child: E) -> Self {
48 self.children.push(child);
49 self
50 }
51
52 pub fn children(mut self, children: impl IntoIterator<Item = E>) -> Self {
56 self.children.extend(children);
57 self
58 }
59}
60
61impl<E: SidebarItem> Collapsible for SidebarGroup<E> {
62 fn is_collapsed(&self) -> bool {
63 self.collapsed
64 }
65
66 fn collapsed(mut self, collapsed: bool) -> Self {
67 self.collapsed = collapsed;
68 self
69 }
70}
71
72impl<E: SidebarItem> SidebarItem for SidebarGroup<E> {
73 fn render(
74 self,
75 id: impl Into<ElementId>,
76 window: &mut Window,
77 cx: &mut App,
78 ) -> impl IntoElement {
79 let id = id.into();
80
81 v_flex()
82 .relative()
83 .when(!self.collapsed, |this| {
84 this.child(
85 h_flex()
86 .flex_row_dir(cx)
87 .flex_shrink_0()
88 .px_2()
89 .rounded(cx.theme().radius)
90 .text_xs()
91 .text_color(cx.theme().sidebar_foreground.opacity(0.7))
92 .h_8()
93 .map(|this| match self.suffix {
94 Some(suffix) => this
95 .gap_2()
96 .justify_between()
97 .child(
98 div()
99 .flex_1()
100 .min_w_0()
101 .overflow_hidden()
102 .whitespace_nowrap()
103 .text_ellipsis()
104 .child(self.label),
105 )
106 .child(
107 h_flex()
108 .flex_shrink_0()
109 .items_center()
110 .child(suffix(window, cx)),
111 ),
112 None => this.child(self.label),
113 }),
114 )
115 })
116 .child(
117 div()
118 .gap_2()
119 .flex_col()
120 .children(self.children.into_iter().enumerate().map(|(ix, child)| {
121 child
122 .collapsed(self.collapsed)
123 .render(format!("{}-{}", id, ix), window, cx)
124 .into_any_element()
125 })),
126 )
127 }
128}
129
130#[cfg(test)]
131mod tests {
132 use gpui::{Context, InteractiveElement as _, Render, TestAppContext, px};
133
134 use super::*;
135 use crate::sidebar::SidebarMenu;
136
137 struct GroupHarness {
138 collapsed: bool,
139 }
140
141 impl Render for GroupHarness {
142 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
143 div().w(px(240.)).child(
144 SidebarGroup::<SidebarMenu>::new("A project name long enough to shorten")
145 .suffix(|_, _| div().debug_selector(|| "group-suffix".into()).size(px(16.)))
146 .collapsed(self.collapsed)
147 .render("group", window, cx),
148 )
149 }
150 }
151
152 #[gpui::test]
153 fn the_suffix_sits_at_the_end_of_the_header(cx: &mut TestAppContext) {
154 cx.update(crate::init);
155 let (_, cx) = cx.add_window_view(|_, _| GroupHarness { collapsed: false });
156 cx.update(|window, cx| window.draw(cx).clear(cx));
157
158 let suffix = cx
159 .debug_bounds("group-suffix")
160 .expect("the suffix is drawn in the header");
161 assert_eq!(suffix.size.width, px(16.));
162 assert!(
163 suffix.right() <= px(240.) && suffix.right() > px(220.),
164 "a long label leaves the suffix at the end of the header: {suffix:?}"
165 );
166 }
167
168 #[gpui::test]
169 fn a_collapsed_group_hides_the_suffix(cx: &mut TestAppContext) {
170 cx.update(crate::init);
171 let (_, cx) = cx.add_window_view(|_, _| GroupHarness { collapsed: true });
172 cx.update(|window, cx| window.draw(cx).clear(cx));
173
174 assert!(cx.debug_bounds("group-suffix").is_none());
175 }
176}