scoop:scoop.ui:0.1.0
src/focus/lazy.scoop
Selected production source · src/focus/lazy.scoop
1
package scoop.ui3
import scoop.surface.*4
import scoop.window.*6
public enum ItemFocusEdge {7
First, Last8
}10
/** Resolves one keyed item through its ordinary mounted focus targets. */11
public class LazyFocus internal constructor(12
internal val node: MountedNode,13
internal val viewport: ScrollViewport,14
private val items: LazyChildren,15
initial: LazyFocusOptions16
): Closeable {17
private val input = node.scope.input18
private var current = initial19
private var closed = false20
private var recent: ViewKey? = None21
private var registration: Closeable? = None22
private var observation: Closeable? = None24
init {25
checkLazyFocusOptions(initial)26
registration = Some(input.addDefaultAction { context, event -> navigate(context, event) })27
try {28
observation = Some(input.observeActivity { remember() })29
} catch (failure: Throwable) {30
registration?.close()31
throw failure32
}33
}35
internal val active: Boolean get() =36
!closed && node.active && node.root.isOpen && viewport.active && !input.isClosed37
internal val available: Boolean get() = active && node.scope.forChildren().isFocusAllowed38
internal val usesItems: Boolean get() = active && current.navigation != ItemNavigation.None39
internal val count: Long get() = items.count40
public var options: LazyFocusOptions41
get() {42
node.root.checkThread()43
return current44
}45
set(value) {46
node.root.checkThread()47
checkFocusMutation()48
if (!active)49
throw IllegalStateException(Some("item focus is no longer mounted"))50
checkLazyFocusOptions(value)51
if (value != current) {52
node.root.focus.cancel(this)53
current = value54
}55
}56
public val isClosed: Boolean get() {57
node.root.checkThread()58
return closed || !viewport.active || input.isClosed59
}60
public val isPending: Boolean get() {61
node.root.checkThread()62
return active && node.root.focus.isPending(this)63
}64
/** The item still requiring preparation; exact target reveal continues after this becomes None. */65
public val pendingItem: ViewKey? get() {66
node.root.checkThread()67
return if (active)68
node.root.focus.itemToPrepare(this)69
else70
None71
}72
public fun requestFocus(key: ViewKey, edge: ItemFocusEdge = ItemFocusEdge.First): Boolean {73
node.root.checkThread()74
checkFocusMutation()75
requireUi(key != ViewKey.Position, "item focus requires an explicit key")76
return node.root.focus.request(this, key, edge)77
}78
/** Selects a real descendant after the requested item has an accepted layout. */79
public fun requestFocus(key: ViewKey, target: () -> FocusHandle?): Boolean {80
node.root.checkThread()81
checkFocusMutation()82
requireUi(key != ViewKey.Position, "item focus requires an explicit key")83
return node.root.focus.request(this, key, ItemFocusEdge.First, Some(target))84
}85
public fun cancelRequest() {86
node.root.checkThread()87
checkFocusMutation()88
node.root.focus.cancel(this)89
}90
internal fun pendingIndex(): Long? {91
val key = pendingItem ?: return None92
return items.indexOf(key)93
}94
internal fun entry(key: ViewKey): LazyItemEntry? = items.entry(key)95
internal fun keyAt(index: Long): ViewKey = items.keyAt(index)96
internal fun indexOf(key: ViewKey): Long? = items.indexOf(key)97
internal fun focusedEntry(): LazyItemEntry? = items.focusedEntry()98
internal fun entryContaining(node: MountedNode): LazyItemEntry? = items.entryContaining(node)99
internal fun preferredKey(): ViewKey? {100
when (focusedEntry()) {101
case Some(val entry) -> return Some(entry.key)102
case None -> {}103
}104
when (recent) {105
case Some(val key) -> if (items.indexOf(key) != None)106
return Some(key)107
case None -> {}108
}109
return if (count > 0L)110
Some(items.keyAt(0L))111
else112
None113
}114
private fun primary(item: LazyItemEntry): FocusTarget? {115
for (target in focusOrder(item.node))116
if (target.acceptsTab)117
return Some(target)118
return None119
}120
private fun remember() {121
if (!active)122
return123
when (focusedEntry()) {124
case Some(val entry) -> recent = Some(entry.key)125
case None -> {}126
}127
}128
private fun navigate(context: RoutedEventContext, event: InputEvent) {129
if (!available)130
return131
val key =132
when (event) {133
case Keyboard(val value) -> value134
else -> return135
}136
val modifiers = key.modifiers137
if (138
key.state !=139
ElementState.Pressed ||140
key.synthetic ||141
modifiers.control ||142
modifiers.alt ||143
modifiers.shift ||144
modifiers.superKey ||145
modifiers.function146
)147
return148
when (current.navigation) {149
case Roving(val direction, val loop) -> {150
val step =151
navigationStep(152
current.layout,153
direction ?: node.layoutDirection,154
key.logicalKey155
) ?:156
return157
val entry = focusedEntry() ?: return158
val target = primary(entry) ?: return159
if (!target.area.hasFocus)160
return161
context.consume()162
node.root.focus.navigate(this, entry, step, loop)163
}164
else -> {}165
}166
}167
internal fun sourceChanged() {168
when (recent) {169
case Some(val key) -> if (items.indexOf(key) == None)170
recent = None171
case None -> {}172
}173
val key = node.root.focus.itemKey(this) ?: return174
if (items.indexOf(key) == None)175
node.root.focus.cancel(this)176
}177
public override fun close() {178
node.root.checkThread()179
checkFocusMutation()180
if (closed)181
return182
closed = true183
node.root.focus.cancel(this)184
node.lazyFocus = None185
var failure = cleanupUi(None) { registration?.close() }186
registration = None187
failure = cleanupUi(failure) { observation?.close() }188
observation = None189
throwUi(failure)190
}191
}