Scoop API

scoop:scoop.ui.component:0.1.0

src/choice/radio_item.scoop

Selected production source · src/choice/radio_item.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.window.accessibility.*
9internal 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: () -> SemanticActionResult
25internal class RadioItem(
26 private val props: Props<RadioItemProps>,
27 scope: ComponentScope
28): 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 = true
43 scope.routeSemanticsChildren(Some(handle))
44 }
45 case None -> {}
46 }
47 }
49 public override fun render(): View {
50 val input = props.value
51 val interaction = control.press.interaction
52 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.Center
85 )
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 }
97private class RadioInkProps(
98 val input: RadioItemProps,
99 val control: PressControl,
100 val semantics: SemanticsHandle?
103private class RadioInk(private val props: Props<RadioInkProps>, scope: ComponentScope): Element<
104 Unit
105>(scope) {
106 private var field: FieldLink? = None
107 public override fun onMount() {
108 val control = props.value.control
109 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.input
120 val control = props.value.control
121 control.press.update(PressOptions(input.enabled, false), input.activate)
122 control.area.scope.enabled = input.enabled
123 control.focus.options = input.focus
124 field!!.update(input.labelTarget, input.enabled, input.activate)
125 updateSemantics()
126 return UiInvalidation.Layout
127 }
128 private fun updateSemantics() {
129 val value = props.value
130 val handle = value.semantics ?: return
131 if (handle.isClosed)
132 return
133 val input = value.input
134 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.On
144 else
145 SemanticChecked.Off
146 )
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 Unit
154 > {
155 val child = context.measureChild(0L, constraints)
156 val width = constraints.maxWidth ?: child.size.width
157 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 Unit
165 )
166 }
167 public override fun scene(context: SceneContext, measured: Unit) {
168 val input = props.value.input
169 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 }