a11y/node.go
Functions
func Actions
func (n *Node) Actions() []Action {
n.mu.RLock()
defer n.mu.RUnlock()
if len(n.actions) == 0 {
return nil
}
result := make([]Action, len(n.actions))
copy(result, n.actions)
return result
}
func Bounds
Bounds returns the node's bounding rectangle in screen coordinates.
func (n *Node) Bounds() geometry.Rect {
n.mu.RLock()
defer n.mu.RUnlock()
return n.bounds
}
func ChildCount
ChildCount returns the number of child nodes.
func (n *Node) ChildCount() int {
n.mu.RLock()
defer n.mu.RUnlock()
return len(n.children)
}
func Children
Children returns a copy of the node's child nodes.
The returned slice is safe to modify without affecting the node.
func (n *Node) Children() []*Node {
n.mu.RLock()
defer n.mu.RUnlock()
if len(n.children) == 0 {
return nil
}
result := make([]*Node, len(n.children))
copy(result, n.children)
return result
}
func Hint
Hint returns the node's hint text.
func (n *Node) Hint() string {
n.mu.RLock()
defer n.mu.RUnlock()
return n.hint
}
func ID
ID returns the node's unique identifier.
The ID is assigned at creation and never changes.
func (n *Node) ID() NodeID {
// ID is immutable after creation, no lock needed.
return n.id
}
func IsValid
IsValid returns true if the node ID is not zero.
A zero NodeID indicates an uninitialized or invalid node.
func (id NodeID) IsValid() bool {
return id != 0
}
func Label
Label returns the node's label text.
func (n *Node) Label() string {
n.mu.RLock()
defer n.mu.RUnlock()
return n.label
}
func NewNode
NewNode creates a new accessibility node with the given role and label.
The node is assigned a unique [NodeID] automatically.
Example:
node := a11y.NewNode(a11y.RoleButton, "OK")
func NewNode(role Role, label string) *Node {
return &Node{
id: NextNodeID(),
role: role,
label: label,
}
}
func NewNodeFromAccessible
NewNodeFromAccessible creates a new accessibility node populated from
the given [Accessible] widget.
The node copies all properties from the widget and stores a reference
to it for future updates via [Node.SyncFromSource].
Example:
var button Accessible = myButton
node := a11y.NewNodeFromAccessible(button)
func NewNodeFromAccessible(a Accessible) *Node {
return &Node{
id: NextNodeID(),
role: a.AccessibilityRole(),
label: a.AccessibilityLabel(),
hint: a.AccessibilityHint(),
value: a.AccessibilityValue(),
state: a.AccessibilityState(),
actions: a.AccessibilityActions(),
source: a,
}
}
func NextNodeID
NextNodeID returns a new unique NodeID.
IDs are generated from an atomic counter starting at 1, ensuring they
are unique within the process lifetime. This function is safe to call
from any goroutine.
func NextNodeID() NodeID {
return NodeID(nodeIDCounter.Add(1))
}
func Parent
Parent returns the node's parent, or nil if this is the root node.
func (n *Node) Parent() *Node {
n.mu.RLock()
defer n.mu.RUnlock()
return n.parent
}
func Role
Role returns the node's accessibility role.
func (n *Node) Role() Role {
n.mu.RLock()
defer n.mu.RUnlock()
return n.role
}
func SetActions
SetActions sets the node's supported accessibility actions.
func (n *Node) SetActions(actions []Action) {
n.mu.Lock()
defer n.mu.Unlock()
n.actions = actions
}
func SetBounds
SetBounds sets the node's bounding rectangle.
func (n *Node) SetBounds(bounds geometry.Rect) {
n.mu.Lock()
defer n.mu.Unlock()
n.bounds = bounds
}
func SetHint
SetHint sets the node's hint text.
func (n *Node) SetHint(hint string) {
n.mu.Lock()
defer n.mu.Unlock()
n.hint = hint
}
func SetLabel
SetLabel sets the node's label text.
func (n *Node) SetLabel(label string) {
n.mu.Lock()
defer n.mu.Unlock()
n.label = label
}
func SetRole
SetRole sets the node's accessibility role.
func (n *Node) SetRole(role Role) {
n.mu.Lock()
defer n.mu.Unlock()
n.role = role
}
func SetSource
SetSource sets the [Accessible] widget backing this node.
func (n *Node) SetSource(a Accessible) {
n.mu.Lock()
defer n.mu.Unlock()
n.source = a
}
func SetState
SetState sets the node's accessibility state.
func (n *Node) SetState(state State) {
n.mu.Lock()
defer n.mu.Unlock()
n.state = state
}
func SetValue
SetValue sets the node's current value.
func (n *Node) SetValue(value string) {
n.mu.Lock()
defer n.mu.Unlock()
n.value = value
}
func Source
Source returns the [Accessible] widget backing this node, or nil.
func (n *Node) Source() Accessible {
n.mu.RLock()
defer n.mu.RUnlock()
return n.source
}
func State
State returns the node's current accessibility state.
func (n *Node) State() State {
n.mu.RLock()
defer n.mu.RUnlock()
return n.state
}
func String
String returns a string representation of the node ID.
func (id NodeID) String() string {
return fmt.Sprintf("NodeID(%d)", uint64(id))
}
func String
String returns a human-readable representation of the node.
func (n *Node) String() string {
n.mu.RLock()
defer n.mu.RUnlock()
return fmt.Sprintf("Node{ID: %d, Role: %s, Label: %q}", uint64(n.id), n.role, n.label)
}
func SyncFromSource
SyncFromSource updates the node's properties from its source [Accessible].
If the node has no source, this is a no-op and returns false.
Returns true if the node was updated.
func (n *Node) SyncFromSource() bool {
n.mu.Lock()
defer n.mu.Unlock()
if n.source == nil {
return false
}
n.role = n.source.AccessibilityRole()
n.label = n.source.AccessibilityLabel()
n.hint = n.source.AccessibilityHint()
n.value = n.source.AccessibilityValue()
n.state = n.source.AccessibilityState()
n.actions = n.source.AccessibilityActions()
return true
}
func Value
Value returns the node's current value as a string.
func (n *Node) Value() string {
n.mu.RLock()
defer n.mu.RUnlock()
return n.value
}
Structs
type Node struct
Node represents a single element in the accessibility tree.
Each Node has a stable [NodeID] for identification, a [Role] for semantics,
and optional properties like label, hint, value, state, bounds, and actions.
Nodes form a tree structure through parent-child relationships.
Nodes are safe for concurrent access. All reads and writes are protected
by a sync.RWMutex.
# Creating Nodes
Create a node with [NewNode] or [NewNodeFromAccessible]:
node := a11y.NewNode(a11y.RoleButton, "Save")
node.SetHint("Saves the current document")
node.SetActions([]a11y.Action{a11y.ActionClick})
# Tree Relationships
Nodes maintain parent-child relationships. Use [Tree.Insert] and
[Tree.Remove] to manage the tree structure rather than manipulating
parent/children directly.
type Node struct {
mu sync.RWMutex
id NodeID
role Role
label string
hint string
value string
state State
actions []Action
bounds geometry.Rect
parent *Node
children []*Node
// source is the Accessible widget that this node represents.
// It may be nil for synthetic nodes not backed by a widget.
source Accessible
}
Actions returns the node's supported accessibility actions.
The returned slice is a copy and safe to modify.