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
selecteditem (PopupMenu::pickersets it), that item sits over the element and the menu is at least as wide, as a SwiftUI menu-stylePickeropens. Without one, the menu drops down from the element's bottom-left. APointerPressor(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'spopUpMenuPositioningItem: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 andAppHandle::updatekeep running while it's open. The pointer's press, capture and hover are released before it opens. Windows usesTrackPopupMenuEx, opened from a message posted to the window after winit's handler returns, so Rune keeps drawing there too: frames, wakes,wake_aftertimers 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 isselected, opens with its top-left at a point, and sizes itself to its items;.symboldoesn'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 (answeredNone) are the system's, overlays included. A click right after the menu closes lands where the pointer is, moved or not. Elsewherepopup_menu_supported()is false andon_pickgetsNoneon the next frame, so keep an in-app fallback for those platforms.cargo run -p rune --example popup_menushows both kinds. - Headless:
--treehas apopupMenuobject (items, anchor,selected,queued) while a menu is open, and--tracenotespopup-menu:open,popup-menu:pick:{id}andpopup-menu:cancel. Scripts answer it with{"type": "menu-pick", "id": "dark"}or{"type": "menu-cancel"}.