Scoop API

scoop:scoop.window:0.1.0

src/accessibility/relations.scoop

Selected production source · src/accessibility/relations.scoop

1package scoop.window.accessibility
3import scoop.collections.HashSet
4import scoop.collections.contains
5import scoop.collections.toList
6import scoop.window.*
8internal fun checkSemanticRelations(source: AccessibilityNode, next: SemanticRelations) {
9 for (ids in next.references)
10 checkSemanticTargets(source, ids)
11 when (next.activeDescendant) {
12 case Some(Item(val id, _)) -> {
13 val target = source.provider.lookup(id)!!
14 requireWindow(
15 target.role ==
16 SemanticRole.List ||
17 target.role ==
18 SemanticRole.Table ||
19 target.role ==
20 SemanticRole.Tree,
21 "active item requires a collection target"
22 )
23 }
25 else -> {}
26 }
29internal fun refreshSemanticItemReferences(collection: AccessibilityNode) {
30 for (source in collection.referrers.values.toList()) {
31 when (source.relationValues.activeDescendant) {
32 case Some(Item(val id, val key)) -> {
33 if (id == collection.id && !(collection.collectionValue?.contains(key) ?: false)) {
34 val previous = source.relationValues
35 val next = SemanticRelations(
36 previous.labelledBy,
37 previous.describedBy,
38 previous.errorMessages,
39 previous.controls,
40 None,
41 previous.rowHeaders,
42 previous.columnHeaders
43 )
44 installSemanticReferences(source, next, source.content)
45 source.provider.changed(source)
46 }
47 }
49 else -> {}
50 }
51 }
54internal fun checkSemanticTargets(source: AccessibilityNode, ids: Array<SemanticNodeId>) {
55 val seen = HashSet<SemanticNodeId>()
56 for (id in ids) {
57 val target = source.provider.lookup(id)
58 requireWindow(
59 (target?.active ?: false) && id != source.id,
60 "semantic relation requires another active node in the same provider"
61 )
62 requireWindow(seen.add(id), "duplicate semantic relation target")
63 }
66/** Maintains only real references, so retirement and text updates visit their dependents. */
67internal fun installSemanticReferences(
68 source: AccessibilityNode,
69 next: SemanticRelations,
70 content: Array<SemanticNodeId>,
71 textChildren: Array<SemanticTextChild> = source.textChildValues
72) {
73 val previous = source.relationValues
74 for (ids in previous.references)
75 for (id in ids) {
76 val target = source.provider.lookup(id) ?: continue
77 target.referrers.remove(source.id.number)
78 source.provider.changed(target)
79 }
80 for (id in source.content) {
81 val target = source.provider.lookup(id) ?: continue
82 target.referrers.remove(source.id.number)
83 source.provider.changed(target)
84 }
85 for (child in source.textChildValues) {
86 val target = source.provider.lookup(child.node) ?: continue
87 target.referrers.remove(source.id.number)
88 }
89 source.relationValues = next
90 source.content = content
91 source.textChildValues = textChildren
92 for (ids in next.references)
93 for (id in ids) {
94 val target = source.provider.lookup(id) ?: continue
95 target.referrers[source.id.number] = source
96 source.provider.changed(target)
97 }
98 for (id in content) {
99 val target = source.provider.lookup(id) ?: continue
100 target.referrers[source.id.number] = source
101 source.provider.changed(target)
102 }
103 for (child in textChildren) {
104 val target = source.provider.lookup(child.node) ?: continue
105 target.referrers[source.id.number] = source
106 }
109internal fun retireSemanticRelations(node: AccessibilityNode) {
110 for (source in node.referrers.values.toList()) {
111 if (!source.active) continue
112 val old = source.relationValues
113 val next = SemanticRelations(
114 withoutSemanticId(old.labelledBy, node.id),
115 withoutSemanticId(old.describedBy, node.id),
116 withoutSemanticId(old.errorMessages, node.id),
117 withoutSemanticId(old.controls, node.id),
118 when (old.activeDescendant) {
119 case Some(val value) -> if (value.node == node.id) None else old.activeDescendant
120 case None -> None
121 },
122 withoutSemanticId(old.rowHeaders, node.id),
123 withoutSemanticId(old.columnHeaders, node.id)
124 )
125 val content = withoutSemanticId(source.content, node.id)
126 val nameChanged = source.properties.name ==
127 None &&
128 (
129 !sameSemanticIds(old.labelledBy, next.labelledBy) ||
130 !sameSemanticIds(source.content, content)
131 )
132 installSemanticReferences(
133 source,
134 next,
135 content,
136 withoutSemanticTextChild(source.textChildValues, node.id)
137 )
138 if (nameChanged) source.invalidateContent()
139 semanticTextChanged(source, nameChanged)
140 source.provider.changed(source)
141 }
142 installSemanticReferences(node, SemanticRelations(), [], [])
143 node.referrers.clear()
146private fun withoutSemanticId(ids: Array<SemanticNodeId>, removed: SemanticNodeId): Array<
147 SemanticNodeId
148> {
149 val result = ArrayList<SemanticNodeId>()
150 for (id in ids)
151 if (id != removed) result.add(id)
152 return result.toArray()
155internal fun semanticTextChanged(node: AccessibilityNode, nameChanged: Boolean) {
156 node.textDirty = true
157 if (!nameChanged) return
158 node.nameDirty = true
159 val pending = ArrayList<AccessibilityNode>()
160 val visited = HashSet<SemanticNodeId>()
161 pending.add(node)
162 while (pending.size > 0L) {
163 val target = pending.removeAt(pending.size - 1L)
164 if (!visited.add(target.id)) continue
165 for (source in target.referrers.values) {
166 if (!source.active) continue
167 val name = source.properties.name ==
168 None &&
169 (
170 source.relationValues.labelledBy.contains(target.id) ||
171 source.content.contains(target.id)
172 )
173 val description = source.properties.description ==
174 None &&
175 source.relationValues.describedBy.contains(target.id)
176 val error = source.properties.error ==
177 None &&
178 source.relationValues.errorMessages.contains(target.id)
179 if (name || description || error) {
180 source.textDirty = true
181 source.provider.changed(source)
182 }
183 if (name) {
184 source.nameDirty = true
185 source.invalidateContent()
186 pending.add(source)
187 }
188 }
189 }
192internal fun resolveSemanticText(node: AccessibilityNode) {
193 if (!node.textDirty) return
194 resolveSemanticName(node)
195 node.resolvedDescription = semanticTextOr(
196 node,
197 node.properties.description,
198 node.relationValues.describedBy
199 )
200 node.resolvedError = semanticTextOr(
201 node,
202 node.properties.error,
203 node.relationValues.errorMessages
204 )
205 node.textDirty = false
208private fun resolveSemanticName(node: AccessibilityNode): String? {
209 if (!node.nameDirty) return node.resolvedName
210 requireWindow(!node.resolvingName, "semantic labels would create a name cycle")
211 node.resolvingName = true
212 try {
213 var name = semanticTextOr(node, node.properties.name, node.relationValues.labelledBy)
214 if (name == None) name = semanticTextOr(node, None, node.content)
215 node.resolvedName = name
216 node.nameDirty = false
217 return name
218 } finally {
219 node.resolvingName = false
220 }
223private fun semanticTextOr(
224 source: AccessibilityNode,
225 explicit: String?,
226 ids: Array<SemanticNodeId>
227): String? {
228 when (explicit) {
229 case Some(_) -> return explicit
230 case None -> {}
231 }
232 var known = false
233 var text = ""
234 for (id in ids) {
235 val target = source.provider.lookup(id) ?: continue
236 if (!target.active) continue
237 var value = resolveSemanticName(target)
238 if (value == None && target.role == SemanticRole.Text && !target.properties.protectedText)
239 value = target.properties.value
240 when (value) {
241 case Some(val content) -> {
242 known = true
243 if (content.byteLength > 0L) {
244 if (text.byteLength > 0L) text += " "
245 text += content
246 }
247 }
249 case None -> {}
250 }
251 }
252 return if (known) Some(text) else None