Scoop API

scoop:scoop.ui.component:0.1.0

src/forms/switch_ink.scoop

Selected production source · src/forms/switch_ink.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.window.accessibility.*
9internal class SwitchInkProps(
10 val input: SwitchProps,
11 val theme: Theme,
12 val checked: Boolean,
13 val control: PressControl,
14 val visual: InteractionVisual,
15 val activate: () -> Unit,
16 val semantics: SemanticsHandle?,
17 val toggle: () -> SemanticActionResult
20internal class SwitchDrawing(
21 val surface: SurfaceGeometry,
22 val visual: SurfaceVisual,
23 val thumb: Rect,
24 val thumbStyle: Style,
25 val hit: Rect
28internal class SwitchInk(private val props: Props<SwitchInkProps>, scope: ComponentScope): Element<
29 Unit
30>(scope) {
31 private var previousMetrics: SurfaceMetrics? = None
32 private var previousTheme: Theme? = None
33 private var motion: InteractionMotion? = None
34 private var selection: Animatable<Double>? = None
35 private var painters: ThemePainterCache? = None
36 private var thumb: StylePainter? = None
37 private var content: StylePainter? = None
38 private var field: FieldLink? = None
39 public override fun onMount() {
40 val control = props.value.control
41 field =
42 Some(
43 scope.resource {
44 FieldLink(scope, control.area, control.focus, props.value.semantics) {
45 updateSemantics()
46 }
47 }
48 )
49 painters = Some(scope.resource { ThemePainterCache() })
50 thumb = Some(scope.resource { StylePainter() })
51 content = Some(scope.resource { StylePainter() })
52 motion = Some(InteractionMotion(scope, props.value.visual))
53 selection =
54 Some(
55 scope.animation(
56 if (props.value.checked)
57 1.0
58 else
59 0.0
60 )
61 )
62 }
63 public override fun update(): UiInvalidation {
64 val value = props.value
65 val control = value.control
66 control.press.update(PressOptions(value.input.enabled, false), value.activate)
67 control.area.scope.enabled = value.input.enabled
68 control.focus.options = value.input.focus
69 field!!.update(value.input.labelTarget, value.input.enabled, value.activate)
70 updateSemantics()
71 val specs = value.theme.tokens.motion
72 motion!!.update(
73 value.visual,
74 specs.resolve(value.input.style.transition.resolve(specs.hover)),
75 specs.resolve(value.input.style.transition.resolve(specs.press))
76 )
77 changeProgress(
78 selection!!,
79 if (value.checked)
80 1.0
81 else
82 0.0,
83 if (value.input.enabled)
84 specs.resolve(value.input.style.selectionTransition.resolve(specs.selection))
85 else
86 None
87 )
88 painters!!.get(value.theme)
89 val visual = SurfaceVisual(SurfaceKind.Plain, value.visual)
90 val metrics =
91 value.theme.surfaceMetrics(
92 visual,
93 switchOptions(
94 value.input,
95 value.theme,
96 value.visual,
97 if (value.checked)
98 1.0
99 else
100 0.0
101 ),
102 scope.layoutDirection
103 )
104 val changed = previousMetrics != Some(metrics) || !sameTheme(previousTheme, value.theme)
105 previousMetrics = Some(metrics)
106 previousTheme = Some(value.theme)
107 return if (changed)
108 UiInvalidation.Layout
109 else
110 UiInvalidation.Scene
111 }
112 private fun updateSemantics() {
113 val value = props.value
114 val handle = value.semantics ?: return
115 if (handle.isClosed)
116 return
117 handle.bindReference(value.input.semantics.reference)
118 handle.update(
119 value.input.semantics.properties(
120 enabled = value.input.enabled,
121 invalid = value.input.invalid,
122 required = field!!.isRequired(handle),
123 checked =
124 Some(
125 if (value.checked)
126 SemanticChecked.On
127 else
128 SemanticChecked.Off
129 )
130 ),
131 SemanticActions(toggle = Some(value.toggle)),
132 field!!.relations(handle, value.input.semantics.relations)
133 )
134 }
135 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
136 Unit
137 > {
138 val value = props.value
139 val visual = SurfaceVisual(SurfaceKind.Plain, value.visual)
140 val progress =
141 if (value.checked)
142 1.0
143 else
144 0.0
145 val options = switchOptions(value.input, value.theme, visual.interaction, progress)
146 val metrics = value.theme.surfaceMetrics(visual, options, context.layoutDirection)
147 val size =
148 constraints.constrain(
149 Size(
150 lowerBound(metrics.suggestedSize.width, metrics.minimumSize.width),
151 lowerBound(metrics.suggestedSize.height, metrics.minimumSize.height)
152 )
153 )
154 return LayoutResult(LayoutMetrics(size), Unit)
155 }
156 public override fun scene(context: SceneContext, measured: Unit) {
157 val input = props.value
158 val density = input.theme.tokens.typography.body.fontSize / 14.0
159 val visual = SurfaceVisual(SurfaceKind.Plain, motion!!.sample(input.visual))
160 val progress = selection!!.value
161 val options = switchOptions(input.input, input.theme, visual.interaction, progress)
162 val size = context.size
163 val bounds = Rect(0.0, 0.0, size.width, size.height)
164 when (val handle = input.semantics) {
165 case Some(val value) -> context.semantics(value, bounds)
166 case None -> {}
167 }
168 val geometry = input.theme.surfaceGeometry(bounds, visual, options, context.layoutDirection)
169 val thumbSide =
170 (
171 if (input.input.size == SwitchSize.Sm)
172 12.0
173 else
174 16.0
175 ) *
176 density
177 val drawing =
178 SwitchDrawing(
179 geometry,
180 visual,
181 switchThumbBounds(geometry.content, thumbSide, progress, context.layoutDirection),
182 switchThumbStyle(input.input, input.theme, progress),
183 switchHitBounds(
184 bounds,
185 input.input.style.hitInsets ?:
186 Insets(12.0 * density, 8.0 * density, 12.0 * density, 8.0 * density)
187 )
188 )
190 val value = drawing
191 val painter = painters!!.get(props.value.theme)
192 if (value.surface.opacity < 1.0)
193 context.saveLayer(value.surface.opacity)
194 context.draw {
195 paint -> painter.drawSurface(
196 paint.canvas,
197 value.surface,
198 value.visual,
199 SurfaceLayer.Background
200 )
201 }
202 content!!.scene(context, value.surface.bounds, value.surface.contentStyle) {
203 children -> thumb!!.scene(children, value.thumb, value.thumbStyle) { _ -> }
204 }
205 context.draw {
206 paint -> painter.drawSurface(
207 paint.canvas,
208 value.surface,
209 value.visual,
210 SurfaceLayer.Foreground
211 )
212 }
213 if (value.surface.opacity < 1.0)
214 context.restore()
215 context.place(props.value.control.area, value.hit)
216 }