Scoop API

scoop:scoop.ui.component:0.1.0

src/choice/radio_indicator.scoop

Selected production source · src/choice/radio_indicator.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.Animatable
5import scoop.ui.*
6import scoop.ui.style.*
8internal class RadioIndicatorProps(
9 val theme: Theme,
10 val options: RadioOptions,
11 val visual: RadioVisual
14internal class RadioIndicator(
15 private val props: Props<RadioIndicatorProps>,
16 scope: ComponentScope
17) : Element<Rect>(scope) {
18 private var previousMetrics: RadioMetrics? = None
19 private var previousTheme: Theme? = None
20 private var motion: InteractionMotion? = None
21 private var selection: Animatable<Double>? = None
22 private var painters: ThemePainterCache? = None
23 public override fun onMount() {
24 painters = Some(scope.resource { ThemePainterCache() })
25 motion = Some(InteractionMotion(scope, props.value.visual.interaction))
26 selection = Some(
27 scope
28 .animation(
29 if (props.value.visual.selected) 1.0 else 0.0
30 )
31 )
32 }
33 public override fun update(): UiInvalidation {
34 val input = props.value
35 val specs = input.theme.tokens.motion
36 motion!!
37 .update(
38 input.visual.interaction,
39 specs.resolve(specs.hover),
40 specs.resolve(specs.press)
41 )
42 changeProgress(
43 selection!!,
44 if (input.visual.selected) 1.0 else 0.0,
45 if (input.visual.interaction.enabled) specs.resolve(specs.selection) else None
46 )
47 painters!!.get(input.theme)
48 val metrics = input.theme.radioMetrics(input.options)
49 val changed = previousMetrics != Some(metrics) || !sameTheme(previousTheme, input.theme)
50 previousMetrics = Some(metrics)
51 previousTheme = Some(input.theme)
52 return if (changed) UiInvalidation.Layout else UiInvalidation.Scene
53 }
54 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
55 Rect
56 > {
57 val input = props.value
58 val metrics = input.theme.radioMetrics(input.options)
59 return markerLayout(
60 metrics.suggestedSize, metrics.minimumSize,
61 Insets.all(metrics.focusOutset), constraints
62 )
63 }
64 public override fun scene(context: SceneContext, bounds: Rect) {
65 val input = props.value
66 val visual = RadioVisual(
67 input.visual.selected,
68 motion!!.sample(input.visual.interaction),
69 Some(selection!!.value)
70 )
71 val resolved = input.theme.radioGeometry(bounds, visual, input.options)
72 // The item applies disabled opacity once to its indicator and arbitrary content together.
73 val geometry = RadioGeometry(
74 resolved.bounds,
75 resolved.outer,
76 resolved.interior,
77 resolved.dot,
78 resolved.ring,
79 resolved.style,
80 resolved.dotColor,
81 resolved.selection,
82 1.0,
83 resolved.stateLayer
84 )
86 context
87 .draw { paint ->
88 painters!!.get(props.value.theme).drawRadio(paint.canvas, geometry, visual)
89 }
90 }