aboutsummaryrefslogtreecommitdiff
path: root/vendor/gioui.org/io/semantic/semantic.go
blob: 7499fc8a9d02ebf6da76ff044fcebf0386d4d7c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// SPDX-License-Identifier: Unlicense OR MIT

// Package semantic provides operations for semantic descriptions of a user
// interface, to facilitate presentation and interaction in external software
// such as screen readers.
//
// Semantic descriptions are organized in a tree, with clip operations as
// nodes. Operations in this package are associated with the current semantic
// node, that is the most recent pushed clip operation.
package semantic

import (
	"gioui.org/internal/ops"
	"gioui.org/op"
)

// LabelOp provides the content of a textual component.
type LabelOp string

// DescriptionOp describes a component.
type DescriptionOp string

// ClassOp provides the component class.
type ClassOp int

const (
	Unknown ClassOp = iota
	Button
	CheckBox
	Editor
	RadioButton
	Switch
)

// SelectedOp describes the selected state for components that have
// boolean state.
type SelectedOp bool

// DisabledOp describes the disabled state.
type DisabledOp bool

func (l LabelOp) Add(o *op.Ops) {
	s := string(l)
	data := ops.Write1(&o.Internal, ops.TypeSemanticLabelLen, &s)
	data[0] = byte(ops.TypeSemanticLabel)
}

func (d DescriptionOp) Add(o *op.Ops) {
	s := string(d)
	data := ops.Write1(&o.Internal, ops.TypeSemanticDescLen, &s)
	data[0] = byte(ops.TypeSemanticDesc)
}

func (c ClassOp) Add(o *op.Ops) {
	data := ops.Write(&o.Internal, ops.TypeSemanticClassLen)
	data[0] = byte(ops.TypeSemanticClass)
	data[1] = byte(c)
}

func (s SelectedOp) Add(o *op.Ops) {
	data := ops.Write(&o.Internal, ops.TypeSemanticSelectedLen)
	data[0] = byte(ops.TypeSemanticSelected)
	if s {
		data[1] = 1
	}
}

func (d DisabledOp) Add(o *op.Ops) {
	data := ops.Write(&o.Internal, ops.TypeSemanticDisabledLen)
	data[0] = byte(ops.TypeSemanticDisabled)
	if d {
		data[1] = 1
	}
}

func (c ClassOp) String() string {
	switch c {
	case Unknown:
		return "Unknown"
	case Button:
		return "Button"
	case CheckBox:
		return "CheckBox"
	case Editor:
		return "Editor"
	case RadioButton:
		return "RadioButton"
	case Switch:
		return "Switch"
	default:
		panic("invalid ClassOp")
	}
}