menu

package
v0.1.16 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 4 Imported by: 0

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

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

func NewBar(menus []TopMenu, opts ...BarOption) *Bar

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) A11yLabel

func (b *Bar) A11yLabel() string

A11yLabel returns the accessibility label.

func (*Bar) A11yRole

func (b *Bar) A11yRole() string

A11yRole returns the ARIA role for the menu bar.

func (*Bar) Children

func (b *Bar) Children() []widget.Widget

Children returns nil; the menu bar is a leaf widget (menus are overlays).

func (*Bar) Close

func (b *Bar) Close(ctx widget.Context)

Close closes any open menu.

func (*Bar) Draw

func (b *Bar) Draw(ctx widget.Context, canvas widget.Canvas)

Draw renders the menu bar.

func (*Bar) Event

func (b *Bar) Event(ctx widget.Context, e event.Event) bool

Event handles input events for the menu bar.

func (*Bar) HoveredIndex

func (b *Bar) HoveredIndex() int

HoveredIndex returns the index of the hovered label, or -1.

func (*Bar) IsFocusable

func (b *Bar) IsFocusable() bool

IsFocusable reports whether the menu bar can currently receive focus.

func (*Bar) IsOpen

func (b *Bar) IsOpen() bool

IsOpen returns true if any top-level menu is currently open.

func (*Bar) Layout

func (b *Bar) Layout(_ widget.Context, constraints geometry.Constraints) geometry.Size

Layout calculates the menu bar's preferred size. The bar occupies the full available width and a fixed height.

func (*Bar) Menus

func (b *Bar) Menus() []TopMenu

Menus returns the top-level menu definitions.

func (*Bar) OpenIndex

func (b *Bar) OpenIndex() int

OpenIndex returns the index of the currently open top-level menu, or -1.

type BarOption

type BarOption func(*Bar)

BarOption configures a Bar during construction.

func PainterOpt

func PainterOpt(p Painter) BarOption

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.

func (*ContextMenu) Show

func (cm *ContextMenu) Show(ctx widget.Context, position geometry.Point)

Show opens the context menu at the given position via the overlay manager. If the context menu is already open, it is closed first.

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 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 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 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

func Item(label, shortcut string, onAction func()) MenuItem

Item creates a regular menu item with a label, shortcut display text, and action callback.

func ItemDisabled

func ItemDisabled(label, shortcut string) MenuItem

ItemDisabled creates a disabled menu item that cannot be activated.

func Sep

func Sep() MenuItem

Sep creates a separator menu item displayed as a horizontal line.

func SubMenu(label string, children ...MenuItem) MenuItem

SubMenu creates a menu item that opens a nested submenu on hover.

func (m MenuItem) HasChildren() bool

HasChildren reports whether this item has a submenu.

func (m MenuItem) IsSeparator() bool

IsSeparator reports whether this item is a separator line.

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.

func BarMenu

func BarMenu(label string, items ...MenuItem) TopMenu

BarMenu creates a TopMenu definition for use with NewBar.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL