Scoop API

scoop:scoop.ui.component:0.1.0

src/actions/loading_motion.scoop

Selected production source · src/actions/loading_motion.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.time.Duration
6import scoop.ui.*
8/** One scope-owned phase shared by the three looping loading indicators. */
9internal class LoadingMotion(scope: ComponentScope) {
10 val phase: Animatable<Double> = scope.animation(0.0)
11 private var period: Duration? = None
12 fun update(active: Boolean, duration: Duration) {
13 if (active) {
14 if (period != Some(duration)) {
15 phase.snapTo(0.0)
16 phase.animateTo(1.0, AnimationSpec.Tween(duration), Some(Repeat()))
17 period = Some(duration)
18 }
19 } else
20 if (period != None) {
21 phase.snapTo(0.0)
22 period = None
23 }
24 }
27internal fun loadingVisible(canvas: Canvas, bounds: Rect, alpha: Double): Boolean =
28 bounds.width >
29 0.0 &&
30 bounds.height >
31 0.0 &&
32 alpha >
33 0.0 &&
34 when (canvas.localClipBounds) {
35 case Some(val clip) -> bounds.x <
36 clip.x +
37 clip.width &&
38 bounds.x +
39 bounds.width >
40 clip.x &&
41 bounds.y <
42 clip.y +
43 clip.height &&
44 bounds.y +
45 bounds.height >
46 clip.y
47 case None -> false
48 }
50internal fun loadingParticipation(
51 context: SceneContext,
52 animation: Animatable<Double>,
53 visible: Boolean
54) {
55 context.onFrameComplete {
56 frame -> if (frame.submission == FrameSubmission.Accepted)
57 animation.isFrameEnabled = visible
58 }