Upstream: crates/rune/README.md § Pop-up and context menus at 65f8eb43f710, MIT © Anomaly. Bundled by build-rune-apps; links point at this snapshot or at GitHub at that rev.

Pop-up and context menus

tx.popup_menu(menu, anchor, on_pick) opens a native menu once the handler's frame is done. on_pick runs like an update, with the picked item's id after the menu closes, or None when it's dismissed. It's called exactly once per request. A menu asked for while another is open waits its turn. Hiding, closing or quitting the window ends a tracking menu and answers every waiting request None first.

row().key("theme").child(text(theme_title(&s.theme)))
    .on_press_tx(|s: &mut Settings, tx: &mut Tx| {
        let menu = PopupMenu::picker([("system", "System"), ("light", "Light"), ("dark", "Dark")], &s.theme);
        tx.popup_menu(menu, "theme", |s: &mut Settings, picked, _| {
            if let Some(id) = picked { s.theme = id.into(); }
        });
    })
// A context menu at the pointer:
.on_secondary_press(|_: &mut Settings, press, tx: &mut Tx| {
    let menu = PopupMenu::new()
        .item(PopupItem::new("copy", "Copy").symbol("doc.on.doc").key_equivalent("cmd+c".parse().unwrap()))
        .item(PopupItem::new("paste", "Paste").enabled(false))
        .separator()
        .submenu("Share", PopupMenu::new().item(PopupItem::new("mail", "Mail")));
    tx.popup_menu(menu, press, |s: &mut Settings, picked, _| { /* … */ });
})
  • Anchors: an element key opens over that element's drawn rect. With a selected item (PopupMenu::picker sets it), that item sits over the element and the menu is at least as wide, as a SwiftUI menu-style Picker opens. Without one, the menu drops down from the element's bottom-left. A PointerPress or (x, y) in window points opens the menu there.
  • Items: PopupItem::new(id, title) with .enabled, .checked, .key_equivalent (a hint; the menu doesn't register the shortcut) and .symbol (an SF Symbol name on macOS 11+).
  • Platforms: macOS uses NSMenu's popUpMenuPositioningItem:atLocation:inView: in windows and overlay panels. The menu tracks from a run-loop block outside winit's handler, so frames, springs, the display link and AppHandle::update keep running while it's open. The pointer's press, capture and hover are released before it opens. Windows uses TrackPopupMenuEx, opened from a message posted to the window after winit's handler returns, so Rune keeps drawing there too: frames, wakes, wake_after timers and overlays. It follows Windows placement: the menu drops down below an element anchor (flipping up clear of it near the screen's bottom edge) whether or not an item is selected, opens with its top-left at a point, and sizes itself to its items; .symbol doesn't show. Menus are dark or light as the window's content is (Windows 10 1903 and later), whatever the system's appearance. Keyboard navigation and dismissal by a click outside (answered None) are the system's, overlays included. A click right after the menu closes lands where the pointer is, moved or not. Elsewhere popup_menu_supported() is false and on_pick gets None on the next frame, so keep an in-app fallback for those platforms. cargo run -p rune --example popup_menu shows both kinds.
  • Headless: --tree has a popupMenu object (items, anchor, selected, queued) while a menu is open, and --trace notes popup-menu:open, popup-menu:pick:{id} and popup-menu:cancel. Scripts answer it with {"type": "menu-pick", "id": "dark"} or {"type": "menu-cancel"}.