scoop:scoop.ui.component:0.1.0
src/content/skeleton.scoop
Selected production source · src/content/skeleton.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.math.min5
import scoop.surface.*6
import scoop.time.Duration7
import scoop.ui.*8
import scoop.ui.style.*10
public enum SkeletonShape {11
Rectangle, Circle12
}14
public class SkeletonProps public constructor(15
public val content: View,16
public val loading: Boolean = true,17
public val animated: Boolean = true,18
public val shape: SkeletonShape = SkeletonShape.Rectangle,19
public val width: Double? = None,20
public val height: Double? = None,21
public val period: Duration = Duration.seconds(2L),22
public val color: Color? = None,23
public val theme: ThemeSource? = None24
) {25
init {26
when (width) {27
case Some(val value) -> nonnegative(value, "invalid skeleton width");28
case None -> {}29
}30
when (height) {31
case Some(val value) -> nonnegative(value, "invalid skeleton height");32
case None -> {}33
}34
requireComponent(period > Duration.ZERO, "skeleton period must be positive")35
}36
}38
public class Skeleton public constructor(39
private val props: Props<SkeletonProps>,40
scope: ComponentScope41
): Component(scope) {42
private val themes = ThemeAccess(scope)43
public override fun onMount() {44
themes.mount(props.value.theme)45
}46
public override fun render(): View {47
val input = props.value48
val theme = themes.read(input.theme)49
val colors = theme.tokens.palette50
return element(51
SkeletonInkProps(52
input,53
input.color ?:54
mixThemeColor(colors.muted.background, colors.background.foreground, 0.12),55
theme.tokens.corners.small,56
theme.tokens.motion.preference == MotionPreference.Full57
),58
{ current, mount -> SkeletonInk(current, scope = mount) },59
[input.content]60
)61
}62
}64
public class SkeletonBuilder internal constructor() {65
public var loading: Boolean = true66
public var animated: Boolean = true67
public var shape: SkeletonShape = SkeletonShape.Rectangle68
public var width: Double? = None69
public var height: Double? = None70
public var period: Duration = Duration.seconds(2L)71
public var color: Color? = None72
public var theme: ThemeSource? = None73
public var key: ViewKey = ViewKey.Position74
}76
public fun skeleton(configure: SkeletonBuilder.() -> Unit = {}): View =77
makeSkeleton(empty(), Some(16.0), configure)79
public fun skeleton(content: View, configure: SkeletonBuilder.() -> Unit = {}): View =80
makeSkeleton(content, None, configure)82
internal fun makeSkeleton(83
content: View,84
height: Double?,85
configure: SkeletonBuilder.() -> Unit86
): View {87
val builder = SkeletonBuilder()88
builder.height = height89
builder.configure()90
return component(91
SkeletonProps(92
content,93
builder.loading,94
builder.animated,95
builder.shape,96
builder.width,97
builder.height,98
builder.period,99
builder.color,100
builder.theme101
),102
{ current, mount -> Skeleton(current, scope = mount) },103
builder.key104
)105
}107
private class SkeletonInkProps(108
val input: SkeletonProps,109
val color: Color,110
val radius: Double,111
val motion: Boolean112
)114
private class SkeletonInk(115
private val props: Props<SkeletonInkProps>,116
scope: ComponentScope117
): Element<Unit>(scope) {118
private var previous: SkeletonProps? = None119
private var motion: LoadingMotion? = None120
private var visible = false121
private val easing = Easing.cubicBezier(0.4, 0.0, 0.6, 1.0)122
public override fun onMount() {123
motion = Some(LoadingMotion(scope))124
}125
public override fun update(): UiInvalidation {126
val value = props.value127
motion!!.update(128
value.input.loading && value.input.animated && value.motion && value.color.alpha > 0.0,129
value.input.period130
)131
val changed =132
when (previous) {133
case Some(val old) -> old.width !=134
value.input.width ||135
old.height !=136
value.input.height ||137
old.loading !=138
value.input.loading139
case None -> true140
}141
previous = Some(value.input)142
return if (changed)143
UiInvalidation.Layout144
else145
UiInvalidation.Scene146
}147
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<148
Unit149
> {150
val input = props.value.input151
val child = context.measureChild(0L, skeletonConstraints(input, constraints))152
if (!input.loading)153
context.placeChild(0L, Point(0.0, 0.0))154
return LayoutResult(child, Unit)155
}156
public override fun scene(context: SceneContext, measured: Unit) {157
val value = props.value158
val size = context.size159
if (value.input.loading) {160
val bounds = skeletonBounds(size, value.input.shape)161
val opacity = skeletonOpacity(motion!!.phase.value, easing)162
context.draw {163
paint ->164
visible = loadingVisible(paint.canvas, bounds, value.color.alpha)165
if (visible)166
paint.canvas.drawRoundRect(167
bounds,168
if (value.input.shape == SkeletonShape.Circle)169
bounds.width / 2.0170
else171
value.radius,172
if (value.input.shape == SkeletonShape.Circle)173
bounds.width / 2.0174
else175
value.radius,176
Paint(value.color.withAlpha(value.color.alpha * opacity))177
)178
}179
} else {180
visible = false181
context.children()182
}183
loadingParticipation(context, motion!!.phase, visible)184
}185
}187
internal fun skeletonConstraints(input: SkeletonProps, limits: Constraints): Constraints {188
val width =189
when (input.width) {190
case Some(val value) -> Some(limits.constrain(Size(value, 0.0)).width)191
case None -> limits.maxWidth192
}193
val height =194
when (input.height) {195
case Some(val value) -> Some(limits.constrain(Size(0.0, value)).height)196
case None -> None197
}198
return Constraints(199
width ?: limits.minWidth,200
width,201
height ?: limits.minHeight,202
when (height) {203
case Some(val value) -> Some(value);204
case None -> limits.maxHeight205
}206
)207
}209
internal fun skeletonBounds(size: Size, shape: SkeletonShape): Rect {210
if (shape == SkeletonShape.Rectangle)211
return Rect(0.0, 0.0, size.width, size.height)212
val side = min(size.width, size.height)213
return Rect((size.width - side) / 2.0, (size.height - side) / 2.0, side, side)214
}216
internal fun skeletonOpacity(phase: Double, easing: Easing): Double =217
1.0 -218
0.5 *219
easing.transform(220
if (phase <= 0.5)221
phase * 2.0222
else223
(1.0 - phase) * 2.0224
)