scoop:scoop.ui.component:0.1.0
src/actions/loading_motion.scoop
Selected production source · src/actions/loading_motion.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.*5
import scoop.time.Duration6
import scoop.ui.*8
/** One scope-owned phase shared by the three looping loading indicators. */9
internal class LoadingMotion(scope: ComponentScope) {10
val phase: Animatable<Double> = scope.animation(0.0)11
private var period: Duration? = None12
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
} else20
if (period != None) {21
phase.snapTo(0.0)22
period = None23
}24
}25
}27
internal 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.y47
case None -> false48
}50
internal fun loadingParticipation(51
context: SceneContext,52
animation: Animatable<Double>,53
visible: Boolean54
) {55
context.onFrameComplete {56
frame -> if (frame.submission == FrameSubmission.Accepted)57
animation.isFrameEnabled = visible58
}59
}