Menu
A menu — for a window's menu bar or a right-click pop-up. Build it by adding
entries, each of which may carry a command callback.
Example — a menu bar
const fileMenu = app.Menu({ tearOff: false });
fileMenu.addCommand({ label: 'New', command: () => newDoc() });
fileMenu.addCommand({ label: 'Open…', command: () => openDoc() });
fileMenu.addSeparator();
fileMenu.addCommand({ label: 'Quit', command: () => app.quit() });
const bar = app.Menu({ tearOff: false });
bar.addCascade({ label: 'File', menu: fileMenu });
app.menu = bar; // attach to the window
Example — a pop-up
const ctx = app.Menu({ tearOff: false });
ctx.addCommand({ label: 'Copy', command: copy });
widget.bind('<Button-3>', (e) => ctx.post(e.screenX, e.screenY));
Options
| Option | Type | Description |
|---|---|---|
tearOff |
boolean | Whether the menu can be torn off — usually false. |
Methods
| Method | Description |
|---|---|
addCommand(opts) |
Add an item. opts: label, command, accelerator, state, … |
addCascade(opts) |
Add a submenu: { label, menu }. |
addCheckbutton(opts) / addRadiobutton(opts) |
Add a toggle / choice item (bind a variable). |
addSeparator() |
Add a divider. |
insert(index, type, opts?) |
Insert an entry at a position. |
entryConfig(index, opts) |
Reconfigure an entry. |
delete(index1, index2?) |
Remove entries. |
invoke(index) |
Activate an entry programmatically. |
post(x, y) / unpost() |
Show / hide as a pop-up at screen coordinates. |
count() |
Number of entries. |
Plus the inherited widget methods — see Widget basics.
See also
- The notes editor (menu bar) and file browser (context menu) examples
- Events & binding