scoop:scoop.ui:0.1.0
src/accessibility/tree.scoop
Selected production source · src/accessibility/tree.scoop
1
package scoop.ui3
import scoop.window.accessibility.*5
/** Only an actual structural change walks the mounted tree, in declaration order. */6
internal class SemanticOrder {7
private var next = 0L8
val handles = ArrayList<SemanticsHandle>()9
fun apply(node: MountedNode, inherited: SemanticsHandle?) {10
if (!node.active)11
return12
val ancestor =13
when (node.body) {14
case Portal(_) -> None15
else -> inherited16
}17
for (handle in node.semanticHandles) {18
if (!handle.active)19
continue20
val parent = semanticParent(handle.explicitParent, ancestor)21
handle.resource.reparent(parent?.resource, next)22
handle.parent = parent23
handles.add(handle)24
next += 1L25
}26
val route = semanticParent(node.semanticRoute, ancestor)27
for (child in node.children)28
apply(child, route)29
}30
}32
internal fun semanticParent(33
local: SemanticsHandle?,34
inherited: SemanticsHandle?35
): SemanticsHandle? =36
when (local) {37
case Some(val value) -> Some(value)38
case None -> inherited39
}41
internal fun inheritedSemantics(node: MountedNode): SemanticsHandle? {42
var ancestor = node.parent43
while (true) {44
val current = ancestor ?: return None45
when (current.body) {46
case Portal(_) -> return None47
else -> {}48
}49
when (val route = current.semanticRoute) {50
case Some(val handle) -> if (handle.active)51
return Some(handle)52
case None -> {}53
}54
ancestor = current.parent55
}56
}58
internal fun checkSemanticParent(node: MountedNode, parent: SemanticsHandle?) {59
when (parent) {60
case Some(val handle) -> checkSemanticOwner(node, handle)61
case None -> {}62
}63
}65
internal fun checkSemanticOwner(node: MountedNode, handle: SemanticsHandle) {66
handle.checkOpen()67
var ancestor: MountedNode? = Some(node)68
while (true) {69
val current = ancestor ?: break70
if (current === handle.owner)71
return72
ancestor = current.parent73
}74
throw IllegalArgumentException(75
Some("semantic resource must belong to this mount or an ancestor")76
)77
}