scoop:scoop.ui.component:0.1.0
src/checkbox.scoop
Selected production source · src/checkbox.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.Animatable5
import scoop.ui.*6
import scoop.ui.style.*7
import scoop.window.accessibility.*9
public enum CheckState {10
Unchecked, Checked, Indeterminate11
}13
internal fun nextCheckState(value: CheckState): CheckState = if (value == CheckState.Checked)14
CheckState.Unchecked15
else16
CheckState.Checked18
internal 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 }21
)23
public 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()32
)34
public class Checkbox public constructor(35
private val props: Props<CheckboxProps>,36
scope: ComponentScope37
) : Component(scope) {38
private val activate: () -> Unit = {39
val binding = props.value.checked40
binding.value = nextCheckState(binding.value)41
}42
private val control = PressControl(43
scope,44
PressOptions(props.value.enabled, false),45
props.value.focus,46
activate47
)48
private val semantics = createComponentSemantics(scope, SemanticRole.Checkbox)49
private val toggle: () -> SemanticActionResult = {50
val input = props.value51
if (!input.enabled)52
SemanticActionResult.Unavailable53
else {54
val binding = input.checked55
val expected = nextCheckState(binding.value)56
activate()57
if (binding.value == expected)58
SemanticActionResult.Completed59
else60
SemanticActionResult.Rejected61
}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.value79
val theme = themes.read(input.theme)80
val checked = input.checked.value81
val interaction = control.press.interaction82
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
Insets86
.all(87
lowerBound(88
theme.tokens.spacing.small,89
theme.checkboxMetrics(options, scope.layoutDirection).focusOutset90
)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
toggle104
),105
{ current, mount -> CheckboxInk(current, scope = mount) }106
)107
}108
}110
public fun checkbox(checked: Binding<Boolean>, configure: CheckboxBuilder.() -> Unit = {}): View =111
makeCheckbox(checkboxStateBinding(checked), configure)113
public fun checkbox(114
checked: Binding<CheckState>,115
configure: CheckboxBuilder.() -> Unit = {}116
): View = makeCheckbox(checked, configure)118
public fun checkbox(119
initialChecked: Boolean = false,120
configure: CheckboxBuilder.() -> Unit = {}121
): View = makeOwnedCheckbox(initialChecked, configure)123
internal fun makeCheckbox(checked: Binding<Boolean>, configure: CheckboxBuilder.() -> Unit): View =124
makeCheckbox(checkboxStateBinding(checked), configure)126
internal fun makeCheckbox(127
checked: Binding<CheckState>,128
configure: CheckboxBuilder.() -> Unit129
): View {130
val builder = CheckboxBuilder()131
builder.configure()132
return component(133
builder.snapshot(checked),134
{ props, mount -> Checkbox(props, scope = mount) },135
builder.key136
)137
}139
internal fun makeOwnedCheckbox(140
initialChecked: Boolean,141
configure: CheckboxBuilder.() -> Unit142
): 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.semantics155
),156
{ props, mount -> OwnedCheckbox(props, scope = mount) },157
builder.key158
)159
}161
private 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: ComponentSemantics170
)172
private class OwnedCheckbox(173
private val props: Props<OwnedCheckboxProps>,174
scope: ComponentScope175
) : 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.enabled179
this.style = this @OwnedCheckbox.props.value.style180
this.theme = this @OwnedCheckbox.props.value.theme181
this.focus = this @OwnedCheckbox.props.value.focus182
this.invalid = this @OwnedCheckbox.props.value.invalid183
this.labelTarget = this @OwnedCheckbox.props.value.labelTarget184
this.semantics = this @OwnedCheckbox.props.value.semantics185
}186
}188
private 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: () -> SemanticActionResult199
)201
private class CheckboxInk(202
private val props: Props<CheckboxInkProps>,203
scope: ComponentScope204
) : Element<Rect>(scope) {205
private var previousMetrics: CheckboxMetrics? = None206
private var previousPadding: Insets? = None207
private var previousTheme: Theme? = None208
private var motion: InteractionMotion? = None209
private var selection: Animatable<Double>? = None210
private var painters: ThemePainterCache? = None211
private var field: FieldLink? = None212
public override fun onMount() {213
val control = props.value.control214
field = Some(215
scope216
.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
scope226
.animation(227
if (props.value.checked != CheckState.Unchecked) 1.0 else 0.0228
)229
)230
}231
public override fun update(): UiInvalidation {232
val value = props.value233
val control = value.control234
control.press.update(PressOptions(value.input.enabled, false), value.activate)235
control.area.scope.enabled = value.input.enabled236
control.focus.options = value.input.focus237
field!!.update(value.input.labelTarget, value.input.enabled, value.activate)238
updateSemantics()239
val specs = value.theme.tokens.motion240
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
else252
None253
)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.Scene265
}266
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<267
Rect268
> {269
val value = props.value270
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.value275
val handle = value.semantics ?: return276
if (handle.isClosed) return277
handle.bindReference(value.input.semantics.reference)278
handle279
.update(280
value281
.input282
.semantics283
.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.Off290
case Checked -> SemanticChecked.On291
case Indeterminate -> SemanticChecked.Mixed292
}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.value301
when (val handle = value.semantics) {302
case Some(val semantic) ->303
context304
.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.Off313
case Checked -> CheckMark.On314
case Indeterminate -> CheckMark.Mixed315
},316
motion!!.sample(value.visual),317
Some(selection!!.value)318
)319
val geometry = value320
.theme321
.checkboxGeometry(bounds, visual, value.options, context.layoutDirection)322
context323
.draw { paint ->324
painters!!.get(props.value.theme).drawCheckbox(paint.canvas, geometry, visual)325
}326
context327
.place(328
props.value.control.area,329
Rect(0.0, 0.0, context.size.width, context.size.height)330
)331
}332
}