scoop:scoop.ui.component:0.1.0
src/select/component.scoop
Selected production source · src/select/component.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.surface.*6
import scoop.ui.*7
import scoop.ui.style.*8
import scoop.ui.style.Theme9
import scoop.window.accessibility.*10
import scoop.ui.component.row as selectRow11
import scoop.ui.component.label as selectLabel13
public class Select<T : Equals<T>> public constructor(14
private val props: Props<SelectProps<T>>,15
scope: ComponentScope16
) : 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? = None27
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 = scope34
.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.typeahead45
return TypeaheadOptions(46
value.timeout,47
value.enabled && props.value.enabled && !open.value,48
value.keyboard,49
value.matches50
)51
}52
private fun updated() {53
search54
.update(55
candidates,56
{ selectedKey() },57
{ key -> selectChoice(props.value, key) },58
Some(searchOptions())59
)60
if (open.value) pendingTab = None61
}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 None65
}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.value72
if (value) pendingTab = None73
if (value == previous) return74
when (props.value.open) {75
case Some(val binding) -> binding.value = value76
case None -> local.value = value77
}78
if (open.value != previous) props.value.onOpenChange(open.value)79
}80
private fun show(backwards: Boolean) {81
if (!props.value.enabled) return82
if (last.value != backwards) last.value = backwards83
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) return91
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 = None97
}98
private fun closed(reason: OverlayCloseReason) {99
val next = pendingTab100
pendingTab = None101
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
MenuEntry114
.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.semantics124
),125
{ props.value.value.value == Some(item.choice.value) },126
{ selectChoice(props.value, item.choice.key) },127
true128
)129
}130
}131
)132
private fun semanticOpen(value: Boolean): SemanticActionResult {133
if (!scope.isMounted) return SemanticActionResult.Unavailable134
if (!props.value.enabled) return SemanticActionResult.Rejected135
if (open.value != value) {136
if (value) setOpen(true) else close()137
}138
return if (open.value == value)139
SemanticActionResult.Completed140
else141
SemanticActionResult.Rejected142
}143
public override fun render(): View {144
val input = props.value145
val selectedText = when (input.value.value) {146
case None -> None147
case Some(_) ->148
Some(149
selectedChoice(input.content, input.value.value)?.label ?: input.unavailableText150
)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 = input163
.popupSemantics164
.copy { reference = Some(reference ?: this @Select.popupReference) }165
)166
val button = popupSemanticButton(167
ButtonProps(168
ButtonContent169
.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 = input182
.semantics183
.copy {184
relations = semanticControls(185
relations,186
(input.popupSemantics.reference ?: this @Select.popupReference).id187
)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.initialFocus198
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.value241
val choice = selectedChoice(input.content, input.value.value)242
val density = theme.tokens.typography.body.fontSize / 14.0243
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 = appearance249
.text250
.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.foreground256
}257
text(258
choice?.label ?:259
if (input.value.value == None)260
input.placeholder261
else262
input.unavailableText263
)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.color284
else285
theme.tokens.palette.muted.foreground286
)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.Center292
}293
}294
}296
internal fun selectTriggerStyle(297
local: ButtonStyle,298
theme: Theme,299
size: SelectSize,300
invalid: Boolean301
): ButtonStyle {302
val density = theme.tokens.typography.body.fontSize / 14.0303
val borderColor = if (invalid)304
(local.surface.validation ?: theme.tokens.validation).borderColor ?:305
theme.tokens.palette.destructive.background306
else307
theme.tokens.palette.input308
val focusColor = if (invalid)309
borderColor310
else311
(local.surface.focus?.borderColor ?: None) ?: theme.tokens.palette.ring312
return local313
.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) * density320
)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
Corners329
.all(330
(if (size == SelectSize.Sm) 10.0 else 8.0) * density331
)332
)333
boxShadows([])334
}335
.then(local.surface.style)336
}337
states = local338
.states339
.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.focused354
)355
}356
}357
}359
internal fun selectTriggerBackground(theme: Theme, active: Boolean): Color {360
val colors = theme.tokens.palette361
if (theme.preferences.highContrast)362
return if (active) colors.muted.background else colors.background.background363
return if (theme.tokens.mode == ThemeMode.Dark)364
componentBackground(365
theme,366
colors367
.input368
.withAlpha(369
colors.input.alpha * if (active) 0.5 else 0.3370
)371
)372
else373
Color.Transparent374
}376
internal fun selectTriggerContent(377
theme: Theme,378
local: ButtonStyle,379
appearance: ActionContentStyle380
): ActionContentStyle {381
if ((local.text?.color ?: None) != None) return appearance382
val visual = appearance.interaction383
val colors = theme.tokens.palette384
val foreground = if (theme.preferences.highContrast)385
actionContrastPair(386
theme, ActionShape.Button(ButtonSize.Default), ButtonVariant.Outline, visual387
)388
.foreground389
else if (!visual.enabled && theme.tokens.disabledOpacity >= 1.0)390
colors.disabledText391
else392
colors.background.foreground393
val fill = selectTriggerBackground(theme, visual.enabled && (visual.hovered || visual.pressed))394
return ActionContentStyle(395
appearance396
.text397
.copy {398
color = componentBackground(399
theme, foreground,400
if (fill.alpha == 0.0) colors.background.background else fill401
)402
},403
appearance.iconSize, appearance.spacing, visual, appearance.underline404
)405
}