Documentation
¶
Overview ¶
Package menu provides a menu system with MenuBar and ContextMenu widgets.
MenuBar is a horizontal bar at the top of the window containing top-level menu labels. Clicking a label opens a vertical dropdown menu. ContextMenu is a floating menu shown at a specific position, typically on right-click.
Both widgets support:
- Nested submenus (hover to open)
- Separator items
- Disabled items (grayed out)
- Keyboard navigation (arrow keys, Enter, Escape)
- Shortcut display text (cosmetic only)
- Pluggable Painter for design-system independence
- Overlay integration for popup menus
MenuBar example:
bar := menu.NewBar(
menu.BarMenu("File",
menu.Item("New", "Ctrl+N", onNew),
menu.Item("Open", "Ctrl+O", onOpen),
menu.Sep(),
menu.Item("Save", "Ctrl+S", onSave),
),
menu.BarMenu("Edit",
menu.Item("Undo", "Ctrl+Z", onUndo),
menu.Item("Redo", "Ctrl+Y", onRedo),
),
)
ContextMenu example:
ctx := menu.NewContextMenu(
menu.Item("Cut", "Ctrl+X", onCut),
menu.Item("Copy", "Ctrl+C", onCopy),
menu.Item("Paste", "Ctrl+V", onPaste),
)
ctx.Show(widgetCtx, position)
The visual appearance is controlled by a pluggable Painter interface. The default painter provides a minimal fallback; use a design system painter (e.g., material3.MenuPainter) for production styling.
Index ¶
- type Bar
- func (b *Bar) A11yLabel() string
- func (b *Bar) A11yRole() string
- func (b *Bar) Children() []widget.Widget
- func (b *Bar) Close(ctx widget.Context)
- func (b *Bar) Draw(ctx widget.Context, canvas widget.Canvas)
- func (b *Bar) Event(ctx widget.Context, e event.Event) bool
- func (b *Bar) HoveredIndex() int
- func (b *Bar) IsFocusable() bool
- func (b *Bar) IsOpen() bool
- func (b *Bar) Layout(_ widget.Context, constraints geometry.Constraints) geometry.Size
- func (b *Bar) Menus() []TopMenu
- func (b *Bar) OpenIndex() int
- type BarOption
- type ContextMenu
- type ContextMenuOption
- type DefaultPainter
- type MenuBarPaintState
- type MenuColorScheme
- type MenuItem
- type MenuPaintState
- type Painter
- type PanelState
- type TopMenu
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bar ¶
type Bar struct {
widget.WidgetBase
// contains filtered or unexported fields
}
Bar is a horizontal menu bar widget displayed at the top of a window. It contains top-level menu labels that open dropdown menus when clicked.
Bar follows the functional options pattern for construction. Create with NewBar.
func NewBar ¶
NewBar creates a new MenuBar widget with the given top-level menus and options.
The returned widget is visible and enabled by default.
func (*Bar) HoveredIndex ¶
HoveredIndex returns the index of the hovered label, or -1.
func (*Bar) IsFocusable ¶
IsFocusable reports whether the menu bar can currently receive focus.
type BarOption ¶
type BarOption func(*Bar)
BarOption configures a Bar during construction.
func PainterOpt ¶
PainterOpt sets the painter used to render the menu bar and its menus.
type ContextMenu ¶
type ContextMenu struct {
// contains filtered or unexported fields
}
ContextMenu is a floating popup menu shown at a specific position, typically on right-click. It contains a list of menu items that the user can select via mouse or keyboard.
Create with NewContextMenu.
func NewContextMenu ¶
func NewContextMenu(items []MenuItem, opts ...ContextMenuOption) *ContextMenu
NewContextMenu creates a new context menu with the given items and options.
func (*ContextMenu) Hide ¶
func (cm *ContextMenu) Hide(ctx widget.Context)
Hide closes the context menu.
func (*ContextMenu) IsOpen ¶
func (cm *ContextMenu) IsOpen() bool
IsOpen returns true if the context menu is currently visible.
func (*ContextMenu) Items ¶
func (cm *ContextMenu) Items() []MenuItem
Items returns the context menu items.
func (*ContextMenu) Panel ¶
func (cm *ContextMenu) Panel() PanelState
Panel returns the active menu panel for testing. Returns nil when closed. The returned value implements widget.Widget and PanelState.
type ContextMenuOption ¶
type ContextMenuOption func(*ContextMenu)
ContextMenuOption configures a ContextMenu during construction.
func ContextPainterOpt ¶
func ContextPainterOpt(p Painter) ContextMenuOption
ContextPainterOpt sets the painter used to render the context menu.
type DefaultPainter ¶
type DefaultPainter struct{}
DefaultPainter provides a minimal fallback painter for menus. It draws simple rectangles and text without design system styling.
func (DefaultPainter) PaintMenu ¶
func (p DefaultPainter) PaintMenu(canvas widget.Canvas, st *MenuPaintState)
PaintMenu renders a minimal popup menu panel.
func (DefaultPainter) PaintMenuBar ¶
func (p DefaultPainter) PaintMenuBar(canvas widget.Canvas, st *MenuBarPaintState)
PaintMenuBar renders a minimal menu bar.
type MenuBarPaintState ¶
type MenuBarPaintState struct {
// Bounds is the menu bar's bounds.
Bounds geometry.Rect
// Menus is the list of top-level menu definitions.
Menus []TopMenu
// MenuRects contains the bounding rectangles for each top-level menu label.
MenuRects []geometry.Rect
// OpenIndex is the index of the currently open top-level menu (-1 for none).
OpenIndex int
// HoveredIndex is the index of the hovered top-level menu label (-1 for none).
HoveredIndex int
// Focused is true if the menu bar has keyboard focus.
Focused bool
// ColorScheme provides theme-derived colors. Zero value means use defaults.
ColorScheme MenuColorScheme
}
MenuBarPaintState provides the current menu bar state to the painter.
type MenuColorScheme ¶
type MenuColorScheme struct {
BarBackground widget.Color
BarText widget.Color
BarHover widget.Color
BarActiveText widget.Color
MenuBackground widget.Color
MenuBorder widget.Color
ItemText widget.Color
ItemHover widget.Color
ItemDisabledText widget.Color
ShortcutText widget.Color
SeparatorColor widget.Color
SubMenuArrow widget.Color
}
MenuColorScheme provides theme-derived colors for menu painting. Zero value means the painter should use its built-in defaults.
type MenuItem ¶
type MenuItem struct {
// Label is the display text for this item.
Label string
// Shortcut is the keyboard shortcut display text (e.g., "Ctrl+S").
// This is cosmetic only; actual shortcut handling is separate.
Shortcut string
// OnAction is the callback invoked when the item is activated.
// Nil for separators and submenu parents.
OnAction func()
// Disabled prevents this item from being activated.
Disabled bool
// Children holds submenu items. If non-empty, this item opens a submenu
// on hover instead of invoking OnAction.
Children []MenuItem
// contains filtered or unexported fields
}
MenuItem defines a single entry in a menu. It can be a regular action item, a separator, or a submenu parent containing children.
func Item ¶
Item creates a regular menu item with a label, shortcut display text, and action callback.
func ItemDisabled ¶
ItemDisabled creates a disabled menu item that cannot be activated.
func (MenuItem) HasChildren ¶
HasChildren reports whether this item has a submenu.
func (MenuItem) IsSeparator ¶
IsSeparator reports whether this item is a separator line.
type MenuPaintState ¶
type MenuPaintState struct {
// Bounds is the menu panel's bounds in window coordinates.
Bounds geometry.Rect
// Items is the list of menu items.
Items []MenuItem
// HighlightedIndex is the index of the currently highlighted item (-1 for none).
HighlightedIndex int
// ItemHeight is the height of each regular item row.
ItemHeight float32
// SeparatorHeight is the height of separator rows.
SeparatorHeight float32
// SubMenuOpenIndex is the index of the item whose submenu is open (-1 for none).
SubMenuOpenIndex int
// ColorScheme provides theme-derived colors. Zero value means use defaults.
ColorScheme MenuColorScheme
}
MenuPaintState provides the current popup menu state to the painter.
type Painter ¶
type Painter interface {
// PaintMenuBar draws the horizontal menu bar background and top-level labels.
PaintMenuBar(canvas widget.Canvas, state *MenuBarPaintState)
// PaintMenu draws a popup menu panel with its items.
PaintMenu(canvas widget.Canvas, state *MenuPaintState)
}
Painter draws the visual representation of menus and menu bars. Each design system (Material 3, Fluent, Cupertino) provides its own Painter implementation. If no Painter is set, DefaultPainter is used.
type PanelState ¶
type PanelState interface {
widget.Widget
// Bounds returns the panel's bounding rectangle.
Bounds() geometry.Rect
// HighlightedIndex returns the currently highlighted item index (-1 for none).
HighlightedIndex() int
// SubMenuIndex returns the index of the item whose submenu is open (-1 for none).
SubMenuIndex() int
// SubMenuPanel returns the open submenu panel, or nil.
SubMenuPanel() PanelState
}
PanelState is the exported interface for accessing menu panel state. This is primarily used for testing.
type TopMenu ¶
type TopMenu struct {
// Label is the text displayed in the menu bar.
Label string
// Items are the menu entries shown when this top-level menu is opened.
Items []MenuItem
}
TopMenu defines a top-level menu in a MenuBar. It has a label shown in the bar and a list of items displayed in the dropdown.