focus

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: 3 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultFocusRingOffset float32 = 2.0

DefaultFocusRingOffset is the distance between the widget bounds and the focus ring, in logical pixels.

View Source
const DefaultFocusRingStrokeWidth float32 = 2.0

DefaultFocusRingStrokeWidth is the line width of the focus ring stroke, in logical pixels.

Variables

This section is empty.

Functions

func DrawFocusRing

func DrawFocusRing(canvas widget.Canvas, bounds geometry.Rect, color widget.Color, radius float32)

DrawFocusRing draws a focus indicator around the given bounds.

The ring is drawn as a rounded rectangle outline, offset slightly outside the bounds. The radius controls corner rounding; use 0 for square corners.

Types

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager tracks keyboard focus within a widget tree.

It maintains a reference to the widget tree root and the currently focused widget. Tab and Shift+Tab key events are handled automatically to cycle focus through focusable widgets in depth-first order.

The Manager also supports global keyboard shortcuts that take precedence over tab navigation.

func New

func New(root widget.Widget) *Manager

New creates a new focus Manager for the given widget tree root.

The root widget is the top-level widget whose subtree will be traversed for focus management. It may be nil, in which case the manager operates as a no-op until SetRoot is called.

func (*Manager) Blur

func (m *Manager) Blur()

Blur removes focus from the currently focused widget.

After calling Blur, Manager.Focused returns nil.

func (*Manager) Focus

func (m *Manager) Focus(w widget.Focusable)

Focus sets keyboard focus to the given widget.

If another widget currently has focus, it is blurred first. If w is nil, focus is cleared (equivalent to Manager.Blur). If w is not focusable (IsFocusable returns false), this is a no-op.

func (*Manager) Focused

func (m *Manager) Focused() widget.Focusable

Focused returns the currently focused widget, or nil if no widget has focus.

func (*Manager) HandleKeyEvent

func (m *Manager) HandleKeyEvent(e *event.KeyEvent) bool

HandleKeyEvent processes a key event for focus management.

The event is checked in the following order:

  1. Registered keyboard shortcuts
  2. Tab key (moves focus to next widget)
  3. Shift+Tab (moves focus to previous widget)

Returns true if the event was consumed (shortcut matched or tab navigation occurred), false otherwise.

func (*Manager) Next

func (m *Manager) Next()

Next moves focus to the next focusable widget in tab order.

Tab order is determined by depth-first traversal of the widget tree. If no widget currently has focus, focus moves to the first focusable widget. If the last focusable widget has focus, focus wraps to the first. If no focusable widgets exist, this is a no-op.

func (*Manager) Previous

func (m *Manager) Previous()

Previous moves focus to the previous focusable widget in tab order.

If no widget currently has focus, focus moves to the last focusable widget. If the first focusable widget has focus, focus wraps to the last. If no focusable widgets exist, this is a no-op.

func (*Manager) RegisterShortcut

func (m *Manager) RegisterShortcut(s Shortcut, handler func())

RegisterShortcut registers a global keyboard shortcut.

When the shortcut's key combination is pressed, the handler function is called. Shortcuts take precedence over tab navigation.

func (*Manager) SetRoot

func (m *Manager) SetRoot(root widget.Widget)

SetRoot replaces the widget tree root.

If the currently focused widget is no longer in the new tree, focus is cleared.

func (*Manager) UnregisterShortcut

func (m *Manager) UnregisterShortcut(s Shortcut)

UnregisterShortcut removes all handlers for the given shortcut.

type Shortcut

type Shortcut struct {
	// Key is the key code that triggers this shortcut.
	Key event.Key

	// Ctrl indicates whether the Control modifier must be held.
	Ctrl bool

	// Shift indicates whether the Shift modifier must be held.
	Shift bool

	// Alt indicates whether the Alt modifier must be held.
	Alt bool
}

Shortcut defines a keyboard shortcut as a combination of a key and modifier flags.

Shortcuts are matched against key press events. A shortcut matches when the key matches and all specified modifiers are held.

func (Shortcut) Matches

func (s Shortcut) Matches(e *event.KeyEvent) bool

Matches reports whether the shortcut matches the given key event.

Jump to

Keyboard shortcuts

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