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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
//! Widget adapters for reusable menus.
//!
//! `ContextMenu` is a small controller that other widgets can embed, while
//! `MenuBar` is a visible widget for top-level menus.
use std::sync::Arc;
use crate::draw_ctx::DrawCtx;
use crate::event::{Event, EventResult, Key, Modifiers, MouseButton};
use crate::font_settings;
use crate::geometry::{Point, Rect, Size};
use crate::text::Font;
use crate::widget::{current_viewport, BackbufferCache, Widget};
use super::geometry::{contains, item_at_path, BAR_H};
use super::model::{MenuEntry, MenuSelection};
use super::paint::{
bar_button_text_color, paint_check_mark, paint_item_row_bg, paint_menu_bar_button_bg,
paint_panel, paint_separator, paint_submenu_chevron, MenuStyle,
};
use super::state::{MenuAnchorKind, MenuResponse, PopupMenuState};
use labels::{BarLabels, PopupLabels};
mod labels;
/// Layout direction for `MenuBar`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MenuOrientation {
Horizontal,
HorizontalBottom,
Vertical,
}
pub const VERTICAL_ROW_H: f64 = 36.0;
/// Mouse events synthesised from a touch tap arrive within a few
/// milliseconds of the corresponding `touchstart`/`touchend`. Allow a
/// generous window (50 ms) so a busy frame doesn't accidentally
/// classify a synthesised event as a desktop click.
const TOUCH_SYNTH_WINDOW_MS: u128 = 50;
fn is_touch_synthesized() -> bool {
crate::touch_state::last_touch_event_age()
.map(|d| d.as_millis() < TOUCH_SYNTH_WINDOW_MS)
.unwrap_or(false)
}
#[derive(Clone)]
pub struct PopupMenu {
pub items: Vec<MenuEntry>,
pub state: PopupMenuState,
pub style: MenuStyle,
/// Cached row labels for popup text and shortcuts.
labels: PopupLabels,
}
impl PopupMenu {
pub fn new(items: Vec<MenuEntry>) -> Self {
Self {
items,
state: PopupMenuState::default(),
style: MenuStyle::default(),
labels: PopupLabels::new(),
}
}
pub fn open_at(&mut self, pos: Point) {
self.state.open_at(pos, MenuAnchorKind::Context);
}
pub fn close(&mut self) {
self.state.close();
}
pub fn is_open(&self) -> bool {
self.state.open
}
pub fn take_suppress_mouse_up(&mut self) -> bool {
self.state.take_suppress_mouse_up()
}
pub fn handle_event(&mut self, event: &Event, viewport: Size) -> (EventResult, MenuResponse) {
self.state.handle_event(&mut self.items, event, viewport)
}
/// Return `true` if `pos` falls inside any of the popup's currently
/// laid-out panels (the open menu plus any nested submenus). Used
/// by `MenuBar` to detect a mouse-up in "neutral space" — outside
/// both the menu bar AND the popup body — so the bar can dismiss
/// the popup without waiting for a follow-up event.
pub fn body_contains(&self, pos: Point, viewport: Size) -> bool {
self.state
.layouts(&self.items, viewport)
.iter()
.any(|layout| {
pos.x >= layout.rect.x
&& pos.x <= layout.rect.x + layout.rect.width
&& pos.y >= layout.rect.y
&& pos.y <= layout.rect.y + layout.rect.height
})
}
pub fn handle_shortcut(&mut self, key: &Key, modifiers: Modifiers) -> MenuResponse {
self.state.handle_shortcut(&mut self.items, key, modifiers)
}
pub fn paint(
&mut self,
ctx: &mut dyn DrawCtx,
font: Arc<Font>,
font_size: f64,
viewport: Size,
) {
let layouts = self.state.layouts(&self.items, viewport);
// Refresh the per-row `Label` cache against the current open
// tree. Cheap when nothing changed — `sync_to` only mutates
// entries whose text or font differs from the cached state.
self.labels.sync_to(&font, font_size, &self.items, &layouts);
// Set the popup's default font / size on the ctx for the
// inline glyphs we still paint directly (icons, check / radio
// marks, submenu chevron). Label widgets push their own font
// through their internal `set_font` call so this only affects
// the inline glyphs.
ctx.set_font(Arc::clone(&font));
ctx.set_font_size(font_size);
for (level_idx, layout) in layouts.iter().enumerate() {
paint_panel(ctx, layout.rect, &self.style);
paint_popup_level(
ctx,
level_idx,
layout,
&self.items,
&self.state,
&self.style,
&mut self.labels,
);
}
}
}
pub struct MenuBar {
bounds: Rect,
children: Vec<Box<dyn Widget>>,
font: Arc<Font>,
font_size: f64,
menus: Vec<TopMenu>,
open_index: Option<usize>,
hover_index: Option<usize>,
popup: PopupMenu,
on_action: Box<dyn FnMut(&str)>,
/// Top-menu index whose hover highlight is suppressed until cursor exit.
suppress_hover_for: Option<usize>,
/// When `true`, [`Widget::layout`] returns the tight content width
/// (sum of menu-button widths) instead of the full available width.
/// Set via [`MenuBar::with_fit_width`] when the bar shares a FlexRow
/// with right-aligned chrome (e.g. project title, About button) and
/// shouldn't claim every spare pixel.
fit_width: bool,
orientation: MenuOrientation,
/// CPU backbuffer cache for the mostly-static bar pixels.
cache: BackbufferCache,
/// Cached labels for each top-menu bar button.
bar_labels: BarLabels,
}
pub struct TopMenu {
pub label: String,
pub items: Vec<MenuEntry>,
rect: Rect,
}
impl TopMenu {
pub fn new(label: impl Into<String>, items: Vec<MenuEntry>) -> Self {
Self {
label: label.into(),
items,
rect: Rect::default(),
}
}
}
impl MenuBar {
pub fn new(
font: Arc<Font>,
menus: Vec<TopMenu>,
on_action: impl FnMut(&str) + 'static,
) -> Self {
Self {
bounds: Rect::default(),
children: Vec::new(),
font,
font_size: 14.0,
menus,
open_index: None,
hover_index: None,
popup: PopupMenu::new(Vec::new()),
on_action: Box::new(on_action),
suppress_hover_for: None,
fit_width: false,
orientation: MenuOrientation::Horizontal,
cache: BackbufferCache::new(),
bar_labels: BarLabels::new(),
}
}
/// Refresh the per-button label cache to match `self.menus`.
fn sync_bar_labels(&mut self) {
let labels: Vec<&str> = self.menus.iter().map(|m| m.label.as_str()).collect();
self.bar_labels
.sync_to(&self.active_font(), self.font_size, &labels);
}
/// Use a vertical layout — the bar stacks its menu buttons top-to-
/// bottom (Y-up: highest local Y first) and opens popups to the
/// RIGHT of each button. Intended for narrow, tall chrome strips
/// such as a left-side mobile sidebar.
pub fn with_orientation(mut self, orientation: MenuOrientation) -> Self {
self.orientation = orientation;
self
}
/// Opt into tight-width sizing — `Widget::layout` will report the
/// summed menu-button width rather than the full available width.
/// Use when the MenuBar is hosted inside a `FlexRow` with sibling
/// chrome on the right (project title, status indicators, etc.)
/// that needs to share the same row.
pub fn with_fit_width(mut self, fit: bool) -> Self {
self.fit_width = fit;
self
}
pub fn with_font_size(mut self, font_size: f64) -> Self {
self.font_size = font_size;
self
}
/// Override the popup's [`MenuStyle`] — geometry and inline-glyph
/// characters (submenu chevron, check mark, radio mark). Hosts
/// that bundle Font Awesome typically swap the default Unicode
/// chars for FA equivalents so the menu indicators visually match
/// the icons used everywhere else.
pub fn with_menu_style(mut self, style: MenuStyle) -> Self {
self.popup.style = style;
self.cache.invalidate();
self
}
/// Replace the bar's top-level menu list at runtime. Used by callers
/// that derive menu contents from app state (e.g. radio-style theme
/// pickers) and need to refresh the items each frame so the popup's
/// check/radio marks reflect the canonical state. Invalidates the
/// backbuffer cache so the next paint re-rasters bar labels.
pub fn set_menus(&mut self, menus: Vec<TopMenu>) {
self.menus = menus;
self.cache.invalidate();
}
/// Read-only access to the configured top-level menus. Mainly for
/// tests that need to inspect labels / items without going through
/// the popup state machine.
pub fn menus(&self) -> &[TopMenu] {
&self.menus
}
/// Resolve the font used for layout/paint. Prefers the system-wide
/// font override so the System window's font picker propagates live;
/// falls back to the per-instance font otherwise. Mirrors the
/// `Label::active_font` pattern.
fn active_font(&self) -> Arc<Font> {
font_settings::current_system_font().unwrap_or_else(|| Arc::clone(&self.font))
}
fn menu_at(&self, pos: Point) -> Option<usize> {
self.menus.iter().position(|menu| contains(menu.rect, pos))
}
fn open_menu(&mut self, idx: usize) {
let rect = self.menus[idx].rect;
self.popup.items = self.menus[idx].items.clone();
// Horizontal: anchor at the BAR'S bottom-left (rect.x, rect.y)
// — popup opens straight DOWN under the bar item, allowed to
// extend off-bar via the `Bar` kind's negative-y clamp.
//
// Vertical: anchor at the BUTTON'S top-right corner — popup
// opens to the RIGHT of the button with its top aligned to the
// button's top. `Context` kind clamps the popup inside the
// viewport so we don't trail off the top.
let (anchor, kind) = match self.orientation {
MenuOrientation::Horizontal => (Point::new(rect.x, rect.y), MenuAnchorKind::Bar),
// Anchor at the bar item's TOP edge so the popup
// rises FROM it instead of hanging below.
MenuOrientation::HorizontalBottom => (
Point::new(rect.x, rect.y + rect.height),
MenuAnchorKind::BottomBar,
),
MenuOrientation::Vertical => (
Point::new(rect.x + rect.width, rect.y + rect.height),
MenuAnchorKind::Context,
),
};
self.popup.state.open_at(anchor, kind);
self.open_index = Some(idx);
self.hover_index = Some(idx);
self.cache.invalidate();
crate::animation::request_draw();
}
fn open_menu_for_drag_release(&mut self, idx: usize) {
self.open_menu(idx);
self.popup.state.arm_mouse_up_activation();
}
fn switch_open_menu(&mut self, delta: isize) -> EventResult {
let Some(current) = self.open_index else {
return EventResult::Ignored;
};
if self.menus.is_empty() {
return EventResult::Ignored;
}
let len = self.menus.len() as isize;
let next = (current as isize + delta).rem_euclid(len) as usize;
self.open_menu(next);
EventResult::Consumed
}
fn should_switch_top_menu(&self, key: &Key) -> bool {
match key {
Key::ArrowLeft => self.popup.state.open_path.is_empty(),
Key::ArrowRight => {
if !self.popup.state.open_path.is_empty() {
return false;
}
self.popup
.state
.hover_path
.as_deref()
.and_then(|path| item_at_path(&self.popup.items, path))
.map_or(true, |item| !item.has_submenu())
}
_ => false,
}
}
fn set_hover_index(&mut self, hover: Option<usize>) {
// Touch devices have no real cursor; the synth-MouseMove fired
// alongside a touchstart would otherwise paint a hover panel that
// sticks after the tap (no MouseMove ever leaves the bar to clear
// it). Coerce hover to `None` for any input within the touch-synth
// window so a tap-to-open / tap-to-close cycle leaves no residue.
let hover = if is_touch_synthesized() { None } else { hover };
if self.hover_index != hover {
self.hover_index = hover;
// `request_draw()` (NOT `_without_invalidation`) — the bar's
// hover paint lives inside the parent Window's retained
// backbuffer, so the cache must invalidate or the next paint
// composites a stale bitmap. The epoch bump in `request_draw`
// is what `dispatch_event` reads to mark the ancestor path
// dirty even when this MouseMove returns `Ignored`.
crate::animation::request_draw();
// The bar itself is backbuffered too — invalidate so the
// next paint re-rasterises the hover-tinted bar item.
self.cache.invalidate();
}
// Cursor moved to a different top-menu (or off any) — clear
// the post-close hover suppression so the next genuine hover
// re-enters with the usual highlight.
if self.suppress_hover_for != hover {
self.suppress_hover_for = None;
self.cache.invalidate();
}
}
}
impl Widget for MenuBar {
fn type_name(&self) -> &'static str {
"MenuBar"
}
fn bounds(&self) -> Rect {
self.bounds
}
fn set_bounds(&mut self, bounds: Rect) {
if (bounds.width - self.bounds.width).abs() > 0.5
|| (bounds.height - self.bounds.height).abs() > 0.5
{
self.cache.invalidate();
}
self.bounds = bounds;
}
fn children(&self) -> &[Box<dyn Widget>] {
&self.children
}
fn children_mut(&mut self) -> &mut Vec<Box<dyn Widget>> {
&mut self.children
}
fn backbuffer_cache_mut(&mut self) -> Option<&mut BackbufferCache> {
Some(&mut self.cache)
}
fn backbuffer_mode(&self) -> crate::widget::BackbufferMode {
// Mirror Label: when the global LCD toggle is on (which the
// default rule wires to "scale ≤ 1.25"), use the per-channel LCD
// coverage cache so text on the bar is subpixel-rendered exactly
// like Label text. Falls back to RGBA at HiDPI where LCD gains
// nothing. MenuBar paints `top_bar_bg` as an opaque full-width
// fill before any other content, satisfying LcdCoverage's
// "widget must cover its bounds with opaque content" contract.
if crate::font_settings::lcd_enabled() {
crate::widget::BackbufferMode::LcdCoverage
} else {
crate::widget::BackbufferMode::Rgba
}
}
fn layout(&mut self, available: Size) -> Size {
// Keep the bar's Label cache in lock-step with `self.menus`
// before measuring — handles dynamic menu lists.
self.sync_bar_labels();
match self.orientation {
MenuOrientation::Horizontal | MenuOrientation::HorizontalBottom => {
let mut x = 0.0;
for menu in &mut self.menus {
let width = (menu.label.chars().count() as f64 * 8.0 + 22.0).max(52.0);
menu.rect = Rect::new(x, 0.0, width, BAR_H);
x += width;
}
// Fit-width mode leaves room for sibling chrome.
let report_w = if self.fit_width { x } else { available.width };
Size::new(report_w, BAR_H)
}
MenuOrientation::Vertical => {
// Stack top-to-bottom in Y-up coordinates.
let mut y = available.height;
for menu in &mut self.menus {
y -= VERTICAL_ROW_H;
menu.rect = Rect::new(0.0, y, available.width, VERTICAL_ROW_H);
}
let used_h = self.menus.len() as f64 * VERTICAL_ROW_H;
Size::new(available.width, used_h)
}
}
}
fn paint(&mut self, ctx: &mut dyn DrawCtx) {
// Re-sync in case paint runs without a preceding layout.
self.sync_bar_labels();
ctx.set_font(self.active_font());
ctx.set_font_size(self.font_size);
let v = ctx.visuals();
ctx.set_fill_color(v.top_bar_bg);
ctx.begin_path();
let bg_h = match self.orientation {
MenuOrientation::Horizontal | MenuOrientation::HorizontalBottom => BAR_H,
MenuOrientation::Vertical => self.bounds.height,
};
ctx.rect(0.0, 0.0, self.bounds.width, bg_h);
ctx.fill();
// First pass: button chrome under the text.
for (idx, menu) in self.menus.iter().enumerate() {
// After a click-to-close-toggle, the cursor is still over
// the bar item so `hover_index` still points at it —
// suppress the hover highlight until the cursor moves off
// and back on, so the closed menu doesn't read as "still
// selected".
let hovered = self.hover_index == Some(idx) && self.suppress_hover_for != Some(idx);
let open = self.open_index == Some(idx);
paint_menu_bar_button_bg(ctx, menu.rect, open, hovered);
}
// Second pass: paint each button's `Label` through
// `paint_subtree` so glyphs flow through Label's backbuffer +
// LCD path. Done after backgrounds so the text composites on
// top of the hover fill.
let menu_rects: Vec<(Rect, bool)> = self
.menus
.iter()
.enumerate()
.map(|(idx, menu)| (menu.rect, self.open_index == Some(idx)))
.collect();
for (idx, (rect, open)) in menu_rects.into_iter().enumerate() {
let color = bar_button_text_color(ctx, open);
self.bar_labels.paint_in(ctx, idx, rect, color);
}
}
fn hit_test_global_overlay(&self, _local_pos: Point) -> bool {
self.popup.is_open()
}
fn has_active_modal(&self) -> bool {
self.popup.is_open()
}
fn on_event(&mut self, event: &Event) -> EventResult {
if let Event::MouseMove { pos } = event {
let hovered = self.menu_at(*pos);
self.set_hover_index(hovered);
// Hover-switch a different open menu while a popup is open.
// Suppressed when this MouseMove was synthesised by the
// touch shell from a touchstart — on mobile the synth move
// arrives at the tap position immediately followed by a
// synth MouseDown at the same point; switching the open
// menu here would make that MouseDown look like a click on
// the currently-open menu and toggle-close the popup the
// user just tapped to open. On desktop the
// `last_touch_event_age` is `None` (or very large), so
// hover-switch works as before.
let from_touch = is_touch_synthesized();
if self.popup.is_open() && !from_touch {
if let Some(idx) = hovered {
if self.open_index != Some(idx) {
let activate_on_release = self.popup.state.is_mouse_up_activation_armed();
self.open_menu(idx);
if activate_on_release {
self.popup.state.arm_mouse_up_activation();
}
}
return EventResult::Consumed;
}
}
}
if self.popup.is_open() {
if let Event::KeyDown { key, .. } = event {
if self.should_switch_top_menu(key) {
return match key {
Key::ArrowLeft => self.switch_open_menu(-1),
Key::ArrowRight => self.switch_open_menu(1),
_ => EventResult::Ignored,
};
}
}
// Tap-to-switch: when one menu is already open and a
// MouseDown lands on a DIFFERENT top menu's bar, switch
// directly. Without this, the popup handler would see the
// MouseDown as outside-the-popup-body and close the menu,
// leaving the user staring at an empty bar. Clicking the
// currently-open menu falls through to the popup so it can
// close (toggle, the desktop convention).
if let Event::MouseDown {
pos,
button: MouseButton::Left,
..
} = event
{
if let Some(idx) = self.menu_at(*pos) {
if self.open_index != Some(idx) {
self.open_menu(idx);
return EventResult::Consumed;
}
}
}
// Drag-release in neutral space cancels. The user pressed
// a top menu, dragged off both the bar and the popup body,
// and let go — the standard menu convention is to dismiss.
// The popup state's drag-release handler treats outside-
// popup-body as a no-op (so a mouse-up still on the bar
// doesn't close), so the bar enforces the cancel here
// since only the bar knows where its own top-menu rects
// live.
if let Event::MouseUp {
pos,
button: MouseButton::Left,
..
} = event
{
if self.popup.state.is_mouse_up_activation_armed()
&& self.menu_at(*pos).is_none()
&& !self.popup.body_contains(*pos, current_viewport())
{
self.popup.close();
self.open_index = None;
self.cache.invalidate();
crate::animation::request_draw();
return EventResult::Consumed;
}
}
let (result, response) = self.popup.handle_event(event, current_viewport());
if let MenuResponse::Action(action) = response {
if let Some(idx) = self.open_index {
self.menus[idx].items = self.popup.items.clone();
}
(self.on_action)(&action);
if !self.popup.is_open() {
self.open_index = None;
self.cache.invalidate();
}
} else if matches!(response, MenuResponse::Closed) {
self.open_index = None;
// Suppress the hover highlight on the menu the cursor
// is still over — without this, click-to-close-toggle
// leaves the bar item painted in the hover tint and
// reads as "still selected". Cleared once the cursor
// moves to a different top-menu (or off the bar).
self.suppress_hover_for = self.hover_index;
self.cache.invalidate();
}
if result == EventResult::Consumed {
return result;
}
}
match event {
Event::MouseDown {
pos,
button: MouseButton::Left,
..
} => {
if let Some(idx) = self.menu_at(*pos) {
self.open_menu_for_drag_release(idx);
EventResult::Consumed
} else {
EventResult::Ignored
}
}
Event::MouseMove { .. } => EventResult::Ignored,
_ => EventResult::Ignored,
}
}
fn on_unconsumed_key(&mut self, key: &Key, modifiers: Modifiers) -> EventResult {
let response = if self.popup.is_open() {
self.popup.handle_shortcut(key, modifiers)
} else {
self.menus
.iter_mut()
.find_map(|menu| {
let mut popup = PopupMenu::new(menu.items.clone());
match popup.handle_shortcut(key, modifiers) {
MenuResponse::Action(action) => {
menu.items = popup.items;
Some(action)
}
MenuResponse::None | MenuResponse::Closed => None,
}
})
.map(MenuResponse::Action)
.unwrap_or(MenuResponse::None)
};
if let MenuResponse::Action(action) = response {
if let Some(idx) = self.open_index {
self.menus[idx].items = self.popup.items.clone();
}
(self.on_action)(&action);
if !self.popup.is_open() {
self.open_index = None;
self.cache.invalidate();
}
EventResult::Consumed
} else {
EventResult::Ignored
}
}
fn paint_global_overlay(&mut self, ctx: &mut dyn DrawCtx) {
let font = self.active_font();
self.popup
.paint(ctx, font, self.font_size, current_viewport());
}
}
/// Paint one popup panel: hover backgrounds, separators, icons,
/// check / radio marks, submenu chevrons, AND each row's text via the
/// shared `PopupLabels` cache. Text colour is resolved per-row from
/// the item's enabled / open / hovered state so a hover flip retints
/// just one Label.
fn paint_popup_level(
ctx: &mut dyn DrawCtx,
level_idx: usize,
layout: &super::geometry::PopupLayout,
items: &[MenuEntry],
state: &PopupMenuState,
style: &MenuStyle,
labels: &mut PopupLabels,
) {
let level_items = items_for_layout(items, &layout.path_prefix);
for (row_idx, row_layout) in layout.rows.iter().enumerate() {
let Some(item_idx) = row_layout.item_index else {
// Separator row.
paint_separator(ctx, row_layout.rect);
continue;
};
let Some(MenuEntry::Item(item)) = level_items.get(item_idx) else {
continue;
};
let mut path = layout.path_prefix.clone();
path.push(item_idx);
let hovered = state.hover_path.as_ref() == Some(&path);
let open = state.open_path.starts_with(&path);
// Hover / open backdrop sits underneath the row's content.
paint_item_row_bg(ctx, row_layout.rect, hovered, open, item.enabled);
// Inline glyphs (icon / check / radio) — single chars that
// change infrequently; keeping them direct `fill_text` avoids
// a Label per glyph at the cost of a single rasterise call.
let inline_color =
super::paint::popup_row_text_color(ctx, item.enabled, open && item.enabled);
ctx.set_fill_color(inline_color);
if let Some(color) = item.swatch {
// Colour-swatch row (e.g., an accent palette). A small
// rounded filled rect in the icon slot, anchored
// vertically to the row centre. The selected radio gets a
// thin stroke around the swatch instead of the usual radio
// glyph so the colour itself stays the dominant cue.
let size = 12.0;
let sx = row_layout.rect.x + style.icon_x - size * 0.5 + 4.0;
let sy = row_layout.rect.y + (row_layout.rect.height - size) * 0.5;
let fill = if item.enabled {
color
} else {
// Wash out disabled swatches so they read as "not
// pickable" without losing their identity.
color.with_alpha(0.45)
};
ctx.set_fill_color(fill);
ctx.begin_path();
ctx.rounded_rect(sx, sy, size, size, 3.0);
ctx.fill();
if matches!(item.selection, MenuSelection::Radio { selected: true }) {
ctx.set_stroke_color(inline_color);
ctx.set_line_width(1.5);
ctx.begin_path();
ctx.rounded_rect(sx - 2.0, sy - 2.0, size + 4.0, size + 4.0, 4.0);
ctx.stroke();
}
} else if let Some(icon) = item.icon {
let icon = icon.to_string();
ctx.fill_text(
&icon,
row_layout.rect.x + style.icon_x,
row_layout.rect.y + 7.0,
);
}
// Selection indicator. Vector-drawn check mark shared by both
// Check and Radio rows so the indicator looks identical
// regardless of the host's icon font (or lack of one). When
// the row already has a left-side marker (icon or swatch),
// the check paints at the right edge so the marker's
// identity stays visible; otherwise it occupies the icon
// slot.
let selected = matches!(
item.selection,
MenuSelection::Check { selected: true } | MenuSelection::Radio { selected: true }
);
if selected {
let has_left_marker = item.swatch.is_some() || item.icon.is_some();
let cx = if has_left_marker {
let right_offset = if item.has_submenu() { 30.0 } else { 12.0 };
row_layout.rect.x + row_layout.rect.width - right_offset
} else {
row_layout.rect.x + style.icon_x
};
let cy = row_layout.rect.y + row_layout.rect.height * 0.5;
paint_check_mark(ctx, cx, cy, inline_color);
}
if item.has_submenu() {
paint_submenu_chevron(ctx, row_layout.rect, inline_color);
}
// Row text (label + shortcut) via the Label cache so glyph
// rendering routes through the backbuffer + LCD path.
labels.paint_row_with_state(
ctx,
level_idx,
row_idx,
row_layout.rect,
style.label_x,
style.shortcut_right,
item.enabled,
open && item.enabled,
);
}
}
fn items_for_layout<'a>(items: &'a [MenuEntry], path: &[usize]) -> &'a [MenuEntry] {
let mut current = items;
for &idx in path {
let Some(MenuEntry::Item(item)) = current.get(idx) else {
return current;
};
current = &item.submenu;
}
current
}
#[cfg(test)]
mod tests_1;
#[cfg(test)]
mod tests_2;