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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
//! A menubar: the menus of an app in a row, File, Edit, View, each opened from its title.
use std::{
collections::HashMap,
sync::{
Arc, LazyLock, Mutex, Weak,
atomic::{AtomicBool, Ordering},
},
};
use super::{
menu::{Menu, MenuEntry},
names,
recipe::{Case, Interaction, Name, Recipe, ResolvedPart, ResolvedRecipe},
shared::{fading, pressable},
theme::{Themed, current_theme},
};
use crate::{
app::{
events::KeyboardEvent,
theme::{BorderSize, OpacityLevel, PaletteColor, RadiusSize, Theme, TypographyStyle},
},
components::{self, Column, Row},
core::{ElementRef, Signal, WatchHandle},
layout::{
Alignment,
text_style::{FontWeight, TextUnderline},
},
node::{CursorIcon, Element, FocusGroup, border::Border},
};
/// Whether a key was pressed with no modifier held but Shift.
fn plain(event: &KeyboardEvent) -> bool {
!event.ctrl && !event.alt && !event.meta
}
/// The parts of a menubar a recipe styles.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum MenubarPart {
/// The bar that holds the titles.
Bar,
/// An item of the bar: the title of a menu, what is pressed to open it, and what is lit while it is open.
Item,
/// The text of an item.
Label,
}
names!(MenubarPart {
Bar => "bar",
Item => "item",
Label => "label",
});
/// The states of a menubar a recipe styles. An item is resolved in the states of the bar and its own.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum MenubarState {
/// The item's menu is open.
Open,
/// The bar, or the item, cannot be opened.
Disabled,
}
names!(MenubarState {
Open => "open",
Disabled => "disabled",
});
/// What builds a menubar's nodes: a theme's in place of the default one.
pub type MenubarRenderer = Arc<dyn Fn(MenubarView) -> Element + Send + Sync>;
/// One menu of a menubar: its title, what it holds, and whether it can be opened.
///
/// ```
/// use lurq::ui::{MenuItem, MenubarMenu};
///
/// let window = MenubarMenu::new("Window", [MenuItem::new("Minimize"), MenuItem::new("Zoom")]).disabled(true);
/// ```
pub struct MenubarMenu {
pub title: Arc<str>,
/// What the menu holds, in order.
pub entries: Vec<MenuEntry>,
pub disabled: bool,
/// The id of its item on the bar, in place of `{id}-{index}` of the bar's id.
pub id: Option<Arc<str>>,
pub key: Option<Arc<str>>,
}
impl MenubarMenu {
/// A menu called `title` holding `entries`: [`MenuItem`](super::MenuItem)s, and [`MenuEntry::Separator`] and
/// [`MenuEntry::Heading`] between them.
pub fn new(title: &str, entries: impl IntoIterator<Item = impl Into<MenuEntry>>) -> Self {
Self {
title: Arc::from(title),
entries: entries.into_iter().map(Into::into).collect(),
disabled: false,
id: None,
key: None,
}
}
/// Whether the menu cannot be opened: its item is dimmed, and the arrows pass it by.
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
/// The id of the menu's item on the bar: what is pressed. Its panel is `{id}-menu`.
pub fn id(mut self, id: impl Into<Arc<str>>) -> Self {
self.id = Some(id.into());
self
}
pub fn key(mut self, key: impl Into<Arc<str>>) -> Self {
self.key = Some(key.into());
self
}
}
/// A menubar: the menus of an app in a row, File, Edit, View, each opened from its title, as the title bar of a
/// window on Windows or Linux has them.
///
/// ```
/// use lurq::{
/// core::Signal,
/// ui::{MenuEntry, MenuItem, Menubar, MenubarMenu},
/// };
///
/// let open: Signal<Option<usize>> = Signal::new(None);
/// let bar = Menubar::new(open)
/// .label("Application")
/// .menu(MenubarMenu::new(
/// "File",
/// [
/// MenuItem::new("New").shortcut("Ctrl+N").on_select(|| println!("new")).into(),
/// MenuItem::new("Open…").shortcut("Ctrl+O").into(),
/// MenuEntry::Separator,
/// MenuItem::new("Quit").shortcut("Ctrl+Q").into(),
/// ],
/// ))
/// .menu(MenubarMenu::new("Edit", [MenuItem::new("Undo").shortcut("Ctrl+Z"), MenuItem::new("Redo").disabled(true)]))
/// .menu(MenubarMenu::new("View", [MenuItem::new("Show grid").checked(true)]));
/// ```
///
/// Its menus are [`Menu`]s, of the same [`MenuItem`](super::MenuItem)s: submenus, checks, radio items, shortcuts,
/// disabled items, separators and headings. Which of them is open is the app's: the `Signal<Option<usize>>` it gives
/// the bar, which a press of a title sets and choosing an item, a press outside and Escape set to `None`. One is open
/// at a time.
///
/// The bar is one stop of Tab. Left and Right move between the titles, a letter goes to the title that starts with
/// it, and Down, Enter and Space open the focused title's menu at its first item, Up at its last. While a menu is
/// open, Left and Right go to the menu beside it (Left first closes a submenu that has the focus, and Right opens
/// the submenu of the item it is on, if it has one), and the pointer coming onto another title opens that one.
/// Escape closes the menu and gives the focus back to its title. Alt pressed alone puts the focus on the first
/// title and underlines the first letter of each, and Alt again gives the focus back; Alt with that letter opens
/// the title's menu from wherever the focus is.
pub struct Menubar {
open: Signal<Option<usize>>,
menus: Vec<MenubarMenu>,
disabled: bool,
label: Option<Arc<str>>,
states: Vec<Name>,
rules: Recipe,
forced: Option<Interaction>,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl Menubar {
/// A menubar whose open menu is `open`'s: the index of the menu among the bar's, `None` while all are closed.
pub fn new(open: Signal<Option<usize>>) -> Self {
Self {
open,
menus: Vec::new(),
disabled: false,
label: None,
states: Vec::new(),
rules: Recipe::new(),
forced: None,
id: None,
key: None,
}
}
/// A menu, after the bar's others.
pub fn menu(mut self, menu: MenubarMenu) -> Self {
self.menus.push(menu);
self
}
/// Menus, in order, after the bar's others.
pub fn menus(mut self, menus: impl IntoIterator<Item = MenubarMenu>) -> Self {
self.menus.extend(menus);
self
}
/// The bar's name to what reads the tree: "Application", "Editor".
pub fn label(mut self, label: &str) -> Self {
self.label = Some(Arc::from(label));
self
}
/// Whether no menu of the bar can be opened.
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
/// Puts the bar in a state the theme's recipe names, or takes it out.
pub fn state(mut self, state: impl Into<Name>, on: bool) -> Self {
let state = state.into();
self.states.retain(|held| *held != state);
if on {
self.states.push(state);
}
self
}
/// Styles one part of this bar over what the theme gives it.
pub fn style(mut self, part: impl Into<Name>, style: impl FnOnce(super::PartStyle) -> super::PartStyle) -> Self {
self.rules = self.rules.base(part, style);
self
}
/// Lays rules over the theme's recipe for this bar.
pub fn rules(mut self, rules: &Recipe) -> Self {
self.rules = self.rules.extend(rules);
self
}
/// Draws the item Tab would stop on (the first that is not disabled) as it is under the pointer, held down or
/// focused from the keyboard, with nothing pointing at it: for a gallery or a test. The other items are drawn as
/// they are.
pub fn force(mut self, interaction: Option<Interaction>) -> Self {
self.forced = interaction;
self
}
/// The id of the bar. Its items are `{id}-0`, `{id}-1` and on, and the panel of each menu `{id}-0-menu`, but for
/// a menu that has an id of its own.
pub fn id(mut self, id: impl Into<Arc<str>>) -> Self {
self.id = Some(id.into());
self
}
pub fn key(mut self, key: impl Into<Arc<str>>) -> Self {
self.key = Some(key.into());
self
}
/// Builds the bar in `theme`. Converting a bar into an element does this in the theme of what is being rendered.
pub fn build(self, theme: &Theme) -> Element {
let case = Case {
variant: None,
size: None,
density: None,
breakpoint: super::current_breakpoint(),
states: self.states.clone(),
}
.state(MenubarState::Disabled, self.disabled);
let kept = kept(&self.open, self.menus.len());
let open = self.open.get();
let mnemonics =
!self.disabled && (kept.held.get() || (kept.stay.get() && (kept.bar.focus_visible_within() || open.is_some())));
let view = MenubarView {
menus: self.menus,
disabled: self.disabled,
label: self.label,
open: open.filter(|_| !self.disabled),
mnemonics,
recipe: theme
.resolve::<Menubar>(&case, Some(&self.rules))
.owned_by(self.id.as_ref()),
case,
rules: self.rules,
forced: self.forced,
kept,
id: self.id,
key: self.key,
};
match theme.renderer::<Menubar>() {
Some(renderer) => renderer(view),
None => view.render(),
}
}
}
impl From<Menubar> for Element {
fn from(bar: Menubar) -> Self {
bar.build(¤t_theme())
}
}
/// What a menubar keeps between builds, for the open signal it was given, while it is in the tree (`alive` is held
/// by its handlers): an open signal for each menu, which a [`Menu`] takes, kept as one with the app's, and the
/// anchor each menu hangs from; whether the menu opens at its last item; whether Alt shows the titles' letters; and
/// the bar, whose focus they follow.
struct Kept {
state: State,
/// What keeps each menu's signal and the app's one: the app's open menu follows a menu closing itself, and each
/// menu the app's.
_follows: Vec<WatchHandle<bool>>,
_leads: WatchHandle<Option<usize>>,
alive: Weak<()>,
}
/// What a bar keeps, as its builds and its handlers share it.
#[derive(Clone)]
struct State {
open: Signal<Option<usize>>,
menus: Arc<Mutex<Vec<(Signal<bool>, ElementRef)>>>,
/// The menu opens with the focus on its last item: it was opened with Up.
from_end: Arc<AtomicBool>,
/// Alt is down.
held: Signal<bool>,
/// Alt went down and up alone: the letters show while the bar or one of its menus has the keyboard.
stay: Signal<bool>,
/// Alt is down and nothing else was pressed since.
alone: Arc<AtomicBool>,
bar: ElementRef,
}
/// What a bar keeps, with what keeps it: the handlers of the bar built hold `alive`.
#[derive(Clone)]
struct Shared {
state: State,
alive: Arc<()>,
}
impl std::ops::Deref for Shared {
type Target = State;
fn deref(&self) -> &State {
&self.state
}
}
impl State {
/// The open signal and the anchor of the menu `index`.
fn menu(&self, index: usize) -> Option<(Signal<bool>, ElementRef)> {
self.menus.lock().unwrap().get(index).cloned()
}
/// Opens the menu `index`, at its last item where `from_end`.
fn open_at(&self, index: usize, from_end: bool) {
self.from_end.store(from_end, Ordering::Relaxed);
if self.open.get_untracked() != Some(index) {
self.open.set(Some(index));
}
}
}
static KEPT: LazyLock<Mutex<HashMap<usize, Kept>>> = LazyLock::new(Default::default);
/// What the bar of `open` keeps, with a signal and an anchor for each of its `count` menus.
fn kept(open: &Signal<Option<usize>>, count: usize) -> Shared {
let mut all = KEPT.lock().unwrap();
all.retain(|_, kept| kept.alive.strong_count() > 0);
let held = all
.get(&open.id())
.and_then(|kept| Some((kept.alive.upgrade()?, kept.state.clone())));
let (alive, state) = match held {
Some(held) => held,
None => {
let state = State {
open: open.clone(),
menus: Arc::new(Mutex::new(Vec::new())),
from_end: Arc::new(AtomicBool::new(false)),
held: Signal::new(false),
stay: Signal::new(false),
alone: Arc::new(AtomicBool::new(false)),
bar: ElementRef::new(),
};
// Each menu is open while the app's signal names it.
let (menus, from_end, app) = (state.menus.clone(), state.from_end.clone(), open.clone());
let leads = open.watch(move || {
let now = app.get_untracked();
if now.is_none() {
from_end.store(false, Ordering::Relaxed);
}
let menus = menus.lock().unwrap().clone();
for (index, (menu, _)) in menus.iter().enumerate() {
let want = now == Some(index);
if menu.get_untracked() != want {
menu.set(want);
}
}
});
let alive = Arc::new(());
all.insert(
open.id(),
Kept {
state: state.clone(),
_follows: Vec::new(),
_leads: leads,
alive: Arc::downgrade(&alive),
},
);
(alive, state)
}
};
let kept = all.get_mut(&open.id()).expect("kept above");
let mut menus = state.menus.lock().unwrap();
while menus.len() < count {
let index = menus.len();
let menu = Signal::new(open.get_untracked() == Some(index));
// A menu that closes itself (an item chosen, Escape, a press outside) closes the app's.
let (own, app) = (menu.clone(), open.clone());
kept._follows.push(menu.watch(move || {
let (on, now) = (own.get_untracked(), app.get_untracked());
if on && now != Some(index) {
app.set(Some(index));
} else if !on && now == Some(index) {
app.set(None);
}
}));
menus.push((menu, ElementRef::new()));
}
drop(menus);
Shared { state, alive }
}
/// A menubar as a renderer sees it: its menus, the style of its parts, and its behavior ready to be dressed.
pub struct MenubarView {
/// The bar's menus, in order. [`menu`](Self::menu) takes what one holds.
pub menus: Vec<MenubarMenu>,
/// Whether no menu of the bar can be opened.
pub disabled: bool,
/// The bar's name to what reads the tree, if the app gave it one.
pub label: Option<Arc<str>>,
/// The menu open now.
pub open: Option<usize>,
/// Whether the first letter of each title is underlined: Alt is down, or put the focus on the bar.
pub mnemonics: bool,
/// The style of each part of the bar, in the bar's states.
pub recipe: ResolvedRecipe,
case: Case,
rules: Recipe,
forced: Option<Interaction>,
kept: Shared,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl MenubarView {
/// The style of a part of the bar.
pub fn part(&self, part: impl Into<Name>) -> ResolvedPart {
self.recipe.part(part)
}
/// The title Tab stops on: the first that is not disabled. None where all are.
pub fn entry(&self) -> Option<usize> {
(0..self.menus.len()).find(|&index| !self.is_disabled(index))
}
pub fn theme(&self) -> &Theme {
self.recipe.theme()
}
/// Whether the menu `index` cannot be opened.
pub fn is_disabled(&self, index: usize) -> bool {
self.disabled || self.menus.get(index).is_some_and(|menu| menu.disabled)
}
/// The style of a part of the item `index`, in its states: open, disabled.
pub fn item_part(&self, index: usize, part: impl Into<Name>) -> ResolvedPart {
let case = self
.case
.clone()
.state(MenubarState::Open, self.open == Some(index))
.state(MenubarState::Disabled, self.is_disabled(index));
self
.theme()
.resolve::<Menubar>(&case, Some(&self.rules))
.owned_like(&self.recipe)
.item(
self.menus.get(index).and_then(|menu| menu.id.as_ref()),
MenubarPart::Item,
)
.part(part)
.forced(self.forced.filter(|_| self.entry() == Some(index)))
}
/// The anchor the menu `index` hangs from: its title. It is kept with the bar, so that the runtime knows the
/// menu for the one it was in the build before.
fn anchor(&self, index: usize) -> ElementRef {
self.kept.menu(index).map_or_else(ElementRef::new, |(_, anchor)| anchor)
}
/// The bar in the style of its part, with nothing in it yet: a menubar to what reads the tree, one stop of Tab
/// that the arrows move in, and a row of menus to the runtime, which hands an open menu to its neighbour with
/// Left and Right and takes Alt. It notes Alt going down and up, for the titles' letters.
pub fn bar(&self) -> Row {
let mut bar = Row::new()
.align_items(Alignment::Center)
.part(&self.part(MenubarPart::Bar))
.ref_element(self.kept.bar.clone())
.focus_group(
FocusGroup::horizontal()
.one_stop(true)
.typeahead(true)
.follow_pointer(true)
.menu_row(true),
)
.describe("a11y_role", "menubar");
if let Some(label) = &self.label {
bar = bar.describe("a11y_name", label.clone());
}
// The handlers hold what the bar keeps while it is in the tree, disabled or not.
{
let (down, up) = (self.kept.clone(), self.kept.clone());
bar = bar
.on_key_down(move |event: KeyboardEvent| {
let kept = &down;
if event.key == "Alt" && !event.ctrl && !event.meta {
kept.alone.store(true, Ordering::Relaxed);
if !kept.held.get_untracked() {
kept.held.set(true);
}
} else {
kept.alone.store(false, Ordering::Relaxed);
// A key with the keyboard away from the bar and its menus: the letters go.
if !event.focus_within && kept.open.get_untracked().is_none() && kept.stay.get_untracked() {
kept.stay.set(false);
}
}
})
.on_key_up(move |event: KeyboardEvent| {
let kept = &up;
if event.key != "Alt" {
return;
}
if kept.held.get_untracked() {
kept.held.set(false);
}
if kept.alone.swap(false, Ordering::Relaxed) && !kept.stay.get_untracked() {
kept.stay.set(true);
}
});
}
if let Some(id) = &self.id {
bar = bar.id(id.clone());
}
if let Some(key) = &self.key {
bar = bar.key(key.clone());
}
bar
}
/// The title `index`'s behavior with the style of its part, with nothing in it yet: a menu item that opens a
/// menu to what reads the tree, and the anchor its menu hangs from. A press opens its menu or closes it; the
/// pointer coming onto it while another menu is open opens its own; Down opens it at its first item, Up at its
/// last.
pub fn item(&self, index: usize) -> components::Button {
let Some(menu) = self.menus.get(index) else {
return components::Button::empty();
};
let disabled = self.is_disabled(index);
let open = self.open == Some(index);
let mut title = pressable(&self.item_part(index, MenubarPart::Item), disabled)
.ref_element(self.anchor(index))
.describe("a11y_role", "menuitem")
.describe("a11y_name", menu.title.clone())
.describe("a11y_haspopup", "menu")
.describe("a11y_expanded", if open { "true" } else { "false" })
// A letter typed on the bar goes to the title that starts with it.
.focus_label(menu.title.clone());
if !disabled {
let (press, enter, keys) = (self.kept.clone(), self.kept.clone(), self.kept.clone());
title = title
.on_click(move |_| {
let _alive = &press.alive;
if press.open.get_untracked() == Some(index) {
press.open.set(None);
} else {
press.open_at(index, false);
}
})
.on_mouse_enter(move || {
if enter.open.get_untracked().is_some_and(|open| open != index) {
enter.open_at(index, false);
}
})
.on_key_down(move |event: KeyboardEvent| {
if !event.focus_within || !plain(&event) {
return;
}
let from_end = match event.key.as_str() {
"ArrowDown" => false,
"ArrowUp" => true,
_ => return,
};
event.prevent_default();
keys.open_at(index, from_end);
});
}
match (&menu.id, &self.id) {
(Some(id), _) => title = title.id(id.clone()),
(None, Some(id)) => title = title.id(format!("{id}-{index}")),
(None, None) => {}
}
if let Some(key) = &menu.key {
title = title.key(key.clone());
}
title
}
/// The text of the title `index`, its first letter underlined while [`mnemonics`](Self::mnemonics) show.
pub fn label(&self, index: usize) -> Element {
let part = self.item_part(index, MenubarPart::Label);
let title = self.menus.get(index).map(|menu| menu.title.clone()).unwrap_or_default();
let mut letters = title.chars();
let first = letters.next();
let row = Row::new().part(&part);
match first {
Some(first) if self.mnemonics && !self.is_disabled(index) => row
.child(
part
.text(&first.to_string())
.nowrap()
.underline(TextUnderline::default()),
)
.child(part.text(letters.as_str()).nowrap())
.into(),
_ => row.child(part.text(&title).nowrap()).into(),
}
}
/// The menu `index`, hung from its title, open while the bar's open menu is it. It takes what the menu holds out
/// of the view: it is built once.
pub fn menu(&mut self, index: usize) -> Element {
let Some((signal, anchor)) = self.kept.menu(index) else {
return Row::new().into();
};
let Some(menu) = self.menus.get_mut(index) else {
return Row::new().into();
};
let mut entries = std::mem::take(&mut menu.entries);
// Opened with Up, the focus goes to its last item that can be chosen.
if self.open == Some(index) && self.kept.from_end.load(Ordering::Relaxed) {
entries = super::menu::autofocus_last(entries);
}
let mut built = Menu::new(signal, anchor);
for entry in entries {
built = built.entry(entry);
}
let titled = self.menus.get(index).map(|menu| menu.title.clone());
if let Some(title) = titled {
built = built.label(&title);
}
match (self.menus.get(index).and_then(|menu| menu.id.clone()), &self.id) {
(Some(id), _) => built = built.id(format!("{id}-menu")),
(None, Some(id)) => built = built.id(format!("{id}-{index}-menu")),
(None, None) => {}
}
built.build(self.theme())
}
/// How the default renderer builds a bar: its titles in a row, each with its label and its menu, which takes no
/// room, after it.
pub fn render(mut self) -> Element {
let mut bar = self.bar();
for index in 0..self.menus.len() {
let title = self.item(index).child(self.label(index));
let menu = self.menu(index);
bar = bar.child(Column::new().child(title).child(menu));
}
bar.into()
}
}
impl crate::ui::Styled for Menubar {
type Component = Self;
fn rules(self, rules: &crate::ui::Recipe) -> Self {
Self::rules(self, rules)
}
fn state(self, state: impl Into<crate::ui::Name>, on: bool) -> Self {
Self::state(self, state, on)
}
}
impl Themed for Menubar {
const NAME: &'static str = "menubar";
const ROOT: &'static str = "bar";
type Renderer = MenubarRenderer;
fn parts() -> crate::ui::Declared {
MenubarPart::declared()
}
fn states() -> crate::ui::Declared {
MenubarState::declared()
}
/// The design's menubar (`pencil/lurq-design-system.pen`, Menubar): a bar 36 tall of the background's color,
/// padded 4, with the medium radius and an edge of 1 in the border's color, its titles 2 apart. A title is 28
/// tall, padded 10 at the sides, with the small radius; its text is 13/500 in the foreground. The title whose menu
/// is open is filled with the highlight (the board's accent), as one under the pointer or focused from the
/// keyboard is; a disabled one is dimmed.
fn recipe() -> Recipe {
use MenubarPart::{Bar, Item, Label};
use PaletteColor::{Background, Foreground, Highlight};
Recipe::new()
.base(Bar, |s| {
s.height(36.0)
.padding_x(4.0)
.padding_y(4.0)
.gap(2.0)
.radius(RadiusSize::Md)
.background(Background)
.border(Border::inside(BorderSize::Sm, PaletteColor::Border))
})
.base(Item, |s| {
fading(
s.height(28.0)
.padding_x(10.0)
.radius(RadiusSize::Sm)
.cursor(CursorIcon::Pointer),
)
})
.base(Label, |s| {
s.typography(TypographyStyle::Body)
.font_size(13.0)
.weight(FontWeight::Medium)
.foreground(Foreground)
})
.hovered(Item, |s| s.background(Highlight))
.focus_visible(Item, |s| s.background(Highlight))
.state(MenubarState::Open, Item, |s| s.background(Highlight))
.state(MenubarState::Disabled, Item, |s| {
s.opacity(OpacityLevel::Disabled).cursor(CursorIcon::Default)
})
}
}