scoop:scoop.ui:0.1.0
src/focus/navigation_order.scoop
Selected production source · src/focus/navigation_order.scoop
1
package scoop.ui3
import scoop.window.*5
internal enum NavigationAxis {6
Linear, Row, Column7
}9
internal struct NavigationStep(10
val axis: NavigationAxis,11
val forward: Boolean,12
val edge: Boolean = false13
)15
internal fun navigationStep(16
options: NavigationOptions,17
key: LogicalKey,18
inherited: LayoutDirection = LayoutDirection.Ltr19
): NavigationStep? = navigationStep(options.layout, options.direction ?: inherited, key)21
internal fun navigationStep(22
layout: NavigationLayout,23
direction: LayoutDirection,24
key: LogicalKey25
): NavigationStep? {26
val grid = layout == NavigationLayout.Grid27
val horizontal =28
if (grid)29
NavigationAxis.Row30
else31
NavigationAxis.Linear32
val vertical =33
if (grid)34
NavigationAxis.Column35
else36
NavigationAxis.Linear37
return when (key) {38
case Named(NamedKey.Home) -> Some(NavigationStep(horizontal, false, true))39
case Named(NamedKey.End) -> Some(NavigationStep(horizontal, true, true))40
case Named(NamedKey.ArrowLeft) -> if (layout != NavigationLayout.Vertical)41
Some(NavigationStep(horizontal, direction == LayoutDirection.Rtl))42
else43
None44
case Named(NamedKey.ArrowRight) -> if (layout != NavigationLayout.Vertical)45
Some(NavigationStep(horizontal, direction != LayoutDirection.Rtl))46
else47
None48
case Named(NamedKey.ArrowUp) -> if (layout != NavigationLayout.Horizontal)49
Some(NavigationStep(vertical, false))50
else51
None52
case Named(NamedKey.ArrowDown) -> if (layout != NavigationLayout.Horizontal)53
Some(NavigationStep(vertical, true))54
else55
None56
else -> None57
}58
}60
private fun navigationCoordinate(61
index: Long,62
axis: NavigationAxis,63
row: (Long) -> Int,64
column: (Long) -> Int65
): Long =66
when (axis) {67
case Linear -> index68
case Row -> column(index).toLong()69
case Column -> row(index).toLong()70
}72
internal fun navigationDestination(73
items: Array<NavigationItem>,74
index: Long,75
step: NavigationStep,76
loop: Boolean,77
accepts: (Long) -> Boolean78
): Long? =79
navigationDestination(80
items.size,81
Some(index),82
step,83
loop,84
{ item -> items[item].row },85
{ item -> items[item].column },86
accepts87
)89
/** Shared candidate search; applying its result is separate from focus ownership. */90
internal fun navigationDestination(91
count: Long,92
index: Long?,93
step: NavigationStep,94
loop: Boolean,95
row: (Long) -> Int,96
column: (Long) -> Int,97
accepts: (Long) -> Boolean98
): Long? {99
val lower =100
if (step.edge)101
!step.forward102
else103
step.forward104
val current =105
when (index) {106
case Some(val value) -> value107
case None -> {108
var chosen: Long? = None109
for (candidate in 0L ..< count)110
if (accepts(candidate)) {111
if (lower)112
return Some(candidate)113
chosen = Some(candidate)114
}115
return chosen116
}117
}118
val origin = navigationCoordinate(current, step.axis, row, column)119
var boundary: Long? = None120
var nearest: Long? = None121
for (candidate in 0L ..< count) {122
val sameAxis =123
when (step.axis) {124
case Linear -> true125
case Row -> row(candidate) == row(current)126
case Column -> column(candidate) == column(current)127
}128
if (!sameAxis || !accepts(candidate))129
continue130
val coordinate = navigationCoordinate(candidate, step.axis, row, column)131
val previous = boundary ?: candidate132
val previousCoordinate = navigationCoordinate(previous, step.axis, row, column)133
if (134
(lower && coordinate <= previousCoordinate) ||135
(!lower && coordinate >= previousCoordinate)136
)137
boundary = Some(candidate)138
if (139
!step.edge &&140
((step.forward && coordinate > origin) || (!step.forward && coordinate < origin))141
) {142
val prior = nearest ?: candidate143
val priorCoordinate = navigationCoordinate(prior, step.axis, row, column)144
if (145
(lower && coordinate <= priorCoordinate) ||146
(!lower && coordinate >= priorCoordinate)147
)148
nearest = Some(candidate)149
}150
}151
if (step.edge)152
return boundary153
return when (nearest) {154
case Some(_) -> nearest;155
case None -> if (loop)156
boundary157
else158
None159
}160
}