Scoop API

scoop:scoop.ui.component:0.1.0

src/actions/spinner.scoop

Selected production source · src/actions/spinner.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.svg.*
5import scoop.icons.lucide.loaderCircle
6import scoop.math.PI
7import scoop.math.min
8import scoop.surface.*
9import scoop.time.Duration
10import scoop.ui.*
11import scoop.window.accessibility.*
12import scoop.ui.style.*
14public class SpinnerProps public constructor(
15 public val size: Double? = None,
16 public val color: Color? = None,
17 public val running: Boolean = true,
18 public val period: Duration = Duration.seconds(1L),
19 public val theme: ThemeSource? = None,
20 public val semantics: ComponentSemantics? = None
21) {
22 init {
23 when (size) {
24 case Some(val value) -> nonnegative(value, "invalid spinner size")
25 case None -> {}
26 }
27 requireComponent(period > Duration.ZERO, "spinner period must be positive")
28 }
31public class Spinner public constructor(
32 private val props: Props<SpinnerProps>,
33 scope: ComponentScope
34): Component(scope) {
35 private val themes = ThemeAccess(scope)
36 public override fun onMount() {
37 themes.mount(props.value.theme)
38 }
39 public override fun render(): View {
40 val input = props.value
41 val theme = themes.read(input.theme)
42 val size = input.size ?: theme.tokens.typography.action.fontSize * 16.0 / 14.0
43 val motion = theme.tokens.motion.resolve(Some(AnimationSpec.Tween(input.period)))
44 val content =
45 element(
46 SpinnerInkProps(
47 size,
48 input.color ?: theme.tokens.typography.action.color,
49 input.running && size > 0.0,
50 input.period,
51 motion
52 ),
53 { current, mount -> SpinnerInk(current, scope = mount) }
54 )
55 return when (input.semantics) {
56 case Some(val options) -> semanticContent(
57 content,
58 SemanticRole.ProgressBar,
59 options.properties(readOnly = true).copy { busy = input.running || options.busy },
60 relations = options.relations,
61 reference = options.reference
62 )
63 case None -> content
64 }
65 }
68public fun spinner(configure: SpinnerBuilder.() -> Unit = {}): View = makeSpinner(configure)
70private class SpinnerInkProps(
71 val size: Double,
72 val color: Color,
73 val running: Boolean,
74 val period: Duration,
75 val motion: AnimationSpec?
78private class SpinnerInk(private val props: Props<SpinnerInkProps>, scope: ComponentScope): Element<
79 Unit
80>(scope) {
81 private var previousSize: Double? = None
82 private var motion: LoadingMotion? = None
83 private var outline: SvgDocument? = None
84 private var visible = false
85 public override fun onMount() {
86 motion = Some(LoadingMotion(scope))
87 outline = Some(scope.resource { SvgDocument.parse(loaderCircle.source) })
88 }
89 public override fun update(): UiInvalidation {
90 val value = props.value
91 motion!!.update(
92 value.running && value.color.alpha > 0.0 && panelPresent(value.motion),
93 value.period
94 )
95 val changed = previousSize != Some(value.size)
96 previousSize = Some(value.size)
97 return if (changed)
98 UiInvalidation.Layout
99 else
100 UiInvalidation.Scene
101 }
102 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
103 Unit
104 > =
105 LayoutResult(
106 LayoutMetrics(constraints.constrain(Size(props.value.size, props.value.size))),
107 Unit
108 )
109 public override fun scene(context: SceneContext, measured: Unit) {
110 val size = context.size
111 val side = min(size.width, size.height)
112 val angle = motion!!.phase.value * PI * 2.0
113 val value = props.value
114 val bounds = Rect((size.width - side) / 2.0, (size.height - side) / 2.0, side, side)
115 context.draw {
116 paint ->
117 visible = loadingVisible(paint.canvas, bounds, value.color.alpha)
118 if (visible)
119 paint.canvas.withState {
120 canvas ->
121 canvas.translate(size.width / 2.0, size.height / 2.0)
122 canvas.rotate(angle)
123 canvas.scale(side / 24.0, side / 24.0)
124 canvas.translate(-12.0, -12.0)
125 canvas.drawSvg(
126 outline!!,
127 Rect(0.0, 0.0, 24.0, 24.0),
128 SvgDrawOptions(currentColor = Some(value.color))
129 )
130 }
131 }
132 loadingParticipation(context, motion!!.phase, visible)
133 }