Scoop API

scoop:scoop.ui.component:0.1.0

src/select/component.scoop

Selected production source · src/select/component.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.surface.*
6import scoop.ui.*
7import scoop.ui.style.*
8import scoop.ui.style.Theme
9import scoop.window.accessibility.*
10import scoop.ui.component.row as selectRow
11import scoop.ui.component.label as selectLabel
13public class Select<T : Equals<T>> public constructor(
14 private val props: Props<SelectProps<T>>,
15 scope: ComponentScope
16) : Component(scope) {
17 private val local = state(false)
18 private val last = state(false)
19 private val anchor = LayoutAnchor()
20 private val trigger = FocusHandle()
21 private val layer = OverlayHandle()
22 private val popupReference = SemanticsReference()
23 private val candidates = MenuFocus()
24 private val scroll = ScrollState()
25 private val themes = ThemeAccess(scope)
26 private var pendingTab: Boolean? = None
27 private val open = binding(
28 { props.value.open?.value ?: local.value },
29 { value -> setOpen(value) }
30 )
31 private val chain = MenuChain({ close() }, { reverse -> tab(reverse) })
33 private val search = scope
34 .typeahead(
35 candidates,
36 { selectedKey() },
37 { key -> selectChoice(props.value, key) },
38 searchOptions()
39 )
40 public override fun onMount() {
41 themes.mount(props.value.theme)
42 }
43 private fun searchOptions(): TypeaheadOptions {
44 val value = props.value.typeahead
45 return TypeaheadOptions(
46 value.timeout,
47 value.enabled && props.value.enabled && !open.value,
48 value.keyboard,
49 value.matches
50 )
51 }
52 private fun updated() {
53 search
54 .update(
55 candidates,
56 { selectedKey() },
57 { key -> selectChoice(props.value, key) },
58 Some(searchOptions())
59 )
60 if (open.value) pendingTab = None
61 }
62 private fun selectedKey(): ViewKey? {
63 val choice = selectedChoice(props.value.content, props.value.value.value)
64 return if (choice?.enabled ?: false) choice?.key else None
65 }
66 private fun initialFocus(): FocusHandle? = when (selectedKey()) {
67 case Some(val key) -> Some(candidates.handle(key))
68 case None -> candidates.edge(last.value)
69 }
70 private fun setOpen(value: Boolean) {
71 val previous = open.value
72 if (value) pendingTab = None
73 if (value == previous) return
74 when (props.value.open) {
75 case Some(val binding) -> binding.value = value
76 case None -> local.value = value
77 }
78 if (open.value != previous) props.value.onOpenChange(open.value)
79 }
80 private fun show(backwards: Boolean) {
81 if (!props.value.enabled) return
82 if (last.value != backwards) last.value = backwards
83 setOpen(true)
84 if (open.value) initialFocus()?.requestFocus(FocusOrigin.Keyboard)
85 }
86 private fun close() {
87 (props.value.handle ?: layer).requestClose()
88 }
89 private fun toggle() {
90 if (!props.value.enabled) return
91 if (open.value) close() else show(false)
92 }
93 private fun tab(reverse: Boolean) {
94 pendingTab = Some(reverse)
95 close()
96 if (open.value) pendingTab = None
97 }
98 private fun closed(reason: OverlayCloseReason) {
99 val next = pendingTab
100 pendingTab = None
101 when (next) {
102 case Some(val reverse) -> if (scope.isMounted) scope.moveFocus(reverse)
103 case None -> {}
104 }
105 props.value.onClosed(reason)
106 }
107 private fun menu(): MenuContent = MenuContent(
108 Array<MenuEntry>(props.value.content.entries.size) { index ->
109 when (props.value.content.entries[index]) {
110 case Label(val key, val content) -> MenuEntry.Label(key, content)
111 case Separator(val key) -> MenuEntry.Separator(key)
112 case Item(val item) ->
113 MenuEntry
114 .Radio(
115 MenuItemSpec(
116 item.choice.key,
117 item.choice.label,
118 item.choice.enabled,
119 content = item.content,
120 icon = item.icon,
121 focus = item.focus,
122 style = item.style,
123 semantics = item.semantics
124 ),
125 { props.value.value.value == Some(item.choice.value) },
126 { selectChoice(props.value, item.choice.key) },
127 true
128 )
129 }
130 }
131 )
132 private fun semanticOpen(value: Boolean): SemanticActionResult {
133 if (!scope.isMounted) return SemanticActionResult.Unavailable
134 if (!props.value.enabled) return SemanticActionResult.Rejected
135 if (open.value != value) {
136 if (value) setOpen(true) else close()
137 }
138 return if (open.value == value)
139 SemanticActionResult.Completed
140 else
141 SemanticActionResult.Rejected
142 }
143 public override fun render(): View {
144 val input = props.value
145 val selectedText = when (input.value.value) {
146 case None -> None
147 case Some(_) ->
148 Some(
149 selectedChoice(input.content, input.value.value)?.label ?: input.unavailableText
150 )
151 }
152 val theme = themes.read(input.theme)
153 val content = menu()
154 candidates.resolve(content)
155 val options = MenuOptions(
156 mode = input.mode,
157 loop = input.loop,
158 enabled = input.enabled,
159 fonts = input.fonts,
160 theme = input.theme,
161 typeahead = Some(input.typeahead),
162 semantics = input
163 .popupSemantics
164 .copy { reference = Some(reference ?: this @Select.popupReference) }
165 )
166 val button = popupSemanticButton(
167 ButtonProps(
168 ButtonContent
169 .Resolved { appearance ->
170 valueView(selectTriggerContent(theme, input.style, appearance), theme)
171 },
172 { toggle() },
173 input.enabled,
174 selectTriggerStyle(input.style, theme, input.size, input.invalid),
175 input.theme,
176 choiceFocus(input.focus, input.focus.handle ?: trigger),
177 input.invalid,
178 ButtonVariant.Outline,
179 group = input.group,
180 labelTarget = input.labelTarget,
181 semantics = input
182 .semantics
183 .copy {
184 relations = semanticControls(
185 relations,
186 (input.popupSemantics.reference ?: this @Select.popupReference).id
187 )
188 }
189 ),
190 open.value,
191 { value -> semanticOpen(value) },
192 value = selectedText,
193 fallbackName = Some(selectedText ?: input.placeholder)
194 )
195 val focus = OverlayFocusOptions(
196 when (input.popupFocus.initialFocus) {
197 case Some(_) -> input.popupFocus.initialFocus
198 case None -> initialFocus()
199 },
200 input.popupFocus.restoreFocus,
201 input.popupFocus.autoFocus,
202 Some(input.popupFocus.restoreTarget ?: input.focus.handle ?: trigger)
203 )
204 val popup = component(
205 SelectPopupProps(
206 open,
207 content,
208 selectedKey(),
209 candidates,
210 chain,
211 anchor,
212 input.handle ?: layer,
213 options,
214 focus,
215 input.position,
216 input.maxHeight,
217 input.scrollButtons,
218 input.scroll ?: scroll,
219 input.popupStyle,
220 input.transition,
221 { reason -> props.value.onCloseRequest(reason) },
222 { reason -> closed(reason) }
223 ),
224 { current, mount -> SelectPopup(current, scope = mount) }
225 )
226 return element(
227 SelectRootProps(
228 anchor,
229 input.width,
230 input.enabled,
231 open.value,
232 { backwards -> show(backwards) },
233 { updated() }
234 ),
235 { current, mount -> SelectRootLayout(current, scope = mount) },
236 [button, popup]
237 )
238 }
239 private fun valueView(appearance: ActionContentStyle, theme: Theme): View {
240 val input = props.value
241 val choice = selectedChoice(input.content, input.value.value)
242 val density = theme.tokens.typography.body.fontSize / 14.0
243 val content = when (input.valueContent) {
244 case Some(val build) -> build(SelectValue(input.value.value, choice, appearance))
245 case None ->
246 selectLabel(
247 styledText {
248 style = appearance
249 .text
250 .copy {
251 weight = 400;
252 fontSize = 14.0 * density;
253 lineHeight = Some(20.0 / 14.0)
254 if (input.value.value == None && !theme.preferences.highContrast)
255 color = theme.tokens.palette.muted.foreground
256 }
257 text(
258 choice?.label ?:
259 if (input.value.value == None)
260 input.placeholder
261 else
262 input.unavailableText
263 )
264 }
265 ) {
266 fonts = input.fonts;
267 settings = Some(
268 paragraphStyle {
269 maxLines = Some(1L);
270 ellipsis = "…"
271 }
272 )
273 }
274 }
275 return selectRow {
276 item(content) { grow = 1.0 }
277 item(
278 input.icon ?:
279 navigationIcon(
280 NavigationGlyph.Down,
281 16.0 * density,
282 if (theme.preferences.highContrast)
283 appearance.text.color
284 else
285 theme.tokens.palette.muted.foreground
286 )
287 ) { shrink = 0.0 }
288 if (input.style.surface.padding == None)
289 logicalPadding = Some(LogicalInsets(start = 2.0 * density))
290 spacing = 6.0 * density;
291 alignment = CrossAlignment.Center
292 }
293 }
296internal fun selectTriggerStyle(
297 local: ButtonStyle,
298 theme: Theme,
299 size: SelectSize,
300 invalid: Boolean
301): ButtonStyle {
302 val density = theme.tokens.typography.body.fontSize / 14.0
303 val borderColor = if (invalid)
304 (local.surface.validation ?: theme.tokens.validation).borderColor ?:
305 theme.tokens.palette.destructive.background
306 else
307 theme.tokens.palette.input
308 val focusColor = if (invalid)
309 borderColor
310 else
311 (local.surface.focus?.borderColor ?: None) ?: theme.tokens.palette.ring
312 return local
313 .copy {
314 surface {
315 if (suggestedSize == None)
316 suggestedSize = Some(
317 Size(
318 144.0 * density,
319 (if (size == SelectSize.Sm) 28.0 else 32.0) * density
320 )
321 )
322 if (minimumSize == None) minimumSize = Some(Size(0.0, 0.0))
323 if (padding == None) padding = Some(Insets(8.0 * density, 0.0, 8.0 * density, 0.0))
324 style = styleOverride {
325 background(Some(Paint(selectTriggerBackground(theme, false))))
326 border(Some(Border.all(1.0, borderColor)))
327 corners(
328 Corners
329 .all(
330 (if (size == SelectSize.Sm) 10.0 else 8.0) * density
331 )
332 )
333 boxShadows([])
334 }
335 .then(local.surface.style)
336 }
337 states = local
338 .states
339 .copy {
340 hovered = styleOverride {
341 background(Some(Paint(selectTriggerBackground(theme, true))))
342 }
343 .then(local.surface.style)
344 .then(local.states.hovered)
345 if (theme.preferences.highContrast)
346 pressed = styleOverride {
347 background(Some(Paint(selectTriggerBackground(theme, true))))
348 }
349 .then(local.surface.style)
350 .then(local.states.pressed)
351 focused = styleOverride { border(Some(Border.all(1.0, focusColor))) }
352 .then(
353 local.states.focused
354 )
355 }
356 }
359internal fun selectTriggerBackground(theme: Theme, active: Boolean): Color {
360 val colors = theme.tokens.palette
361 if (theme.preferences.highContrast)
362 return if (active) colors.muted.background else colors.background.background
363 return if (theme.tokens.mode == ThemeMode.Dark)
364 componentBackground(
365 theme,
366 colors
367 .input
368 .withAlpha(
369 colors.input.alpha * if (active) 0.5 else 0.3
370 )
371 )
372 else
373 Color.Transparent
376internal fun selectTriggerContent(
377 theme: Theme,
378 local: ButtonStyle,
379 appearance: ActionContentStyle
380): ActionContentStyle {
381 if ((local.text?.color ?: None) != None) return appearance
382 val visual = appearance.interaction
383 val colors = theme.tokens.palette
384 val foreground = if (theme.preferences.highContrast)
385 actionContrastPair(
386 theme, ActionShape.Button(ButtonSize.Default), ButtonVariant.Outline, visual
387 )
388 .foreground
389 else if (!visual.enabled && theme.tokens.disabledOpacity >= 1.0)
390 colors.disabledText
391 else
392 colors.background.foreground
393 val fill = selectTriggerBackground(theme, visual.enabled && (visual.hovered || visual.pressed))
394 return ActionContentStyle(
395 appearance
396 .text
397 .copy {
398 color = componentBackground(
399 theme, foreground,
400 if (fill.alpha == 0.0) colors.background.background else fill
401 )
402 },
403 appearance.iconSize, appearance.spacing, visual, appearance.underline
404 )