scoop:scoop.ui.component:0.1.0
src/carousel/control.scoop
Selected production source · src/carousel/control.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.math.*5
import scoop.platform.time.MonotonicTimeSource6
import scoop.surface.*7
import scoop.time.Duration8
import scoop.ui.*9
import scoop.ui.style.*10
import scoop.window.*12
internal class CarouselGesture(val origin: Double, val geometry: CarouselGeometry)14
internal class CarouselControl(val scope: ComponentScope, val read: () -> CarouselInput) {15
val area =16
scope.createArea(focusable = true) {17
context,18
event -> if (context.phase == RoutedPhase.Notification && active) {19
when (event) {20
case FocusChanged(false) -> when (gesture) {21
case Some(_) -> cancel(DragCancelReason.Explicit)22
case None -> {}23
}24
else -> {}25
}26
if (!active)27
return@createArea28
refreshAutoplay();29
scope.requestFrame()30
}31
}32
val focus = scope.focusTarget(area, read().props.viewport.focus)33
val motion = scope.animation(0.0)34
val timer = scope.createDeadline { autoplayTick() }35
private val clock = CarouselClock()36
var geometry: CarouselGeometry? = None37
var gesture: CarouselGesture? = None38
private var handle: CarouselHandle? = None39
private var index = read().selected40
private var observed = read().props.selected?.value41
private var retired = false42
private val drag =43
scope.drag(44
area,45
dragOptions(),46
{ _ -> available() && geometry!!.track.maximum > 0.0 },47
{ event -> carouselDrag(this, event) }48
)49
private val swipe =50
scope.swipeGesture(51
area,52
swipeOptions(),53
{ _ -> available() && geometry!!.track.maximum > 0.0 },54
{ value -> carouselSwipe(this, value) }55
)56
val active: Boolean get() = !retired && scope.isMounted57
val position: Double get() =58
if (active)59
motion.value60
else61
0.062
val snaps: Array<Double> get() = geometry?.track?.snaps ?: []63
init {64
scope.routeChildren(area)65
scope.resource {66
area.scope.observeActivity {67
if (active)68
refreshAutoplay()69
}70
}71
scope.resource {72
area.scope.observePointer {73
_ -> if (active)74
refreshAutoplay()75
}76
}77
scope.observeWindowActivity { _ -> refreshAutoplay() }78
scope.resource {79
area.scope.addDefaultAction {80
context,81
event -> when (event) {82
case Keyboard(val key) -> carouselKeyboard(this, context, key)83
case Pointer(84
InputPointerEvent.Button(_, MouseButton.Left, ElementState.Pressed, _)85
) -> {86
if (context.target === area.scope && read().props.enabled)87
focus.handle.requestFocus(FocusOrigin.Pointer)88
}89
else -> {}90
}91
}92
}93
}94
private fun dragOptions(): DragOptions =95
DragOptions(96
if (read().props.orientation == Orientation.Horizontal)97
DragAxis.Horizontal98
else99
DragAxis.Vertical,100
read().props.dragThreshold,101
read().props.enabled102
)103
private fun swipeOptions(): SwipeGestureOptions =104
SwipeGestureOptions(105
if (read().props.orientation == Orientation.Horizontal)106
DragAxis.Horizontal107
else108
DragAxis.Vertical,109
enabled = read().props.enabled && read().props.nativeSwipe,110
contacts = false111
)112
fun current(): Long {113
val input = read()114
return carouselSelected(115
input.keys,116
input.props.selected?.value,117
input.owner.key.value,118
input.owner.fallback119
)120
}121
private fun matches(shape: CarouselGeometry): Boolean {122
val input = read()123
if (124
shape.orientation !=125
input.props.orientation ||126
shape.direction !=127
scope.layoutDirection ||128
shape.keys.size !=129
input.keys.size130
)131
return false132
for (at in 0L ..< shape.keys.size)133
if (shape.keys[at] != input.keys[at])134
return false135
return true136
}137
fun available(): Boolean =138
active &&139
read().props.enabled &&140
when (geometry) {141
case Some(val shape) -> matches(shape) &&142
shape.inner.width >143
0.0 &&144
shape.inner.height >145
0.0 &&146
shape.keys.size >147
0L;148
case None -> false149
}150
fun update() {151
val input = read()152
val sameHandle =153
when ((handle, input.props.handle)) {154
case(Some(val old), Some(val next)) -> old === next155
case(None, None) -> true156
else -> false157
}158
if (!sameHandle) {159
handle?.unbind(this);160
handle = input.props.handle;161
handle?.bind(this)162
}163
val selected = current()164
if (index != selected || observed != input.props.selected?.value || !input.props.enabled) {165
cancel(DragCancelReason.ConfigurationChanged)166
if (!active)167
return168
index = selected;169
observed = input.props.selected?.value170
when (geometry) {171
case Some(val shape) -> if (matches(shape)) {172
val position = anchor(shape)173
if (motion.value != position || motion.state == AnimationState.Running)174
motion.snapTo(position)175
} else176
geometry = None177
case None -> {}178
}179
}180
focus.options =181
input.props.viewport.focus.copy {182
tabStop = input.props.viewport.focus.tabStop && input.props.enabled183
}184
if (!active)185
return186
drag.update(187
dragOptions(),188
{ _ -> available() && geometry!!.track.maximum > 0.0 },189
{ event -> carouselDrag(this, event) }190
)191
if (!active)192
return193
swipe.update(194
swipeOptions(),195
{ _ -> available() && geometry!!.track.maximum > 0.0 },196
{ value -> carouselSwipe(this, value) }197
)198
when (transition()) {199
case None -> if (motion.state == AnimationState.Running)200
motion.snapTo(motion.target)201
case Some(_) -> {}202
}203
publish();204
refreshAutoplay()205
}206
private fun anchor(shape: CarouselGeometry): Double {207
val selected = current()208
return if (selected >= 0L && selected < shape.track.snaps.size)209
shape.track.snaps[selected]210
else211
0.0212
}213
fun visualPosition(shape: CarouselGeometry): Double =214
when (geometry) {215
case Some(val old) -> if (sameCarouselGeometry(old, shape))216
motion.value217
else218
anchor(shape)219
case None -> anchor(shape)220
}221
fun accept(shape: CarouselGeometry) {222
if (!active || !matches(shape))223
return224
val changed =225
when (geometry) {226
case Some(val old) -> !sameCarouselGeometry(old, shape);227
case None -> true228
}229
if (changed) {230
cancel(DragCancelReason.GeometryUnavailable)231
if (!active)232
return233
geometry = Some(shape);234
motion.snapTo(anchor(shape))235
}236
index = current()237
val owner = read().owner238
if (index >= 0L) {239
owner.fallback = index240
when (read().props.selected) {241
case None -> if (owner.key.value != Some(shape.keys[index]))242
owner.key.value = Some(shape.keys[index])243
case Some(_) -> {}244
}245
}246
publish();247
refreshAutoplay()248
}249
fun select(request: Long, animated: Boolean, destination: Double? = None): Boolean {250
if (!available())251
return false252
val shape = geometry!!253
if (request < 0L || request >= shape.keys.size)254
return false255
cancel(DragCancelReason.Explicit)256
if (!active)257
return false258
val input = read()259
val before = current()260
when (input.props.selected) {261
case Some(val binding) -> if (binding.value != request)262
binding.value = request263
case None -> input.owner.key.value = Some(input.keys[request])264
}265
if (!active)266
return false267
val accepted = current()268
observed = read().props.selected?.value;269
index = accepted270
if (accepted >= 0L && matches(shape)) {271
val base = shape.track.snaps[accepted]272
val target =273
if (destination != None && accepted == request)274
destination!!275
else276
nearestPosition(shape.track, base)277
moveTo(target, animated)278
}279
clock.cancel()280
timer.cancel();281
publish()282
if (before != accepted && accepted >= 0L)283
read().props.onSelect(accepted, read().keys[accepted])284
if (active)285
refreshAutoplay()286
return before != accepted287
}288
private fun nearestPosition(track: CarouselTrack, base: Double): Double {289
if (!track.looping)290
return base291
val current = carouselModulo(motion.value, track.cycle)292
if (motion.value != current)293
motion.snapTo(current)294
var best = base295
for (candidate in [base - track.cycle, base + track.cycle])296
if (abs(candidate - current) < abs(best - current))297
best = candidate298
return best299
}300
fun step(next: Boolean, animated: Boolean): Boolean {301
if (!available())302
return false303
val target = carouselAdjacent(geometry!!.track, current(), next) ?: return false304
return select(target, animated)305
}306
fun transition(): AnimationSpec? {307
val input = read()308
return input.theme.tokens.motion.resolve(309
input.props.transition.resolve(310
Some(AnimationSpec.Tween(Duration.milliseconds(300L), Easing.EaseOut))311
)312
)313
}314
fun moveTo(value: Double, animated: Boolean) {315
if (!active)316
return317
val spec =318
if (animated)319
transition()320
else321
None322
val immediate =323
when (spec) {324
case None -> true;325
case Some(_) -> false326
}327
if (motion.value == value || immediate) {328
val destination =329
when (geometry) {330
case Some(val shape) -> if (shape.track.looping)331
carouselModulo(value, shape.track.cycle)332
else333
value334
case None -> value335
}336
if (motion.value != destination || motion.state == AnimationState.Running)337
motion.snapTo(destination)338
return339
}340
motion.animateTo(341
value,342
spec!!,343
onFinished =344
Some(345
{346
_ -> if (active) {347
when (geometry) {348
case Some(val shape) -> if (shape.track.looping)349
motion.snapTo(carouselModulo(motion.value, shape.track.cycle));350
case None -> {}351
}352
refreshAutoplay()353
}354
}355
)356
)357
}358
fun cancel(reason: DragCancelReason) {359
when (gesture) {360
case None -> {361
drag.cancel();362
return363
}364
case Some(_) -> {}365
}366
gesture = None;367
drag.cancel()368
if (active) {369
when (geometry) {370
case Some(val shape) -> moveTo(anchor(shape), true);371
case None -> {}372
}373
publish();374
refreshAutoplay()375
}376
read().props.onDragCancel(reason)377
}378
fun publish() {379
if (!active)380
return381
val shape = geometry382
val selected = current()383
val allowed = available()384
val next =385
CarouselSnapshot(386
selected,387
if (selected >= 0L)388
Some(read().keys[selected])389
else390
None,391
read().keys.size,392
allowed && carouselAdjacent(shape!!.track, selected, false) != None,393
allowed && carouselAdjacent(shape!!.track, selected, true) != None,394
shape?.track?.looping ?: false,395
drag.isDragging396
)397
if (read().owner.snapshot.value != next)398
read().owner.snapshot.value = next399
handle?.publish(next)400
}401
fun refreshAutoplay() {402
if (!active)403
return404
val options = read().props.autoplay405
val configured =406
when (options) {407
case Some(_) -> true;408
case None -> false409
}410
val allowed =411
available() &&412
configured &&413
!drag.isDragging &&414
motion.state !=415
AnimationState.Running &&416
scope.nativeWindow.state ==417
WindowState.Open &&418
scope.nativeWindow.isFocused &&419
scope.nativeWindow.isVisible &&420
!scope.nativeWindow.isOccluded &&421
area.visibleBounds !=422
None &&423
area.scope.isInputAllowed &&424
!read().owner.group!!.scope.activity.hasContact &&425
!(options!!.pauseOnFocus && read().owner.group!!.scope.activity.hasFocus) &&426
!(options!!.pauseOnHover && read().owner.group!!.scope.isHovered) &&427
carouselAdjacent(geometry!!.track, current(), true) !=428
None429
if (clock.configure(options?.interval, allowed, MonotonicTimeSource.now()))430
when (clock.deadline) {431
case Some(val due) -> timer.schedule(due)432
case None -> timer.cancel()433
}434
}435
private fun autoplayTick() {436
if (!active)437
return438
refreshAutoplay()439
if (!clock.take(MonotonicTimeSource.now()))440
return441
timer.cancel();442
step(true, true);443
refreshAutoplay()444
}445
fun retire() {446
cancel(DragCancelReason.Closed)447
retired = true;448
clock.cancel()449
timer.cancel();450
handle?.unbind(this)451
read().owner.control = None452
}453
}