Skip to main content

Module menu

Module menu 

Source
Expand description

Menus as data: the rows of a context menu or the application menu bar, what the window has open, and what a host still has to do after a row is chosen.

An app meets this module through Ui::open_menu / Core::open_menu (a Menu over a node, usually from a contextmenu event), through Core::declare_menu_bar (a MenuBar of BarMenus), and through the menu event a chosen row posts on the node the menu was about. A host that holds the core drains MenuActions with Core::take_menu_actions: the clipboard work the core cannot do itself. Nothing here draws; the stock renderer builds ordinary nodes into the frame, and a platform that owns menus (the macOS menu bar) gets the same list.

use kui_core::{
    Accel, BarMenu, Core, Key, Menu, MenuAction, MenuBar, MenuItem, MenuRole, Vec2,
};

let bar = MenuBar::new(vec![
    BarMenu::new("File", vec![
        MenuItem::new("Open...").id("open").accel("mod+o"),
        MenuItem::separator(),
        MenuItem::new("Quit").id("quit"),
    ]),
    BarMenu::new("Edit", vec![
        MenuItem::role(MenuRole::Cut),
        MenuItem::role(MenuRole::Copy),
        MenuItem::role(MenuRole::Paste),
    ]),
]);
let mut core = Core::new();
core.declare_menu_bar(bar);
assert_eq!(core.menu_bar().map(|b| b.menus.len()), Some(2));

// A context menu over a node, at the point the press landed.
let items = vec![
    MenuItem::new("Inspect").id("inspect"),
    MenuItem::new("Delete").id("delete").enabled(false),
];
core.open_menu(Menu::new(Key::ROOT, Vec2::new(40.0, 30.0), items));

// After a row is chosen, the host finishes what the core cannot.
for action in core.take_menu_actions() {
    match action {
        MenuAction::SetClipboard { text, .. } => println!("copy {text}"),
        MenuAction::Paste => println!("read the clipboard"),
        other => println!("{other:?}"),
    }
}

// Accelerators are display text; `Accel` parses them for a native bar.
let accel = Accel::parse("mod+shift+s").unwrap();
assert!(accel.mods.shift);

Structs§

Accel
A keyboard shortcut, parsed out of the string an item declares.
BarMenu
One menu of the bar: what the bar reads, and what drops out of it.
Menu
The menu a window has open: its items, where it opened, and what it is about. One per window — opening a second closes the first, the way one selection closes the last.
MenuBar
The application menu: what a frame declares, in order (Core::declare_menu_bar).
MenuItem
One row of a menu.

Enums§

MenuAction
What choosing an item leaves for the host to do, drained with crate::runtime::Core::take_menu_actions the way window and audio commands are.
MenuRole
What an item means, as far as anything outside the app is concerned.