Skip to content

a11y ​

go
import "github.com/tangoui/tango/ui/a11y"

本页由 site/tools/apigen 从源码注释生成(按 darwin 构建标签选文件),请勿手改。

Package a11y defines the platform-neutral accessibility (semantics) tree produced by the Tango UI runner.

Every frame in which a host asks for it (ui.AccessibilityHost), the runner derives a Tree from the widget tree: one Node per semantic element (a button, a text field, a table cell, a dialog …) with a stable ID, a Role, an accessible name, value, states, bounds and the actions a screen reader may perform. Nodes that carry no meaning (layout rows, decorations) are flattened away. The runner compares the new tree with the previous one and reports only the IDs that were added, changed or removed, so a platform bridge (NSAccessibility, UI Automation, AT-SPI, UIAccessibility, AccessibilityNodeInfo) can update its proxy objects incrementally.

The package has no dependency on the ui package; bridges only need the Tree, the Node fields and the role mapping in Mappings (documented in docs/accessibility.md).

变量 ​

go
var Mappings = map[Role]Platform{
	RoleWindow:        {"AXGroup", "Pane", "PANEL", "container", "android.view.View"},
	RoleGroup:         {"AXGroup", "Group", "PANEL", "container", "android.view.ViewGroup"},
	RoleText:          {"AXStaticText", "Text", "STATIC", "staticText", "android.widget.TextView"},
	RoleHeading:       {"AXHeading", "Text", "HEADING", "header", "android.widget.TextView"},
	RoleParagraph:     {"AXStaticText", "Text", "PARAGRAPH", "staticText", "android.widget.TextView"},
	RoleLink:          {"AXLink", "Hyperlink", "LINK", "link", "android.widget.TextView"},
	RoleButton:        {"AXButton", "Button", "PUSH_BUTTON", "button", "android.widget.Button"},
	RoleCheckbox:      {"AXCheckBox", "CheckBox", "CHECK_BOX", "button+toggleButton", "android.widget.CheckBox"},
	RoleRadio:         {"AXRadioButton", "RadioButton", "RADIO_BUTTON", "button", "android.widget.RadioButton"},
	RoleRadioGroup:    {"AXRadioGroup", "Group", "GROUPING", "container", "android.widget.RadioGroup"},
	RoleSwitch:        {"AXCheckBox/AXSwitch", "Button", "TOGGLE_BUTTON", "button+toggleButton", "android.widget.Switch"},
	RoleTextbox:       {"AXTextField", "Edit", "ENTRY", "none (UITextInput)", "android.widget.EditText"},
	RoleSearchbox:     {"AXTextField/AXSearchField", "Edit", "ENTRY", "searchField", "android.widget.EditText"},
	RoleSpinButton:    {"AXIncrementor", "Spinner", "SPIN_BUTTON", "adjustable", "android.widget.EditText"},
	RoleCombobox:      {"AXComboBox", "ComboBox", "COMBO_BOX", "button", "android.widget.Spinner"},
	RoleListbox:       {"AXList", "List", "LIST_BOX", "container", "android.widget.ListView"},
	RoleOption:        {"AXStaticText", "ListItem", "LIST_ITEM", "button", "android.widget.CheckedTextView"},
	RoleMenu:          {"AXMenu", "Menu", "MENU", "container", "android.widget.ListView"},
	RoleMenuBar:       {"AXMenuBar", "MenuBar", "MENU_BAR", "container", "android.view.ViewGroup"},
	RoleMenuItem:      {"AXMenuItem", "MenuItem", "MENU_ITEM", "button", "android.view.View"},
	RoleMenuItemRadio: {"AXMenuItem", "MenuItem", "RADIO_MENU_ITEM", "button", "android.view.View"},
	RoleTab:           {"AXRadioButton/AXTabButton", "TabItem", "PAGE_TAB", "button", "android.view.View"},
	RoleTabList:       {"AXTabGroup", "Tab", "PAGE_TAB_LIST", "tabBar", "android.widget.TabWidget"},
	RoleTabPanel:      {"AXGroup", "Pane", "SCROLL_PANE", "container", "android.view.ViewGroup"},
	RoleDialog:        {"AXGroup/AXApplicationDialog", "Pane", "DIALOG", "container (accessibilityViewIsModal)", "android.view.ViewGroup"},
	RoleAlertDialog:   {"AXGroup/AXApplicationAlertDialog", "Pane", "ALERT", "container (accessibilityViewIsModal)", "android.view.ViewGroup"},
	RoleAlert:         {"AXGroup/AXApplicationAlert", "Group", "NOTIFICATION", "staticText", "android.view.View"},
	RoleStatus:        {"AXGroup/AXApplicationStatus", "StatusBar", "STATUS_BAR", "staticText", "android.view.View"},
	RoleTooltip:       {"AXGroup/AXUserInterfaceTooltip", "ToolTip", "TOOL_TIP", "staticText", "android.widget.TextView"},
	RoleSlider:        {"AXSlider", "Slider", "SLIDER", "adjustable", "android.widget.SeekBar"},
	RoleProgressBar:   {"AXProgressIndicator", "ProgressBar", "PROGRESS_BAR", "updatesFrequently", "android.widget.ProgressBar"},
	RoleTable:         {"AXTable", "Table", "TABLE", "container (dataTable)", "android.widget.GridView"},
	RoleGrid:          {"AXTable", "DataGrid", "TABLE", "container (dataTable)", "android.widget.GridView"},
	RoleRow:           {"AXRow", "DataItem", "TABLE_ROW", "container", "android.view.View"},
	RoleCell:          {"AXCell", "DataItem", "TABLE_CELL", "staticText", "android.view.View"},
	RoleColumnHeader:  {"AXCell", "HeaderItem", "COLUMN_HEADER", "header", "android.view.View"},
	RoleRowHeader:     {"AXCell", "HeaderItem", "ROW_HEADER", "header", "android.view.View"},
	RoleTree:          {"AXOutline", "Tree", "TREE", "container", "android.widget.ExpandableListView"},
	RoleTreeItem:      {"AXRow/AXOutlineRow", "TreeItem", "TREE_ITEM", "button", "android.view.View"},
	RoleList:          {"AXList", "List", "LIST", "container", "android.widget.ListView"},
	RoleListItem:      {"AXGroup", "ListItem", "LIST_ITEM", "staticText", "android.view.View"},
	RoleImage:         {"AXImage", "Image", "IMAGE", "image", "android.widget.ImageView"},
	RoleNavigation:    {"AXGroup/AXLandmarkNavigation", "Group", "LANDMARK", "container", "android.view.ViewGroup"},
	RoleToolbar:       {"AXToolbar", "ToolBar", "TOOL_BAR", "container", "android.widget.Toolbar"},
	RoleSeparator:     {"AXSplitter", "Separator", "SEPARATOR", "adjustable (splitter only)", "android.view.View"},
	RoleScrollArea:    {"AXScrollArea", "Pane", "SCROLL_PANE", "container", "android.widget.ScrollView"},
	RoleForm:          {"AXGroup/AXLandmarkForm", "Group", "FORM", "container", "android.view.ViewGroup"},
	RoleFigure:        {"AXGroup", "Group", "CHART", "image (AXChart descriptor)", "android.view.View"},
	RoleTimer:         {"AXGroup/AXApplicationTimer", "Text", "TIMER", "staticText+updatesFrequently", "android.widget.TextView"},
	RoleMeter:         {"AXLevelIndicator", "ProgressBar", "LEVEL_BAR", "staticText", "android.widget.ProgressBar"},
}

Mappings maps every exposed role to its native counterparts.

类型 ​

Action ​

go
type Action uint16

Action is a set of operations a host may ask a node to perform (ui.Runner.PerformAction).

Append-only: a new flag takes the next free bit; existing bits never change.

go
const (
	Press          Action = 1 << iota // activate (click, toggle, select an option)
	Increment                         // step a slider / spin button up
	Decrement                         // step it down
	Expand                            // open a combobox, expand a tree item / collapse panel
	Collapse                          // close it
	SetValue                          // replace the value (text, or a number for ranges)
	Focus                             // move keyboard focus here
	ScrollIntoView                    // scroll ancestors so the node is visible
	Dismiss                           // close a dialog, popup or message (Escape)
)

func (Action) Has ​

go
func (a Action) Has(x Action) bool

Has reports whether every action of x is supported.

func (Action) String ​

go
func (a Action) String() string

String lists the actions ("press|focus").

Announcement ​

go
type Announcement struct {
	Text string
	Live Live
}

Announcement is a message for the screen reader that is not tied to a persistent node (ui.Context.Announce). Hosts speak it once.

Live ​

go
type Live uint8

Live is the politeness of a live region: how a screen reader announces changes of its content.

Append-only: never reorder or insert; the zero value stays LiveOff.

go
const (
	LiveOff       Live = iota
	LivePolite         // announce when the user is idle (status, toast)
	LiveAssertive      // interrupt (errors, alerts)
)

func (Live) String ​

go
func (l Live) String() string

Node ​

go
type Node struct {
	ID       NodeID
	Parent   NodeID   // 0 for the root
	Children []NodeID // in reading order
	Role     Role

	Name        string // accessible name (label)
	Description string // longer help / validation message / chart summary
	Value       string // text value (text field content, selected option, "3 of 5")
	Placeholder string // hint shown in an empty text field
	// RoleDescription refines the role for speech ("chart", "color picker").
	RoleDescription string

	// Range values (slider, progress bar, spin button, meter); valid when
	// HasRange.
	HasRange                           bool
	ValueNow, ValueMin, ValueMax, Step float64

	States  State
	Actions Action
	Bounds  Rect
	Live    Live

	Level              int // heading level 1-6, tree item depth (1 = top level)
	PosInSet, SetSize  int // 1-based position among siblings of a set (options, tabs, tree items)
	RowIndex, ColIndex int // 1-based position in a table / grid (rows, cells, headers)
	RowCount, ColCount int // table / grid size (including rows not built by virtualization)
	Sort               Sort

	// Relations (IDs of other nodes in the same tree, 0 = none).
	LabelledBy       NodeID // node whose text is the name (form label)
	DescribedBy      NodeID // node whose text is the description (error message)
	Controls         NodeID // popup / panel this node opens or drives
	ActiveDescendant NodeID // focused item of a composite that keeps focus itself

	Shortcut string // keyboard shortcut, e.g. "Ctrl+K"

	// RTL reports that the node's content lays out and reads right to
	// left (its ConfigProvider scope; nested scopes may differ from the
	// window, see Tree.RTL). It is informational for bridges that expose a
	// text direction: Bounds are already the mirrored screen rectangles and
	// Children stay in reading order, so hosts must not flip either.
	RTL bool
}

Node is one element of the semantics tree. Zero fields mean "not applicable"; hosts should not expose them.

func (*Node) Equal ​

go
func (a *Node) Equal(b *Node) bool

Equal reports whether a and b describe the same node (all fields, including the order of children).

func (*Node) String ​

go
func (n *Node) String() string

String formats the node for logs and test failures.

NodeID ​

go
type NodeID uint64

NodeID identifies a node across frames. It equals the ui.ID of the widget the node was derived from, so it stays stable as long as the widget does.

func Diff ​

go
func Diff(prev, next *Tree) []NodeID

Diff returns the IDs of nodes added or changed in next, followed by the IDs of nodes of prev that next no longer has (look them up in next: a nil Node means "removed"). A nil prev reports every node of next.

Platform ​

go
type Platform struct {
	Mac     string // NSAccessibility role, "/Subrole" when one is needed
	UIA     string // UI Automation ControlType (UIA_<X>ControlTypeId)
	ATSPI   string // AT-SPI role (ATSPI_ROLE_<X>)
	IOS     string // UIAccessibilityTraits (or a note for containers)
	Android string // AccessibilityNodeInfo.className (TalkBack derives the spoken role from it)
}

Platform names how a role is exposed by each native accessibility API. Bridges look roles up in Mappings; docs/accessibility.md prints the same table (TestMappingsDocumented keeps them in sync).

Rect ​

go
type Rect struct{ X, Y, W, H float32 }

Rect is a rectangle in window logical pixels (the coordinates of ui.Window.Size; multiply by Window.Scale for device pixels).

Role ​

go
type Role uint8

Role is the semantic type of a node (WAI-ARIA vocabulary).

Append-only: never reorder or insert; the zero value stays RoleNone.

go
const (
	RoleNone      Role = iota // not exposed (internal use)
	RoleWindow                // the root of a tree: the window content
	RoleGroup                 // generic grouping (card, form item, fieldset)
	RoleText                  // static text
	RoleHeading               // title; Node.Level is 1-6
	RoleParagraph             // block of text
	RoleLink
	RoleButton // push button; a toggle button carries Toggleable (+ Pressed)
	RoleCheckbox
	RoleRadio
	RoleRadioGroup
	RoleSwitch
	RoleTextbox    // single or multi-line (Multiline) text input; Protected for passwords
	RoleSearchbox  // text input for searching
	RoleSpinButton // numeric input with step buttons
	RoleCombobox   // input or button that opens a listbox / tree / grid
	RoleListbox
	RoleOption
	RoleMenu
	RoleMenuBar
	RoleMenuItem
	RoleTab
	RoleTabList
	RoleTabPanel
	RoleDialog
	RoleAlertDialog
	RoleAlert  // important, usually time-sensitive message (assertive)
	RoleStatus // advisory message (polite)
	RoleTooltip
	RoleSlider
	RoleProgressBar
	RoleTable
	RoleGrid // interactive table (keyboard moves between cells)
	RoleRow
	RoleCell
	RoleColumnHeader
	RoleRowHeader
	RoleTree
	RoleTreeItem
	RoleList
	RoleListItem
	RoleImage
	RoleNavigation // breadcrumb, pagination, anchor
	RoleToolbar
	RoleSeparator // static divider; focusable (with Value range) when it resizes panes
	RoleScrollArea
	RoleForm
	RoleFigure        // chart: Name + Description summary
	RoleTimer         // countdown
	RoleMeter         // read-only scalar (Statistic, Gauge)
	RoleMenuItemRadio // a checkable single-choice menu item

)

func ParseRole ​

go
func ParseRole(s string) (Role, bool)

ParseRole returns the role named s (as printed by Role.String).

func Roles ​

go
func Roles() []Role

Roles returns every exposed role (RoleNone excluded) in declaration order.

func (Role) Atomic ​

go
func (r Role) Atomic() bool

Atomic reports whether the role's descendants are presentational: their text becomes the node's name and plain text children are not exposed separately (a button's label, an option's text).

func (Role) Interactive ​

go
func (r Role) Interactive() bool

Interactive reports whether the role is a control a user operates (and therefore needs an accessible name and keyboard access).

func (Role) NameFromContent ​

go
func (r Role) NameFromContent() bool

NameFromContent reports whether a node of this role without an explicit name is named by the text it contains (WAI-ARIA "name from content").

func (Role) String ​

go
func (r Role) String() string

String returns the WAI-ARIA style role name ("button", "treeitem").

Sort ​

go
type Sort uint8

Sort is the sort direction of a column header.

Append-only: never reorder or insert; the zero value stays SortNone.

go
const (
	SortNone Sort = iota
	SortAscending
	SortDescending
)

func (Sort) String ​

go
func (s Sort) String() string

State ​

go
type State uint32

State is a set of boolean node states.

Append-only: a new flag takes the next free bit; existing bits never change.

go
const (
	Disabled        State = 1 << iota
	Focusable             // can take keyboard focus
	Focused               // has keyboard focus
	Checkable             // has a checked state (checkbox, radio, switch, menu item)
	Checked               // checked / on
	Mixed                 // indeterminate (checkbox, tri-state tree item)
	Selectable            // can be selected (option, tab, row, cell, tree item)
	Selected              // selected / current tab
	Expandable            // has an expanded state
	Expanded              // expanded / open
	Toggleable            // toggle button: has a pressed state
	Pressed               // toggle button pressed
	Required              // form field must be filled
	Invalid               // failed validation; Description holds the message
	ReadOnly              // value can be read and focused but not edited
	Multiline             // multi-line text input
	Protected             // password input: Value is not exposed
	Modal                 // dialog that makes the rest of the window inert
	Multiselectable       // listbox / tree / table allows several selected items
	Busy                  // loading; content may change
	Offscreen             // scrolled out of the visible area
	Current               // current item of a set (page, step, breadcrumb)
	HasPopup              // opens a menu, listbox or dialog
	Visited               // visited link
	Vertical              // orientation (sliders, separators, tab lists, menus)
	Horizontal
	Editable // text can be typed (combobox with search)
)

func (State) Has ​

go
func (s State) Has(x State) bool

Has reports whether every state of x is set.

func (State) String ​

go
func (s State) String() string

String lists the set states ("focusable|checked").

Tree ​

go
type Tree struct {
	Root  NodeID
	Focus NodeID // node with keyboard focus (0: none)
	// Announcements made during the frame that produced this tree.
	Announcements []Announcement
	// RTL is the window's default direction (its root scope). Nodes carry
	// their own scope's direction in Node.RTL.
	RTL bool
	// contains filtered or unexported fields
}

Tree is a snapshot of the semantics of one window. It is immutable once handed to a host: the runner builds a new Tree every frame it reports.

func NewTree ​

go
func NewTree(root NodeID) *Tree

NewTree returns an empty tree; Add nodes parent-first.

func (*Tree) Add ​

go
func (t *Tree) Add(n *Node)

Add inserts n (its ID must be unique in the tree). Builders add nodes in depth-first order so that Walk and Nodes follow reading order.

func (*Tree) Ancestor ​

go
func (t *Tree) Ancestor(id NodeID, role Role) *Node

Ancestor returns the nearest ancestor of id with role, or nil.

func (*Tree) Dump ​

go
func (t *Tree) Dump() string

Dump formats the tree as an indented outline (debugging, golden tests).

func (*Tree) Find ​

go
func (t *Tree) Find(role Role, name string) *Node

Find returns the first node (reading order) with role and name; role RoleNone matches any role and name "" any name.

func (*Tree) FindAll ​

go
func (t *Tree) FindAll(role Role, name string) []*Node

FindAll returns every node with role and name (see Find).

func (*Tree) Len ​

go
func (t *Tree) Len() int

Len returns the number of nodes.

func (*Tree) Node ​

go
func (t *Tree) Node(id NodeID) *Node

Node returns the node with id, or nil.

func (*Tree) Nodes ​

go
func (t *Tree) Nodes() []*Node

Nodes returns every node in depth-first (reading) order.

func (*Tree) Remove ​

go
func (t *Tree) Remove(id NodeID)

Remove deletes node id (not its children) from the tree. Builders use it to fold presentational children into their parent; the caller fixes the parent's Children.

func (*Tree) Text ​

go
func (t *Tree) Text(id NodeID) string

Text returns the names of n and its descendants joined by spaces (useful to read the content of a dialog or a live region).

func (*Tree) Walk ​

go
func (t *Tree) Walk(fn func(n *Node, depth int) bool)

Walk calls fn for every node depth first starting at the root; depth is 0 for the root. Returning false skips the node's children.

以 MIT 许可发布