tui_vision/menus/
macros.rs

1// Export macros for convenience
2#[macro_export]
3macro_rules! menu_bar {
4    ($($menu:expr),* $(,)?) => {
5        {
6            let mut menu_bar = $crate::menus::MenuBar::new();
7            $(
8                menu_bar.add_menu($menu);
9            )*
10            menu_bar
11        }
12    };
13}
14
15#[macro_export]
16macro_rules! menu {
17    ($title:expr, $hotkey:expr, $($item:expr),* $(,)?) => {
18        {
19            let menu = $crate::menus::Menu::with_hotkey($title, $hotkey);
20            $(
21                let menu = menu.item($item);
22            )*
23            menu
24        }
25    };
26}
27
28#[macro_export]
29macro_rules! item {
30    (action: $text:expr, command: $command:expr $(, hotkey: $hotkey:expr)? $(, shortcut: $shortcut:expr)?) => {
31        {
32            let item = $crate::menus::MenuItem::action($text, $command);
33            $(let item = item.with_hotkey($hotkey);)?
34            $(let item = item.with_shortcut($shortcut);)?
35            item
36        }
37    };
38    (separator) => {
39        $crate::menus::MenuItem::separator()
40    };
41    (submenu: $text:expr, items: [$($sub_item:expr),*] $(, hotkey: $hotkey:expr)?) => {
42        {
43            let submenu = $crate::menus::MenuItem::submenu($text, vec![$($sub_item),*]);
44            $(let submenu = submenu.with_hotkey($hotkey);)?
45            submenu
46        }
47    };
48}
49
50#[cfg(test)]
51mod tests {
52    #[test]
53    fn menu_bar() {
54        let menu_bar = menu_bar![
55            menu![
56                "File",
57                'F',
58                item![action: "New", command: "file.new", hotkey: 'N'],
59                item![action: "Open", command: "file.open", hotkey: 'O', shortcut: "Ctrl+O"],
60                item![separator],
61                item![action: "Save", command: "file.save", hotkey: 'S', shortcut: "Ctrl+S"],
62                item![action: "Save As...", command: "file.save_as", hotkey: 'A'],
63                item![separator],
64                item![action: "Exit", command: "file.exit", hotkey: 'x', shortcut: "Alt+F4"],
65            ],
66            menu![
67                "Edit",
68                'E',
69                item![action: "Undo", command: "edit.undo", hotkey: 'U', shortcut: "Ctrl+Z"],
70                item![action: "Redo", command: "edit.redo", hotkey: 'R', shortcut: "Ctrl+Y"],
71                item![separator],
72                item![action: "Cut", command: "edit.cut", hotkey: 't', shortcut: "Ctrl+X"],
73                item![action: "Copy", command: "edit.copy", hotkey: 'C', shortcut: "Ctrl+C"],
74                item![action: "Paste", command: "edit.paste", hotkey: 'P', shortcut: "Ctrl+V"],
75                item![separator],
76                item![submenu: "Find", items: [
77                item![action: "Find", command: "edit.find", hotkey: 'F', shortcut: "Ctrl+F"],
78                item![action: "Find Next", command: "edit.find_next", hotkey: 'N'],
79                item![action: "Replace", command: "edit.replace", hotkey: 'R', shortcut: "Ctrl+H"]
80            ], hotkey: 'i'],
81            ],
82            menu![
83                "Help",
84                'H',
85                item![action: "Help Topics", command: "help.topics", hotkey: 'T'],
86                item![separator],
87                item![action: "About", command: "help.about", hotkey: 'A'],
88            ],
89        ];
90
91        assert_eq!(menu_bar.menus.len(), 3);
92        assert_eq!(menu_bar.menus[0].title, "File");
93        assert_eq!(menu_bar.menus[1].title, "Edit");
94        assert_eq!(menu_bar.menus[2].title, "Help");
95    }
96}