scoop:scoop.ui:0.1.0
src/scroll/state.scoop
Selected production source · src/scroll/state.scoop
1
package scoop.ui3
import scoop.graphics.*4
import scoop.platform.concurrent.currentThreadId5
import scoop.surface.AnimationSpec6
import scoop.time.Duration8
/** Observable scroll position, independent of a particular mount. */9
public class ScrollState public constructor(initialOffset: Point = Point(0.0, 0.0)) {10
private val thread = currentThreadId()11
private val signal = StateSignal()12
private val subscriptions = ArrayList<ScrollIntentSubscription>()13
private val notifications = ArrayList<ScrollIntent>()14
private var notifying = false15
private var viewportOwner: ScrollViewport? = None16
private var initialized = false17
internal var current = ScrollSnapshot(Point(0.0, 0.0), Size(0.0, 0.0), Size(0.0, 0.0))18
internal var request = ScrollRequest.Position(initialOffset)19
internal var revision =20
if (initialOffset == Point(0.0, 0.0))21
0UL22
else23
1UL24
internal var publishedRevision = revision26
init {27
checkScrollPoint(initialOffset)28
}30
private fun observeState() {31
checkUiThread(thread)32
when (currentEvaluation()) {33
case Some(val evaluation) -> evaluation.collector?.read(signal)34
case None -> {}35
}36
}37
internal fun checkMutation() {38
checkUiThread(thread)39
when (currentEvaluation()) {40
case Some(_) -> throw IllegalStateException(41
Some("scroll state cannot change during render, measure, scene or draw")42
)43
case None -> {}44
}45
}46
public val snapshot: ScrollSnapshot get() {47
observeState();48
return current49
}50
public val offset: Point get() = snapshot.offset51
public val viewport: Size get() = snapshot.viewport52
public val content: Size get() = snapshot.content53
public val maximum: Point get() = snapshot.maximum54
public val hasSnapshot: Boolean get() {55
observeState()56
return initialized57
}58
public val requestRevision: ULong get() {59
checkUiThread(thread)60
return revision61
}62
public val hasPositionRequest: Boolean get() {63
checkUiThread(thread)64
return revision != 0UL65
}66
public val isAttached: Boolean get() {67
observeState();68
return viewportOwner?.active ?: false69
}70
public val isAnimating: Boolean get() {71
observeState();72
return viewportOwner?.moving ?: false73
}74
/** An explicit animateTo request, including its wait for the first accepted layout. */75
public val hasPositionAnimation: Boolean get() {76
observeState()77
return viewportOwner?.positioning ?: false78
}80
public fun observeIntent(handler: (ScrollIntent) -> Unit): Closeable {81
checkMutation()82
val value = ScrollIntentSubscription(this, handler)83
subscriptions.add(value)84
return value85
}86
internal fun removeSubscription(value: ScrollIntentSubscription) {87
var index = 0L88
while (index < subscriptions.size) {89
if (subscriptions[index] === value) {90
subscriptions.removeAt(index)91
return92
}93
index += 1L94
}95
}96
internal fun notifyIntent(value: ScrollIntent) {97
notifications.add(value)98
if (notifying)99
return100
notifying = true101
var failure: Throwable? = None102
try {103
var index = 0L104
while (index < notifications.size) {105
val event = notifications[index]106
index += 1L107
for (subscription in subscriptions.toArray())108
if (!subscription.closed)109
failure = cleanupUi(failure) { subscription.handler(event) }110
}111
} finally {112
notifications.clear()113
notifying = false114
}115
throwUi(failure)116
}117
private fun notifyRequest(origin: ScrollOrigin, token: ULong) {118
notifyIntent(119
when (origin) {120
case User -> ScrollIntent.User(token)121
case Programmatic -> ScrollIntent.Programmatic(token)122
}123
)124
}125
public fun scrollTo(offset: Point, origin: ScrollOrigin = ScrollOrigin.Programmatic) {126
position(offset, origin)127
}128
internal fun position(offset: Point, origin: ScrollOrigin = ScrollOrigin.Programmatic): ULong {129
checkMutation()130
checkScrollPoint(offset)131
return change(ScrollRequest.Position(offset), origin)132
}133
public fun scrollBy(delta: Point, origin: ScrollOrigin = ScrollOrigin.Programmatic): Point {134
checkMutation()135
checkScrollPoint(delta)136
val base =137
if (viewportOwner?.moving ?: false)138
current.offset139
else140
if (initialized)141
plan(current.viewport, current.content, current.padding).offset142
else143
when (request) {144
case Position(val value) -> value145
case Reveal(_, _, _) -> current.offset146
}147
val next = Point(base.x + delta.x, base.y + delta.y)148
checkScrollPoint(next)149
val bounded =150
if (initialized)151
clampScroll(next, current.viewport, current.content)152
else153
base154
val consumed = Point(bounded.x - base.x, bounded.y - base.y)155
change(ScrollRequest.Position(next), origin)156
return consumed157
}158
/** Routed input preserves the current held edge; explicit position requests cancel it. */159
internal fun consumeInput(delta: Point, notify: Boolean = true): Point {160
checkMutation()161
checkScrollPoint(delta)162
val base = plan(current.viewport, current.content, current.padding).offset163
val next = Point(base.x + delta.x, base.y + delta.y)164
checkScrollPoint(next)165
val bounded = clampScroll(next, current.viewport, current.content)166
if (!notify && bounded == base)167
return Point(0.0, 0.0)168
request = ScrollRequest.Position(bounded)169
if (notify)170
revision += 1UL171
val token = revision172
viewportOwner?.requestFrame()173
if (notify)174
notifyRequest(ScrollOrigin.User, token)175
return Point(bounded.x - base.x, bounded.y - base.y)176
}177
public fun reveal(178
rect: Rect,179
horizontal: ScrollAlignment = ScrollAlignment.Nearest,180
vertical: ScrollAlignment = ScrollAlignment.Nearest,181
origin: ScrollOrigin = ScrollOrigin.Programmatic182
) {183
checkMutation()184
checkScrollRect(rect)185
change(ScrollRequest.Reveal(rect, horizontal, vertical), origin)186
}187
/** Registers a target that a layout will resolve through ScrollAdjustment. */188
public fun beginLayoutRequest(origin: ScrollOrigin = ScrollOrigin.Programmatic): ULong {189
checkMutation()190
return change(ScrollRequest.Position(current.offset), origin)191
}192
public fun animateTo(193
offset: Point,194
spec: AnimationSpec = AnimationSpec.Tween(Duration.milliseconds(300L)),195
origin: ScrollOrigin = ScrollOrigin.Programmatic196
) {197
checkMutation()198
checkScrollPoint(offset)199
val owner =200
viewportOwner ?:201
throw IllegalStateException(Some("smooth scrolling requires an attached viewport"))202
val token = owner.animateTo(offset, spec)203
notifyRequest(origin, token)204
}205
public fun cancelAnimation() {206
checkMutation()207
viewportOwner?.cancelAnimation()208
}209
private fun change(next: ScrollRequest, origin: ScrollOrigin): ULong {210
viewportOwner?.cancelAnimation()211
request = next212
revision += 1UL213
val token = revision214
viewportOwner?.requestFrame()215
notifyRequest(origin, token)216
return token217
}218
internal fun plan(219
viewport: Size,220
content: Size,221
padding: ScrollPadding = ScrollPadding()222
): ScrollSnapshot {223
val dx = padding.left - current.padding.left224
val dy = padding.top - current.padding.top225
val preserve =226
if (initialized)227
revision == publishedRevision228
else229
revision == 0UL230
val next =231
when (request) {232
case Position(val point) -> if (preserve)233
ScrollRequest.Position(Point(point.x + dx, point.y + dy))234
else235
request236
case Reveal(_, _, _) -> request237
}238
return resolveScroll(239
next,240
Point(current.offset.x + dx, current.offset.y + dy),241
viewport,242
content,243
padding244
)245
}247
internal fun publish(248
value: ScrollSnapshot,249
settle: Boolean,250
acceptedRevision: ULong = revision251
) {252
val changed = current != value || !initialized253
current = value254
publishedRevision = acceptedRevision255
initialized = true256
if (settle)257
request = ScrollRequest.Position(value.offset)258
if (changed)259
signal.changed()260
}261
internal fun changed() {262
signal.changed()263
}264
internal fun checkBinding() {265
checkMutation()266
when (viewportOwner) {267
case Some(_) -> throw IllegalStateException(Some("scroll state already has a viewport"))268
case None -> {}269
}270
}271
internal fun attach(owner: ScrollViewport) {272
viewportOwner = Some(owner)273
signal.changed()274
}275
internal fun detach(owner: ScrollViewport) {276
when (viewportOwner) {277
case Some(val value) -> if (value === owner) {278
viewportOwner = None279
signal.changed()280
}281
case None -> {}282
}283
}284
}