Application Menu
Use ApplicationMenu.setApplicationMenu() in the main process. A role invokes
native operating-system behavior; an action emits an event for application
code.
import { ApplicationMenu } from "electrobun/main";
ApplicationMenu.setApplicationMenu([ { label: "File", submenu: [ { label: "New project", action: "new-project", accelerator: "n", }, { type: "separator" }, { role: "close" }, { role: "quit" }, ], }, { label: "Edit", submenu: [ { role: "undo" }, { role: "redo" }, { type: "separator" }, { role: "cut" }, { role: "copy" }, { role: "paste" }, { role: "selectAll" }, ], },]);
ApplicationMenu.on("application-menu-clicked", (event: unknown) => { console.log("Application menu action", event);});import { ApplicationMenu } from "electrobun/main";
ApplicationMenu.setApplicationMenu([ { label: "File", submenu: [ { label: "New project", action: "new-project", accelerator: "n", }, { type: "separator" }, { role: "close" }, { role: "quit" }, ], }, { label: "Edit", submenu: [ { role: "undo" }, { role: "redo" }, { type: "separator" }, { role: "cut" }, { role: "copy" }, { role: "paste" }, { role: "selectAll" }, ], },]);
ApplicationMenu.on("application-menu-clicked", (event: unknown) => { console.log("Application menu action", event);});fn onMenuAction(item_id: u32, action: [*:0]const u8) callconv(.C) void { std.debug.print("application menu action: {d} {s}\n", .{ item_id, action });}
const menu_json = \\[{"label": "File", "submenu": [ \\ {"label": "New project", "action": "new-project", "accelerator": "n"}, \\ {"type": "separator"}, \\ {"role": "close"}, \\ {"role": "quit"} \\]}];
try core.setApplicationMenuJson(menu_json, onMenuAction);extern "C" fn on_menu_action(item_id: u32, action: *const c_char) { let action = electrobun::c_string_to_string(action); println!("application menu action: {item_id} {action}");}
let menu_json = r#"[ {"label": "File", "submenu": [ {"label": "New project", "action": "new-project", "accelerator": "n"}, {"type": "separator"}, {"role": "close"}, {"role": "quit"} ]}]"#;
core.set_application_menu_json(menu_json, Some(on_menu_action))?;menuJSON := `[ {"label": "File", "submenu": [ {"label": "New project", "action": "new-project", "accelerator": "n"}, {"type": "separator"}, {"role": "close"}, {"role": "quit"} ]}]`
err := core.SetApplicationMenuJSON(menuJSON, func(itemID uint32, action string) { fmt.Printf("application menu action: %d %s\n", itemID, action)})on_menu_action :: proc "c" (item_id: u32, action: cstring) { context = runtime.default_context() fmt.printfln("application menu action: %d %s", item_id, action)}
MENU_JSON :: `[ {"label": "File", "submenu": [ {"label": "New project", "action": "new-project", "accelerator": "n"}, {"type": "separator"}, {"role": "close"}, {"role": "quit"} ]}]`
_ = electrobun.setApplicationMenuJson(core, MENU_JSON, on_menu_action)Menu items
A normal item supports these fields:
| Field | Type | Purpose |
|---|---|---|
label | string | Visible item text. Optional when a role supplies its standard label. |
action | string | Custom action emitted when selected. |
role | string | Native menu command. Do not combine it with action. |
submenu | ApplicationMenuItemConfig[] | Nested items. |
enabled | boolean | Disable interaction when false. |
checked | boolean | Show the checked state. |
hidden | boolean | Hide the item. |
tooltip | string | Native tooltip where supported. |
accelerator | string | Single-key custom shortcut; Command on macOS and Control on Windows. |
data | unknown | Serializable application data attached to the action. |
Use { type: "separator" } or { type: "divider" } for a divider.
Roles
Common roles include about, quit, hide, hideOthers, showAll,
minimize, zoom, close, toggleFullScreen, undo, redo, cut,
copy, paste, pasteAndMatchStyle, delete, and selectAll.
Electrobun also accepts native text movement, selection, deletion, insertion, speech, and scrolling roles. Roles are forwarded to the platform menu system; unsupported roles have no native action on that platform.
Stateful items
Rebuild the menu when application state changes:
import { ApplicationMenu } from "electrobun/main";
let sidebarVisible = true;
function updateMenu() { ApplicationMenu.setApplicationMenu([ { label: "View", submenu: [ { label: "Show sidebar", action: "toggle-sidebar", checked: sidebarVisible, }, ], }, ]);}
ApplicationMenu.on("application-menu-clicked", (event: unknown) => { console.log(event); sidebarVisible = !sidebarVisible; updateMenu();});
updateMenu();Application menus are currently implemented on macOS and Windows. Linux apps should expose essential actions in application UI or a supported tray menu.