scoop:scoop.ui:0.1.0
src/focus/cursors.scoop
Selected production source · src/focus/cursors.scoop
1
package scoop.ui3
/** One finite mounted sequence, optionally wrapping once at its scope boundary. */4
internal class NodeFocusCursor(5
val node: MountedNode,6
val reverse: Boolean,7
private val loop: Boolean,8
private val excluded: MountedNode? = None9
) {10
val stops = tabOrder(node)11
private var index =12
if (reverse)13
stops.size14
else15
-1L16
private var remaining = stops.size18
fun seekAfter(position: Long) {19
index = position20
remaining =21
if (loop)22
stops.size23
else24
if (reverse)25
position26
else27
stops.size - position - 1L28
}29
fun seekAfterSubtree(boundary: MountedNode) {30
var found: Long? = None31
for (position in 0L ..< stops.size) {32
if (!withinFocusScope(focusStopNode(stops[position]), boundary))33
continue34
if (!reverse || found == None)35
found = Some(position)36
}37
when (found) {38
case Some(val position) -> seekAfter(position)39
case None -> {}40
}41
}42
fun focusedPosition(): Long? {43
for (position in 0L ..< stops.size) {44
val focused =45
when (stops[position]) {46
case Target(val target) -> target.active && target.area.hasFocus47
case Items(val owner) -> when (owner.focusedEntry()) {48
case Some(_) -> true49
case None -> false50
}51
}52
if (focused)53
return Some(position)54
}55
return None56
}57
fun next(): FocusStop? {58
if (!node.active || !node.scope.input.isInputAllowed)59
return None60
while (remaining > 0L) {61
remaining -= 1L62
index += if (reverse)63
-1L64
else65
1L66
if (index < 0L || index >= stops.size) {67
if (!loop)68
return None69
index =70
if (reverse)71
stops.size - 1L72
else73
0L74
}75
val stop = stops[index]76
val skip =77
when (excluded) {78
case Some(val boundary) -> withinFocusScope(focusStopNode(stop), boundary)79
case None -> false80
}81
if (!skip)82
return Some(stop)83
}84
return None85
}86
}88
/** Stores only a stable current key and a finite scan length, never one slot per source item. */89
internal class ItemFocusCursor(90
val owner: LazyFocus,91
private val reverse: Boolean,92
private val initialIndex: Long,93
private var remaining: Long,94
private val loop: Boolean,95
val edge: ItemFocusEdge,96
private var exact: ViewKey? = None97
) {98
var current: ViewKey? = None100
fun next(): ViewKey? {101
if (!owner.available || remaining == 0L)102
return None103
when (exact) {104
case Some(val key) -> {105
exact = None106
remaining = 0L107
current = Some(key)108
return Some(key)109
}110
case None -> {}111
}112
val count = owner.count113
if (count == 0L)114
return None115
var index = initialIndex116
when (current) {117
case Some(val key) -> {118
val previous = owner.indexOf(key) ?: return None119
index =120
previous +121
if (reverse)122
-1L123
else124
1L125
}126
case None -> {}127
}128
if (index < 0L || index >= count) {129
if (!loop)130
return None131
index =132
if (reverse)133
count - 1L134
else135
0L136
}137
remaining -= 1L138
val key = owner.keyAt(index)139
current = Some(key)140
return Some(key)141
}142
}144
internal enum FocusCursor {145
Nodes(NodeFocusCursor), Items(ItemFocusCursor)146
}148
internal class FocusItemVisit(val owner: LazyFocus, val key: ViewKey, val edge: ItemFocusEdge)150
internal enum FocusSearchStep {151
Target(FocusTarget), Item(FocusItemVisit), End152
}