core/menu/menubar.go
Functions
func A11yLabel
func (b *Bar) A11yLabel() string {
return a11yLabelMenuBar
}
func A11yRole
A11yRole returns the ARIA role for the menu bar.
func (b *Bar) A11yRole() string {
return a11yRoleMenuBar
}
func Children
Children returns nil; the menu bar is a leaf widget (menus are overlays).
func (b *Bar) Children() []widget.Widget {
return nil
}
func Close
Close closes any open menu.
func (b *Bar) Close(ctx widget.Context) {
b.closeMenu(ctx)
}
func Draw
Draw renders the menu bar.
func (b *Bar) Draw(ctx widget.Context, canvas widget.Canvas) {
if canvas == nil {
return
}
b.painter.PaintMenuBar(canvas, &MenuBarPaintState{
Bounds: b.Bounds(),
Menus: b.menus,
MenuRects: b.toAbsoluteRects(),
OpenIndex: b.openIndex,
HoveredIndex: b.hoveredIndex,
Focused: b.IsFocused(),
})
}
func Event
Event handles input events for the menu bar.
func (b *Bar) Event(ctx widget.Context, e event.Event) bool {
switch ev := e.(type) {
case *event.MouseEvent:
return b.handleMouseEvent(ctx, ev)
case *event.KeyEvent:
return b.handleKeyEvent(ctx, ev)
default:
return false
}
}
func HoveredIndex
HoveredIndex returns the index of the hovered label, or -1.
func (b *Bar) HoveredIndex() int {
return b.hoveredIndex
}
func IsFocusable
IsFocusable reports whether the menu bar can currently receive focus.
func (b *Bar) IsFocusable() bool {
return b.IsVisible() && b.IsEnabled()
}
func IsOpen
IsOpen returns true if any top-level menu is currently open.
func (b *Bar) IsOpen() bool {
return b.openIndex >= 0
}
func Layout
Layout calculates the menu bar's preferred size.
The bar occupies the full available width and a fixed height.
func (b *Bar) Layout(_ widget.Context, constraints geometry.Constraints) geometry.Size {
preferred := geometry.Sz(constraints.MaxWidth, dfltBarHeight)
if preferred.Width >= geometry.Infinity {
preferred.Width = 400 // reasonable fallback
}
size := constraints.Constrain(preferred)
// Compute label rectangles.
b.computeMenuRects(size)
return size
}
func Menus
Menus returns the top-level menu definitions.
func (b *Bar) Menus() []TopMenu {
return b.menus
}
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 NewBar(menus []TopMenu, opts ...BarOption) *Bar {
b := &Bar{
menus: menus,
painter: DefaultPainter{},
openIndex: -1,
hoveredIndex: -1,
}
b.SetVisible(true)
b.SetEnabled(true)
for _, opt := range opts {
opt(b)
}
return b
}
func OpenIndex
OpenIndex returns the index of the currently open top-level menu, or -1.
func (b *Bar) OpenIndex() int {
return b.openIndex
}
func PainterOpt
PainterOpt sets the painter used to render the menu bar and its menus.
func PainterOpt(p Painter) BarOption {
return func(b *Bar) {
b.painter = p
}
}
Structs
type Bar struct
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].
type Bar struct {
widget.WidgetBase
menus []TopMenu
menuRects []geometry.Rect
painter Painter
openIndex int // index of open top-level menu (-1 for none)
hoveredIndex int // index of hovered label (-1 for none)
// Active menu panel state.
activePanel *menuPanel
}
A11yLabel returns the accessibility label.