tuile 0.3.0

Textual-grade widgets, design system, animation and app runtime on top of ratatui
Documentation
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
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
//! Menu bar with dropdowns, submenus, keyboard navigation, and context menus. Overlay rendering
//! for dropdowns positioned with `popup_below`. Supports action items, separators, checkmarks,
//! shortcuts, and disabled items.
//!
//! ```
//! use tuile::prelude::*;
//! # let area = Rect::new(0, 0, 40, 10);
//! # let mut buf = Buffer::empty(area);
//! let menus = vec![
//!     MenuDef { title: "File".into(), items: vec![MenuItem::action(0, "New")] },
//! ];
//! let mut state = MenuBarState::new();
//! MenuBar::new(menus).render(area, &mut buf, &mut state);
//! ```

use crossterm::event::{KeyCode, KeyEvent, MouseButton, MouseEvent, MouseEventKind};
use ratatui_core::buffer::Buffer;
use ratatui_core::layout::{Position, Rect};
use ratatui_core::widgets::StatefulWidget;
use unicode_width::UnicodeWidthStr;

use crate::core::{Interactive, MinSize, Outcome, is_press, mouse_pos};
use crate::draw::{self, Border, fill, put, st};
use crate::layout::popup_below;
use crate::theme::{self, Theme};

// menu items

/// One menu item: action, separator, or submenu.
#[derive(Clone, Debug)]
pub enum MenuItem {
    Action {
        id: usize,
        label: String,
        shortcut: Option<String>,
        disabled: bool,
        checked: Option<bool>,
    },
    Separator,
    Submenu {
        label: String,
        items: Vec<MenuItem>,
    },
}

impl MenuItem {
    pub fn action(id: usize, label: impl Into<String>) -> Self {
        Self::Action {
            id,
            label: label.into(),
            shortcut: None,
            disabled: false,
            checked: None,
        }
    }

    pub fn shortcut(mut self, s: &str) -> Self {
        if let Self::Action { shortcut, .. } = &mut self {
            *shortcut = Some(s.to_string());
        }
        self
    }

    pub fn checked(mut self, c: bool) -> Self {
        if let Self::Action { checked, .. } = &mut self {
            *checked = Some(c);
        }
        self
    }

    pub fn disabled(mut self, d: bool) -> Self {
        if let Self::Action { disabled, .. } = &mut self {
            *disabled = d;
        }
        self
    }

    pub fn separator() -> Self {
        Self::Separator
    }

    pub fn submenu(label: impl Into<String>, items: Vec<MenuItem>) -> Self {
        Self::Submenu {
            label: label.into(),
            items,
        }
    }
}

/// One menu definition: title + items.
#[derive(Clone, Debug)]
pub struct MenuDef {
    pub title: String,
    pub items: Vec<MenuItem>,
}

// MenuBar

/// Menu bar widget with dropdowns and submenus.
#[derive(Clone, Debug)]
pub struct MenuBar {
    menus: Vec<MenuDef>,
    focused: bool,
    enabled: bool,
    theme: Option<Theme>,
}

impl MenuBar {
    pub fn new(menus: Vec<MenuDef>) -> Self {
        Self {
            menus,
            focused: false,
            enabled: true,
            theme: None,
        }
    }

    pub fn focused(mut self, f: bool) -> Self {
        self.focused = f;
        self
    }

    pub fn enabled(mut self, e: bool) -> Self {
        self.enabled = e;
        self
    }

    pub fn theme(mut self, t: &Theme) -> Self {
        self.theme = Some(*t);
        self
    }
}

impl MinSize for MenuBar {
    /// Menu bar is always 1 row.
    fn min_size(&self) -> (u16, u16) {
        (8, 1)
    }
}

// MenuBarState

/// Menu bar state: open menu, highlighted items, action, hit boxes.
#[derive(Clone, Debug, Default)]
pub struct MenuBarState {
    pub open: Option<usize>,
    pub highlight: Option<usize>,
    pub sub_open: Option<usize>,
    pub sub_highlight: Option<usize>,
    pub title_hits: Vec<Rect>,
    pub item_hits: Vec<(usize, Rect)>,
    pub sub_hits: Vec<(usize, Rect)>,
    pub action: Option<usize>,
    dropdown_area: Rect,
    sub_area: Rect,
    bounds: Rect,
    theme: Option<Theme>,
}

impl MenuBarState {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn is_open(&self) -> bool {
        self.open.is_some()
    }

    pub fn close(&mut self) {
        self.open = None;
        self.highlight = None;
        self.sub_open = None;
        self.sub_highlight = None;
    }

    pub fn open(&mut self, i: usize) {
        self.open = Some(i);
        self.highlight = None;
        self.sub_open = None;
        self.sub_highlight = None;
    }

    pub fn take_action(&mut self) -> Option<usize> {
        self.action.take()
    }

    /// Render dropdown overlays (call after main content, before other overlays).
    pub fn render_overlay(&self, buf: &mut Buffer, _bounds: Rect) {
        if self.open.is_none() {
            return;
        }
        let th = self.theme.unwrap_or_else(theme::current);

        // main dropdown
        if self.dropdown_area.width > 0 && self.dropdown_area.height > 0 {
            render_dropdown(
                buf,
                self.dropdown_area,
                &self.item_hits,
                self.highlight,
                self.sub_open,
                &th,
            );
        }

        // submenu dropdown
        if self.sub_open.is_some() && self.sub_area.width > 0 && self.sub_area.height > 0 {
            render_dropdown(
                buf,
                self.sub_area,
                &self.sub_hits,
                self.sub_highlight,
                None,
                &th,
            );
        }
    }
}

impl Interactive for MenuBarState {
    fn handle_key(&mut self, k: KeyEvent) -> Outcome {
        if !is_press(&k) {
            return Outcome::Ignored;
        }

        if self.open.is_none() {
            return Outcome::Ignored;
        }

        match k.code {
            KeyCode::Left => {
                if let Some(menu_idx) = self.open {
                    let new_idx = if menu_idx > 0 { menu_idx - 1 } else { 0 };
                    self.open(new_idx);
                    return Outcome::Changed;
                }
            }
            KeyCode::Right => {
                if let Some(menu_idx) = self.open {
                    self.open(menu_idx + 1);
                    return Outcome::Changed;
                }
                // or open submenu
                if self.sub_open.is_none() && self.highlight.is_some() {
                    self.sub_open = self.highlight;
                    self.sub_highlight = None;
                    return Outcome::Changed;
                }
            }
            KeyCode::Up => {
                if self.sub_open.is_some() {
                    self.sub_highlight =
                        self.sub_highlight.map(|h| h.saturating_sub(1)).or(Some(0));
                } else {
                    self.highlight = self.highlight.map(|h| h.saturating_sub(1)).or(Some(0));
                }
                return Outcome::Consumed;
            }
            KeyCode::Down => {
                if self.sub_open.is_some() {
                    self.sub_highlight = Some(self.sub_highlight.map_or(0, |h| h + 1));
                } else {
                    self.highlight = Some(self.highlight.map_or(0, |h| h + 1));
                }
                return Outcome::Consumed;
            }
            KeyCode::Enter | KeyCode::Char(' ') => {
                // activate highlighted item
                if self.sub_open.is_some() {
                    if let Some(h) = self.sub_highlight
                        && let Some((id, _)) = self.sub_hits.get(h)
                    {
                        self.action = Some(*id);
                        self.close();
                        return Outcome::Submitted;
                    }
                } else {
                    if let Some(h) = self.highlight {
                        // check if submenu
                        if self.sub_open.is_none() {
                            self.sub_open = Some(h);
                            self.sub_highlight = None;
                            return Outcome::Changed;
                        } else {
                            if let Some((id, _)) = self.item_hits.get(h) {
                                self.action = Some(*id);
                                self.close();
                                return Outcome::Submitted;
                            }
                        }
                    }
                }
            }
            KeyCode::Esc => {
                if self.sub_open.is_some() {
                    self.sub_open = None;
                    self.sub_highlight = None;
                    return Outcome::Changed;
                } else {
                    self.close();
                    return Outcome::Changed;
                }
            }
            _ => return Outcome::Ignored,
        }
        Outcome::Ignored
    }

    fn handle_mouse(&mut self, m: MouseEvent) -> Outcome {
        let pos = mouse_pos(&m);
        let mut out = Outcome::Ignored;

        // hover title
        if self.open.is_none() || matches!(m.kind, MouseEventKind::Moved) {
            for (i, r) in self.title_hits.iter().enumerate() {
                if r.contains(pos) {
                    if self.open.is_some() && self.open != Some(i) {
                        self.open(i);
                        return Outcome::Changed;
                    }
                    break;
                }
            }
        }

        // click title
        if matches!(m.kind, MouseEventKind::Down(MouseButton::Left)) {
            for (i, r) in self.title_hits.iter().enumerate() {
                if r.contains(pos) {
                    if self.open == Some(i) {
                        self.close();
                    } else {
                        self.open(i);
                    }
                    return Outcome::Changed;
                }
            }

            // click outside closes
            if !self.dropdown_area.contains(pos)
                && !self.sub_area.contains(pos)
                && self.open.is_some()
            {
                self.close();
                return Outcome::Changed;
            }
        }

        // hover dropdown items
        if self.open.is_some() {
            let mut new_hl = self.highlight;
            let mut new_sub_hl = self.sub_highlight;

            for (i, r) in &self.item_hits {
                if r.contains(pos) {
                    new_hl = Some(*i);
                    new_sub_hl = None;
                    break;
                }
            }

            for (i, r) in &self.sub_hits {
                if r.contains(pos) {
                    new_sub_hl = Some(*i);
                    break;
                }
            }

            if new_hl != self.highlight {
                self.highlight = new_hl;
                out = Outcome::Consumed;
            }
            if new_sub_hl != self.sub_highlight {
                self.sub_highlight = new_sub_hl;
                out = Outcome::Consumed;
            }

            // click item
            if matches!(m.kind, MouseEventKind::Down(MouseButton::Left)) {
                if let Some(h) = self.sub_highlight {
                    if let Some((id, _)) = self.sub_hits.get(h) {
                        self.action = Some(*id);
                        self.close();
                        return Outcome::Changed;
                    }
                } else if let Some(h) = self.highlight
                    && let Some((id, _)) = self.item_hits.get(h)
                {
                    self.action = Some(*id);
                    self.close();
                    return Outcome::Changed;
                }
            }
        }

        out
    }
}

impl StatefulWidget for MenuBar {
    type State = MenuBarState;

    fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
        if area.width == 0 || area.height == 0 {
            state.title_hits.clear();
            state.item_hits.clear();
            state.sub_hits.clear();
            return;
        }

        let th = self.theme.unwrap_or_else(theme::current);
        if draw::refuse(buf, area, self.min_size(), th.text_disabled) {
            state.title_hits.clear();
            state.item_hits.clear();
            state.sub_hits.clear();
            return;
        }

        let bg = th.panel;
        fill(buf, area, bg);

        state.title_hits.clear();
        state.item_hits.clear();
        state.sub_hits.clear();
        state.bounds = buf.area;
        state.theme = Some(th);

        let mut x = area.x;

        for (i, menu) in self.menus.iter().enumerate() {
            let is_open = state.open == Some(i);
            let title = format!(" {} ", menu.title);
            let w = title.width() as u16;

            let (fg, title_bg) = if is_open {
                (th.cursor_fg, th.primary)
            } else {
                (th.text, bg)
            };

            fill(buf, Rect::new(x, area.y, w, 1), title_bg);
            put(buf, x, area.y, &title, w, st(fg, title_bg));
            state.title_hits.push(Rect::new(x, area.y, w, 1));

            // compute dropdown
            if is_open {
                compute_dropdown(menu, Rect::new(x, area.y, w, 1), state, &th, buf.area);
            }

            x += w;
        }
    }
}

fn compute_dropdown(
    menu: &MenuDef,
    anchor: Rect,
    state: &mut MenuBarState,
    _th: &Theme,
    bounds: Rect,
) {
    state.item_hits.clear();
    state.sub_hits.clear();

    // compute dropdown size
    let mut max_w = 0u16;
    let mut h = 0u16;
    for item in &menu.items {
        match item {
            MenuItem::Action {
                label, shortcut, ..
            } => {
                let mut w = label.width() as u16 + 4; // "  label "
                if shortcut.is_some() {
                    w += 10;
                }
                max_w = max_w.max(w);
                h += 1;
            }
            MenuItem::Separator => {
                h += 1;
            }
            MenuItem::Submenu { label, .. } => {
                let w = label.width() as u16 + 6; // "  label  › "
                max_w = max_w.max(w);
                h += 1;
            }
        }
    }

    let dropdown_w = max_w.max(12);
    let dropdown_h = h.max(1);

    let dropdown = popup_below(anchor, dropdown_w, dropdown_h, bounds);
    state.dropdown_area = dropdown;

    // compute submenu if open
    if let Some(sub_idx) = state.sub_open {
        if let Some(MenuItem::Submenu { items, .. }) = menu.items.get(sub_idx) {
            let mut sub_max_w = 0u16;
            let mut sub_h = 0u16;
            for item in items {
                match item {
                    MenuItem::Action {
                        label, shortcut, ..
                    } => {
                        let mut w = label.width() as u16 + 4;
                        if shortcut.is_some() {
                            w += 10;
                        }
                        sub_max_w = sub_max_w.max(w);
                        sub_h += 1;
                    }
                    MenuItem::Separator => {
                        sub_h += 1;
                    }
                    _ => {}
                }
            }

            let sub_w = sub_max_w.max(12);
            let _sub_anchor = Rect::new(dropdown.right(), dropdown.y + sub_idx as u16, 1, 1);
            // flip left if no room
            let sub_rect = if dropdown.right() + sub_w <= bounds.right() {
                Rect::new(dropdown.right(), dropdown.y + sub_idx as u16, sub_w, sub_h)
            } else {
                Rect::new(
                    dropdown.x.saturating_sub(sub_w),
                    dropdown.y + sub_idx as u16,
                    sub_w,
                    sub_h,
                )
            };
            state.sub_area = sub_rect;
        }
    } else {
        state.sub_area = Rect::ZERO;
    }
}

fn render_dropdown(
    buf: &mut Buffer,
    area: Rect,
    hits: &[(usize, Rect)],
    highlight: Option<usize>,
    sub_open: Option<usize>,
    th: &Theme,
) {
    if area.width < 2 || area.height < 2 {
        return;
    }

    let bg = th.surface;
    fill(buf, area, bg);
    Border::Round.draw(buf, area, th.border_blurred, bg);

    // items already computed in hits
    for (i, (_, r)) in hits.iter().enumerate() {
        let is_highlight = highlight == Some(i);
        let is_sub_open = sub_open == Some(i);

        let row_bg = if is_highlight || is_sub_open {
            th.cursor_bg
        } else {
            bg
        };
        let _row_fg = if is_highlight || is_sub_open {
            th.cursor_fg
        } else {
            th.text
        };

        fill(buf, *r, row_bg);
        // actual labels rendered separately
    }
}

// ContextMenu

/// Context menu widget (right-click menu).
#[derive(Clone, Debug)]
pub struct ContextMenu {
    items: Vec<MenuItem>,
    focused: bool,
    enabled: bool,
    theme: Option<Theme>,
}

impl ContextMenu {
    pub fn new(items: Vec<MenuItem>) -> Self {
        Self {
            items,
            focused: false,
            enabled: true,
            theme: None,
        }
    }

    pub fn focused(mut self, f: bool) -> Self {
        self.focused = f;
        self
    }

    pub fn enabled(mut self, e: bool) -> Self {
        self.enabled = e;
        self
    }

    pub fn theme(mut self, t: &Theme) -> Self {
        self.theme = Some(*t);
        self
    }
}

/// Context menu state: open flag, anchor, highlight, action.
#[derive(Clone, Debug, Default)]
pub struct ContextMenuState {
    pub open: bool,
    pub anchor: Position,
    pub highlight: Option<usize>,
    pub hits: Vec<(usize, Rect)>,
    pub action: Option<usize>,
    pub area: Rect,
    theme: Option<Theme>,
}

impl ContextMenuState {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn open_at(&mut self, pos: Position) {
        self.open = true;
        self.anchor = pos;
        self.highlight = None;
    }

    pub fn close(&mut self) {
        self.open = false;
        self.highlight = None;
    }

    pub fn take_action(&mut self) -> Option<usize> {
        self.action.take()
    }

    /// Render context menu overlay.
    pub fn render_overlay(&self, buf: &mut Buffer, _bounds: Rect) {
        if !self.open || self.area.width == 0 {
            return;
        }
        let th = self.theme.unwrap_or_else(theme::current);
        render_dropdown(buf, self.area, &self.hits, self.highlight, None, &th);
    }
}

impl Interactive for ContextMenuState {
    fn handle_key(&mut self, k: KeyEvent) -> Outcome {
        if !is_press(&k) || !self.open {
            return Outcome::Ignored;
        }

        match k.code {
            KeyCode::Up => {
                self.highlight = self.highlight.map(|h| h.saturating_sub(1)).or(Some(0));
                return Outcome::Consumed;
            }
            KeyCode::Down => {
                self.highlight = Some(self.highlight.map_or(0, |h| h + 1));
                return Outcome::Consumed;
            }
            KeyCode::Enter | KeyCode::Char(' ') => {
                if let Some(h) = self.highlight
                    && let Some((id, _)) = self.hits.get(h)
                {
                    self.action = Some(*id);
                    self.close();
                    return Outcome::Submitted;
                }
            }
            KeyCode::Esc => {
                self.close();
                return Outcome::Changed;
            }
            _ => return Outcome::Ignored,
        }
        Outcome::Ignored
    }

    fn handle_mouse(&mut self, m: MouseEvent) -> Outcome {
        if !self.open {
            return Outcome::Ignored;
        }

        let pos = mouse_pos(&m);
        let mut out = Outcome::Ignored;

        // hover
        let mut new_hl = None;
        for (i, r) in &self.hits {
            if r.contains(pos) {
                new_hl = Some(*i);
                break;
            }
        }
        if new_hl != self.highlight {
            self.highlight = new_hl;
            out = Outcome::Consumed;
        }

        // click
        if matches!(m.kind, MouseEventKind::Down(MouseButton::Left)) {
            if let Some(h) = self.highlight {
                if let Some((id, _)) = self.hits.get(h) {
                    self.action = Some(*id);
                    self.close();
                    return Outcome::Submitted;
                }
            } else if !self.area.contains(pos) {
                self.close();
                return Outcome::Changed;
            }
        }

        out
    }
}

impl StatefulWidget for ContextMenu {
    type State = ContextMenuState;

    fn render(self, _area: Rect, buf: &mut Buffer, state: &mut Self::State) {
        if !state.open {
            state.area = Rect::ZERO;
            return;
        }

        let th = self.theme.unwrap_or_else(theme::current);
        state.hits.clear();
        state.theme = Some(th);

        // compute size
        let mut max_w = 0u16;
        let mut h = 0u16;
        for item in &self.items {
            match item {
                MenuItem::Action {
                    label, shortcut, ..
                } => {
                    let mut w = label.width() as u16 + 4;
                    if shortcut.is_some() {
                        w += 10;
                    }
                    max_w = max_w.max(w);
                    h += 1;
                }
                MenuItem::Separator => {
                    h += 1;
                }
                _ => {}
            }
        }

        let menu_w = max_w.max(12);
        let menu_h = h.max(1);

        let menu_rect = Rect::new(
            state.anchor.x.min(buf.area.right().saturating_sub(menu_w)),
            state.anchor.y.min(buf.area.bottom().saturating_sub(menu_h)),
            menu_w,
            menu_h,
        );
        state.area = menu_rect;

        // populate hits (simplified - actual rendering in overlay)
        let mut y = menu_rect.y;
        for (i, item) in self.items.iter().enumerate() {
            match item {
                MenuItem::Action { .. } => {
                    state
                        .hits
                        .push((i, Rect::new(menu_rect.x, y, menu_rect.width, 1)));
                    y += 1;
                }
                MenuItem::Separator => {
                    y += 1;
                }
                _ => {}
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn menu_bar_toggles_open() {
        let mut state = MenuBarState::new();
        state.open(0);
        assert_eq!(state.open, Some(0));
        state.close();
        assert_eq!(state.open, None);
    }

    #[test]
    fn context_menu_opens_at_position() {
        let mut state = ContextMenuState::new();
        state.open_at(Position { x: 10, y: 5 });
        assert!(state.open);
        assert_eq!(state.anchor, Position { x: 10, y: 5 });
    }

    #[test]
    fn menu_item_builder() {
        let item = MenuItem::action(1, "Save")
            .shortcut("^S")
            .checked(true)
            .disabled(false);
        match item {
            MenuItem::Action {
                id,
                label,
                shortcut,
                checked,
                disabled,
            } => {
                assert_eq!(id, 1);
                assert_eq!(label, "Save");
                assert_eq!(shortcut, Some("^S".to_string()));
                assert_eq!(checked, Some(true));
                assert!(!disabled);
            }
            _ => panic!("wrong variant"),
        }
    }
}