scoop:scoop.ui.component:0.1.0
src/conversation/shimmer.scoop
Selected production source · src/conversation/shimmer.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.*5
import scoop.time.Duration6
import scoop.ui.*7
import scoop.ui.style.*9
private class ConversationShimmerProps(val active: Boolean, val opacity: Double)11
/** Only display content uses the moving alpha band; its layout and input remain unchanged. */12
internal fun conversationShimmer(13
content: View,14
active: Boolean,15
theme: Theme,16
opacity: Double = 1.017
): View =18
element(19
ConversationShimmerProps(20
active && theme.tokens.motion.preference == MotionPreference.Full,21
opacity22
),23
{ current, scope -> ConversationShimmer(current, scope) },24
[content]25
)27
private class ConversationShimmer(28
private val props: Props<ConversationShimmerProps>,29
scope: ComponentScope30
): Element<Unit>(scope) {31
private val motion = LoadingMotion(scope)32
private var visible = false33
public override fun update(): UiInvalidation {34
motion.update(props.value.active && props.value.opacity > 0.0, Duration.milliseconds(1600L))35
return UiInvalidation.Scene36
}37
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<38
Unit39
> {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.value46
val size = context.size47
val bounds = Rect(0.0, 0.0, size.width, size.height)48
visible = false49
context.draw { paint -> visible = loadingVisible(paint.canvas, bounds, input.opacity) }50
val animated = input.active && visible51
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.width56
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
}75
}