Skip to main content

menu

Macro menu 

Source
macro_rules! menu {
    ( $ui:expr => $window:ident as $bar:ident : $($entries:tt)* ) => { ... };
    ( $ui:expr => $window:ident : $($entries:tt)* ) => { ... };
}
Expand description

Builds, populates, and attaches a window menu bar from a terse block.

Submenus and items are declared with ids that become ordinary MenuId / MenuItemId values in the caller’s scope, so Ui::on_menu_item_selected and event matching work exactly as with the imperative calls.

Grammar:

  • menu! { <ui> => <window-id>: ...entries... } — attaches the bar to the window; the bar id stays internal (__native_gui_menu_bar).
  • menu! { <ui> => <window-id> as <bar-id>: ...entries... } — also binds the bar id (for example to reuse it through Ui::attach_context_menu).
  • Submenu: <id> "<label>" { ...items... }, comma-separated; a trailing comma is allowed.
  • Item: <id> "<label>" or <id> "<label>" @ "<shortcut>"; the shortcut registers exactly like Ui::add_menu_item_with_shortcut.
use native_gui_mock::MockBackend;
use native_gui_core::Ui;

let mut ui = Ui::new(MockBackend::default());

native_gui_core::ui! { ui => window root: "Menus" => { label body: "hi", } }
native_gui_core::menu! { ui => root:
    file "File" {
        new "New" @ "Ctrl+N",
        quit "Quit" @ "Ctrl+Q",
    },
    help "Help" {
        about "About",
    },
}

assert_eq!(ui.backend().menus.get(&file).map(Vec::len), Some(2));
assert_eq!(ui.backend().menu_shortcuts.get(&new).map(String::as_str), Some("Ctrl+N"));