scoop:scoop.ui:0.1.0
src/focus/order.scoop
Selected production source · src/focus/order.scoop
1
package scoop.ui3
import scoop.collections.sortedBy5
private enum FocusEntry {6
Target(FocusTarget), Group(FocusScope), Items(LazyFocus)7
}9
internal enum FocusStop {10
Target(FocusTarget), Items(LazyFocus)11
}13
internal fun focusStopNode(stop: FocusStop): MountedNode =14
when (stop) {15
case Target(val value) -> value.node16
case Items(val value) -> value.node17
}19
private fun entryOrder(entry: FocusEntry): Int =20
when (entry) {21
case Target(val value) -> value.options.order22
case Group(val value) -> value.options.order23
case Items(val value) -> value.options.order24
}26
private fun collectFocus(27
node: MountedNode,28
boundary: MountedNode,29
entries: ArrayList<FocusEntry>,30
virtual: Boolean31
) {32
if (!node.active)33
return34
if (node !== boundary) {35
when (node.focusScope) {36
case Some(val group) -> {37
entries.add(FocusEntry.Group(group))38
return39
}40
case None -> {}41
}42
}43
for (target in node.focusTargets)44
entries.add(FocusEntry.Target(target))45
if (virtual) {46
when (node.lazyFocus) {47
case Some(val value) -> if (value.usesItems) {48
entries.add(FocusEntry.Items(value))49
return50
}51
case None -> {}52
}53
}54
for (child in node.children)55
collectFocus(child, boundary, entries, virtual)56
}58
internal fun focusOrder(node: MountedNode): Array<FocusTarget> {59
val entries = ArrayList<FocusEntry>()60
collectFocus(node, node, entries, false)61
val result = ArrayList<FocusTarget>()62
for (entry in entries.sortedBy<FocusEntry, Int> { value -> entryOrder(value) }) {63
when (entry) {64
case Target(val value) -> result.add(value)65
case Group(val value) -> {66
for (target in focusOrder(value.node))67
result.add(target)68
}69
case Items(val value) -> {70
for (child in value.node.children)71
for (target in focusOrder(child))72
result.add(target)73
}74
}75
}76
return result.toArray()77
}79
/** A lazy item sequence occupies one logical position without enumerating its data. */80
internal fun tabOrder(node: MountedNode): Array<FocusStop> {81
val entries = ArrayList<FocusEntry>()82
collectFocus(node, node, entries, true)83
val result = ArrayList<FocusStop>()84
for (entry in entries.sortedBy<FocusEntry, Int> { value -> entryOrder(value) }) {85
when (entry) {86
case Target(val value) -> result.add(FocusStop.Target(value))87
case Items(val value) -> result.add(FocusStop.Items(value))88
case Group(val value) -> {89
for (stop in tabOrder(value.node))90
result.add(stop)91
}92
}93
}94
return result.toArray()95
}97
internal fun withinFocusScope(node: MountedNode, boundary: MountedNode): Boolean {98
var cursor: MountedNode? = Some(node)99
while (true) {100
val current = cursor ?: return false101
if (current === boundary)102
return true103
cursor = current.parent104
}105
}