fc_ui/capabilities/navigation/
sidebar.rs1use crate::capabilities::foundation::theme::use_theme;
4use crate::capabilities::primitives::icon::Icon;
5use crate::capabilities::primitives::icon_source::IconSource;
6use gpui::{prelude::FluentBuilder as _, prelude::*, *};
7use std::sync::Arc;
8
9actions!(
10 sidebar,
11 [ToggleSidebar, FocusNext, FocusPrevious, ActivateItem]
12);
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
15pub enum SidebarVariant {
16 Fixed,
17 #[default]
18 Collapsible,
19 Overlay,
20}
21
22#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
23pub enum SidebarPosition {
24 #[default]
25 Left,
26 Right,
27}
28
29#[derive(Clone)]
30pub struct SidebarItem<T: Clone> {
31 pub id: T,
32 pub label: SharedString,
33 pub icon: Option<IconSource>,
34 pub badge: Option<SharedString>,
35 pub disabled: bool,
36 pub separator: bool,
37}
38
39impl<T: Clone> SidebarItem<T> {
40 pub fn new(id: T, label: impl Into<SharedString>) -> Self {
41 Self {
42 id,
43 label: label.into(),
44 icon: None,
45 badge: None,
46 disabled: false,
47 separator: false,
48 }
49 }
50
51 pub fn with_icon(mut self, icon: impl Into<IconSource>) -> Self {
52 self.icon = Some(icon.into());
53 self
54 }
55
56 pub fn with_badge(mut self, badge: impl Into<SharedString>) -> Self {
57 self.badge = Some(badge.into());
58 self
59 }
60
61 pub fn disabled(mut self, disabled: bool) -> Self {
62 self.disabled = disabled;
63 self
64 }
65
66 pub fn separator(mut self, separator: bool) -> Self {
67 self.separator = separator;
68 self
69 }
70}
71
72#[derive(Clone, IntoElement)]
73pub struct Sidebar<T: Clone + PartialEq + 'static> {
74 items: Vec<SidebarItem<T>>,
75 selected_id: Option<T>,
76 variant: SidebarVariant,
77 position: SidebarPosition,
78 expanded_width: Pixels,
79 collapsed_width: Pixels,
80 is_expanded: bool,
81 show_toggle_button: bool,
82 on_select: Option<Arc<dyn Fn(&T, &mut Window, &mut App) + Send + Sync + 'static>>,
83 on_toggle: Option<Arc<dyn Fn(bool, &mut Window, &mut App) + Send + Sync + 'static>>,
84 focus_handle: FocusHandle,
85 focused_index: Option<usize>,
86 style: StyleRefinement,
87}
88
89impl<T: Clone + PartialEq + 'static> Sidebar<T> {
90 pub fn new(cx: &mut App) -> Self {
91 Self {
92 items: Vec::new(),
93 selected_id: None,
94 variant: SidebarVariant::default(),
95 position: SidebarPosition::default(),
96 expanded_width: px(280.0),
97 collapsed_width: px(64.0),
98 is_expanded: true,
99 show_toggle_button: true,
100 on_select: None,
101 on_toggle: None,
102 focus_handle: cx.focus_handle(),
103 focused_index: None,
104 style: StyleRefinement::default(),
105 }
106 }
107
108 pub fn items(mut self, items: Vec<SidebarItem<T>>) -> Self {
109 self.items = items;
110 self
111 }
112
113 pub fn selected_id(mut self, id: T) -> Self {
114 self.selected_id = Some(id);
115 self
116 }
117
118 pub fn variant(mut self, variant: SidebarVariant) -> Self {
119 self.variant = variant;
120 self
121 }
122
123 pub fn position(mut self, position: SidebarPosition) -> Self {
124 self.position = position;
125 self
126 }
127
128 pub fn expanded_width(mut self, width: impl Into<Pixels>) -> Self {
129 self.expanded_width = width.into();
130 self
131 }
132
133 pub fn collapsed_width(mut self, width: impl Into<Pixels>) -> Self {
134 self.collapsed_width = width.into();
135 self
136 }
137
138 pub fn expanded(mut self, expanded: bool) -> Self {
139 self.is_expanded = expanded;
140 self
141 }
142
143 pub fn show_toggle_button(mut self, show: bool) -> Self {
144 self.show_toggle_button = show;
145 self
146 }
147
148 pub fn on_select<F>(mut self, f: F) -> Self
149 where
150 F: Fn(&T, &mut Window, &mut App) + Send + Sync + 'static,
151 {
152 self.on_select = Some(Arc::new(f));
153 self
154 }
155
156 pub fn on_toggle<F>(mut self, f: F) -> Self
157 where
158 F: Fn(bool, &mut Window, &mut App) + Send + Sync + 'static,
159 {
160 self.on_toggle = Some(Arc::new(f));
161 self
162 }
163
164 fn current_width(&self) -> Pixels {
165 if self.is_expanded {
166 self.expanded_width
167 } else {
168 self.collapsed_width
169 }
170 }
171}
172
173impl<T: Clone + PartialEq + 'static> Styled for Sidebar<T> {
174 fn style(&mut self) -> &mut StyleRefinement {
175 &mut self.style
176 }
177}
178
179impl<T: Clone + PartialEq + 'static> RenderOnce for Sidebar<T> {
180 fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
181 let theme = use_theme(cx);
182 let current_width = self.current_width();
183 let is_collapsible = self.variant == SidebarVariant::Collapsible;
184
185 let on_toggle_for_button = self.on_toggle.clone();
186 let on_toggle_for_keyboard = self.on_toggle.clone();
187
188 let variant = self.variant;
190 let position = self.position;
191 let show_toggle_button = self.show_toggle_button;
192 let is_expanded = self.is_expanded;
193 let selected_id = self.selected_id.clone();
194 let focused_index = self.focused_index;
195
196 let mut item_elements = Vec::new();
198 for (index, item) in self.items.iter().enumerate() {
199 if item.separator {
200 item_elements.push(
201 div()
202 .w_full()
203 .h(px(1.0))
204 .bg(theme.tokens.border.opacity(0.5))
205 .my(px(8.0))
206 .into_any_element(),
207 );
208 } else {
209 let is_selected = matches!(selected_id.as_ref(), Some(id) if id == &item.id);
210 let is_focused = Some(index) == focused_index;
211
212 let item_element = self.render_sidebar_item(
213 item,
214 index,
215 is_selected,
216 is_focused,
217 is_expanded,
218 &theme,
219 cx,
220 );
221 item_elements.push(item_element);
222 }
223 }
224
225 let user_style = self.style;
226
227 let mut sidebar = div()
228 .flex()
229 .flex_col()
230 .h_full()
231 .bg(theme.tokens.card)
232 .border_r_1()
233 .border_color(theme.tokens.border)
234 .w(current_width);
235
236 sidebar = match variant {
237 SidebarVariant::Overlay => sidebar
238 .absolute()
239 .shadow_lg()
240 .when(position == SidebarPosition::Right, |s| s.right_0())
241 .when(position == SidebarPosition::Left, |s| s.left_0()),
242 _ => sidebar,
243 };
244
245 let header = if show_toggle_button && is_collapsible {
246 let toggle_button = div()
247 .flex()
248 .items_center()
249 .justify_center()
250 .w_full()
251 .h(px(48.0))
252 .cursor(CursorStyle::PointingHand)
253 .hover(|style| style.bg(theme.tokens.muted.opacity(0.5)))
254 .on_mouse_down(MouseButton::Left, move |_, window, cx| {
255 if let Some(on_toggle) = on_toggle_for_button.clone() {
256 on_toggle(!is_expanded, window, cx);
257 }
258 })
259 .child(
260 Icon::new(if is_expanded {
261 "chevron-left"
262 } else {
263 "chevron-right"
264 })
265 .size(px(16.0))
266 .color(theme.tokens.muted_foreground),
267 );
268
269 Some(toggle_button)
270 } else {
271 None
272 };
273
274 let mut content = div()
275 .flex()
276 .flex_col()
277 .flex_1()
278 .gap(px(2.0))
279 .px(px(8.0))
280 .py(px(16.0));
281
282 content = content.children(item_elements);
283
284 let focus_handle = self.focus_handle.clone();
286
287 sidebar = sidebar
288 .track_focus(&focus_handle)
289 .on_key_down(move |event: &KeyDownEvent, window, cx| {
290 if event.keystroke.key == "escape" && is_collapsible && is_expanded {
291 if let Some(on_toggle) = on_toggle_for_keyboard.clone() {
292 on_toggle(false, window, cx);
293 }
294 }
295 })
296 .map(|this| {
297 let mut div = this;
298 div.style().refine(&user_style);
299 div
300 });
301
302 sidebar.children(
303 vec![
304 header.map(|h| h.into_any_element()),
305 Some(content.into_any_element()),
306 ]
307 .into_iter()
308 .flatten(),
309 )
310 }
311}
312
313impl<T: Clone + PartialEq + 'static> Sidebar<T> {
314 fn render_sidebar_item(
315 &self,
316 item: &SidebarItem<T>,
317 _index: usize,
318 is_selected: bool,
319 is_focused: bool,
320 sidebar_expanded: bool,
321 theme: &crate::capabilities::foundation::theme::Theme,
322 _cx: &mut App,
323 ) -> AnyElement {
324 let on_select = self.on_select.clone();
325
326 let mut item_container = div()
327 .flex()
328 .items_center()
329 .w_full()
330 .h(px(40.0))
331 .px(px(12.0))
332 .rounded(px(6.0))
333 .cursor(if item.disabled {
334 CursorStyle::Arrow
335 } else {
336 CursorStyle::PointingHand
337 });
338
339 if is_selected {
340 item_container = item_container
341 .bg(theme.tokens.primary.opacity(0.1))
342 .text_color(theme.tokens.primary);
343 } else if is_focused {
344 item_container = item_container
345 .bg(theme.tokens.accent.opacity(0.1))
346 .text_color(theme.tokens.accent_foreground);
347 } else {
348 item_container = item_container
349 .text_color(theme.tokens.foreground)
350 .hover(|style| style.bg(theme.tokens.muted.opacity(0.5)));
351 }
352
353 if item.disabled {
354 item_container = item_container.opacity(0.5).cursor(CursorStyle::Arrow);
355 }
356
357 if !item.disabled {
358 item_container = item_container.on_mouse_down(MouseButton::Left, {
359 let item_id = item.id.clone();
360 move |_, window, cx| {
361 if let Some(on_select) = on_select.clone() {
362 on_select(&item_id, window, cx);
363 }
364 }
365 });
366 }
367
368 let mut children = Vec::new();
369
370 if let Some(icon) = &item.icon {
371 let icon_element = Icon::new(icon.clone())
372 .size(px(18.0))
373 .color(if is_selected {
374 theme.tokens.primary
375 } else if item.disabled {
376 theme.tokens.muted_foreground
377 } else {
378 theme.tokens.foreground
379 });
380
381 children.push(icon_element.into_any_element());
382 }
383
384 if sidebar_expanded {
385 let label_element = div()
386 .flex_1()
387 .ml(px(12.0))
388 .text_size(px(14.0))
389 .font_family(theme.tokens.font_family.clone())
390 .font_weight(if is_selected {
391 FontWeight::SEMIBOLD
392 } else {
393 FontWeight::NORMAL
394 })
395 .text_color(if is_selected {
396 theme.tokens.primary
397 } else if item.disabled {
398 theme.tokens.muted_foreground
399 } else {
400 theme.tokens.foreground
401 })
402 .child(item.label.clone());
403
404 children.push(label_element.into_any_element());
405
406 if let Some(badge) = &item.badge {
407 let badge_element = div()
408 .px(px(6.0))
409 .py(px(2.0))
410 .rounded(px(8.0))
411 .bg(if is_selected {
412 theme.tokens.primary.opacity(0.2)
413 } else {
414 theme.tokens.muted
415 })
416 .text_size(px(10.0))
417 .font_family(theme.tokens.font_family.clone())
418 .font_weight(FontWeight::SEMIBOLD)
419 .text_color(if is_selected {
420 theme.tokens.primary
421 } else {
422 theme.tokens.muted_foreground
423 })
424 .child(badge.clone());
425
426 children.push(badge_element.into_any_element());
427 }
428 }
429
430 item_container.children(children).into_any_element()
431 }
432}
433
434pub fn init_sidebar(cx: &mut App) {
435 crate::capabilities::foundation::initialization::begin(cx, "sidebar");
436}