Scoop API

scoop:scoop.ui.component:0.1.0

src/choice/toggle_group.scoop

Selected production source · src/choice/toggle_group.scoop

1package scoop.ui.component
3import scoop.graphics.text.*
4import scoop.ui.*
5import scoop.ui.style.*
6import scoop.window.accessibility.SemanticRole
8public class ToggleGroupEntry<T: Equals<T>> public constructor(
9 public val choice: Choice<T>,
10 public val content: ButtonContent? = None,
11 public val focus: FocusOptions = FocusOptions(),
12 public val invalid: Boolean = false,
13 public val style: ButtonStyle? = None,
14 public val semantics: ComponentSemantics = ComponentSemantics()
17public class ToggleGroupProps<T: Equals<T>> public constructor(
18 public val entries: Array<ToggleGroupEntry<T>>,
19 public val selected: ToggleGroupSelection<T>,
20 public val axis: FlexAxis = FlexAxis.Horizontal,
21 public val spacing: Double = 8.0,
22 public val loop: Boolean = true,
23 public val enabled: Boolean = true,
24 public val invalid: Boolean = false,
25 public val variant: ToggleVariant = ToggleVariant.Default,
26 public val size: ToggleSize = ToggleSize.Default,
27 public val fonts: FontCollection? = None,
28 public val theme: ThemeSource? = None,
29 public val style: StyleOverride = styleOverride(),
30 public val semantics: ComponentSemantics = ComponentSemantics()
31) {
32 init {
33 nonnegative(spacing, "toggle group spacing must be finite and nonnegative")
34 validateChoices(Array<Choice<T>>(entries.size) { index -> entries[index].choice })
35 }
38public class ToggleGroup<T: Equals<T>> public constructor(
39 private val props: Props<ToggleGroupProps<T>>,
40 scope: ComponentScope
41): Component(scope) {
42 private val handles = ChoiceHandles()
43 private val themes = ThemeAccess(scope)
44 private val semantics = createComponentSemantics(scope, SemanticRole.Group)
45 init {
46 scope.routeSemanticsChildren(semantics)
47 }
48 public override fun onMount() {
49 themes.mount(props.value.theme)
50 }
51 public override fun render(): View {
52 val input = props.value
53 val theme = themes.read(input.theme)
54 val keys = Array<ViewKey>(input.entries.size) { index -> input.entries[index].choice.key }
55 val focus =
56 handles.resolve(
57 keys,
58 Array<FocusOptions>(keys.size) { index -> input.entries[index].focus }
59 )
60 val children =
61 Array<View>(keys.size) {
62 index ->
63 val entry = input.entries[index]
64 val pressed =
65 binding(
66 { choiceSelected(props.value.selected, entry.choice.value) },
67 {
68 next ->
69 val latest = props.value
70 for (candidate in latest.entries)
71 if (
72 candidate.choice.key ==
73 entry.choice.key &&
74 latest.enabled &&
75 candidate.choice.enabled
76 )
77 setChoice(latest.selected, candidate.choice.value, next)
78 }
79 )
80 val base = entry.style ?: buttonStyle { this.kind = SurfaceKind.Plain }
81 val style =
82 if (input.spacing == 0.0)
83 base.copy {
84 surface {
85 if (this.padding == None)
86 this.padding =
87 Some(
88 Insets(
89 8.0 *
90 theme.tokens.typography.action.fontSize /
91 14.0,
92 0.0,
93 8.0 *
94 theme.tokens.typography.action.fontSize /
95 14.0,
96 0.0
97 )
98 )
99 }
100 }
101 else
102 base
103 val content =
104 entry.content ?:
105 ButtonContent.Label(
106 LabelProps(
107 LabelText.Plain(entry.choice.label, None),
108 input.fonts,
109 Some(paragraphStyle { this.alignment = TextAlignment.Center })
110 )
111 )
112 component(
113 ToggleProps(
114 content,
115 pressed,
116 input.enabled && entry.choice.enabled,
117 input.invalid || entry.invalid,
118 input.variant,
119 input.size,
120 style,
121 input.theme,
122 choiceFocus(entry.focus, focus[index]),
123 if (input.spacing == 0.0)
124 Some(GroupPosition(input.axis, index == 0L, index == keys.size - 1L))
125 else
126 None,
127 entry.semantics
128 ),
129 { current, mount -> Toggle(current, scope = mount) }
130 )
131 }
132 return choiceLayout(
133 children,
134 keys,
135 focus,
136 input.axis,
137 input.spacing,
138 input.loop,
139 input.enabled,
140 input.style,
141 semantics = semantics,
142 semanticOptions = input.semantics
143 )
144 }