Scoop API

scoop:scoop.ui:0.1.0

src/accessibility/content.scoop

Selected production source · src/accessibility/content.scoop

1package scoop.ui
3import scoop.collections.HashMap
4import scoop.collections.HashSet
5import scoop.window.accessibility.*
6import scoop.window.accessibility.copy
7import scoop.surface.InputScope
9/** Reconciles declared content only when its membership or eligibility changes. */
10internal class SemanticContent(private val ordered: Array<SemanticsHandle>) {
11 private val children = HashMap<SemanticNodeId, ArrayList<SemanticsHandle>>()
12 private val concealed = HashSet<SemanticNodeId>()
13 private val merged = HashSet<SemanticNodeId>()
14 private val content = HashMap<SemanticNodeId, Array<SemanticNodeId>>()
16 init {
17 for (handle in ordered) {
18 if (!handle.active) continue
19 var hidden = handle.properties.hidden || semanticInputExposure(handle) == 2
20 when (val parent = handle.parent) {
21 case Some(val value) -> {
22 val siblings = when (val existing = children[value.id]) {
23 case Some(val items) -> items
25 case None -> {
26 val items = ArrayList<SemanticsHandle>()
27 children[value.id] = items
28 items
29 }
30 }
31 siblings.add(handle)
32 hidden = hidden || concealed.contains(value.id)
33 }
35 case None -> {}
36 }
37 if (hidden) concealed.add(handle.id)
38 }
39 val empty: Array<SemanticNodeId> = []
40 for (handle in ordered) {
41 if (!handle.active) continue
42 if (handle.mergeLabels && !handle.properties.ignored && !concealed.contains(handle.id)
43 ) {
44 val labels = ArrayList<SemanticNodeId>()
45 collect(handle.id, labels)
46 content[handle.id] = labels.toArray()
47 } else
48 content[handle.id] = empty
49 }
50 }
52 private fun collect(parent: SemanticNodeId, labels: ArrayList<SemanticNodeId>) {
53 val values = children[parent] ?: return
54 for (child in values) {
55 if (concealed.contains(child.id)) continue
56 if (child.properties.ignored)
57 collect(child.id, labels)
58 else if (child.mergeCandidate && !children.containsKey(child.id)) {
59 labels.add(child.id)
60 merged.add(child.id)
61 }
62 }
63 }
65 fun apply() {
66 for (handle in ordered) {
67 if (!handle.active) continue
68 val next = content[handle.id]!!
69 if (!sameContent(handle.resource.contentLabels, next))
70 handle.resource.contentLabels = []
71 }
72 for (handle in ordered) {
73 if (!handle.active) continue
74 val ignored = merged.contains(handle.id)
75 if (handle.merged != ignored || handle.contentExposure != semanticInputExposure(handle)
76 ) {
77 handle.merged = ignored
78 handle.publishFacts(handle.properties, handle.actions, handle.resource.relations)
79 }
80 handle.resource.contentLabels = content[handle.id]!!
81 }
82 }
85private fun sameContent(before: Array<SemanticNodeId>, after: Array<SemanticNodeId>): Boolean {
86 if (before.size != after.size) return false
87 for (index in 0L..<before.size)
88 if (before[index] != after[index]) return false
89 return true
92internal fun semanticProperties(
93 handle: SemanticsHandle,
94 value: SemanticProperties,
95 merged: Boolean,
96 exposure: Int = semanticInputExposure(handle)
97): SemanticProperties = value
98 .copy {
99 ignored = value.ignored || merged || exposure == 1
100 hidden = value.hidden || exposure == 2
101 }
103private fun semanticWithin(child: InputScope, parent: InputScope): Boolean {
104 var current: InputScope? = Some(child)
105 while (true) {
106 val scope = current ?: return false
107 if (scope === parent) return true
108 current = scope.parent
109 }
112/** Keeps only ancestry needed to connect the currently admitted modal subtree. */
113internal fun semanticInputExposure(handle: SemanticsHandle): Int {
114 if (handle.owner.layoutConcealed) return 2
115 val boundary = handle.owner.root.surface.inputBoundary ?: return 0
116 val scope = handle.owner.scope.input
117 if (semanticWithin(scope, boundary.input)) return 0
118 return if (semanticWithin(boundary.input, scope)) 1 else 2