toolbar

package
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2026 License: MIT Imports: 5 Imported by: 0

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:

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

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 Custom

func Custom(w widget.Widget) Item

Custom creates an item that embeds an arbitrary widget.

func IconButton

func IconButton(label string, ic icon.IconData, onClick func()) Item

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.

func Separator

func Separator() Item

Separator creates a vertical divider item.

func Spacer

func Spacer() Item

Spacer creates a flexible gap item that pushes subsequent items to the right.

func TextIconButton

func TextIconButton(label string, ic icon.IconData, onClick func()) Item

TextIconButton creates a button item that displays both icon and label text.

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
)

func (ItemKind) String

func (k ItemKind) String() string

String returns a human-readable name for the item kind.

type Option

type Option func(*config)

Option configures a toolbar during construction.

func ButtonSize added in v0.1.4

func ButtonSize(px float32) Option

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

func Gap(px float32) Option

Gap sets the spacing between toolbar items in logical pixels. Default is 2px. JetBrains IDE uses 10px for main toolbar.

func Height

func Height(h float32) Option

Height sets the toolbar height in logical pixels.

func Items

func Items(items ...Item) Option

Items sets the toolbar's items.

func PainterOpt

func PainterOpt(p Painter) Option

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

type PaintToolbarState struct {
	Bounds geometry.Rect
}

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

func New(opts ...Option) *Widget

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

func (w *Widget) AccessibilityActions() []a11y.Action

AccessibilityActions returns nil. Toolbar actions are on individual items.

func (*Widget) AccessibilityHint

func (w *Widget) AccessibilityHint() string

AccessibilityHint returns an empty string.

func (*Widget) AccessibilityLabel

func (w *Widget) AccessibilityLabel() string

AccessibilityLabel returns "Toolbar".

func (*Widget) AccessibilityRole

func (w *Widget) AccessibilityRole() a11y.Role

AccessibilityRole returns a11y.RoleToolbar.

func (*Widget) AccessibilityState

func (w *Widget) AccessibilityState() a11y.State

AccessibilityState returns the default state.

func (*Widget) AccessibilityValue

func (w *Widget) AccessibilityValue() string

AccessibilityValue returns an empty string.

func (*Widget) Children

func (w *Widget) Children() []widget.Widget

Children returns the custom widget items embedded in the toolbar.

func (*Widget) Draw

func (w *Widget) Draw(ctx widget.Context, canvas widget.Canvas)

Draw renders the toolbar background and all items.

func (*Widget) Event

func (w *Widget) Event(ctx widget.Context, e event.Event) bool

Event handles input events for the toolbar and its items.

func (*Widget) HitTestPoint added in v0.1.4

func (w *Widget) HitTestPoint(local geometry.Point) bool

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

func (w *Widget) IsFocusable() bool

IsFocusable reports whether the toolbar can receive focus.

func (*Widget) ItemAt

func (w *Widget) ItemAt(idx int) Item

ItemAt returns the item at the given index, or an empty Item if out of range.

func (*Widget) ItemCount

func (w *Widget) ItemCount() int

ItemCount returns the number of items in the toolbar.

func (*Widget) Layout

func (w *Widget) Layout(ctx widget.Context, constraints geometry.Constraints) geometry.Size

Layout calculates the toolbar's preferred size within the given constraints.

Jump to

Keyboard shortcuts

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