1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
use crate::{
ActiveTheme as _, Collapsible, Icon, IconName, Placement, Sizable as _, StyledExt,
button::{Button, ButtonVariants as _},
h_flex,
menu::{ContextMenuExt, PopupMenu},
sidebar::SidebarItem,
tooltip::{ManagedTooltipExt as _, Tooltip},
v_flex,
};
use gpui::{
AnyElement, App, ClickEvent, ElementId, InteractiveElement as _, IntoElement,
ParentElement as _, SharedString, StatefulInteractiveElement as _, StyleRefinement, Styled,
Window, div, percentage, prelude::FluentBuilder,
};
use std::rc::Rc;
/// Menu for the [`super::Sidebar`]
#[derive(Clone)]
pub struct SidebarMenu {
style: StyleRefinement,
collapsed: bool,
items: Vec<SidebarMenuItem>,
}
impl SidebarMenu {
/// Create a new SidebarMenu
pub fn new() -> Self {
Self {
style: StyleRefinement::default(),
items: Vec::new(),
collapsed: false,
}
}
/// Add a [`SidebarMenuItem`] child menu item to the sidebar menu.
///
/// See also [`SidebarMenu::children`].
pub fn child(mut self, child: impl Into<SidebarMenuItem>) -> Self {
self.items.push(child.into());
self
}
/// Add multiple [`SidebarMenuItem`] child menu items to the sidebar menu.
pub fn children(
mut self,
children: impl IntoIterator<Item = impl Into<SidebarMenuItem>>,
) -> Self {
self.items = children.into_iter().map(Into::into).collect();
self
}
}
impl Collapsible for SidebarMenu {
fn is_collapsed(&self) -> bool {
self.collapsed
}
fn collapsed(mut self, collapsed: bool) -> Self {
self.collapsed = collapsed;
self
}
}
impl SidebarItem for SidebarMenu {
fn render(
self,
id: impl Into<ElementId>,
window: &mut Window,
cx: &mut App,
) -> impl IntoElement {
let id = id.into();
v_flex()
.gap_2()
.refine_style(&self.style)
.children(self.items.into_iter().enumerate().map(|(ix, item)| {
let id = SharedString::from(format!("{}-{}", id, ix));
item.collapsed(self.collapsed)
.render(id, window, cx)
.into_any_element()
}))
}
}
impl Styled for SidebarMenu {
fn style(&mut self) -> &mut StyleRefinement {
&mut self.style
}
}
/// Menu item for the [`SidebarMenu`]
#[derive(Clone)]
pub struct SidebarMenuItem {
icon: Option<Icon>,
label: SharedString,
handler: Rc<dyn Fn(&ClickEvent, &mut Window, &mut App)>,
active: bool,
default_open: bool,
click_to_open: bool,
collapsed: bool,
click_to_toggle: bool,
children: Vec<Self>,
suffix: Option<Rc<dyn Fn(&mut Window, &mut App) -> AnyElement + 'static>>,
disabled: bool,
context_menu: Option<Rc<dyn Fn(PopupMenu, &mut Window, &mut App) -> PopupMenu + 'static>>,
}
impl SidebarMenuItem {
/// Create a new [`SidebarMenuItem`] with a label.
pub fn new(label: impl Into<SharedString>) -> Self {
Self {
icon: None,
label: label.into(),
handler: Rc::new(|_, _, _| {}),
active: false,
collapsed: false,
default_open: false,
click_to_open: false,
click_to_toggle: false,
children: Vec::new(),
suffix: None,
disabled: false,
context_menu: None,
}
}
/// Set the icon for the menu item
pub fn icon(mut self, icon: impl Into<Icon>) -> Self {
self.icon = Some(icon.into());
self
}
/// Set the active state of the menu item
pub fn active(mut self, active: bool) -> Self {
self.active = active;
self
}
/// Add a click handler to the menu item
pub fn on_click(
mut self,
handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
) -> Self {
self.handler = Rc::new(handler);
self
}
/// Set the collapsed state of the menu item
pub fn collapsed(mut self, collapsed: bool) -> Self {
self.collapsed = collapsed;
self
}
/// Set the default open state of the Submenu, default is `false`.
///
/// This only used on initial render, the internal state will be used afterwards.
pub fn default_open(mut self, open: bool) -> Self {
self.default_open = open;
self
}
/// Set whether clicking the menu item open the submenu.
///
/// Default is `false`.
///
/// If `false` we only handle open/close via the caret button.
pub fn click_to_open(mut self, click_to_open: bool) -> Self {
self.click_to_open = click_to_open;
self
}
/// Set whether clicking the menu item toggles the submenu.
///
/// If click_to_open is `true`, this has no effect.
///
/// Default is `false`.
pub fn click_to_toggle(mut self, click_to_toggle: bool) -> Self {
self.click_to_toggle = click_to_toggle;
self
}
pub fn children(mut self, children: impl IntoIterator<Item = impl Into<Self>>) -> Self {
self.children = children.into_iter().map(Into::into).collect();
self
}
/// Set the suffix for the menu item.
pub fn suffix<F, E>(mut self, builder: F) -> Self
where
F: Fn(&mut Window, &mut App) -> E + 'static,
E: IntoElement,
{
self.suffix = Some(Rc::new(move |window, cx| {
builder(window, cx).into_any_element()
}));
self
}
/// Set disabled flat for menu item.
pub fn disable(mut self, disable: bool) -> Self {
self.disabled = disable;
self
}
fn is_submenu(&self) -> bool {
self.children.len() > 0
}
fn collapsed_tooltip(&self) -> Option<SharedString> {
(self.collapsed && self.icon.is_some()).then(|| self.label.clone())
}
/// Set the context menu for the menu item.
pub fn context_menu(
mut self,
f: impl Fn(PopupMenu, &mut Window, &mut App) -> PopupMenu + 'static,
) -> Self {
self.context_menu = Some(Rc::new(f));
self
}
}
impl FluentBuilder for SidebarMenuItem {}
impl Collapsible for SidebarMenuItem {
fn is_collapsed(&self) -> bool {
self.collapsed
}
fn collapsed(mut self, collapsed: bool) -> Self {
self.collapsed = collapsed;
self
}
}
impl SidebarItem for SidebarMenuItem {
fn render(
self,
id: impl Into<ElementId>,
window: &mut Window,
cx: &mut App,
) -> impl IntoElement {
let click_to_open = self.click_to_open;
let click_to_toggle = self.click_to_toggle;
let default_open = self.default_open;
let collapsed_tooltip = self.collapsed_tooltip();
let id = id.into();
let is_submenu = self.is_submenu();
let open_state = if is_submenu {
Some(window.use_keyed_state(id.clone(), cx, |_, _| default_open))
} else {
None
};
let handler = self.handler.clone();
let is_collapsed = self.collapsed;
let is_active = self.active;
let is_hoverable = !is_active && !self.disabled;
let is_disabled = self.disabled;
let is_open = open_state
.as_ref()
.map_or(false, |s| !is_collapsed && *s.read(cx));
div()
.id(id.clone())
.w_full()
.child(
h_flex()
.size_full()
.id("item")
.overflow_x_hidden()
.flex_shrink_0()
.p_2()
.gap_x_2()
.rounded(cx.theme().radius)
.text_sm()
.when(is_hoverable, |this| {
this.hover(|this| {
this.bg(cx.theme().sidebar_accent.opacity(0.8))
.text_color(cx.theme().sidebar_accent_foreground)
})
})
.when(is_active, |this| {
this.font_medium()
.bg(cx.theme().tokens.sidebar_accent)
.text_color(cx.theme().sidebar_accent_foreground)
})
.when_some(self.icon.clone(), |this, icon| this.child(icon))
.when(is_collapsed, |this| {
this.justify_center().when(is_active, |this| {
this.bg(cx.theme().tokens.sidebar_accent)
.text_color(cx.theme().sidebar_accent_foreground)
})
})
.when(!is_collapsed, |this| {
this.h_7()
.child(
h_flex()
.flex_1()
.gap_x_2()
.justify_between()
.overflow_x_hidden()
.child(
h_flex()
.flex_1()
.overflow_x_hidden()
.child(self.label.clone()),
)
.when_some(self.suffix.clone(), |this, suffix| {
this.child(suffix(window, cx).into_any_element())
}),
)
.when_some(open_state.clone(), |this, open_state| {
this.child(
Button::new("caret")
.xsmall()
.ghost()
.icon(
Icon::new(IconName::ChevronRight)
.size_4()
.when(is_open, |this| {
this.rotate(percentage(90. / 360.))
}),
)
.on_click({
move |_, _, cx| {
// Avoid trigger item click, just expand/collapse submenu
cx.stop_propagation();
open_state.update(cx, |is_open, cx| {
*is_open = !*is_open;
cx.notify();
})
}
}),
)
})
})
.when(is_disabled, |this| {
this.text_color(cx.theme().muted_foreground)
})
.when(!is_disabled, |this| {
this.on_click({
let open_state = open_state.clone();
move |ev, window, cx| {
if click_to_open {
if let Some(ref s) = open_state {
s.update(cx, |is_open: &mut bool, cx| {
*is_open = true;
cx.notify();
});
}
} else if click_to_toggle {
if let Some(ref s) = open_state {
s.update(cx, |is_open: &mut bool, cx| {
*is_open = !*is_open;
cx.notify();
});
}
}
handler(ev, window, cx)
}
})
})
.map(|this| {
if let Some(tooltip) = collapsed_tooltip {
this.managed_tooltip_at(Placement::Right, move |window, cx| {
Tooltip::new(tooltip.clone()).build(window, cx)
})
} else {
this
}
})
.map(|this| {
if let Some(context_menu) = self.context_menu {
this.context_menu(move |menu, window, cx| {
context_menu(menu, window, cx)
})
.into_any_element()
} else {
this.into_any_element()
}
}),
)
.when(is_open, |this| {
this.child(
v_flex()
.id("submenu")
.border_l_1()
.border_color(cx.theme().sidebar_border)
.gap_1()
.ml_3p5()
.pl_2p5()
.py_0p5()
.children(self.children.into_iter().enumerate().map(|(ix, item)| {
let id = format!("{}-{}", id, ix);
item.render(id, window, cx).into_any_element()
})),
)
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn collapsed_icon_item_uses_label_as_tooltip() {
let item = SidebarMenuItem::new("Projects")
.icon(Icon::default())
.collapsed(true);
assert_eq!(item.collapsed_tooltip().as_deref(), Some("Projects"));
}
#[test]
fn expanded_or_iconless_item_has_no_collapsed_tooltip() {
let expanded = SidebarMenuItem::new("Projects").icon(Icon::default());
let iconless = SidebarMenuItem::new("Projects").collapsed(true);
assert!(expanded.collapsed_tooltip().is_none());
assert!(iconless.collapsed_tooltip().is_none());
}
}