scoop:scoop.ui.component:0.1.0
src/choice/radio_group.scoop
Selected production source · src/choice/radio_group.scoop
1
package scoop.ui.component3
import scoop.graphics.text.*4
import scoop.ui.*5
import scoop.ui.style.*6
import scoop.window.accessibility.*8
public class RadioItemContent internal constructor(9
public val indicator: View,10
public val selected: Boolean,11
public val enabled: Boolean,12
public val invalid: Boolean,13
public val textStyle: TextStyle14
)16
public class RadioGroupEntry<T: Equals<T>> public constructor(17
public val choice: Choice<T>,18
public val focus: FocusOptions = FocusOptions(),19
public val labelTarget: FieldTarget? = None,20
public val invalid: Boolean = false,21
public val marker: RadioOptions = radioOptions(),22
public val content: ((RadioItemContent) -> View)? = None,23
public val semantics: ComponentSemantics = ComponentSemantics()24
)26
public class RadioGroupProps<T: Equals<T>> public constructor(27
public val entries: Array<RadioGroupEntry<T>>,28
public val selected: Binding<T?>,29
public val axis: FlexAxis = FlexAxis.Vertical,30
public val spacing: Double = 8.0,31
public val loop: Boolean = true,32
public val enabled: Boolean = true,33
public val invalid: Boolean = false,34
public val fonts: FontCollection? = None,35
public val theme: ThemeSource? = None,36
public val style: StyleOverride = styleOverride(),37
public val semantics: ComponentSemantics = ComponentSemantics()38
) {39
init {40
nonnegative(spacing, "radio group spacing must be finite and nonnegative")41
validateChoices(Array<Choice<T>>(entries.size) { index -> entries[index].choice })42
}43
}45
public class RadioGroup<T: Equals<T>> public constructor(46
private val props: Props<RadioGroupProps<T>>,47
scope: ComponentScope48
): Component(scope) {49
private val handles = ChoiceHandles()50
private val themes = ThemeAccess(scope)51
private val semantics = createComponentSemantics(scope, SemanticRole.RadioGroup)52
init {53
scope.routeSemanticsChildren(semantics)54
}55
public override fun onMount() {56
themes.mount(props.value.theme)57
}58
private fun select(key: ViewKey) {59
selectValue(key)60
}61
private fun selectValue(key: ViewKey): SemanticActionResult {62
val input = props.value63
if (!input.enabled)64
return SemanticActionResult.Rejected65
for (entry in input.entries) {66
if (entry.choice.key != key)67
continue68
if (!entry.choice.enabled)69
return SemanticActionResult.Rejected70
val expected = Some(entry.choice.value)71
if (input.selected.value != expected)72
input.selected.value = expected73
return if (input.selected.value == expected)74
SemanticActionResult.Completed75
else76
SemanticActionResult.Rejected77
}78
return SemanticActionResult.Unavailable79
}80
public override fun render(): View {81
val input = props.value82
val theme = themes.read(input.theme)83
val keys = Array<ViewKey>(input.entries.size) { index -> input.entries[index].choice.key }84
val focus =85
handles.resolve(86
keys,87
Array<FocusOptions>(keys.size) { index -> input.entries[index].focus }88
)89
val selected = input.selected.value90
var preferred: FocusHandle? = None91
for (index in 0L ..< keys.size)92
if (selected == Some(input.entries[index].choice.value))93
preferred = Some(focus[index])94
val children =95
Array<View>(keys.size) {96
index ->97
val entry = input.entries[index]98
component(99
RadioItemProps(100
entry.choice.label,101
selected == Some(entry.choice.value),102
input.enabled && entry.choice.enabled,103
input.invalid || entry.invalid,104
choiceFocus(entry.focus, focus[index]),105
entry.labelTarget,106
entry.marker,107
entry.content,108
input.fonts,109
theme,110
{ select(entry.choice.key) },111
entry.semantics,112
{ selectValue(entry.choice.key) }113
),114
{ current, mount -> RadioItem(current, scope = mount) }115
)116
}117
return choiceLayout(118
children,119
keys,120
focus,121
input.axis,122
input.spacing,123
input.loop,124
input.enabled,125
input.style,126
preferred,127
{128
handle -> for (index in 0L ..< focus.size)129
if (focus[index] === handle)130
select(keys[index])131
},132
fitCross = false,133
semantics = semantics,134
semanticOptions = input.semantics135
)136
}137
}