Scoop API

scoop:scoop.ui:0.1.0

src/scroll/viewport.scoop

Selected production source · src/scroll/viewport.scoop

1package scoop.ui
3import scoop.graphics.*
4import scoop.math.max
5import scoop.surface.*
7public 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: ScrollPadding
15) {
16 public val presentationOffset: Point get() =
17 Point(snapshot.offset.x - overscroll.x, snapshot.offset.y - overscroll.y)
20private class ScrollMotion(val target: Point, val spec: AnimationSpec) {
21 var started = false
24/** A mount-owned layout and animation registration for one ScrollState. */
25public 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 = initial
32 private var closed = false
33 private var ready = false
34 private var binding = 0UL
35 private var motion: ScrollMotion? = None
36 private var cachePolicy = PaintCachePolicy.None
37 private var physicsOptions = ScrollPhysicsOptions()
38 private var reduced = false
39 private var keyboardPadding = ScrollPadding()
40 internal var inputRevision = 0UL
41 internal val edge = ScrollEdge(this, edgeAnimation)
42 internal var inertia: ScrollInertia? = None
43 internal val inertiaAnimation: Animatable<Point> get() = animation
44 private val preferences = node.root.preferences.observe { _ -> preferenceChanged() }
45 private var lastActivity = node.root.window.activity
46 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 = value
54 if (lost)
55 cancelAnimation()
56 }
57 }
58 private var inputWatch: ScrollViewportInput? = None
60 init {
61 current.attach(this)
62 }
64 internal val active: Boolean get() = !closed && node.active && node.root.isOpen
65 internal val bindingRevision: ULong get() = binding
66 internal val semanticSnapshot: ScrollSnapshot? get() =
67 if (active && ready)
68 Some(current.snapshot)
69 else
70 None
71 internal fun belongsTo(owner: MountedNode): Boolean = active && node === owner
72 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(_) -> true
78 case None -> false
79 }
80 internal val positioning: Boolean get() = motion?.started != None
81 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 == generation
85 public val isClosed: Boolean get() {
86 node.root.checkThread();
87 return closed
88 }
89 public var state: ScrollState
90 get() {
91 node.root.checkThread();
92 return current
93 }
94 set(value) {
95 checkOpen()
96 current.checkMutation()
97 if (value === current)
98 return
99 value.checkBinding()
100 cancelAnimation()
101 current.detach(this)
102 current = value
103 binding += 1UL
104 ready = false
105 keyboardPadding = ScrollPadding()
106 current.attach(this)
107 requestFrame()
108 }
109 public var paintCache: PaintCachePolicy
110 get() {
111 node.root.checkThread()
112 return cachePolicy
113 }
114 set(value) {
115 checkOpen()
116 current.checkMutation()
117 checkPaintCachePolicy(value)
118 if (cachePolicy == value)
119 return
120 cachePolicy = value
121 node.request(UiInvalidation.Scene)
122 }
123 public var physics: ScrollPhysicsOptions
124 get() {
125 node.root.checkThread();
126 return physicsOptions
127 }
128 set(value) {
129 checkOpen()
130 current.checkMutation()
131 if (sameScrollPhysics(physicsOptions, value))
132 return
133 physicsOptions = value
134 cancelAnimation()
135 }
136 public var reduceMotion: Boolean
137 get() {
138 node.root.checkThread();
139 return reduced
140 }
141 set(value) {
142 checkOpen()
143 current.checkMutation()
144 if (reduced == value)
145 return
146 reduced = value
147 preferenceChanged()
148 }
149 private fun preferenceChanged() {
150 if (!active || !motionReduced)
151 return
152 val target = motion?.target
153 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 return
166 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.Layout
181 else
182 UiInvalidation.Scene
183 )
184 }
185 internal fun setKeyboardPadding(value: ScrollPadding): Boolean {
186 if (!active || keyboardPadding == value)
187 return false
188 keyboardPadding = value
189 cancelAnimation()
190 requestFrame()
191 return true
192 }
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 = padding
203 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.current
213 val coasting = hasInertia
214 val resized =
215 ready &&
216 (
217 before.viewport !=
218 viewport ||
219 before.content !=
220 extent ||
221 before.padding !=
222 effective
223 )
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.top
231 ),
232 viewport,
233 extent
234 ),
235 viewport,
236 extent,
237 effective
238 )
239 else
240 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 else
245 Point(0.0, 0.0)
246 var adjusted = false
247 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.offset
254 result =
255 ScrollSnapshot(
256 clampScroll(next, viewport, extent),
257 viewport,
258 extent,
259 effective
260 )
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 else
275 edge.sample(result),
276 correction,
277 basePadding
278 )
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 == binding
295 )
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 padding
312 )
313 else
314 current.plan(viewport, content, padding)
315 }
316 private fun complete(layout: ScrollLayout) {
317 val offered = layout.snapshot
318 val state = current
319 val superseded = layout.revision != state.revision
320 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.publishedRevision
329 ready = true
330 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.padding
339 )
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.target
352 if (
353 target ==
354 offered.offset &&
355 (
356 !value.started ||
357 changedTarget ||
358 superseded ||
359 animation.state ==
360 AnimationState.Finished
361 )
362 ) {
363 endMotion(changedTarget || superseded)
364 } else
365 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 += 1UL
380 val token = current.revision
381 if (motionReduced) {
382 current.request = ScrollRequest.Position(target)
383 requestFrame()
384 return token
385 }
386 if (
387 ready &&
388 clampScroll(target, current.current.viewport, current.current.content) ==
389 current.current.offset
390 ) {
391 current.request = ScrollRequest.Position(current.current.offset)
392 requestFrame()
393 return token
394 }
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 = None
405 throw failure
406 }
407 current.changed()
408 requestFrame()
409 return token
410 }
411 private fun startMotion(value: ScrollMotion, target: Point) {
412 if (target == current.current.offset) {
413 endMotion(false)
414 return
415 }
416 animation.snapTo(current.current.offset)
417 animation.animateTo(target, value.spec)
418 value.started = true
419 requestFrame()
420 }
421 internal fun cancelAnimation() {
422 inputRevision += 1UL
423 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 += 1UL
434 endMotion(true)
435 }
436 case None -> {}
437 }
438 }
439 private fun endMotion(stop: Boolean) {
440 motion = None
441 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 return
453 cancelInertia()
454 closed = true
455 var index = 0L
456 while (index < node.root.scrollEdges.size) {
457 if (node.root.scrollEdges[index].viewport === this)
458 node.root.scrollEdges.removeAt(index)
459 else
460 index += 1L
461 }
462 if (ready)
463 current.request = ScrollRequest.Position(current.current.offset)
464 motion = None
465 inputWatch?.close()
466 preferences.close()
467 activity.close()
468 edge.close()
469 animation.close()
470 current.detach(this)
471 }