core/toolbar/toolbar.go

Functions Structs

Functions

func AccessibilityActions

AccessibilityActions returns nil. Toolbar actions are on individual items.

func (w *Widget) AccessibilityActions() []a11y.Action {
	return nil
}

func AccessibilityHint

AccessibilityHint returns an empty string.

func (w *Widget) AccessibilityHint() string {
	return ""
}

func AccessibilityLabel

AccessibilityLabel returns "Toolbar".

func (w *Widget) AccessibilityLabel() string {
	return a11yLabel
}

func AccessibilityRole

AccessibilityRole returns [a11y.RoleToolbar].

func (w *Widget) AccessibilityRole() a11y.Role {
	return a11y.RoleToolbar
}

func AccessibilityState

AccessibilityState returns the default state.

func (w *Widget) AccessibilityState() a11y.State {
	return a11y.State{
		Disabled:	!w.IsEnabled(),
		Hidden:		!w.IsVisible(),
	}
}

func AccessibilityValue

AccessibilityValue returns an empty string.

func (w *Widget) AccessibilityValue() string {
	return ""
}

func ButtonSize

ButtonSize sets the icon button size in logical pixels.

Default is 36px. JetBrains IDE uses 30px for main toolbar.

func ButtonSize(px float32) Option {
	return func(c *config) { c.buttonSize = px }
}

func Children

Children returns the custom widget items embedded in the toolbar.

func (w *Widget) Children() []widget.Widget {
	var children []widget.Widget
	for _, item := range w.cfg.items {	//nolint:gocritic
		if item.Kind == ItemCustom && item.Widget != nil {
			children = append(children, item.Widget)
		}
	}
	return children
}

func Draw

Draw renders the toolbar background and all items.

func (w *Widget) Draw(ctx widget.Context, canvas widget.Canvas) {
	if !w.IsVisible() {
		return
	}

	bounds := w.Bounds()

	// Draw toolbar background.
	w.painter.PaintToolbar(canvas, PaintToolbarState{
		Bounds: bounds,
	})

	// Draw items with transform offset.
	canvas.PushTransform(bounds.Min)
	for i, item := range w.cfg.items {	//nolint:gocritic
		itemBounds := w.itemStates[i].bounds
		if itemBounds.IsEmpty() {
			continue
		}

		switch item.Kind {
		case ItemButton:
			w.painter.PaintButtonItem(canvas, PaintButtonState{
				Label:		item.Label,
				Icon:		item.Icon,
				ShowLabel:	item.ShowLabel,
				Hovered:	w.itemStates[i].interaction == stateHover,
				Pressed:	w.itemStates[i].interaction == statePressed,
				Focused:	w.focusIndex == i,
				Disabled:	!item.Enabled,
				Bounds:		itemBounds,
			})
		case ItemSeparator:
			w.painter.PaintSeparator(canvas, itemBounds)
		case ItemSpacer:
			// Spacers are invisible.
		case ItemCustom:
			if item.Widget != nil {
				widget.StampScreenOrigin(item.Widget, canvas)
				item.Widget.Draw(ctx, canvas)
			}
		}
	}
	canvas.PopTransform()
}

func Event

Event handles input events for the toolbar and its items.

func (w *Widget) Event(ctx widget.Context, e event.Event) bool {
	if !w.IsVisible() || !w.IsEnabled() {
		return false
	}

	switch ev := e.(type) {
	case *event.MouseEvent:
		return w.handleMouseEvent(ctx, ev)
	case *event.KeyEvent:
		return w.handleKeyEvent(ctx, ev)
	default:
		// Dispatch to custom widget items.
		return w.dispatchToCustomItems(ctx, e)
	}
}

func Gap

Gap sets the spacing between toolbar items in logical pixels.

Default is 2px. JetBrains IDE uses 10px for main toolbar.

func Gap(px float32) Option {
	return func(c *config) { c.gap = px }
}

func Height

Height sets the toolbar height in logical pixels.

func Height(h float32) Option {
	return func(c *config) {
		c.height = h
	}
}

func HitTestPoint

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 (w *Widget) HitTestPoint(local geometry.Point) bool {
	for i, item := range w.cfg.items {	//nolint:gocritic
		if item.Kind == ItemSpacer {
			continue	// spacers are not interactive
		}
		if w.itemStates[i].bounds.Contains(local) {
			return true
		}
	}
	return false
}

func IsFocusable

IsFocusable reports whether the toolbar can receive focus.

func (w *Widget) IsFocusable() bool {
	return w.IsVisible() && w.IsEnabled()
}

func ItemAt

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

func (w *Widget) ItemAt(idx int) Item {
	if idx < 0 || idx >= len(w.cfg.items) {
		return Item{}
	}
	return w.cfg.items[idx]
}

func ItemCount

ItemCount returns the number of items in the toolbar.

func (w *Widget) ItemCount() int {
	return len(w.cfg.items)
}

func Items

Items sets the toolbar's items.

func Items(items ...Item) Option {
	return func(c *config) {
		c.items = items
	}
}

func Layout

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

func (w *Widget) Layout(ctx widget.Context, constraints geometry.Constraints) geometry.Size {
	preferred := geometry.Sz(constraints.MaxWidth, w.cfg.height)
	size := constraints.Constrain(preferred)

	// Calculate item bounds.
	w.layoutItems(ctx, size)

	w.SetBounds(geometry.FromPointSize(w.Position(), size))
	return size
}

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 New(opts ...Option) *Widget {
	w := &Widget{
		painter:	DefaultPainter{},
		focusIndex:	noFocusIndex,
	}
	w.SetVisible(true)
	w.SetEnabled(true)

	w.cfg.height = defaultHeight

	for _, opt := range opts {
		opt(&w.cfg)
	}

	if w.cfg.painter != nil {
		w.painter = w.cfg.painter
	}

	w.itemStates = make([]itemState, len(w.cfg.items))

	// ADR-028: parent chain for upward dirty propagation.
	// Flutter: RenderObject.adoptChild sets parent on each child.
	for _, item := range w.cfg.items {	//nolint:gocritic // Item is read-only here
		if item.Kind == ItemCustom && item.Widget != nil {
			type parentSetter interface{ SetParent(widget.Widget) }
			if ps, ok := item.Widget.(parentSetter); ok {
				ps.SetParent(w)
			}
		}
	}

	return w
}

func PainterOpt

PainterOpt sets the painter used to render the toolbar.

func PainterOpt(p Painter) Option {
	return func(c *config) {
		c.painter = p
	}
}

Structs

type Widget struct

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

)

type Widget struct {
	widget.WidgetBase

	cfg		config
	painter		Painter
	itemStates	[]itemState
	focusIndex	int	// index of the focused item (-1 = none)
}