Scoop API

scoop:scoop.ui.component:0.1.0

src/actions/button_group.scoop

Selected production source · src/actions/button_group.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.surface.InputScope
6import scoop.ui.*
7import scoop.ui.style.*
9public enum ButtonGroupContent {
10 Button(ButtonProps), Input(InputProps), Text(PanelContent, FontCollection?, StyleOverride),
11 Separator(Color?), Custom((GroupPosition) -> View), Plain(View), Nested(View)
14public class ButtonGroupEntry public constructor(
15 public val content: ButtonGroupContent,
16 public val key: ViewKey = ViewKey.Position,
17 public val layout: FlexItemOptions = FlexItemOptions()
20public class ButtonGroupProps public constructor(
21 public val entries: Array<ButtonGroupEntry>,
22 public val axis: FlexAxis = FlexAxis.Horizontal,
23 public val fonts: FontCollection? = None,
24 public val theme: ThemeSource? = None,
25 public val style: StyleOverride = styleOverride()
28public class ButtonGroup public constructor(
29 private val props: Props<ButtonGroupProps>,
30 scope: ComponentScope
31) : Component(scope) {
32 private val themes = ThemeAccess(scope)
33 public override fun onMount() {
34 themes.mount(props.value.theme)
35 }
36 public override fun render(): View {
37 val value = props.value
38 val theme = themes.read(value.theme)
39 val nested = buttonGroupHasNested(value.entries)
40 val slots = Array<GroupSlot>(value.entries.size) { _ -> GroupSlot() }
41 val items = Array<FlexItem>(value.entries.size) { index ->
42 val entry = value.entries[index]
43 val position = GroupPosition(
44 value.axis,
45 nested || index == 0L,
46 nested || index == value.entries.size - 1L
47 )
48 val child = groupContent(entry.content, position, !nested, value, theme)
49 FlexItem(
50 element(
51 slots[index],
52 { current, mount -> GroupSlotInk(current, scope = mount) },
53 [child],
54 entry.key
55 ),
56 entry.layout
57 )
58 }
59 val layout = LinearProps(
60 items,
61 spacing = if (nested) 8.0 * theme.tokens.typography.body.fontSize / 14.0 else 0.0,
62 alignment = CrossAlignment.Stretch
63 )
64 val decoration = value.style.resolve(style())
65 return styled(
66 element(
67 GroupLayoutProps(layout, value.axis, slots),
68 { current, mount -> GroupLayout(current, scope = mount) },
69 layout.children
70 )
71 ) { this.style = decoration }
72 }
75internal fun buttonGroupHasNested(entries: Array<ButtonGroupEntry>): Boolean {
76 for (entry in entries)
77 when (entry.content) {
78 case Nested(_) -> return true
79 else -> {}
80 }
81 return false
84private fun groupContent(
85 content: ButtonGroupContent,
86 position: GroupPosition,
87 connected: Boolean,
88 props: ButtonGroupProps,
89 theme: Theme
90): View {
91 val group = if (connected) Some(position) else None
92 return when (content) {
93 case Button(val value) ->
94 component(
95 groupButtonProps(value, group, props.fonts, props.theme),
96 { current, mount -> Button(current, scope = mount) }
97 )
98 case Input(val value) ->
99 component(
100 groupInputProps(value, group, props.fonts, props.theme),
101 { current, mount -> Input(current, scope = mount) }
102 )
103 case Text(val value, val fonts, val style) ->
104 groupText(
105 value,
106 if (panelPresent(fonts)) fonts else props.fonts,
107 style,
108 group,
109 theme
110 )
111 case Separator(val color) ->
112 groupSeparator(
113 position.axis,
114 color ?: theme.tokens.palette.input
115 )
116 case Custom(val render) -> render(position)
117 case Plain(val view) -> view
118 case Nested(val view) -> view
119 }
122/** Records only the current wrapper's input subtree, never the opaque child's internals. */
123internal class GroupSlot {
124 var input: InputScope? = None
127internal class GroupSlotInk(private val props: Props<GroupSlot>, scope: ComponentScope) :
128 Element<Unit>(scope) {
129 private var input: InputScope? = None
130 public override fun onMount() {
131 input = Some(scope.input)
132 }
133 public override fun update(): UiInvalidation {
134 props.value.input = input
135 return UiInvalidation.Layout
136 }
137 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
138 Unit
139 > {
140 val child = context.measureChild(0L, constraints)
141 context.placeChild(0L, Point(0.0, 0.0))
142 return LayoutResult(child, Unit)
143 }
144 public override fun scene(context: SceneContext, measured: Unit) {
145 context.children()
146 }
149internal class GroupLayoutProps(
150 val layout: LinearProps,
151 val axis: FlexAxis,
152 val slots: Array<GroupSlot>,
153 val fitCross: Boolean = axis == FlexAxis.Vertical
154) {
155 val arrangement = layout.arrangement(axis)
158internal class GroupLayout(
159 private val props: Props<GroupLayoutProps>,
160 scope: ComponentScope
161) : Element<Unit>(scope) {
162 private var input: InputScope? = None
163 public override fun onMount() {
164 val route = scope.input
165 input = Some(route)
166 scope.resource { route.observeActivity { if (scope.isMounted) scope.requestFrame() } }
167 }
168 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
169 Unit
170 > = LayoutResult(
171 measureFlex(
172 context,
173 constraints,
174 props.value.layout.items,
175 props.value.arrangement,
176 fitCross = props.value.fitCross
177 ),
178 Unit
179 )
180 public override fun scene(context: SceneContext, measured: Unit) {
181 val input = props.value
182 if (!this.input!!.activity.hasFocus) {
183 context.children()
184 return
185 }
186 val order = ArrayList<Long>()
187 for (index in 0L..<input.slots.size)
188 if (!(input.slots[index].input?.activity?.hasFocus ?: false)) order.add(index)
189 for (index in 0L..<input.slots.size)
190 if (input.slots[index].input?.activity?.hasFocus ?: false) order.add(index)
191 context.children(order.toArray())
192 }
195internal fun groupButtonProps(
196 input: ButtonProps,
197 group: GroupPosition?,
198 fonts: FontCollection?,
199 theme: ThemeSource?
200): ButtonProps {
201 val content = when (input.content) {
202 case Label(val value) ->
203 ButtonContent
204 .Label(
205 LabelProps(
206 value.text,
207 if (panelPresent(value.fonts)) value.fonts else fonts,
208 value.settings,
209 value.inlines,
210 value.theme,
211 value.baseStyle,
212 value.underline,
213 value.semantics
214 )
215 )
216 else -> input.content
217 }
218 return ButtonProps(
219 content,
220 input.onClick,
221 input.enabled,
222 input.style,
223 if (panelPresent(input.theme)) input.theme else theme,
224 input.focus,
225 input.invalid,
226 input.variant,
227 input.size,
228 group,
229 input.labelTarget,
230 input.semantics
231 )
234internal fun groupInputProps(
235 input: InputProps,
236 group: GroupPosition?,
237 fonts: FontCollection?,
238 theme: ThemeSource?
239): InputProps = InputProps(
240 input.state,
241 input.placeholder,
242 input.enabled,
243 input.readOnly,
244 input.invalid,
245 input.description,
246 if (panelPresent(input.fonts)) input.fonts else fonts,
247 input.style,
248 if (panelPresent(input.theme)) input.theme else theme,
249 input.focus,
250 input.direction,
251 input.alignment,
252 input.paintCache,
253 input.caretBlink,
254 input.onEdited,
255 input.onSubmit,
256 input.policy,
257 input.projection,
258 input.copy,
259 input.labelTarget,
260 group,
261 input.chrome,
262 input.contextMenu,
263 input.protectedText,
264 input.semantics,
265 input.acceptDrops,
266 input.drag,
267 input.touch,
268 input.touchScroll,
269 input.scrollPhysics,
270 inputPurpose = input.inputPurpose,
271 softwareKeyboard = input.softwareKeyboard,
272 primarySelection = input.primarySelection,
273 automaticTextReplacement = input.automaticTextReplacement,
274 label = input.label