Scoop API

scoop:scoop.ui.component:0.1.0

src/conversation/shimmer.scoop

Selected production source · src/conversation/shimmer.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.time.Duration
6import scoop.ui.*
7import scoop.ui.style.*
9private class ConversationShimmerProps(val active: Boolean, val opacity: Double)
11/** Only display content uses the moving alpha band; its layout and input remain unchanged. */
12internal fun conversationShimmer(
13 content: View,
14 active: Boolean,
15 theme: Theme,
16 opacity: Double = 1.0
17): View =
18 element(
19 ConversationShimmerProps(
20 active && theme.tokens.motion.preference == MotionPreference.Full,
21 opacity
22 ),
23 { current, scope -> ConversationShimmer(current, scope) },
24 [content]
25 )
27private class ConversationShimmer(
28 private val props: Props<ConversationShimmerProps>,
29 scope: ComponentScope
30): Element<Unit>(scope) {
31 private val motion = LoadingMotion(scope)
32 private var visible = false
33 public override fun update(): UiInvalidation {
34 motion.update(props.value.active && props.value.opacity > 0.0, Duration.milliseconds(1600L))
35 return UiInvalidation.Scene
36 }
37 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
38 Unit
39 > {
40 val child = context.measureChild(0L, constraints)
41 context.placeChild(0L, Point(0.0, 0.0))
42 return LayoutResult(child, Unit)
43 }
44 public override fun scene(context: SceneContext, measured: Unit) {
45 val input = props.value
46 val size = context.size
47 val bounds = Rect(0.0, 0.0, size.width, size.height)
48 visible = false
49 context.draw { paint -> visible = loadingVisible(paint.canvas, bounds, input.opacity) }
50 val animated = input.active && visible
51 if (input.opacity < 1.0 || animated)
52 context.saveLayer(input.opacity)
53 context.children()
54 if (animated) {
55 val center = (-0.3 + 1.6 * motion.phase.value) * size.width
56 val band = lowerBound(1.0, size.width * 0.35)
57 val mask =
58 Gradient.linear(
59 Point(center - band, 0.0),
60 Point(center + band, 0.0),
61 [
62 GradientStop(0.0, Color.White.withAlpha(0.5)),
63 GradientStop(0.5, Color.White),
64 GradientStop(1.0, Color.White.withAlpha(0.5))
65 ]
66 )
67 context.saveLayer(blendMode = BlendMode.DstIn)
68 context.draw { paint -> paint.canvas.drawRect(bounds, Paint(gradient = Some(mask))) }
69 context.restore()
70 }
71 if (input.opacity < 1.0 || animated)
72 context.restore()
73 loadingParticipation(context, motion.phase, animated)
74 }