scoop:scoop.ui.component:0.1.0
src/choice/radio_item.scoop
Selected production source · src/choice/radio_item.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.ui.*6
import scoop.ui.style.*7
import scoop.window.accessibility.*9
internal class RadioItemProps(10
val label: String,11
val selected: Boolean,12
val enabled: Boolean,13
val invalid: Boolean,14
val focus: FocusOptions,15
val labelTarget: FieldTarget?,16
val marker: RadioOptions,17
val content: ((RadioItemContent) -> View)?,18
val fonts: FontCollection?,19
val theme: Theme,20
val activate: () -> Unit,21
val semantics: ComponentSemantics,22
val select: () -> SemanticActionResult23
)25
internal class RadioItem(26
private val props: Props<RadioItemProps>,27
scope: ComponentScope28
): Component(scope) {29
private val control =30
PressControl(31
scope,32
PressOptions(props.value.enabled, false),33
props.value.focus,34
{ props.value.activate() }35
)36
private val text = ActionTextStyle()37
private val semantics = createComponentSemantics(scope, SemanticRole.RadioButton)38
init {39
when (semantics) {40
case Some(val handle) -> {41
handle.bindFocus(Some(control.focus))42
handle.mergeLabels = true43
scope.routeSemanticsChildren(Some(handle))44
}45
case None -> {}46
}47
}49
public override fun render(): View {50
val input = props.value51
val interaction = control.press.interaction52
val visual = interaction.asVisual(input.enabled, input.invalid)53
val base =54
text.resolve(55
input.theme.tokens.typography.body,56
input.theme.tokens.palette.background.foreground,57
None,58
lineHeight = Some(20.0 / 14.0)59
)60
val indicator =61
element(62
RadioIndicatorProps(input.theme, input.marker, RadioVisual(input.selected, visual)),63
{ current, mount -> RadioIndicator(current, scope = mount) }64
)65
val content =66
when (input.content) {67
case Some(val render) -> render(68
RadioItemContent(indicator, input.selected, input.enabled, input.invalid, base)69
)70
case None -> {71
val label =72
component(73
LabelProps(74
LabelText.Plain(input.label, None),75
input.fonts,76
baseStyle = Some(base)77
),78
{ current, mount -> Label(current, scope = mount) }79
)80
val layout =81
LinearProps(82
[FlexItem(indicator, FlexItemOptions(shrink = 0.0)), FlexItem(label)],83
spacing = 8.0,84
alignment = CrossAlignment.Center85
)86
component(layout, { current, mount -> Row(current, scope = mount) })87
}88
}89
return element(90
RadioInkProps(input, control, semantics),91
{ current, mount -> RadioInk(current, scope = mount) },92
[content]93
)94
}95
}97
private class RadioInkProps(98
val input: RadioItemProps,99
val control: PressControl,100
val semantics: SemanticsHandle?101
)103
private class RadioInk(private val props: Props<RadioInkProps>, scope: ComponentScope): Element<104
Unit105
>(scope) {106
private var field: FieldLink? = None107
public override fun onMount() {108
val control = props.value.control109
field =110
Some(111
scope.resource {112
FieldLink(scope, control.area, control.focus, props.value.semantics) {113
updateSemantics()114
}115
}116
)117
}118
public override fun update(): UiInvalidation {119
val input = props.value.input120
val control = props.value.control121
control.press.update(PressOptions(input.enabled, false), input.activate)122
control.area.scope.enabled = input.enabled123
control.focus.options = input.focus124
field!!.update(input.labelTarget, input.enabled, input.activate)125
updateSemantics()126
return UiInvalidation.Layout127
}128
private fun updateSemantics() {129
val value = props.value130
val handle = value.semantics ?: return131
if (handle.isClosed)132
return133
val input = value.input134
handle.bindReference(input.semantics.reference)135
handle.update(136
input.semantics.properties(137
enabled = input.enabled,138
invalid = input.invalid,139
required = field!!.isRequired(handle),140
checked =141
Some(142
if (input.selected)143
SemanticChecked.On144
else145
SemanticChecked.Off146
)147
),148
SemanticActions(invoke = Some(input.select)),149
field!!.relations(handle, input.semantics.relations)150
)151
}152
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<153
Unit154
> {155
val child = context.measureChild(0L, constraints)156
val width = constraints.maxWidth ?: child.size.width157
val size = constraints.constrain(Size(width, child.size.height))158
context.placeChild(159
0L,160
Point(logicalX(0.0, child.size.width, size.width, context.layoutDirection), 0.0)161
)162
return LayoutResult(163
LayoutMetrics(size, child.alphabeticBaseline, child.ideographicBaseline),164
Unit165
)166
}167
public override fun scene(context: SceneContext, measured: Unit) {168
val input = props.value.input169
when (val handle = props.value.semantics) {170
case Some(val value) -> context.semantics(171
value,172
Rect(0.0, 0.0, context.size.width, context.size.height)173
)174
case None -> {}175
}176
if (!input.enabled)177
context.saveLayer(input.theme.tokens.disabledOpacity)178
context.children()179
if (!input.enabled)180
context.restore()181
context.place(182
props.value.control.area,183
Rect(0.0, 0.0, context.size.width, context.size.height)184
)185
}186
}