Scoop API

scoop:scoop.ui.component:0.1.0

src/content/skeleton.scoop

Selected production source · src/content/skeleton.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.math.min
5import scoop.surface.*
6import scoop.time.Duration
7import scoop.ui.*
8import scoop.ui.style.*
10public enum SkeletonShape {
11 Rectangle, Circle
14public 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? = None
24) {
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 }
38public class Skeleton public constructor(
39 private val props: Props<SkeletonProps>,
40 scope: ComponentScope
41): 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.value
48 val theme = themes.read(input.theme)
49 val colors = theme.tokens.palette
50 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.Full
57 ),
58 { current, mount -> SkeletonInk(current, scope = mount) },
59 [input.content]
60 )
61 }
64public class SkeletonBuilder internal constructor() {
65 public var loading: Boolean = true
66 public var animated: Boolean = true
67 public var shape: SkeletonShape = SkeletonShape.Rectangle
68 public var width: Double? = None
69 public var height: Double? = None
70 public var period: Duration = Duration.seconds(2L)
71 public var color: Color? = None
72 public var theme: ThemeSource? = None
73 public var key: ViewKey = ViewKey.Position
76public fun skeleton(configure: SkeletonBuilder.() -> Unit = {}): View =
77 makeSkeleton(empty(), Some(16.0), configure)
79public fun skeleton(content: View, configure: SkeletonBuilder.() -> Unit = {}): View =
80 makeSkeleton(content, None, configure)
82internal fun makeSkeleton(
83 content: View,
84 height: Double?,
85 configure: SkeletonBuilder.() -> Unit
86): View {
87 val builder = SkeletonBuilder()
88 builder.height = height
89 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.theme
101 ),
102 { current, mount -> Skeleton(current, scope = mount) },
103 builder.key
104 )
107private class SkeletonInkProps(
108 val input: SkeletonProps,
109 val color: Color,
110 val radius: Double,
111 val motion: Boolean
114private class SkeletonInk(
115 private val props: Props<SkeletonInkProps>,
116 scope: ComponentScope
117): Element<Unit>(scope) {
118 private var previous: SkeletonProps? = None
119 private var motion: LoadingMotion? = None
120 private var visible = false
121 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.value
127 motion!!.update(
128 value.input.loading && value.input.animated && value.motion && value.color.alpha > 0.0,
129 value.input.period
130 )
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.loading
139 case None -> true
140 }
141 previous = Some(value.input)
142 return if (changed)
143 UiInvalidation.Layout
144 else
145 UiInvalidation.Scene
146 }
147 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
148 Unit
149 > {
150 val input = props.value.input
151 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.value
158 val size = context.size
159 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.0
170 else
171 value.radius,
172 if (value.input.shape == SkeletonShape.Circle)
173 bounds.width / 2.0
174 else
175 value.radius,
176 Paint(value.color.withAlpha(value.color.alpha * opacity))
177 )
178 }
179 } else {
180 visible = false
181 context.children()
182 }
183 loadingParticipation(context, motion!!.phase, visible)
184 }
187internal 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.maxWidth
192 }
193 val height =
194 when (input.height) {
195 case Some(val value) -> Some(limits.constrain(Size(0.0, value)).height)
196 case None -> None
197 }
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.maxHeight
205 }
206 )
209internal 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)
216internal fun skeletonOpacity(phase: Double, easing: Easing): Double =
217 1.0 -
218 0.5 *
219 easing.transform(
220 if (phase <= 0.5)
221 phase * 2.0
222 else
223 (1.0 - phase) * 2.0
224 )