scoop:scoop.ui:0.1.0
src/scroll/edge.scoop
Selected production source · src/scroll/edge.scoop
1
package scoop.ui3
import scoop.graphics.Point4
import scoop.math.max5
import scoop.math.min6
import scoop.surface.*8
/** One viewport's held stretch or release spring; it never changes ScrollSnapshot. */9
internal class ScrollEdge(10
private val viewport: ScrollViewport,11
private val animation: Animatable<Point>12
): Closeable {13
private var pull = Point(0.0, 0.0)14
private var releasing = false15
private var momentumEdge = Point(0.0, 0.0)16
val moving: Boolean get() = releasing17
val held: Boolean get() = pull != Point(0.0, 0.0)18
val inputActive: Boolean get() = held || releasing || momentumEdge != Point(0.0, 0.0)20
/** A native momentum tail cannot hold or restart an already reached edge. */21
fun consumeMomentum(amount: Point): Point {22
if (held)23
release()24
val current =25
if (releasing)26
animation.value27
else28
Point(0.0, 0.0)29
val direction =30
Point(31
if (momentumEdge.x == 0.0)32
current.x33
else34
momentumEdge.x,35
if (momentumEdge.y == 0.0)36
current.y37
else38
momentumEdge.y39
)40
val reverseX = scrollOpposesEdge(amount.x, direction.x)41
val reverseY = scrollOpposesEdge(amount.y, direction.y)42
val next =43
Point(44
if (reverseX)45
0.046
else47
direction.x,48
if (reverseY)49
0.050
else51
direction.y52
)53
if (releasing && (reverseX || reverseY)) {54
val speed = animation.velocity55
val start =56
Point(57
if (reverseX)58
0.059
else60
current.x,61
if (reverseY)62
0.063
else64
current.y65
)66
if (start == Point(0.0, 0.0))67
stop()68
else69
beginRelease(70
start,71
Point(72
if (reverseX)73
0.074
else75
speed.x,76
if (reverseY)77
0.078
else79
speed.y80
)81
)82
}83
momentumEdge = next84
return Point(85
if (direction.x != 0.0 && !reverseX)86
amount.x87
else88
0.0,89
if (direction.y != 0.0 && !reverseY)90
amount.y91
else92
0.093
)94
}96
fun absorbMomentum(amount: Point, axes: ScrollAxes): Point {97
val used = absorb(amount, axes)98
if (releasing && used != Point(0.0, 0.0))99
kick(used, Point(0.0, 0.0), used.x != 0.0, used.y != 0.0)100
else101
release()102
momentumEdge =103
Point(104
if (used.x != 0.0)105
used.x106
else107
momentumEdge.x,108
if (used.y != 0.0)109
used.y110
else111
momentumEdge.y112
)113
return used114
}116
fun endMomentum() {117
momentumEdge = Point(0.0, 0.0)118
}120
fun restoringFraction(amount: Point): Double =121
max(122
if (amount.x == 0.0)123
0.0124
else125
scrollRestoringPart(pull.x, amount.x) / amount.x,126
if (amount.y == 0.0)127
0.0128
else129
scrollRestoringPart(pull.y, amount.y) / amount.y130
)132
fun sample(snapshot: ScrollSnapshot): Point {133
val options = viewport.physics134
if (viewport.motionReduced || !options.elastic)135
return Point(0.0, 0.0)136
val amount =137
if (releasing)138
animation.value139
else140
Point(141
scrollElasticDistance(pull.x, options),142
scrollElasticDistance(pull.y, options)143
)144
return Point(145
edgeValue(amount.x, snapshot.offset.x, snapshot.maximum.x, options),146
edgeValue(amount.y, snapshot.offset.y, snapshot.maximum.y, options)147
)148
}150
fun restore(amount: Point): Point {151
val used =152
Point(scrollRestoringPart(pull.x, amount.x), scrollRestoringPart(pull.y, amount.y))153
if (used != Point(0.0, 0.0)) {154
pull = Point(pull.x + used.x, pull.y + used.y)155
viewport.requestFrame()156
}157
return used158
}160
fun absorb(amount: Point, axes: ScrollAxes): Point {161
val options = viewport.physics162
if (viewport.motionReduced || !options.elastic)163
return Point(0.0, 0.0)164
val snapshot = viewport.state.current165
val used =166
Point(167
if (axes.horizontal && (snapshot.maximum.x > 0.0 || options.shortContent))168
amount.x169
else170
0.0,171
if (axes.vertical && (snapshot.maximum.y > 0.0 || options.shortContent))172
amount.y173
else174
0.0175
)176
if (used != Point(0.0, 0.0)) {177
val next = Point(pull.x + used.x, pull.y + used.y)178
checkScrollPoint(next)179
pull = next180
viewport.requestFrame()181
}182
return used183
}185
fun release() {186
if (releasing || pull == Point(0.0, 0.0))187
return188
if (viewport.motionReduced || !viewport.physics.elastic) {189
stop()190
return191
}192
val state = viewport.state193
val accepted = state.current194
val start = sample(state.plan(accepted.viewport, accepted.content, accepted.padding))195
pull = Point(0.0, 0.0)196
if (start == Point(0.0, 0.0)) {197
viewport.requestFrame()198
return199
}200
beginRelease(start, Point(0.0, 0.0))201
}203
fun kick(amount: Point, velocity: Point, horizontal: Boolean, vertical: Boolean) {204
val options = viewport.physics205
if (viewport.motionReduced || !options.elastic)206
return207
val state = viewport.state208
val current = state.current209
val snapshot = state.plan(current.viewport, current.content, current.padding)210
val x = horizontal && (snapshot.maximum.x > 0.0 || options.shortContent)211
val y = vertical && (snapshot.maximum.y > 0.0 || options.shortContent)212
if (!x && !y)213
return214
val before = sample(snapshot)215
val speed =216
if (releasing)217
animation.velocity218
else219
AnimationVector()220
val start =221
Point(222
if (x)223
edgeValue(224
before.x + scrollElasticDistance(amount.x, options),225
snapshot.offset.x,226
snapshot.maximum.x,227
options228
)229
else230
before.x,231
if (y)232
edgeValue(233
before.y + scrollElasticDistance(amount.y, options),234
snapshot.offset.y,235
snapshot.maximum.y,236
options237
)238
else239
before.y240
)241
beginRelease(242
start,243
Point(244
if (x)245
velocity.x * options.resistance246
else247
speed.x,248
if (y)249
velocity.y * options.resistance250
else251
speed.y252
)253
)254
}256
private fun beginRelease(start: Point, velocity: Point) {257
pull = Point(0.0, 0.0)258
animation.snapTo(start)259
val options = viewport.physics260
animation.animateTo(261
Point(0.0, 0.0),262
AnimationSpec.Spring(options.stiffness, options.dampingRatio, 0.1, 1.0),263
initialVelocity = Some(AnimationVector(velocity.x, velocity.y))264
)265
releasing = true266
viewport.state.changed()267
viewport.requestFrame()268
}270
fun complete() {271
if (releasing && animation.state == AnimationState.Finished) {272
releasing = false273
viewport.state.changed()274
}275
}277
fun interruptRelease() {278
if (releasing)279
stop()280
}282
fun stop() {283
val moving = releasing284
val changed = moving || pull != Point(0.0, 0.0)285
releasing = false286
pull = Point(0.0, 0.0)287
momentumEdge = Point(0.0, 0.0)288
if (moving) {289
animation.snapTo(Point(0.0, 0.0))290
viewport.state.changed()291
}292
if (changed)293
viewport.requestFrame()294
}296
public override fun close() {297
stop()298
animation.close()299
}300
}302
private fun scrollOpposesEdge(amount: Double, edge: Double): Boolean =303
(amount < 0.0 && edge > 0.0) || (amount > 0.0 && edge < 0.0)305
private fun edgeValue(306
amount: Double,307
offset: Double,308
maximum: Double,309
options: ScrollPhysicsOptions310
): Double {311
if (maximum == 0.0 && !options.shortContent)312
return 0.0313
val limit = options.maxOverscroll314
if (amount > 0.0 && offset == 0.0)315
return min(limit, amount)316
if (amount < 0.0 && offset == maximum)317
return max(-limit, amount)318
return 0.0319
}