scoop:scoop.ui.component:0.1.0
src/drawer/control.scoop
Selected production source · src/drawer/control.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.math.*5
import scoop.surface.*6
import scoop.time.Duration7
import scoop.ui.*8
import scoop.ui.style.*10
internal class DrawerGesture(val origin: Double, val geometry: DrawerGeometry)12
internal class DrawerControl(val scope: ComponentScope, val read: () -> DrawerInput) {13
val area =14
scope.createArea(focusable = true) {15
context,16
event -> if (context.phase == RoutedPhase.Notification && active) {17
when (event) {18
case FocusChanged(false) -> when (gesture) {19
case Some(_) -> cancel(DragCancelReason.Explicit)20
case None -> {}21
}22
else -> {}23
}24
}25
}26
val focus = scope.focusTarget(area, FocusOptions(tabStop = false))27
val motion = scope.animation(0.0)28
val stackMotion = scope.animation(0.0)29
var geometry: DrawerGeometry? = None30
var gesture: DrawerGesture? = None31
var parent: DrawerControl? = None32
val children = ArrayList<DrawerControl>()33
var frontExtent = 0.034
private var handle: DrawerHandle? = None35
private var scroll: ScrollState? = None36
private var scrollSubscription: Closeable? = None37
private var index = current()38
private var observed = read().props.snap?.value39
private var retired = false40
private var progress = 0.041
private val drag =42
scope.drag(43
area,44
dragOptions(),45
{ point -> acceptsDrag(point) },46
{ event -> drawerDrag(this, event) }47
)48
private val swipe =49
scope.swipeGesture(50
area,51
swipeOptions(),52
{ point -> acceptsDrag(point) },53
{ value -> drawerSwipe(this, value) }54
)55
val active: Boolean get() = !retired && scope.isMounted56
val depth: Long get() {57
var value = 0L58
for (child in children)59
if (child.active)60
value = max(value, child.depth + 1L)61
return value62
}63
val position: Double get() =64
if (active)65
motion.value66
else67
0.068
init {69
scope.routeChildren(area)70
area.scope.captureHandler =71
{72
_,73
event -> when (event) {74
case Pointer(InputPointerEvent.Wheel(_, _, _, _)) -> cancel(75
DragCancelReason.Explicit76
)77
else -> {}78
}79
}80
scope.resource {81
area.scope.addDefaultAction {82
context,83
event -> when (event) {84
case Keyboard(val key) -> drawerKeyboard(this, context, key, false)85
else -> {}86
}87
}88
}89
}90
fun current(): Long {91
val input = read()92
return drawerIndex(93
input.props.snap?.value ?: input.owner.localSnap.value,94
max(1L, input.props.snapPoints.size)95
)96
}97
private fun dragOptions(): DragOptions {98
val side = read().props.side99
return DragOptions(100
if (side == SheetSide.Top || side == SheetSide.Bottom)101
DragAxis.Vertical102
else103
DragAxis.Horizontal,104
read().props.dragThreshold,105
read().props.options.enabled && read().props.open.value106
)107
}108
private fun matches(shape: DrawerGeometry): Boolean {109
val input = read()110
if (shape.side != drawerPhysicalSide(input.props.side, scope.layoutDirection))111
return false112
val snaps = drawerSnaps(input.props.snapPoints, shape.extent, input.density)113
if (snaps.size != shape.snaps.size)114
return false115
for (at in 0L ..< snaps.size)116
if (snaps[at] != shape.snaps[at])117
return false118
return true119
}120
fun available(): Boolean =121
active &&122
read().props.options.enabled &&123
read().props.open.value &&124
when (geometry) {125
case Some(val shape) -> shape.extent >126
0.0 &&127
shape.bounds.width >128
0.0 &&129
shape.bounds.height >130
0.0 &&131
matches(shape)132
case None -> false133
}134
private fun swipeOptions(): SwipeGestureOptions =135
SwipeGestureOptions(136
dragOptions().axis,137
enabled =138
read().props.options.enabled && read().props.open.value && read().props.nativeSwipe,139
contacts = false140
)141
private fun acceptsDrag(point: Point): Boolean {142
if (!available() || children.size > 0L || !area.scope.isInputAllowed)143
return false144
val input = read()145
val mapped = area.toWindow(Rect(point.x, point.y, 0.0, 0.0)) ?: return false146
val windowPoint = Point(mapped.x, mapped.y)147
val onHandle = input.owner.handleArea?.hitTest(windowPoint) ?: false148
if (input.props.swipeHandle.only)149
return onHandle150
if (151
onHandle ||152
!(input.owner.bodyArea?.hitTest(windowPoint) ?: false) ||153
!input.props.options.scrollable154
)155
return true156
return when (geometry!!.side) {157
case Bottom -> input.scroll.offset.y <= 0.0158
case Top -> input.scroll.offset.y >= input.scroll.maximum.y159
else -> true160
}161
}162
fun update() {163
val input = read()164
val sameHandle =165
when ((handle, input.props.controller)) {166
case(Some(val old), Some(val next)) -> old === next167
case(None, None) -> true168
else -> false169
}170
if (!sameHandle) {171
handle?.unbind(this);172
handle = input.props.controller;173
handle?.bind(this)174
}175
val sameScroll =176
when (scroll) {177
case Some(val old) -> old === input.scroll;178
case None -> false179
}180
if (!sameScroll) {181
scrollSubscription?.close();182
scroll = Some(input.scroll)183
scrollSubscription =184
Some(185
input.scroll.observeIntent {186
_ -> if (active)187
cancel(DragCancelReason.Explicit)188
}189
)190
}191
drawerUpdateParent(this)192
if (!active)193
return194
area.scope.enabled = input.props.options.enabled195
if (!active)196
return197
val selected = current()198
if (199
index !=200
selected ||201
observed !=202
input.props.snap?.value ||203
!input.props.options.enabled ||204
!input.props.open.value205
) {206
cancel(DragCancelReason.ConfigurationChanged)207
if (!active)208
return209
index = selected;210
observed = input.props.snap?.value211
when (geometry) {212
case Some(val shape) -> if (matches(shape) && input.props.open.value)213
moveTo(anchor(shape), false)214
case None -> {}215
}216
}217
drag.update(218
dragOptions(),219
{ point -> acceptsDrag(point) },220
{ event -> drawerDrag(this, event) }221
)222
if (!active)223
return224
swipe.update(225
swipeOptions(),226
{ point -> acceptsDrag(point) },227
{ value -> drawerSwipe(this, value) }228
)229
when (transition()) {230
case None -> {231
if (motion.state == AnimationState.Running)232
motion.snapTo(motion.target)233
if (stackMotion.state == AnimationState.Running)234
stackMotion.snapTo(stackMotion.target)235
}236
case Some(_) -> {}237
}238
publish()239
}240
fun anchor(shape: DrawerGeometry): Double =241
shape.snaps[drawerIndex(current(), shape.snaps.size)]242
fun visualPosition(shape: DrawerGeometry): Double =243
when (geometry) {244
case Some(val old) -> if (sameDrawerGeometry(old, shape))245
motion.value246
else247
anchor(shape)248
case None -> anchor(shape)249
}250
fun accept(shape: DrawerGeometry, presence: Double) {251
if (!active || !matches(shape))252
return253
val changed =254
when (geometry) {255
case Some(val old) -> !sameDrawerGeometry(old, shape);256
case None -> true257
}258
if (changed) {259
cancel(DragCancelReason.GeometryUnavailable)260
if (!active)261
return262
geometry = Some(shape)263
motion.snapTo(anchor(shape))264
}265
progress = presence;266
index = current()267
when (parent) {268
case Some(val value) -> if (value.frontExtent != shape.extent) {269
value.frontExtent = shape.extent;270
value.scope.requestFrame()271
}272
case None -> {}273
}274
publish()275
}276
fun select(request: Long, animated: Boolean): Boolean {277
if (!available())278
return false279
val shape = geometry!!280
if (request < 0L || request >= shape.snaps.size)281
return false282
cancel(DragCancelReason.Explicit)283
if (!active)284
return false285
val before = current()286
val input = read()287
when (input.props.snap) {288
case Some(val binding) -> if (binding.value != request)289
binding.value = request290
case None -> input.owner.localSnap.value = request291
}292
if (!active)293
return false294
val accepted = current()295
observed = read().props.snap?.value;296
index = accepted297
if (matches(shape))298
moveTo(anchor(shape), animated)299
publish()300
if (before != accepted)301
read().props.onSnapChange(accepted)302
return before != accepted303
}304
fun transition(): AnimationSpec? {305
val input = read()306
return input.theme.tokens.motion.resolve(307
input.props.options.transition.resolve(308
Some(309
AnimationSpec.Tween(310
Duration.milliseconds(400L),311
Easing.cubicBezier(0.22, 1.0, 0.36, 1.0)312
)313
)314
)315
)316
}317
fun moveTo(value: Double, animated: Boolean) {318
if (!active)319
return320
val spec =321
if (animated)322
transition()323
else324
None325
when (spec) {326
case Some(val animation) -> if (327
motion.value !=328
value &&329
(motion.target != value || motion.state != AnimationState.Running)330
)331
motion.animateTo(value, animation)332
else333
if (motion.value == value && motion.state == AnimationState.Running)334
motion.snapTo(value)335
case None -> if (motion.value != value || motion.state == AnimationState.Running)336
motion.snapTo(value)337
}338
}339
fun cancel(reason: DragCancelReason) {340
when (gesture) {341
case None -> {342
drag.cancel();343
return344
}345
case Some(_) -> {}346
}347
gesture = None;348
drag.cancel()349
if (active) {350
when (geometry) {351
case Some(val shape) -> moveTo(anchor(shape), true);352
case None -> {}353
}354
publish()355
}356
read().props.onDragCancel(reason)357
}358
fun close() {359
if (!active)360
return361
read().overlay.requestClose()362
if (active && read().props.open.value)363
when (geometry) {364
case Some(val shape) -> moveTo(anchor(shape), true)365
case None -> {}366
}367
}368
fun publish() {369
if (!active)370
return371
handle?.publish(372
DrawerSnapshot(373
current(),374
geometry?.extent ?: 0.0,375
max(0.0, position * progress),376
when (gesture) {377
case Some(_) -> true;378
case None -> false379
},380
depth381
)382
)383
}384
fun retire() {385
cancel(DragCancelReason.Closed)386
retired = true387
scrollSubscription?.close();388
scrollSubscription = None389
drawerDetachParent(this)390
for (child in children.toArray())391
child.parent = None392
children.clear();393
handle?.unbind(this)394
read().owner.control = None395
}396
}