scoop:scoop.ui:0.1.0
src/accessibility/content.scoop
Selected production source · src/accessibility/content.scoop
1
package scoop.ui3
import scoop.collections.HashMap4
import scoop.collections.HashSet5
import scoop.window.accessibility.*6
import scoop.window.accessibility.copy7
import scoop.surface.InputScope9
/** Reconciles declared content only when its membership or eligibility changes. */10
internal 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) continue19
var hidden = handle.properties.hidden || semanticInputExposure(handle) == 220
when (val parent = handle.parent) {21
case Some(val value) -> {22
val siblings = when (val existing = children[value.id]) {23
case Some(val items) -> items25
case None -> {26
val items = ArrayList<SemanticsHandle>()27
children[value.id] = items28
items29
}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) continue42
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
} else48
content[handle.id] = empty49
}50
}52
private fun collect(parent: SemanticNodeId, labels: ArrayList<SemanticNodeId>) {53
val values = children[parent] ?: return54
for (child in values) {55
if (concealed.contains(child.id)) continue56
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) continue68
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) continue74
val ignored = merged.contains(handle.id)75
if (handle.merged != ignored || handle.contentExposure != semanticInputExposure(handle)76
) {77
handle.merged = ignored78
handle.publishFacts(handle.properties, handle.actions, handle.resource.relations)79
}80
handle.resource.contentLabels = content[handle.id]!!81
}82
}83
}85
private fun sameContent(before: Array<SemanticNodeId>, after: Array<SemanticNodeId>): Boolean {86
if (before.size != after.size) return false87
for (index in 0L..<before.size)88
if (before[index] != after[index]) return false89
return true90
}92
internal fun semanticProperties(93
handle: SemanticsHandle,94
value: SemanticProperties,95
merged: Boolean,96
exposure: Int = semanticInputExposure(handle)97
): SemanticProperties = value98
.copy {99
ignored = value.ignored || merged || exposure == 1100
hidden = value.hidden || exposure == 2101
}103
private fun semanticWithin(child: InputScope, parent: InputScope): Boolean {104
var current: InputScope? = Some(child)105
while (true) {106
val scope = current ?: return false107
if (scope === parent) return true108
current = scope.parent109
}110
}112
/** Keeps only ancestry needed to connect the currently admitted modal subtree. */113
internal fun semanticInputExposure(handle: SemanticsHandle): Int {114
if (handle.owner.layoutConcealed) return 2115
val boundary = handle.owner.root.surface.inputBoundary ?: return 0116
val scope = handle.owner.scope.input117
if (semanticWithin(scope, boundary.input)) return 0118
return if (semanticWithin(boundary.input, scope)) 1 else 2119
}