scoop:scoop.ui:0.1.0
src/scroll/reveal.scoop
Selected production source · src/scroll/reveal.scoop
1
package scoop.ui3
import scoop.graphics.*4
import scoop.graphics.native.CanvasGeometry5
import scoop.math.abs6
import scoop.math.max7
import scoop.math.min8
import scoop.surface.InputArea10
internal class ScrollReveal(11
val viewport: ScrollViewport,12
val state: ScrollState,13
val layout: ScrollLayout,14
val geometry: CanvasGeometry,15
private val region: Rect? = None,16
val horizontal: Boolean = true,17
val vertical: Boolean = true18
) {19
val area: Rect20
get() = region ?:21
Rect(22
layout.snapshot.padding.left,23
layout.snapshot.padding.top,24
layout.snapshot.visibleSize.width,25
layout.snapshot.visibleSize.height26
)27
val windowBounds: Rect get() = geometry.toWindow(area)28
val visibleBounds: Rect?29
get() = when (geometry.visibleBounds) {30
case Some(val visible) -> intersectReveal(windowBounds, visible)31
case None -> None32
}33
fun constrain(value: Rect, axes: ScrollAxes): ScrollReveal = ScrollReveal(34
viewport,35
state,36
layout,37
geometry,38
Some(intersectReveal(area, value) ?: Rect(0.0, 0.0, 0.0, 0.0)),39
horizontal && axes.horizontal,40
vertical && axes.vertical41
)42
val active: Boolean43
get() = viewport.matches(state, layout.binding) && area.width > 0.0 && area.height > 0.044
val current: Boolean get() = active && state.requestRevision == layout.revision46
fun project(rect: Rect): Rect? {47
val local = localReveal(geometry, rect) ?: return None48
val clipped = projectReveal(local, area, horizontal, vertical) ?: return None49
return Some(geometry.toWindow(clipped))50
}52
fun local(window: Rect): Rect? {53
val rect = localReveal(geometry, window) ?: return None54
val offset = layout.presentationOffset55
val padding = layout.snapshot.padding56
return Some(57
Rect(58
rect.x + offset.x - padding.left,59
rect.y + offset.y - padding.top,60
rect.width,61
rect.height62
)63
)64
}65
fun revealedOffset(rect: Rect, h: ScrollAlignment, v: ScrollAlignment): Point =66
scrollRegionOffset(layout.snapshot, area, rect, horizontal, vertical, h, v)67
fun needsMove(68
rect: Rect,69
horizontal: ScrollAlignment = ScrollAlignment.Nearest,70
vertical: ScrollAlignment = ScrollAlignment.Nearest71
): Boolean {72
if ((this.horizontal && layout.overscroll.x != 0.0) ||73
(this.vertical && layout.overscroll.y != 0.0)74
)75
return true76
val snapshot = layout.snapshot77
val wanted = revealedOffset(rect, horizontal, vertical)78
val visible = when (intersectReveal(79
rect,80
Rect(81
snapshot.offset.x + area.x - snapshot.padding.left,82
snapshot.offset.y + area.y - snapshot.padding.top,83
area.width,84
area.height85
)86
)87
) {88
case Some(_) -> true89
case None -> false90
}91
if (!visible) return wanted != snapshot.offset92
// Bounds come from the renderer's floating-point transforms.93
val tolerance = max(1.0, max(snapshot.viewport.width, snapshot.viewport.height)) * 0.00000194
return abs(wanted.x - snapshot.offset.x) >95
tolerance ||96
abs(wanted.y - snapshot.offset.y) >97
tolerance98
}99
}101
internal enum RevealProgress {102
Ready, Moved, Waiting, Unavailable, Cancelled103
}105
private class RevealMove(106
val viewport: ScrollViewport,107
val state: ScrollState,108
val binding: ULong,109
var revision: ULong110
)112
/** Tracks the scroll bindings and position intents used by one reveal request. */113
internal class RevealAttempt(private val keyboard: KeyboardReveal? = None) {114
private val moves = ArrayList<RevealMove>()115
fun watch(path: Array<ScrollReveal>) {116
for (boundary in path)117
remember(boundary, boundary.state.requestRevision)118
}119
fun watch(viewport: ScrollViewport) {120
val state = viewport.state121
for (move in moves)122
if (move.viewport === viewport) {123
move.revision = state.requestRevision124
return125
}126
moves.add(RevealMove(viewport, state, viewport.bindingRevision, state.requestRevision))127
}128
fun retainFor(node: MountedNode) {129
var index = 0L130
while (index < moves.size) {131
if (moves[index].viewport.contains(node)) index += 1L else moves.removeAt(index)132
}133
}134
fun positionItem(viewport: ScrollViewport) {135
val state = viewport.state136
val revision = state.beginLayoutRequest()137
for (move in moves)138
if (move.viewport === viewport) {139
move.revision = revision140
return141
}142
moves.add(RevealMove(viewport, state, viewport.bindingRevision, revision))143
}144
val isCurrent: Boolean145
get() {146
for (move in moves)147
if (!move.viewport.matches(move.state, move.binding) ||148
move.state.requestRevision !=149
move.revision150
)151
return false152
return true153
}154
fun advance(scene: FocusScene, area: InputArea, region: Rect? = None): RevealProgress {155
if (!isCurrent) return RevealProgress.Cancelled156
val rect = when (region) {157
case Some(val value) -> area.toWindow(value)158
case None -> area.windowBounds159
} ?:160
return RevealProgress.Unavailable161
return advance(scene.path(area), rect, area.canFocus)162
}163
fun advance(164
route: Array<ScrollReveal>,165
bounds: Rect,166
visible: Boolean,167
horizontal: ScrollAlignment = ScrollAlignment.Nearest,168
vertical: ScrollAlignment = ScrollAlignment.Nearest169
): RevealProgress {170
if (!(keyboard?.active ?: true)) return RevealProgress.Cancelled171
if (!isCurrent) return RevealProgress.Cancelled172
var rect = bounds173
if (rect.width <= 0.0 || rect.height <= 0.0) return RevealProgress.Unavailable174
var index = route.size175
while (index > 0L) {176
index -= 1L177
val boundary = route[index]178
if (!boundary.active) return RevealProgress.Unavailable179
if (keyboard?.prepare(boundary, rect) ?: false) {180
remember(boundary, boundary.state.requestRevision)181
return RevealProgress.Moved182
}183
val local = boundary.local(rect) ?: return RevealProgress.Unavailable184
if (!boundary.current) return RevealProgress.Waiting185
val h = if (index == route.size - 1L) horizontal else ScrollAlignment.Nearest186
val v = if (index == route.size - 1L) vertical else ScrollAlignment.Nearest187
if (boundary.needsMove(local, h, v)) {188
val revision = boundary.state.position(boundary.revealedOffset(local, h, v))189
remember(boundary, revision)190
return RevealProgress.Moved191
}192
rect = boundary.project(rect) ?: return RevealProgress.Unavailable193
}194
return if (visible) RevealProgress.Ready else RevealProgress.Unavailable195
}196
private fun remember(boundary: ScrollReveal, revision: ULong) {197
for (move in moves)198
if (move.viewport === boundary.viewport) {199
move.revision = revision200
return201
}202
moves.add(RevealMove(boundary.viewport, boundary.state, boundary.layout.binding, revision))203
}204
}206
internal fun localReveal(geometry: CanvasGeometry, rect: Rect): Rect? {207
val points = [208
Point(rect.x, rect.y),209
Point(rect.x + rect.width, rect.y),210
Point(rect.x, rect.y + rect.height),211
Point(rect.x + rect.width, rect.y + rect.height)212
]213
val first = geometry.toLocal(points[0L]) ?: return None214
var left = first.x215
var right = first.x216
var top = first.y217
var bottom = first.y218
for (index in 1L..<points.size) {219
val point = geometry.toLocal(points[index]) ?: return None220
left = min(left, point.x)221
right = max(right, point.x)222
top = min(top, point.y)223
bottom = max(bottom, point.y)224
}225
return Some(Rect(left, top, right - left, bottom - top))226
}228
internal fun intersectReveal(a: Rect, b: Rect): Rect? {229
val left = max(a.x, b.x)230
val top = max(a.y, b.y)231
val right = min(a.x + a.width, b.x + b.width)232
val bottom = min(a.y + a.height, b.y + b.height)233
return if (right > left && bottom > top)234
Some(Rect(left, top, right - left, bottom - top))235
else236
None237
}239
/** An orthogonal ancestor still needs the target extent on this boundary's inactive axis. */240
internal fun projectReveal(241
rect: Rect,242
region: Rect,243
horizontal: Boolean,244
vertical: Boolean245
): Rect? {246
val left = if (horizontal) max(rect.x, region.x) else rect.x247
val right = if (horizontal)248
min(rect.x + rect.width, region.x + region.width)249
else250
rect.x + rect.width251
val top = if (vertical) max(rect.y, region.y) else rect.y252
val bottom = if (vertical)253
min(rect.y + rect.height, region.y + region.height)254
else255
rect.y + rect.height256
return if (right > left && bottom > top)257
Some(Rect(left, top, right - left, bottom - top))258
else259
None260
}