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
//! Menubar: a desktop-style persistent menu bar (`.cu-menubar`). Each menu is
//! the Carbon `Menu` of [`super::menu`] (size `sm`) hanging under its
//! trigger.
//!
//! - **Behavior**: pressing a trigger (pointer, Enter, Space, ↓) toggles its
//! menu; while a menu is open, pointing at another trigger switches to it
//! and ←/→ inside a menu move to the neighbouring menu (wrapping); ←/→ on a
//! focused trigger move focus along the bar. Escape closes and returns focus
//! to the trigger. Menu keyboard handling is the menu's.
//! - **Appearance** (`custom.css`): a `size-md` (2.5rem) bar on `layer-01`
//! with a 1px `border-subtle-01` bottom line; triggers fill the height with
//! 1rem inline padding, `text-primary`, `layer-hover-01` on hover and while
//! their menu is open (70ms), and a 2px inset `focus` outline.
//!
//! ```ignore
//! Menubar::new("app")
//! .menu("File", vec![MenuItem::item("New tab").shortcut("⌘T")])
//! .menu("Help", vec![MenuItem::item("Documentation")])
//! ```
use super::menu::{self, MenuItem, MenuSize};
use super::overlay::{self, rem};
use crate::{
motion::{self, Motion},
theme::color,
tokens::{ColorRole, SIZE_MD, TypeStyle},
typography::TypeStyled as _,
};
use gpui_kit::{
App, CursorStyle, ElementId, Entity, FocusHandle, Hsla, InteractiveElement, IntoElement,
KeyDownEvent, MouseButton, ParentElement, RenderOnce, SharedString,
StatefulInteractiveElement as _, Styled, TestSupportExt as _, Window, div,
prelude::FluentBuilder as _,
};
use std::rc::Rc;
/// The bar's colors.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct MenubarLook {
/// `.cu-menubar { background: var(--layer-01) }`.
pub background: Hsla,
/// `border-bottom: 1px solid var(--border-subtle-01)`.
pub border: Hsla,
/// `.cu-menubar-trigger { color: var(--text-primary) }`.
pub text: Hsla,
/// `.cu-menubar-trigger:hover, --open { background: var(--layer-hover-01) }`.
pub hover: Hsla,
}
impl MenubarLook {
/// The look on the active theme.
pub fn current() -> Self {
Self {
background: color(ColorRole::Layer01),
border: color(ColorRole::BorderSubtle01),
text: color(ColorRole::TextPrimary),
hover: color(ColorRole::LayerHover01),
}
}
}
/// `.cu-menubar { height: var(--size-md) }`.
pub const BAR_HEIGHT: f32 = SIZE_MD.px_value();
/// The neighbour of `index` in `count` menus, `direction` ±1, wrapping.
pub fn neighbour(index: usize, count: usize, direction: isize) -> usize {
(index as isize + direction).rem_euclid(count.max(1) as isize) as usize
}
/// A menu bar. See the module docs.
#[derive(IntoElement)]
pub struct Menubar {
id: ElementId,
menus: Vec<(SharedString, Vec<MenuItem>)>,
default_open: Option<usize>,
}
struct BarState {
open: Option<usize>,
hovered: Option<usize>,
focus: Vec<FocusHandle>,
}
impl Menubar {
/// A menu bar with a stable `id`.
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
menus: Vec::new(),
default_open: None,
}
}
/// Append a menu (`MenubarMenu label`) with its items.
pub fn menu(mut self, label: impl Into<SharedString>, items: Vec<MenuItem>) -> Self {
self.menus.push((label.into(), items));
self
}
/// Open the menu at `index` on arrival (`defaultOpen`).
pub fn default_open(mut self, index: usize) -> Self {
self.default_open = Some(index);
self
}
}
fn set_open(state: &Entity<BarState>, open: Option<usize>, window: &mut Window, cx: &mut App) {
state.update(cx, |s, cx| {
s.open = open;
cx.notify();
});
window.refresh();
}
/// Moving to another menu while one is open: focus that menu's trigger
/// first, so the closing menu sees focus already gone elsewhere and does not
/// pull it back to its own trigger (whichever of the two renders last would
/// win), and the opening menu returns to its own trigger when it closes.
fn switch_focus(trigger: &FocusHandle, window: &mut Window, cx: &mut App) {
trigger.focus(window, cx);
}
impl RenderOnce for Menubar {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let count = self.menus.len();
let default_open = self.default_open;
let state = window.use_keyed_state((id.clone(), "bar"), cx, |_, _| BarState {
open: default_open,
hovered: None,
focus: Vec::new(),
});
if state.read(cx).focus.len() < count {
state.update(cx, |s, cx| {
while s.focus.len() < count {
s.focus.push(cx.focus_handle());
}
});
}
let look = MenubarLook::current();
let (open, hovered, focus) = {
let s = state.read(cx);
(s.open, s.hovered, s.focus.clone())
};
let mut bar = div()
.id(id.clone())
.flex()
.flex_row()
.h(rem(BAR_HEIGHT))
.bg(look.background)
.border_b_1()
.border_color(look.border);
for (index, (label, items)) in self.menus.into_iter().enumerate() {
let menu_id = ElementId::NamedChild(
std::sync::Arc::new(id.clone()),
SharedString::from(format!("menu-{index}")),
);
let is_open = open == Some(index);
let target = if is_open || hovered == Some(index) {
look.hover
} else {
Hsla::transparent_black()
};
let bg =
motion::animate_color((menu_id.clone(), "bg"), target, Motion::HOVER, window, cx);
let focused = focus[index].is_focused(window);
let trigger = div()
.id((menu_id.clone(), "trigger"))
.test_support()
.relative()
.h_full()
.px(rem(16.))
.flex()
.items_center()
.bg(bg)
.text_color(look.text)
.type_style(TypeStyle::BodyCompact01)
.cursor(CursorStyle::PointingHand)
.track_focus(&focus[index].clone().tab_stop(true))
.on_hover({
let state = state.clone();
move |hovering, window, cx| {
let switched = state.update(cx, |s, cx| {
let mut switched = None;
if *hovering {
s.hovered = Some(index);
// A menubar follows the pointer once a menu is open.
if s.open.is_some() && s.open != Some(index) {
s.open = Some(index);
switched = s.focus.get(index).cloned();
}
} else if s.hovered == Some(index) {
s.hovered = None;
}
cx.notify();
switched
});
if let Some(trigger) = switched {
switch_focus(&trigger, window, cx);
}
window.refresh();
}
})
.on_mouse_down(MouseButton::Left, {
let state = state.clone();
move |_, window, cx| {
set_open(&state, (!is_open).then_some(index), window, cx);
cx.stop_propagation();
}
})
.on_key_down({
let state = state.clone();
let focus = focus.clone();
move |event: &KeyDownEvent, window, cx| match event.keystroke.key.as_str() {
"enter" | "space" | "down" => {
set_open(&state, Some(index), window, cx);
cx.stop_propagation();
}
"left" | "right" => {
let dir = if event.keystroke.key == "left" { -1 } else { 1 };
focus[neighbour(index, count, dir)].focus(window, cx);
cx.stop_propagation();
}
_ => {}
}
})
.when(focused, |this| {
// `.cu-menubar-trigger:focus-visible { outline: 2px solid var(--focus); outline-offset: -2px }`.
this.child(
div()
.absolute()
.top_0()
.left_0()
.size_full()
.border_2()
.border_color(color(ColorRole::Focus)),
)
})
.child(label.clone())
.into_any_element();
let set: overlay::OpenChange = {
let state = state.clone();
Rc::new(move |open, window, cx| {
let current = state.read(cx).open;
if open {
set_open(&state, Some(index), window, cx);
} else if current == Some(index) {
set_open(&state, None, window, cx);
}
})
};
let hook: menu::KeyHook = {
let state = state.clone();
let focus = focus.clone();
Rc::new(move |key, window, cx| match key {
"left" | "right" => {
let dir = if key == "left" { -1 } else { 1 };
let next = neighbour(index, count, dir);
switch_focus(&focus[next], window, cx);
set_open(&state, Some(next), window, cx);
true
}
_ => false,
})
};
bar = bar.child(menu::anchored_menu_full_height(
&menu_id,
trigger,
focus[index].clone(),
menu::MenuSpec {
items,
size: MenuSize::Sm,
label,
open: is_open,
set_open: set,
min_width: None,
on_unhandled_key: Some(hook),
},
window,
cx,
));
}
bar
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tokens::CarbonTheme;
fn hex(c: Hsla) -> String {
let c = c.to_rgb();
let b = |v: f32| (v * 255.).round() as u8;
format!("#{:02x}{:02x}{:02x}", b(c.r), b(c.g), b(c.b))
}
#[test]
fn bar_follows_cu_menubar() {
crate::theme::tests_set(CarbonTheme::White);
let look = MenubarLook::current();
assert_eq!(hex(look.background), "#f4f4f4");
assert_eq!(hex(look.hover), "#e8e8e8");
assert_eq!(BAR_HEIGHT, 40.);
}
#[test]
fn arrows_wrap_between_menus() {
assert_eq!(neighbour(0, 3, -1), 2);
assert_eq!(neighbour(2, 3, 1), 0);
assert_eq!(neighbour(1, 3, 1), 2);
}
}