Documentation
¶
Overview ¶
Package toolbar provides a horizontal action bar widget.
A toolbar displays a horizontal row of icon buttons, separators, spacers, and custom widgets. It is commonly used at the top of an application window to provide quick access to frequently used actions.
Construction uses convenience functions for items and functional options for toolbar-level configuration:
tb := toolbar.New(
toolbar.Items(
toolbar.IconButton("New", icon.Add, onNew),
toolbar.IconButton("Open", icon.Menu, onOpen),
toolbar.Separator(),
toolbar.Spacer(),
toolbar.IconButton("Settings", icon.Settings, onSettings),
),
toolbar.Height(40),
)
Item Types ¶
Four item types are supported:
- ItemButton -- an icon button with optional text label and click handler
- ItemSeparator -- a vertical divider line between button groups
- ItemSpacer -- a flexible gap that pushes subsequent items to the right
- ItemCustom -- any widget.Widget embedded in the toolbar
Visual Style ¶
The visual rendering is provided by a Painter implementation. Each design system (Material 3, Fluent, Cupertino) can supply its own painter. If no painter is set, DefaultPainter is used.
Interaction ¶
Button items respond to mouse events (hover, press, click) and keyboard activation (Enter or Space when focused). Tab navigates between items. Disabled items ignore all interaction.
Accessibility ¶
The toolbar has the a11y.RoleToolbar role. Each button item is individually focusable and announces its label to screen readers.
Index ¶
- type DefaultPainter
- type Item
- type ItemKind
- type Option
- type PaintButtonState
- type PaintToolbarState
- type Painter
- type Widget
- func (w *Widget) AccessibilityActions() []a11y.Action
- func (w *Widget) AccessibilityHint() string
- func (w *Widget) AccessibilityLabel() string
- func (w *Widget) AccessibilityRole() a11y.Role
- func (w *Widget) AccessibilityState() a11y.State
- func (w *Widget) AccessibilityValue() string
- func (w *Widget) Children() []widget.Widget
- func (w *Widget) Draw(ctx widget.Context, canvas widget.Canvas)
- func (w *Widget) Event(ctx widget.Context, e event.Event) bool
- func (w *Widget) HitTestPoint(local geometry.Point) bool
- func (w *Widget) IsFocusable() bool
- func (w *Widget) ItemAt(idx int) Item
- func (w *Widget) ItemCount() int
- func (w *Widget) Layout(ctx widget.Context, constraints geometry.Constraints) geometry.Size
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DefaultPainter ¶
type DefaultPainter struct{}
DefaultPainter provides a minimal fallback painter with no design system styling. It draws simple gray buttons suitable for testing and prototyping.
func (DefaultPainter) PaintButtonItem ¶
func (p DefaultPainter) PaintButtonItem(canvas widget.Canvas, state PaintButtonState)
PaintButtonItem renders a button item with icon and optional text label.
func (DefaultPainter) PaintSeparator ¶
func (p DefaultPainter) PaintSeparator(canvas widget.Canvas, bounds geometry.Rect)
PaintSeparator renders a vertical line.
func (DefaultPainter) PaintToolbar ¶
func (p DefaultPainter) PaintToolbar(canvas widget.Canvas, state PaintToolbarState)
PaintToolbar renders a light gray background for the toolbar.
type Item ¶
type Item struct {
// Kind identifies the type of this item.
Kind ItemKind
// Label is the human-readable text for button items. Used for
// accessibility announcements and optional visual display.
Label string
// Icon is the vector icon data for button items.
Icon icon.IconData
// OnClick is the callback invoked when a button item is activated.
OnClick func()
// Widget is the embedded widget for ItemCustom items.
Widget widget.Widget
// Enabled controls whether a button item responds to interaction.
// Separators and spacers ignore this field.
Enabled bool
// ShowLabel controls whether the text label is displayed next to the icon.
// When false (default for IconButton), only the icon is shown.
ShowLabel bool
}
Item represents a single element within a toolbar.
Use the convenience constructors IconButton, [TextButton], Separator, Spacer, and Custom to create items.
func IconButton ¶
IconButton creates a button item with an icon and label. The label is used for accessibility but not displayed by default. Use TextIconButton to show both icon and label.
type ItemKind ¶
type ItemKind uint8
ItemKind identifies the type of toolbar item.
const ( // ItemButton is a clickable icon button with optional text label. ItemButton ItemKind = iota // ItemSeparator is a vertical divider line between button groups. ItemSeparator // ItemSpacer is a flexible gap that pushes subsequent items to the right. ItemSpacer // ItemCustom embeds an arbitrary widget in the toolbar. ItemCustom )
type Option ¶
type Option func(*config)
Option configures a toolbar during construction.
func ButtonSize ¶ added in v0.1.4
ButtonSize sets the icon button size in logical pixels. Default is 36px. JetBrains IDE uses 30px for main toolbar.
func Gap ¶ added in v0.1.4
Gap sets the spacing between toolbar items in logical pixels. Default is 2px. JetBrains IDE uses 10px for main toolbar.
func PainterOpt ¶
PainterOpt sets the painter used to render the toolbar.
type PaintButtonState ¶
type PaintButtonState struct {
Label string
Icon icon.IconData
ShowLabel bool
Hovered bool
Pressed bool
Focused bool
Disabled bool
Bounds geometry.Rect
}
PaintButtonState provides the current button item state to the painter.
type PaintToolbarState ¶
PaintToolbarState provides the current toolbar state to the painter.
type Painter ¶
type Painter interface {
// PaintToolbar renders the toolbar background.
PaintToolbar(canvas widget.Canvas, state PaintToolbarState)
// PaintButtonItem renders a single button item within the toolbar.
PaintButtonItem(canvas widget.Canvas, state PaintButtonState)
// PaintSeparator renders a separator item within the toolbar.
PaintSeparator(canvas widget.Canvas, bounds geometry.Rect)
}
Painter draws the visual representation of a toolbar. Each design system (Material 3, Fluent, Cupertino) provides its own Painter implementation to render the toolbar in its visual style.
If no Painter is set, the toolbar uses DefaultPainter.
type Widget ¶
type Widget struct {
widget.WidgetBase
// contains filtered or unexported fields
}
Widget implements a horizontal toolbar with icon buttons, separators, spacers, and custom widget items.
A toolbar is created with New using functional options:
tb := toolbar.New(
toolbar.Items(
toolbar.IconButton("New", icon.Add, onNew),
toolbar.Separator(),
toolbar.Spacer(),
toolbar.IconButton("Settings", icon.Settings, onSettings),
),
toolbar.Height(40),
)
func New ¶
New creates a new toolbar Widget with the given options.
The returned widget is visible and enabled by default. The default height is 40 logical pixels.
func (*Widget) AccessibilityActions ¶
AccessibilityActions returns nil. Toolbar actions are on individual items.
func (*Widget) AccessibilityHint ¶
AccessibilityHint returns an empty string.
func (*Widget) AccessibilityLabel ¶
AccessibilityLabel returns "Toolbar".
func (*Widget) AccessibilityRole ¶
AccessibilityRole returns a11y.RoleToolbar.
func (*Widget) AccessibilityState ¶
AccessibilityState returns the default state.
func (*Widget) AccessibilityValue ¶
AccessibilityValue returns an empty string.
func (*Widget) HitTestPoint ¶ added in v0.1.4
HitTestPoint returns true if the local-space point hits a toolbar item (button, separator, or custom widget). Returns false for empty gaps between items and spacers — allowing the parent to treat gaps as drag area.
func (*Widget) IsFocusable ¶
IsFocusable reports whether the toolbar can receive focus.
func (*Widget) ItemAt ¶
ItemAt returns the item at the given index, or an empty Item if out of range.