scoop:scoop.ui.component:0.1.0
src/actions/spinner.scoop
Selected production source · src/actions/spinner.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.svg.*5
import scoop.icons.lucide.loaderCircle6
import scoop.math.PI7
import scoop.math.min8
import scoop.surface.*9
import scoop.time.Duration10
import scoop.ui.*11
import scoop.window.accessibility.*12
import scoop.ui.style.*14
public 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? = None21
) {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
}29
}31
public class Spinner public constructor(32
private val props: Props<SpinnerProps>,33
scope: ComponentScope34
): 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.value41
val theme = themes.read(input.theme)42
val size = input.size ?: theme.tokens.typography.action.fontSize * 16.0 / 14.043
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
motion52
),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.reference62
)63
case None -> content64
}65
}66
}68
public fun spinner(configure: SpinnerBuilder.() -> Unit = {}): View = makeSpinner(configure)70
private class SpinnerInkProps(71
val size: Double,72
val color: Color,73
val running: Boolean,74
val period: Duration,75
val motion: AnimationSpec?76
)78
private class SpinnerInk(private val props: Props<SpinnerInkProps>, scope: ComponentScope): Element<79
Unit80
>(scope) {81
private var previousSize: Double? = None82
private var motion: LoadingMotion? = None83
private var outline: SvgDocument? = None84
private var visible = false85
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.value91
motion!!.update(92
value.running && value.color.alpha > 0.0 && panelPresent(value.motion),93
value.period94
)95
val changed = previousSize != Some(value.size)96
previousSize = Some(value.size)97
return if (changed)98
UiInvalidation.Layout99
else100
UiInvalidation.Scene101
}102
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<103
Unit104
> =105
LayoutResult(106
LayoutMetrics(constraints.constrain(Size(props.value.size, props.value.size))),107
Unit108
)109
public override fun scene(context: SceneContext, measured: Unit) {110
val size = context.size111
val side = min(size.width, size.height)112
val angle = motion!!.phase.value * PI * 2.0113
val value = props.value114
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
}134
}