scoop:scoop.ui.component:0.1.0
src/forms/switch_ink.scoop
Selected production source · src/forms/switch_ink.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.*5
import scoop.ui.*6
import scoop.ui.style.*7
import scoop.window.accessibility.*9
internal 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: () -> SemanticActionResult18
)20
internal class SwitchDrawing(21
val surface: SurfaceGeometry,22
val visual: SurfaceVisual,23
val thumb: Rect,24
val thumbStyle: Style,25
val hit: Rect26
)28
internal class SwitchInk(private val props: Props<SwitchInkProps>, scope: ComponentScope): Element<29
Unit30
>(scope) {31
private var previousMetrics: SurfaceMetrics? = None32
private var previousTheme: Theme? = None33
private var motion: InteractionMotion? = None34
private var selection: Animatable<Double>? = None35
private var painters: ThemePainterCache? = None36
private var thumb: StylePainter? = None37
private var content: StylePainter? = None38
private var field: FieldLink? = None39
public override fun onMount() {40
val control = props.value.control41
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.058
else59
0.060
)61
)62
}63
public override fun update(): UiInvalidation {64
val value = props.value65
val control = value.control66
control.press.update(PressOptions(value.input.enabled, false), value.activate)67
control.area.scope.enabled = value.input.enabled68
control.focus.options = value.input.focus69
field!!.update(value.input.labelTarget, value.input.enabled, value.activate)70
updateSemantics()71
val specs = value.theme.tokens.motion72
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.081
else82
0.0,83
if (value.input.enabled)84
specs.resolve(value.input.style.selectionTransition.resolve(specs.selection))85
else86
None87
)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.099
else100
0.0101
),102
scope.layoutDirection103
)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.Layout109
else110
UiInvalidation.Scene111
}112
private fun updateSemantics() {113
val value = props.value114
val handle = value.semantics ?: return115
if (handle.isClosed)116
return117
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.On127
else128
SemanticChecked.Off129
)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
Unit137
> {138
val value = props.value139
val visual = SurfaceVisual(SurfaceKind.Plain, value.visual)140
val progress =141
if (value.checked)142
1.0143
else144
0.0145
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.value158
val density = input.theme.tokens.typography.body.fontSize / 14.0159
val visual = SurfaceVisual(SurfaceKind.Plain, motion!!.sample(input.visual))160
val progress = selection!!.value161
val options = switchOptions(input.input, input.theme, visual.interaction, progress)162
val size = context.size163
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.0173
else174
16.0175
) *176
density177
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 = drawing191
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.Background200
)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.Foreground211
)212
}213
if (value.surface.opacity < 1.0)214
context.restore()215
context.place(props.value.control.area, value.hit)216
}217
}