scoop:scoop.ui.component:0.1.0
src/slider/control.scoop
Selected production source · src/slider/control.scoop
1
package scoop.ui.component3
import scoop.window.accessibility.SemanticActionResult5
import scoop.graphics.*6
import scoop.math.abs7
import scoop.surface.*8
import scoop.ui.*9
import scoop.ui.style.*10
import scoop.window.*12
private class SliderGesture(13
val index: Long,14
val initial: Array<Double>,15
val range: RangeSpec,16
val orientation: Orientation,17
val direction: LayoutDirection,18
val grab: Double19
) {20
var expected = initial21
}23
internal class SliderControl(24
private val scope: ComponentScope,25
private val model: () -> SliderProps,26
private val activeThumb: State<Long?>,27
private val input: () -> SliderInput28
) {29
private val area = scope.createArea()30
private var drag: Drag? = None31
private var geometry: SliderGeometry? = None32
private var gesture: SliderGesture? = None33
private var checkedValues: Array<Double>? = None34
private var checkedRange: RangeSpec? = None35
private var configured = input().props36
private var direction = scope.layoutDirection37
init {38
scope.routeChildren(area)39
drag =40
Some(41
scope.drag(42
area,43
options(),44
{ point -> candidate(point) != None },45
{ event -> handle(event) }46
)47
)48
}49
private fun options(): DragOptions =50
DragOptions(51
if (input().props.orientation == Orientation.Horizontal)52
DragAxis.Horizontal53
else54
DragAxis.Vertical,55
threshold = 0.0,56
enabled = input().props.enabled57
)58
private fun values(): Array<Double> {59
val props = input().props60
val result = props.values.value61
val same =62
when (checkedValues) {63
case Some(val previous) -> previous ===64
result &&65
sameSliderRange(checkedRange!!, props.range)66
case None -> false67
}68
if (!same) {69
props.range.validate(result)70
checkedRange = Some(props.range)71
checkedValues = Some(result)72
}73
return result74
}75
fun update() {76
val props = input().props77
val current = values()78
requireComponent(79
props.thumbs.size == 0L || props.thumbs.size == current.size,80
"slider thumb options must match its values"81
)82
when (gesture) {83
case Some(val active) -> if (!props.enabled || !compatible(active, current))84
cancel(85
if (!props.enabled)86
DragCancelReason.Disabled87
else88
DragCancelReason.ConfigurationChanged89
)90
case None -> {}91
}92
if (93
!sameSliderRange(configured.range, props.range) ||94
configured.orientation !=95
props.orientation ||96
direction !=97
scope.layoutDirection98
)99
geometry = None100
configured = props101
direction = scope.layoutDirection102
area.scope.enabled = props.enabled103
drag!!.update(options(), { point -> candidate(point) != None }, { event -> handle(event) })104
}105
fun place(context: SceneContext, shape: SliderGeometry) {106
if (shape.size.width > 0.0 && shape.size.height > 0.0 && input().values.size > 0L)107
context.place(area, shape.hit)108
}109
fun accept(shape: SliderGeometry) {110
if (!scope.isMounted)111
return112
val props = input().props113
if (114
shape.orientation !=115
props.orientation ||116
shape.direction !=117
scope.layoutDirection ||118
!sameSliderRange(shape.range, props.range)119
)120
return121
geometry = Some(shape)122
if (shape.travel == 0.0)123
cancel(DragCancelReason.GeometryUnavailable)124
}125
private fun candidate(point: Point): Long? {126
if (!input().props.enabled)127
return None128
val shape = geometry ?: return None129
val current = values()130
if (shape.travel == 0.0 || shape.thumbs.size != current.size)131
return None132
var focused: Long? = None133
for (index in 0L ..< input().slots.size)134
if (input().slots[index].area?.hasFocus ?: false)135
focused = Some(index)136
return nearestSliderThumb(shape, input().props.range, current, point, focused)137
}138
private fun compatible(active: SliderGesture, current: Array<Double>): Boolean =139
sameSliderRange(active.range, input().props.range) &&140
active.orientation ==141
input().props.orientation &&142
active.direction ==143
scope.layoutDirection &&144
sameSliderValues(current, active.expected)145
private fun handle(event: DragEvent) {146
when (event) {147
case Started(val sample) -> start(sample.position)148
case Moved(val sample) -> move(sample.position)149
case Ended(val sample) -> {150
move(sample.position)151
val active = gesture ?: return152
val current = values()153
gesture = None154
input().active.value = None155
if (!sameSliderValues(active.initial, current) && scope.isMounted)156
input().props.onValueCommit(current)157
}158
case Cancelled(_, val reason) -> cancel(reason)159
}160
}161
private fun start(point: Point) {162
val index = candidate(point) ?: return163
val shape = geometry!!164
val slot = input().slots[index]165
val focus = slot.focus ?: return166
if (!focus.handle.requestFocus(FocusOrigin.Pointer)) {167
drag!!.cancel()168
return169
}170
val offset = shape.coordinate(point) - shape.center(index)171
val hit =172
input().props.style.hitInsets ?:173
Insets.all(8.0 * input().theme.tokens.typography.body.fontSize / 14.0)174
val inset =175
if (shape.orientation == Orientation.Horizontal)176
lowerBound(hit.left, hit.right)177
else178
lowerBound(hit.top, hit.bottom)179
val grabbing = abs(offset) <= shape.thumbSize / 2.0 + inset180
val grab =181
if (grabbing && shape.thumbSize > 0.0)182
offset / shape.thumbSize183
else184
0.0185
val current = values()186
gesture =187
Some(188
SliderGesture(189
index,190
current,191
input().props.range,192
input().props.orientation,193
scope.layoutDirection,194
grab195
)196
)197
input().active.value = Some(index)198
if (!grabbing)199
move(point)200
}201
private fun move(point: Point) {202
val active = gesture ?: return203
val current = values()204
if (!input().props.enabled || !compatible(active, current)) {205
cancel(206
if (!input().props.enabled)207
DragCancelReason.Disabled208
else209
DragCancelReason.ConfigurationChanged210
)211
return212
}213
val shape = geometry ?: return214
if (shape.travel == 0.0 || shape.thumbs.size != current.size) {215
cancel(DragCancelReason.GeometryUnavailable)216
return217
}218
val next =219
input().props.range.moveValidated(220
current,221
active.index,222
input().props.range.valueAt(shape.position(point, active.grab))223
)224
if (!sameSliderValues(current, next))225
input().props.values.value = next226
active.expected = values()227
}228
fun cancel(reason: DragCancelReason) {229
val active = gesture ?: return230
gesture = None231
activeThumb.value = None232
drag?.cancel()233
// Retirement keeps the business model, but no longer permits theme queries.234
val props = model()235
val current = props.values.value236
if (237
sameSliderRange(active.range, props.range) &&238
sameSliderValues(current, active.expected) &&239
!sameSliderValues(current, active.initial)240
)241
props.values.value = active.initial242
props.onValueCancel(props.values.value, reason)243
}244
fun lostFocus(index: Long) {245
if (activeThumb.value == Some(index))246
cancel(DragCancelReason.Explicit)247
}248
fun key(index: Long, context: RoutedEventContext, key: KeyEvent) {249
if (!input().props.enabled || !sliderKeyAllowed(key))250
return251
if (key.logicalKey == LogicalKey.Named(NamedKey.Escape) && input().active.value != None) {252
context.consume()253
cancel(DragCancelReason.Explicit)254
return255
}256
val command =257
sliderKeyCommand(input().props, scope.layoutDirection, key.logicalKey) ?: return258
context.consume()259
adjust(index, command)260
}261
fun adjust(index: Long, command: SliderKey): SemanticActionResult {262
if (!scope.isMounted)263
return SemanticActionResult.Unavailable264
if (!input().props.enabled)265
return SemanticActionResult.Rejected266
cancel(DragCancelReason.Explicit)267
val current = values()268
if (index < 0L || index >= current.size)269
return SemanticActionResult.Unavailable270
val value = sliderKeyValue(input().props, current[index], command)271
return setNumber(index, value)272
}273
fun setNumber(index: Long, value: Double): SemanticActionResult {274
if (!scope.isMounted)275
return SemanticActionResult.Unavailable276
if (!input().props.enabled)277
return SemanticActionResult.Rejected278
cancel(DragCancelReason.Explicit)279
if (!scope.isMounted)280
return SemanticActionResult.Unavailable281
val current = values()282
if (index < 0L || index >= current.size)283
return SemanticActionResult.Unavailable284
if (!value.isFinite())285
return SemanticActionResult.InvalidArgument286
val next = input().props.range.moveValidated(current, index, value)287
if (sameSliderValues(current, next))288
return SemanticActionResult.Completed289
input().props.values.value = next290
if (!scope.isMounted)291
return SemanticActionResult.Unavailable292
val accepted = values()293
if (scope.isMounted && !sameSliderValues(current, accepted))294
input().props.onValueCommit(accepted)295
return if (sameSliderValues(next, accepted))296
SemanticActionResult.Completed297
else298
SemanticActionResult.Rejected299
}300
fun retire() {301
cancel(DragCancelReason.Closed)302
geometry = None303
}304
}