Menu Reference
This reference document covers the common menu item types and properties available in Wails v3. These features are shared between application menus and context menus.
Menu Item Types
Regular Menu Items
The most basic type of menu item displays text and triggers an action when clicked:
menuItem := menu.Add("Click Me")menuItem.OnClick(func(ctx *application.Context) { // Handle click})
Checkboxes
Checkbox menu items provide a toggleable state:
checkbox := menu.AddCheckbox("Enable Feature", true) // true = initially checkedcheckbox.OnClick(func(ctx *application.Context) { isChecked := ctx.ClickedMenuItem().Checked() // Handle state change})
Radio Groups
Radio items create mutually exclusive options. Items are grouped automatically when placed adjacently:
menu.AddRadio("Option 1", true) // true = initially selectedmenu.AddRadio("Option 2", false)menu.AddRadio("Option 3", false)
Submenus
Submenus allow you to create nested menu structures:
submenu := menu.AddSubmenu("More Options")submenu.Add("Submenu Item 1")submenu.Add("Submenu Item 2")
Separators
Separators add visual dividers between menu items:
menu.Add("Item 1")menu.AddSeparator()menu.Add("Item 2")
Menu Item Properties
Label
The text displayed for the menu item:
menuItem := menu.Add("Initial Label")menuItem.SetLabel("New Label")
Enabled State
Control whether the menu item can be interacted with:
menuItem := menu.Add("Disabled Item")menuItem.SetEnabled(false)
Checked State
For checkbox and radio items, control or query their checked state:
checkbox := menu.AddCheckbox("Feature", false)checkbox.SetChecked(true)isChecked := checkbox.Checked()
Accelerators
Add keyboard shortcuts to menu items:
menuItem := menu.Add("Save")menuItem.SetAccelerator("CmdOrCtrl+S")
Common accelerator modifiers:
CmdOrCtrl
: Command on macOS, Control on Windows/LinuxShift
Alt
: Option on macOSCtrl
: Control key on all platforms
Event Handling
Click Events
Handle menu item clicks using the OnClick
method:
menuItem.OnClick(func(ctx *application.Context) { // Access the clicked item clickedItem := ctx.ClickedMenuItem()
// Get current state label := clickedItem.Label() isChecked := clickedItem.Checked()
// Update the item clickedItem.SetLabel("New Label")})
Shared Event Handlers
Event handlers can be shared amongst multiple menu items:
handleClick := func(ctx *application.Context) { item := ctx.ClickedMenuItem() // Common handling logic}
menu.Add("Item 1").OnClick(handleClick)menu.Add("Item 2").OnClick(handleClick)
Dynamic Updates
Menu items can be updated dynamically during runtime:
menuItem := menu.Add("Initial State")
// Later, update the itemmenuItem.SetLabel("New Label")menuItem.SetEnabled(false)menuItem.SetChecked(true)
// Apply changesmenu.Update()
Best Practices
- Use clear, concise labels that describe the action
- Group related items together using separators
- Limit submenu depth to maintain usability
- Provide keyboard shortcuts for common actions
- Keep radio groups focused on a single choice
- Update menu items to reflect application state
- Handle all possible states in click handlers