Scoop API

scoop:scoop.ui.component:0.1.0

src/checkbox.scoop

Selected production source · src/checkbox.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.Animatable
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.window.accessibility.*
9public enum CheckState {
10 Unchecked, Checked, Indeterminate
13internal fun nextCheckState(value: CheckState): CheckState = if (value == CheckState.Checked)
14 CheckState.Unchecked
15else
16 CheckState.Checked
18internal fun checkboxStateBinding(value: Binding<Boolean>): Binding<CheckState> = binding(
19 get = { if (value.value) CheckState.Checked else CheckState.Unchecked },
20 set = { next -> value.value = next == CheckState.Checked }
23public class CheckboxProps public constructor(
24 public val checked: Binding<CheckState>,
25 public val enabled: Boolean = true,
26 public val style: CheckboxStyle = checkboxStyle(),
27 public val theme: ThemeSource? = None,
28 public val focus: FocusOptions = FocusOptions(),
29 public val invalid: Boolean = false,
30 public val labelTarget: FieldTarget? = None,
31 public val semantics: ComponentSemantics = ComponentSemantics()
34public class Checkbox public constructor(
35 private val props: Props<CheckboxProps>,
36 scope: ComponentScope
37) : Component(scope) {
38 private val activate: () -> Unit = {
39 val binding = props.value.checked
40 binding.value = nextCheckState(binding.value)
41 }
42 private val control = PressControl(
43 scope,
44 PressOptions(props.value.enabled, false),
45 props.value.focus,
46 activate
47 )
48 private val semantics = createComponentSemantics(scope, SemanticRole.Checkbox)
49 private val toggle: () -> SemanticActionResult = {
50 val input = props.value
51 if (!input.enabled)
52 SemanticActionResult.Unavailable
53 else {
54 val binding = input.checked
55 val expected = nextCheckState(binding.value)
56 activate()
57 if (binding.value == expected)
58 SemanticActionResult.Completed
59 else
60 SemanticActionResult.Rejected
61 }
62 }
63 private val themes = ThemeAccess(scope)
64 init {
65 when (semantics) {
66 case Some(val handle) -> {
67 handle.bindFocus(Some(control.focus))
68 scope.routeSemanticsChildren(Some(handle))
69 }
71 case None -> {}
72 }
73 }
74 public override fun onMount() {
75 themes.mount(props.value.theme)
76 }
77 public override fun render(): View {
78 val input = props.value
79 val theme = themes.read(input.theme)
80 val checked = input.checked.value
81 val interaction = control.press.interaction
82 val visual = interaction.asVisual(input.enabled, input.invalid)
83 val options = input.style.options(theme, visual, checked != CheckState.Unchecked)
84 val padding = input.style.padding ?:
85 Insets
86 .all(
87 lowerBound(
88 theme.tokens.spacing.small,
89 theme.checkboxMetrics(options, scope.layoutDirection).focusOutset
90 )
91 )
92 return element(
93 CheckboxInkProps(
94 input,
95 theme,
96 checked,
97 control,
98 visual,
99 options,
100 padding,
101 activate,
102 semantics,
103 toggle
104 ),
105 { current, mount -> CheckboxInk(current, scope = mount) }
106 )
107 }
110public fun checkbox(checked: Binding<Boolean>, configure: CheckboxBuilder.() -> Unit = {}): View =
111 makeCheckbox(checkboxStateBinding(checked), configure)
113public fun checkbox(
114 checked: Binding<CheckState>,
115 configure: CheckboxBuilder.() -> Unit = {}
116): View = makeCheckbox(checked, configure)
118public fun checkbox(
119 initialChecked: Boolean = false,
120 configure: CheckboxBuilder.() -> Unit = {}
121): View = makeOwnedCheckbox(initialChecked, configure)
123internal fun makeCheckbox(checked: Binding<Boolean>, configure: CheckboxBuilder.() -> Unit): View =
124 makeCheckbox(checkboxStateBinding(checked), configure)
126internal fun makeCheckbox(
127 checked: Binding<CheckState>,
128 configure: CheckboxBuilder.() -> Unit
129): View {
130 val builder = CheckboxBuilder()
131 builder.configure()
132 return component(
133 builder.snapshot(checked),
134 { props, mount -> Checkbox(props, scope = mount) },
135 builder.key
136 )
139internal fun makeOwnedCheckbox(
140 initialChecked: Boolean,
141 configure: CheckboxBuilder.() -> Unit
142): View {
143 val builder = CheckboxBuilder()
144 builder.configure()
145 return component(
146 OwnedCheckboxProps(
147 initialChecked,
148 builder.enabled,
149 builder.style,
150 builder.theme,
151 builder.focus,
152 builder.invalid,
153 builder.labelTarget,
154 builder.semantics
155 ),
156 { props, mount -> OwnedCheckbox(props, scope = mount) },
157 builder.key
158 )
161private class OwnedCheckboxProps(
162 val initial: Boolean,
163 val enabled: Boolean,
164 val style: CheckboxStyle,
165 val theme: ThemeSource?,
166 val focus: FocusOptions,
167 val invalid: Boolean,
168 val labelTarget: FieldTarget?,
169 val semantics: ComponentSemantics
172private class OwnedCheckbox(
173 private val props: Props<OwnedCheckboxProps>,
174 scope: ComponentScope
175) : Component(scope) {
176 private val checked = state(props.value.initial)
177 public override fun render(): View = checkbox(checked.binding()) {
178 this.enabled = this @OwnedCheckbox.props.value.enabled
179 this.style = this @OwnedCheckbox.props.value.style
180 this.theme = this @OwnedCheckbox.props.value.theme
181 this.focus = this @OwnedCheckbox.props.value.focus
182 this.invalid = this @OwnedCheckbox.props.value.invalid
183 this.labelTarget = this @OwnedCheckbox.props.value.labelTarget
184 this.semantics = this @OwnedCheckbox.props.value.semantics
185 }
188private class CheckboxInkProps(
189 val input: CheckboxProps,
190 val theme: Theme,
191 val checked: CheckState,
192 val control: PressControl,
193 val visual: InteractionVisual,
194 val options: CheckboxOptions,
195 val padding: Insets,
196 val activate: () -> Unit,
197 val semantics: SemanticsHandle?,
198 val toggle: () -> SemanticActionResult
201private class CheckboxInk(
202 private val props: Props<CheckboxInkProps>,
203 scope: ComponentScope
204) : Element<Rect>(scope) {
205 private var previousMetrics: CheckboxMetrics? = None
206 private var previousPadding: Insets? = None
207 private var previousTheme: Theme? = None
208 private var motion: InteractionMotion? = None
209 private var selection: Animatable<Double>? = None
210 private var painters: ThemePainterCache? = None
211 private var field: FieldLink? = None
212 public override fun onMount() {
213 val control = props.value.control
214 field = Some(
215 scope
216 .resource {
217 FieldLink(scope, control.area, control.focus, props.value.semantics) {
218 updateSemantics()
219 }
220 }
221 )
222 painters = Some(scope.resource { ThemePainterCache() })
223 motion = Some(InteractionMotion(scope, props.value.visual))
224 selection = Some(
225 scope
226 .animation(
227 if (props.value.checked != CheckState.Unchecked) 1.0 else 0.0
228 )
229 )
230 }
231 public override fun update(): UiInvalidation {
232 val value = props.value
233 val control = value.control
234 control.press.update(PressOptions(value.input.enabled, false), value.activate)
235 control.area.scope.enabled = value.input.enabled
236 control.focus.options = value.input.focus
237 field!!.update(value.input.labelTarget, value.input.enabled, value.activate)
238 updateSemantics()
239 val specs = value.theme.tokens.motion
240 motion!!
241 .update(
242 value.visual,
243 specs.resolve(value.input.style.transition.resolve(specs.hover)),
244 specs.resolve(value.input.style.transition.resolve(specs.press))
245 )
246 changeProgress(
247 selection!!,
248 if (value.checked != CheckState.Unchecked) 1.0 else 0.0,
249 if (value.input.enabled)
250 specs.resolve(value.input.style.selectionTransition.resolve(specs.selection))
251 else
252 None
253 )
254 painters!!.get(value.theme)
255 val metrics = value.theme.checkboxMetrics(value.options, scope.layoutDirection)
256 val changed = previousMetrics !=
257 Some(metrics) ||
258 previousPadding !=
259 Some(value.padding) ||
260 !sameTheme(previousTheme, value.theme)
261 previousMetrics = Some(metrics)
262 previousPadding = Some(value.padding)
263 previousTheme = Some(value.theme)
264 return if (changed) UiInvalidation.Layout else UiInvalidation.Scene
265 }
266 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
267 Rect
268 > {
269 val value = props.value
270 val metrics = value.theme.checkboxMetrics(value.options, context.layoutDirection)
271 return markerLayout(metrics.suggestedSize, metrics.minimumSize, value.padding, constraints)
272 }
273 private fun updateSemantics() {
274 val value = props.value
275 val handle = value.semantics ?: return
276 if (handle.isClosed) return
277 handle.bindReference(value.input.semantics.reference)
278 handle
279 .update(
280 value
281 .input
282 .semantics
283 .properties(
284 enabled = value.input.enabled,
285 invalid = value.input.invalid,
286 required = field!!.isRequired(handle),
287 checked = Some(
288 when (value.checked) {
289 case Unchecked -> SemanticChecked.Off
290 case Checked -> SemanticChecked.On
291 case Indeterminate -> SemanticChecked.Mixed
292 }
293 )
294 ),
295 SemanticActions(toggle = Some(value.toggle)),
296 field!!.relations(handle, value.input.semantics.relations)
297 )
298 }
299 public override fun scene(context: SceneContext, bounds: Rect) {
300 val value = props.value
301 when (val handle = value.semantics) {
302 case Some(val semantic) ->
303 context
304 .semantics(
305 semantic,
306 Rect(0.0, 0.0, context.size.width, context.size.height)
307 )
308 case None -> {}
309 }
310 val visual = CheckboxVisual(
311 when (value.checked) {
312 case Unchecked -> CheckMark.Off
313 case Checked -> CheckMark.On
314 case Indeterminate -> CheckMark.Mixed
315 },
316 motion!!.sample(value.visual),
317 Some(selection!!.value)
318 )
319 val geometry = value
320 .theme
321 .checkboxGeometry(bounds, visual, value.options, context.layoutDirection)
322 context
323 .draw { paint ->
324 painters!!.get(props.value.theme).drawCheckbox(paint.canvas, geometry, visual)
325 }
326 context
327 .place(
328 props.value.control.area,
329 Rect(0.0, 0.0, context.size.width, context.size.height)
330 )
331 }