scoop:scoop.ui.component:0.1.0
src/combobox/input.scoop
Selected production source · src/combobox/input.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.ui.*6
import scoop.ui.style.*7
import scoop.ui.style.Theme8
import scoop.ui.component.row as comboInputRow9
import scoop.ui.component.label as comboInputLabel10
import scoop.ui.component.wrap as comboWrap11
import scoop.ui.component.button as comboButton12
import scoop.ui.component.frame as comboFrame14
internal fun <T: Equals<T>> comboEditor(15
control: ComboboxControl<T>,16
theme: Theme,17
popup: Boolean18
): View {19
val input = control.props.value20
val field = input.input21
val d = theme.tokens.typography.body.fontSize / 14.022
val values = comboValues(input.selection)23
val selected = !popup && !input.multiple && values.size > 0L24
val label =25
if (selected)26
comboValueText(input, values[0L])27
else28
field.placeholder29
val style =30
field.style.copy {31
if (selected)32
placeholder { color = Some(theme.tokens.palette.background.foreground) }33
if (!popup && input.multiple)34
surface {35
suggestedSize = Some(Size(0.0, 0.0));36
minimumSize = Some(Size(0.0, 0.0));37
padding = Some(Insets(4.0 * d, 0.0, 4.0 * d, 0.0))38
}39
}40
val editor =41
component(42
InputProps(43
field.state,44
label,45
field.enabled,46
field.readOnly,47
field.invalid,48
field.description,49
field.fonts,50
style,51
field.theme,52
field.focus.copy { handle = Some(control.editor) },53
field.direction,54
field.alignment,55
field.paintCache,56
field.caretBlink,57
{ query -> control.edited(query) },58
field.onSubmit,59
field.policy,60
field.projection,61
field.copy,62
if (popup)63
None64
else65
field.labelTarget,66
field.group,67
TextFieldChrome.Bare,68
field.contextMenu,69
field.protectedText,70
candidateInputSemantics(71
field.semantics,72
control.list.semanticId,73
when (val key = control.active.value) {74
case Some(val value) -> if (control.results().indexOfKey(value) != None)75
key76
else77
None78
case None -> None79
}80
),81
field.acceptDrops,82
field.drag,83
field.touch,84
field.touchScroll,85
field.scrollPhysics,86
Some(87
TextFieldExpansion(control.open.value, { value -> control.semanticOpen(value) })88
),89
field.inputPurpose,90
field.softwareKeyboard,91
field.primarySelection,92
field.automaticTextReplacement93
),94
{ current, mount -> Input(current, scope = mount) }95
)96
return element(97
control,98
{ current, mount -> ComboEditorEvents(current, scope = mount) },99
[editor],100
ViewKey.Text("editor")101
)102
}104
private fun <T: Equals<T>> comboChip(105
control: ComboboxControl<T>,106
chip: ComboChip<T>,107
theme: Theme108
): View {109
val input = control.props.value110
val d = theme.tokens.typography.body.fontSize / 14.0111
val text =112
theme.tokens.typography.body.copy {113
fontSize = 12.0 * d;114
lineHeight = Some(16.0 / 12.0);115
weight = 500;116
color = theme.tokens.palette.background.foreground117
}118
val fonts = input.input.fonts119
val direction = control.owner.layoutDirection.textDirection()120
val body =121
comboInputRow {122
this.item(123
comboInputLabel(124
styledText {125
style = text;126
this.text(comboValueText(input, chip.value))127
}128
) {129
this.fonts = fonts;130
settings =131
Some(132
paragraphStyle {133
this.direction = direction;134
maxLines = Some(1L)135
}136
)137
}138
) { grow = 1.0 }139
this.item(140
comboButton(modalCloseIcon(12.0 * d, text.color), { control.remove(chip.value) }) {141
enabled = input.mutable;142
variant = ButtonVariant.Ghost;143
size = ButtonSize.IconXs144
focus = FocusOptions(Some(chip.focus));145
this.theme = input.input.theme146
}147
) { shrink = 0.0 }148
spacing = 4.0 * d;149
alignment = CrossAlignment.Center150
}151
return comboFrame(body) {152
key = chip.key;153
height = Some(21.0 * d);154
logicalPadding = Some(LogicalInsets(6.0 * d, 0.0, 0.0, 0.0))155
style =156
styleOverride {157
background(Some(Paint(theme.tokens.palette.muted.background)));158
corners(Corners.all(4.0 * d))159
}160
}161
}163
internal fun <T: Equals<T>> comboField(164
control: ComboboxControl<T>,165
theme: Theme,166
popup: Boolean167
): View {168
val input = control.props.value169
val d = theme.tokens.typography.body.fontSize / 14.0170
val editor = comboEditor(control, theme, popup)171
val body =172
if (!popup && input.multiple)173
comboWrap {174
for (chip in control.chips)175
this.item(comboChip(control, chip, theme)) { shrink = 1.0 }176
this.item(editor) {177
basis = FlexBasis.Fixed(64.0 * d);178
min = 0.0;179
grow = 1.0180
}181
spacing = 4.0 * d;182
lineSpacing = 4.0 * d;183
alignment = CrossAlignment.Center184
padding = Insets(4.0 * d, 4.0 * d, 4.0 * d, 4.0 * d)185
}186
else187
editor188
val addons = ArrayList<InputGroupAddon>()189
val hasValue = comboValues(input.selection).size > 0L || input.query.text.byteLength > 0L190
if (input.showClear && hasValue) {191
addons.add(192
InputGroupAddon(193
comboButton(194
modalCloseIcon(16.0 * d, theme.tokens.palette.muted.foreground),195
{ control.clear() }196
) {197
enabled = input.mutable;198
variant = ButtonVariant.Ghost;199
size = ButtonSize.IconXs;200
semantics = ComponentSemantics(name = Some("Clear selection"));201
this.theme = input.input.theme202
},203
InputGroupAlign.InlineEnd,204
ViewKey.Text("clear"),205
Some(Insets(0.0, 0.0, 2.0 * d, 0.0))206
)207
)208
} else209
if (!popup && input.showTrigger) {210
addons.add(211
InputGroupAddon(212
comboButton(213
navigationIcon(214
NavigationGlyph.Down,215
16.0 * d,216
theme.tokens.palette.muted.foreground217
),218
{ control.toggle() }219
) {220
enabled = input.mutable;221
variant = ButtonVariant.Ghost;222
size = ButtonSize.IconXs;223
semantics =224
input.triggerSemantics.copy {225
if (name == None)226
name = Some("Show options")227
};228
this.theme = input.input.theme229
focus = input.triggerFocus.copy { handle = Some(control.trigger) }230
},231
InputGroupAlign.InlineEnd,232
ViewKey.Text("trigger"),233
Some(Insets(0.0, 0.0, 2.0 * d, 0.0))234
)235
)236
}237
return component(238
InputGroupProps(239
InputGroupControl.Custom(body, control.editor),240
addons.toArray(),241
input.input.enabled,242
input.input.invalid,243
input.input.fonts,244
input.input.theme,245
input.input.style.surface246
),247
{ current, mount -> InputGroup(current, scope = mount) }248
)249
}251
internal fun <T: Equals<T>> comboTrigger(control: ComboboxControl<T>, theme: Theme): View {252
val input = control.props.value253
val values = comboValues(input.selection)254
val text =255
if (values.size == 0L)256
input.input.placeholder257
else258
if (values.size == 1L)259
comboValueText(input, values[0L])260
else261
f"${values.size} selected"262
val d = theme.tokens.typography.body.fontSize / 14.0263
val direction = control.owner.layoutDirection.textDirection()264
val content =265
comboInputRow {266
this.item(267
comboInputLabel(text) {268
fonts = input.input.fonts;269
this.theme = input.input.theme270
settings =271
Some(272
paragraphStyle {273
this.direction = direction;274
maxLines = Some(1L);275
ellipsis = "…"276
}277
)278
}279
) { grow = 1.0 }280
this.item(281
navigationIcon(282
NavigationGlyph.Down,283
16.0 * d,284
theme.tokens.palette.muted.foreground285
)286
) { shrink = 0.0 }287
spacing = 8.0 * d;288
alignment = CrossAlignment.Center289
}290
return popupSemanticButton(291
ButtonProps(292
ButtonContent.Custom(content),293
{ control.toggle() },294
input.mutable,295
input.triggerStyle,296
input.input.theme,297
input.triggerFocus.copy { handle = Some(control.trigger) },298
input.input.invalid,299
ButtonVariant.Outline,300
ButtonSize.Default,301
labelTarget = input.input.labelTarget,302
semantics =303
input.triggerSemantics.copy {304
relations = semanticControls(relations, control.list.semanticId)305
if (name == None && relations.labelledBy.size == 0L)306
name = Some(text)307
}308
),309
control.open.value,310
{ value -> control.semanticOpen(value) }311
)312
}