scoop:scoop.ui:0.1.0
src/focus/navigation.scoop
Selected production source · src/focus/navigation.scoop
1
package scoop.ui3
import scoop.surface.*4
import scoop.window.*6
/** Composes member navigation with the existing routed defaults. */7
public class KeyNavigation internal constructor(8
internal val node: MountedNode,9
initial: NavigationOptions10
): Closeable {11
private val input = node.scope.input12
private var current = initial13
private var closed = false14
private var registration: Closeable? = None16
init {17
validate(initial)18
bind()19
registration = Some(input.addDefaultAction { context, event -> navigate(context, event) })20
}22
internal val active: Boolean get() =23
!closed && node.active && node.root.isOpen && !input.isClosed24
public val isClosed: Boolean get() {25
node.root.checkThread()26
return closed || input.isClosed27
}28
public var options: NavigationOptions29
get() {30
node.root.checkThread()31
return current32
}33
set(value) {34
node.root.checkThread()35
checkFocusMutation()36
if (!active)37
throw IllegalStateException(Some("key navigation is no longer mounted"))38
validate(value)39
unbind()40
current = value41
bind()42
}44
internal fun checkMember(member: MountedNode) {45
if (active)46
requireUi(withinFocusScope(member, node), "navigation member belongs to another scope")47
}48
private fun validate(value: NavigationOptions) {49
var preferredFound = false50
for (index in 0L ..< value.items.size) {51
val item = value.items[index]52
when (value.preferredTab) {53
case Some(val handle) -> if (handle === item.handle)54
preferredFound = true55
case None -> preferredFound = true56
}57
item.handle.checkThread()58
when (item.handle.navigation) {59
case Some(val group) -> requireUi(60
group === this || !group.active,61
"focus handle already belongs to a navigation group"62
)63
case None -> {}64
}65
when (item.handle.target) {66
case Some(val target) -> if (target.active)67
checkMember(target.node)68
case None -> {}69
}70
for (previous in 0L ..< index) {71
val other = value.items[previous]72
requireUi(other.handle !== item.handle, "duplicate navigation handle")73
requireUi(74
value.layout !=75
NavigationLayout.Grid ||76
other.row !=77
item.row ||78
other.column !=79
item.column,80
"duplicate navigation grid position"81
)82
}83
}84
when (value.preferredTab) {85
case Some(_) -> requireUi(86
preferredFound,87
"preferred Tab handle is not a navigation member"88
)89
case None -> {}90
}91
}92
private fun bind() {93
for (item in current.items)94
item.handle.navigation = Some(this)95
}96
private fun unbind() {97
for (item in current.items) {98
when (item.handle.navigation) {99
case Some(val group) -> if (group === this)100
item.handle.navigation = None101
case None -> {}102
}103
}104
}106
internal fun acceptsTab(target: FocusTarget): Boolean {107
if (!active || !current.roving || !input.isEnabledInHierarchy)108
return true109
val member = preferredTab() ?: return false110
return member === target111
}112
private fun preferredTab(): FocusTarget? {113
var first: FocusTarget? = None114
var recent: FocusTarget? = None115
var distance: ULong? = None116
val now = node.root.surface.focusRevision117
for (item in current.items) {118
val target = item.handle.target ?: continue119
if (!target.tabCandidate)120
continue121
if (target.area.hasFocus)122
return Some(target)123
when (first) {124
case None -> first = Some(target)125
case Some(_) -> {}126
}127
when (target.area.focusRevision) {128
case Some(val revision) -> {129
val age = now - revision130
val newer =131
when (distance) {132
case Some(val previous) -> age < previous133
case None -> true134
}135
if (newer) {136
recent = Some(target)137
distance = Some(age)138
}139
}140
case None -> {}141
}142
}143
when (current.preferredTab) {144
case Some(val handle) -> when (handle.target) {145
case Some(val target) -> if (target.tabCandidate)146
return Some(target)147
case None -> {}148
}149
case None -> {}150
}151
return when (recent) {152
case Some(_) -> recent153
case None -> first154
}155
}156
private fun member(index: Long): FocusTarget? {157
val target = current.items[index].handle.target ?: return None158
return if (target.reachable)159
Some(target)160
else161
None162
}163
private fun focusedIndex(): Long? {164
for (index in 0L ..< current.items.size) {165
when (current.items[index].handle.target) {166
case Some(val target) -> if (target.active && target.area.hasFocus)167
return Some(index)168
case None -> {}169
}170
}171
return None172
}173
private fun navigate(context: RoutedEventContext, event: InputEvent) {174
if (!active || !input.isEnabledInHierarchy)175
return176
val key =177
when (event) {178
case Keyboard(val value) -> value179
else -> return180
}181
if (!navigationKeyAllowed(key))182
return183
val step = navigationStep(current, key.logicalKey, node.layoutDirection) ?: return184
val index = focusedIndex() ?: return185
val next =186
navigationDestination(current.items, index, step, current.loop) {187
candidate -> when (member(candidate)) {188
case Some(_) -> true189
case None -> false190
}191
}192
context.consume()193
when (next) {194
case Some(val position) -> {195
val target = member(position) ?: return196
val callback = current.onNavigate197
if (target.handle.requestFocus(FocusOrigin.Keyboard) && active)198
callback(target.handle)199
}200
case None -> {}201
}202
}203
public override fun close() {204
node.root.checkThread()205
checkFocusMutation()206
if (closed)207
return208
closed = true209
unbind()210
registration?.close()211
registration = None212
node.navigation = None213
}214
}