scoop:scoop.ui:0.1.0
src/scroll/viewport.scoop
Selected production source · src/scroll/viewport.scoop
1
package scoop.ui3
import scoop.graphics.*4
import scoop.math.max5
import scoop.surface.*7
public class ScrollLayout internal constructor(8
internal val owner: ScrollViewport,9
internal val binding: ULong,10
internal val revision: ULong,11
public val snapshot: ScrollSnapshot,12
public val overscroll: Point,13
internal val correction: Boolean,14
internal val basePadding: ScrollPadding15
) {16
public val presentationOffset: Point get() =17
Point(snapshot.offset.x - overscroll.x, snapshot.offset.y - overscroll.y)18
}20
private class ScrollMotion(val target: Point, val spec: AnimationSpec) {21
var started = false22
}24
/** A mount-owned layout and animation registration for one ScrollState. */25
public class ScrollViewport internal constructor(26
internal val node: MountedNode,27
initial: ScrollState,28
private val animation: Animatable<Point>,29
edgeAnimation: Animatable<Point>30
): Closeable {31
private var current = initial32
private var closed = false33
private var ready = false34
private var binding = 0UL35
private var motion: ScrollMotion? = None36
private var cachePolicy = PaintCachePolicy.None37
private var physicsOptions = ScrollPhysicsOptions()38
private var reduced = false39
private var keyboardPadding = ScrollPadding()40
internal var inputRevision = 0UL41
internal val edge = ScrollEdge(this, edgeAnimation)42
internal var inertia: ScrollInertia? = None43
internal val inertiaAnimation: Animatable<Point> get() = animation44
private val preferences = node.root.preferences.observe { _ -> preferenceChanged() }45
private var lastActivity = node.root.window.activity46
private val activity =47
node.scope.observeWindowActivity {48
value -> {49
val lost =50
(lastActivity.focused && !value.focused) ||51
(lastActivity.visible && !value.visible) ||52
(ready && !lastActivity.occluded && value.occluded)53
lastActivity = value54
if (lost)55
cancelAnimation()56
}57
}58
private var inputWatch: ScrollViewportInput? = None60
init {61
current.attach(this)62
}64
internal val active: Boolean get() = !closed && node.active && node.root.isOpen65
internal val bindingRevision: ULong get() = binding66
internal val semanticSnapshot: ScrollSnapshot? get() =67
if (active && ready)68
Some(current.snapshot)69
else70
None71
internal fun belongsTo(owner: MountedNode): Boolean = active && node === owner72
internal fun contains(owner: MountedNode): Boolean = withinFocusScope(owner, node)73
internal val moving: Boolean get() =74
active && (motion?.started != None || edge.moving || hasInertia)75
private val hasInertia: Boolean get() =76
when (inertia) {77
case Some(_) -> true78
case None -> false79
}80
internal val positioning: Boolean get() = motion?.started != None81
internal val motionReduced: Boolean get() =82
reduced || (node.root.preferences.value.reduceMotion ?: false)83
internal fun matches(value: ScrollState, generation: ULong): Boolean =84
active && current === value && binding == generation85
public val isClosed: Boolean get() {86
node.root.checkThread();87
return closed88
}89
public var state: ScrollState90
get() {91
node.root.checkThread();92
return current93
}94
set(value) {95
checkOpen()96
current.checkMutation()97
if (value === current)98
return99
value.checkBinding()100
cancelAnimation()101
current.detach(this)102
current = value103
binding += 1UL104
ready = false105
keyboardPadding = ScrollPadding()106
current.attach(this)107
requestFrame()108
}109
public var paintCache: PaintCachePolicy110
get() {111
node.root.checkThread()112
return cachePolicy113
}114
set(value) {115
checkOpen()116
current.checkMutation()117
checkPaintCachePolicy(value)118
if (cachePolicy == value)119
return120
cachePolicy = value121
node.request(UiInvalidation.Scene)122
}123
public var physics: ScrollPhysicsOptions124
get() {125
node.root.checkThread();126
return physicsOptions127
}128
set(value) {129
checkOpen()130
current.checkMutation()131
if (sameScrollPhysics(physicsOptions, value))132
return133
physicsOptions = value134
cancelAnimation()135
}136
public var reduceMotion: Boolean137
get() {138
node.root.checkThread();139
return reduced140
}141
set(value) {142
checkOpen()143
current.checkMutation()144
if (reduced == value)145
return146
reduced = value147
preferenceChanged()148
}149
private fun preferenceChanged() {150
if (!active || !motionReduced)151
return152
val target = motion?.target153
cancelAnimation()154
when (target) {155
case Some(val value) -> {156
current.request = ScrollRequest.Position(value)157
requestFrame()158
}159
case None -> {}160
}161
}162
internal fun watchInput(scope: InputScope) {163
when (inputWatch) {164
case Some(val previous) -> if (previous.scope === scope)165
return166
case None -> {}167
}168
inputWatch?.close()169
inputWatch = Some(ScrollViewportInput(this, scope))170
}171
private fun checkOpen() {172
node.root.checkThread()173
if (!active)174
throw IllegalStateException(Some("scroll viewport is no longer mounted"))175
}176
internal fun requestFrame() {177
if (active)178
node.request(179
if (node.frameInputs.usesLayout(this))180
UiInvalidation.Layout181
else182
UiInvalidation.Scene183
)184
}185
internal fun setKeyboardPadding(value: ScrollPadding): Boolean {186
if (!active || keyboardPadding == value)187
return false188
keyboardPadding = value189
cancelAnimation()190
requestFrame()191
return true192
}193
public fun layout(194
viewport: Size,195
content: Size,196
adjustment: ScrollAdjustment? = None,197
padding: ScrollPadding = ScrollPadding()198
): ScrollLayout {199
checkOpen()200
observeScroll(this)201
checkLayoutSize(content)202
val basePadding = padding203
val effective =204
ScrollPadding(205
max(padding.left, keyboardPadding.left),206
max(padding.top, keyboardPadding.top),207
max(padding.right, keyboardPadding.right),208
max(padding.bottom, keyboardPadding.bottom)209
)210
val margin = effective.extent()211
val extent = Size(content.width + margin.width, content.height + margin.height)212
val before = current.current213
val coasting = hasInertia214
val resized =215
ready &&216
(217
before.viewport !=218
viewport ||219
before.content !=220
extent ||221
before.padding !=222
effective223
)224
var result =225
if (coasting && resized)226
ScrollSnapshot(227
clampScroll(228
Point(229
before.offset.x + effective.left - before.padding.left,230
before.offset.y + effective.top - before.padding.top231
),232
viewport,233
extent234
),235
viewport,236
extent,237
effective238
)239
else240
sample(viewport, extent, effective)241
val pending =242
if (coasting && !resized)243
Point(result.offset.x - before.offset.x, result.offset.y - before.offset.y)244
else245
Point(0.0, 0.0)246
var adjusted = false247
when (adjustment) {248
case Some(val value) -> {249
checkScrollPoint(value.offset)250
if (value.requestRevision == current.revision && !positioning) {251
val next = Point(value.offset.x + pending.x, value.offset.y + pending.y)252
checkScrollPoint(next)253
adjusted = value.offset != before.offset254
result =255
ScrollSnapshot(256
clampScroll(next, viewport, extent),257
viewport,258
extent,259
effective260
)261
}262
}263
case None -> {}264
}265
val correction =266
!positioning && before.offset != result.offset && (!coasting || resized || adjusted)267
return ScrollLayout(268
this,269
binding,270
current.revision,271
result,272
if (resized)273
Point(0.0, 0.0)274
else275
edge.sample(result),276
correction,277
basePadding278
)279
}280
public fun present(281
context: SceneContext,282
layout: ScrollLayout,283
content: ((SceneContext) -> Unit)? = None,284
contentOrigin: Point = Point(0.0, 0.0)285
) {286
checkOpen()287
context.checkOwner(node)288
checkScrollPoint(contentOrigin)289
requireUi(layout.owner === this, "scroll layout belongs to a different viewport")290
requireUi(layout.binding == binding, "scroll layout belongs to a previous binding")291
context.presentScroll(this)292
context.onFrameComplete {293
frame -> if (294
frame.submission == FrameSubmission.Accepted && active && layout.binding == binding295
)296
complete(layout)297
}298
when (content) {299
case Some(val block) -> context.scrollContent(this, layout, contentOrigin, block)300
case None -> {}301
}302
}303
private fun sample(viewport: Size, content: Size, padding: ScrollPadding): ScrollSnapshot {304
checkLayoutSize(viewport)305
checkLayoutSize(content)306
return if (motion?.started ?: false)307
ScrollSnapshot(308
clampScroll(animation.value, viewport, content),309
viewport,310
content,311
padding312
)313
else314
current.plan(viewport, content, padding)315
}316
private fun complete(layout: ScrollLayout) {317
val offered = layout.snapshot318
val state = current319
val superseded = layout.revision != state.revision320
val correction =321
layout.correction &&322
state.hasSnapshot &&323
state.current.offset !=324
offered.offset &&325
layout.revision ==326
state.revision &&327
layout.revision ==328
state.publishedRevision329
ready = true330
if (331
state.hasSnapshot &&332
(333
state.current.viewport !=334
offered.viewport ||335
state.current.content !=336
offered.content ||337
state.current.padding !=338
offered.padding339
)340
)341
{342
cancelInertia(true)343
edge.stop()344
}345
edge.complete()346
inertia?.complete()347
state.publish(offered, layout.revision == state.revision && !positioning, layout.revision)348
when (motion) {349
case Some(val value) -> {350
val target = clampScroll(value.target, offered.viewport, offered.content)351
val changedTarget = value.started && target != animation.target352
if (353
target ==354
offered.offset &&355
(356
!value.started ||357
changedTarget ||358
superseded ||359
animation.state ==360
AnimationState.Finished361
)362
) {363
endMotion(changedTarget || superseded)364
} else365
if (!value.started || changedTarget || superseded) {366
startMotion(value, target)367
}368
}369
case None -> {}370
}371
if (layout.revision != state.revision)372
requestFrame()373
if (correction)374
state.notifyIntent(ScrollIntent.LayoutCorrection(layout.revision))375
}376
internal fun animateTo(target: Point, spec: AnimationSpec): ULong {377
checkOpen()378
cancelAnimation()379
current.revision += 1UL380
val token = current.revision381
if (motionReduced) {382
current.request = ScrollRequest.Position(target)383
requestFrame()384
return token385
}386
if (387
ready &&388
clampScroll(target, current.current.viewport, current.current.content) ==389
current.current.offset390
) {391
current.request = ScrollRequest.Position(current.current.offset)392
requestFrame()393
return token394
}395
val next = ScrollMotion(target, spec)396
motion = Some(next)397
try {398
if (ready)399
startMotion(400
next,401
clampScroll(target, current.current.viewport, current.current.content)402
)403
} catch (failure: Throwable) {404
motion = None405
throw failure406
}407
current.changed()408
requestFrame()409
return token410
}411
private fun startMotion(value: ScrollMotion, target: Point) {412
if (target == current.current.offset) {413
endMotion(false)414
return415
}416
animation.snapTo(current.current.offset)417
animation.animateTo(target, value.spec)418
value.started = true419
requestFrame()420
}421
internal fun cancelAnimation() {422
inputRevision += 1UL423
cancelInertia()424
edge.stop()425
cancelPositioning()426
}427
internal fun cancelInertia(keepPending: Boolean = false) {428
inertia?.stop(keepPending)429
}430
internal fun cancelPositioning() {431
when (motion) {432
case Some(_) -> {433
current.revision += 1UL434
endMotion(true)435
}436
case None -> {}437
}438
}439
private fun endMotion(stop: Boolean) {440
motion = None441
if (stop)442
animation.snapTo(current.current.offset)443
if (ready)444
current.request = ScrollRequest.Position(current.current.offset)445
current.changed()446
requestFrame()447
}448
public override fun close() {449
node.root.checkThread()450
current.checkMutation()451
if (closed)452
return453
cancelInertia()454
closed = true455
var index = 0L456
while (index < node.root.scrollEdges.size) {457
if (node.root.scrollEdges[index].viewport === this)458
node.root.scrollEdges.removeAt(index)459
else460
index += 1L461
}462
if (ready)463
current.request = ScrollRequest.Position(current.current.offset)464
motion = None465
inputWatch?.close()466
preferences.close()467
activity.close()468
edge.close()469
animation.close()470
current.detach(this)471
}472
}