Scoop API

scoop:scoop.ui:0.1.0

src/scroll/reveal.scoop

Selected production source · src/scroll/reveal.scoop

1package scoop.ui
3import scoop.graphics.*
4import scoop.graphics.native.CanvasGeometry
5import scoop.math.abs
6import scoop.math.max
7import scoop.math.min
8import scoop.surface.InputArea
10internal 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 = true
18) {
19 val area: Rect
20 get() = region ?:
21 Rect(
22 layout.snapshot.padding.left,
23 layout.snapshot.padding.top,
24 layout.snapshot.visibleSize.width,
25 layout.snapshot.visibleSize.height
26 )
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 -> None
32 }
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.vertical
41 )
42 val active: Boolean
43 get() = viewport.matches(state, layout.binding) && area.width > 0.0 && area.height > 0.0
44 val current: Boolean get() = active && state.requestRevision == layout.revision
46 fun project(rect: Rect): Rect? {
47 val local = localReveal(geometry, rect) ?: return None
48 val clipped = projectReveal(local, area, horizontal, vertical) ?: return None
49 return Some(geometry.toWindow(clipped))
50 }
52 fun local(window: Rect): Rect? {
53 val rect = localReveal(geometry, window) ?: return None
54 val offset = layout.presentationOffset
55 val padding = layout.snapshot.padding
56 return Some(
57 Rect(
58 rect.x + offset.x - padding.left,
59 rect.y + offset.y - padding.top,
60 rect.width,
61 rect.height
62 )
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.Nearest
71 ): Boolean {
72 if ((this.horizontal && layout.overscroll.x != 0.0) ||
73 (this.vertical && layout.overscroll.y != 0.0)
74 )
75 return true
76 val snapshot = layout.snapshot
77 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.height
85 )
86 )
87 ) {
88 case Some(_) -> true
89 case None -> false
90 }
91 if (!visible) return wanted != snapshot.offset
92 // Bounds come from the renderer's floating-point transforms.
93 val tolerance = max(1.0, max(snapshot.viewport.width, snapshot.viewport.height)) * 0.000001
94 return abs(wanted.x - snapshot.offset.x) >
95 tolerance ||
96 abs(wanted.y - snapshot.offset.y) >
97 tolerance
98 }
101internal enum RevealProgress {
102 Ready, Moved, Waiting, Unavailable, Cancelled
105private class RevealMove(
106 val viewport: ScrollViewport,
107 val state: ScrollState,
108 val binding: ULong,
109 var revision: ULong
112/** Tracks the scroll bindings and position intents used by one reveal request. */
113internal 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.state
121 for (move in moves)
122 if (move.viewport === viewport) {
123 move.revision = state.requestRevision
124 return
125 }
126 moves.add(RevealMove(viewport, state, viewport.bindingRevision, state.requestRevision))
127 }
128 fun retainFor(node: MountedNode) {
129 var index = 0L
130 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.state
136 val revision = state.beginLayoutRequest()
137 for (move in moves)
138 if (move.viewport === viewport) {
139 move.revision = revision
140 return
141 }
142 moves.add(RevealMove(viewport, state, viewport.bindingRevision, revision))
143 }
144 val isCurrent: Boolean
145 get() {
146 for (move in moves)
147 if (!move.viewport.matches(move.state, move.binding) ||
148 move.state.requestRevision !=
149 move.revision
150 )
151 return false
152 return true
153 }
154 fun advance(scene: FocusScene, area: InputArea, region: Rect? = None): RevealProgress {
155 if (!isCurrent) return RevealProgress.Cancelled
156 val rect = when (region) {
157 case Some(val value) -> area.toWindow(value)
158 case None -> area.windowBounds
159 } ?:
160 return RevealProgress.Unavailable
161 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.Nearest
169 ): RevealProgress {
170 if (!(keyboard?.active ?: true)) return RevealProgress.Cancelled
171 if (!isCurrent) return RevealProgress.Cancelled
172 var rect = bounds
173 if (rect.width <= 0.0 || rect.height <= 0.0) return RevealProgress.Unavailable
174 var index = route.size
175 while (index > 0L) {
176 index -= 1L
177 val boundary = route[index]
178 if (!boundary.active) return RevealProgress.Unavailable
179 if (keyboard?.prepare(boundary, rect) ?: false) {
180 remember(boundary, boundary.state.requestRevision)
181 return RevealProgress.Moved
182 }
183 val local = boundary.local(rect) ?: return RevealProgress.Unavailable
184 if (!boundary.current) return RevealProgress.Waiting
185 val h = if (index == route.size - 1L) horizontal else ScrollAlignment.Nearest
186 val v = if (index == route.size - 1L) vertical else ScrollAlignment.Nearest
187 if (boundary.needsMove(local, h, v)) {
188 val revision = boundary.state.position(boundary.revealedOffset(local, h, v))
189 remember(boundary, revision)
190 return RevealProgress.Moved
191 }
192 rect = boundary.project(rect) ?: return RevealProgress.Unavailable
193 }
194 return if (visible) RevealProgress.Ready else RevealProgress.Unavailable
195 }
196 private fun remember(boundary: ScrollReveal, revision: ULong) {
197 for (move in moves)
198 if (move.viewport === boundary.viewport) {
199 move.revision = revision
200 return
201 }
202 moves.add(RevealMove(boundary.viewport, boundary.state, boundary.layout.binding, revision))
203 }
206internal 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 None
214 var left = first.x
215 var right = first.x
216 var top = first.y
217 var bottom = first.y
218 for (index in 1L..<points.size) {
219 val point = geometry.toLocal(points[index]) ?: return None
220 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))
228internal 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 else
236 None
239/** An orthogonal ancestor still needs the target extent on this boundary's inactive axis. */
240internal fun projectReveal(
241 rect: Rect,
242 region: Rect,
243 horizontal: Boolean,
244 vertical: Boolean
245): Rect? {
246 val left = if (horizontal) max(rect.x, region.x) else rect.x
247 val right = if (horizontal)
248 min(rect.x + rect.width, region.x + region.width)
249 else
250 rect.x + rect.width
251 val top = if (vertical) max(rect.y, region.y) else rect.y
252 val bottom = if (vertical)
253 min(rect.y + rect.height, region.y + region.height)
254 else
255 rect.y + rect.height
256 return if (right > left && bottom > top)
257 Some(Rect(left, top, right - left, bottom - top))
258 else
259 None