scoop:scoop.ui.component:0.1.0
src/text_field/component.scoop
Selected production source · src/text_field/component.scoop
1
package scoop.ui.component3
import scoop.graphics.text.*4
import scoop.ui.*5
import scoop.ui.style.*6
import scoop.ui.text.*8
internal class TextFieldInput(9
val state: TextEditorState,10
val placeholder: String,11
val invalid: Boolean,12
val description: String,13
val fonts: FontCollection?,14
val style: TextFieldStyle,15
val theme: ThemeSource?,16
val focus: FocusOptions,17
val options: TextEditOptions,18
val rows: Long,19
val scrollbars: ScrollbarVisibility,20
val onEdited: (TextEditorState) -> Unit,21
val onSubmit: (TextEditorState) -> Unit,22
val labelTarget: FieldTarget?,23
val group: GroupPosition?,24
val chrome: TextFieldChrome,25
val scrollbarBehavior: ScrollbarBehavior = scrollbarBehavior(),26
val semantics: ComponentSemantics = ComponentSemantics(),27
val expansion: TextFieldExpansion? = None,28
val label: String = ""29
)31
/** Shares composition while keeping the two public component identities distinct. */32
internal class TextFieldRenderer(private val scope: ComponentScope) {33
private val themes = ThemeAccess(scope)34
private val body = ActionTextStyle()35
private val hint = ActionTextStyle()36
private val description = ActionTextStyle()37
private val label = TextFieldLabelStyle()38
private val visual = state(TextEditInteraction())39
private val interactionChanged: (TextEditInteraction) -> Unit = { next -> visual.value = next }40
fun render(input: TextFieldInput): View {41
val snapshot = input.state.inputState42
val interaction = visual.value43
val fonts = input.fonts ?: scope.defaultFonts44
val visual = SurfaceVisual(45
SurfaceKind.Plain,46
InteractionVisual(47
input.options.enabled,48
interaction.hovered,49
focusVisible = interaction.focusVisible,50
invalid = input.invalid51
)52
)53
val theme = themes.read(input.theme)54
val palette = theme.tokens.palette55
val text = body56
.resolve(57
theme.tokens.typography.input,58
theme.surfaceTextColor(visual),59
input.style.text60
)61
val placeholder = hint62
.resolve(63
theme.tokens.typography.input,64
palette.muted.foreground,65
input.style.placeholder66
)67
val label = this68
.label69
.resolve(70
input, theme, visual.interaction, interaction.focused,71
snapshot.surroundingText.byteLength > 0L72
)73
val children = ChildrenBuilder()74
when (label) {75
case Some(val appearance) ->76
children77
.add(78
component(79
LabelProps(80
LabelText.Plain(input.label, None), input.fonts,81
settings = Some(82
paragraphStyle {83
direction = this @TextFieldRenderer84
.scope85
.layoutDirection86
.textDirection()87
maxLines = Some(1L);88
ellipsis = "…"89
}90
),91
baseStyle = Some(appearance.text),92
semantics = ComponentSemantics(ignored = true)93
),94
{ props, mount -> Label(props, scope = mount) },95
ViewKey.Text("field-label")96
)97
)98
case None -> {}99
}100
if (input.description.byteLength > 0L)101
children102
.add(103
component(104
LabelProps(105
LabelText.Plain(input.description, None),106
input.fonts,107
baseStyle = Some(108
description109
.resolve(110
theme.tokens.typography.caption,111
if (!input.options.enabled)112
palette.disabledText113
else if (input.invalid)114
palette.destructive.background115
else116
palette.muted.foreground,117
input.style.description118
)119
),120
semantics = ComponentSemantics(ignored = true)121
),122
{ props, mount -> Label(props, scope = mount) },123
ViewKey.Text("field-description")124
)125
)126
return element(127
TextFieldInkProps(128
input,129
theme,130
visual,131
textFieldOptions(input, theme, visual.interaction),132
text,133
placeholder,134
interactionChanged,135
fonts,136
label137
),138
{ props, mount -> TextFieldInk(props, scope = mount) },139
children.childrenSnapshot()140
)141
}142
}144
internal class TextFieldInkProps(145
val input: TextFieldInput,146
val theme: Theme,147
val visual: SurfaceVisual,148
val surface: SurfaceOptions,149
val text: TextStyle,150
val placeholder: TextStyle,151
val onInteractionChanged: (TextEditInteraction) -> Unit,152
val fonts: FontCollection,153
val label: TextFieldLabelAppearance? = None154
)156
internal fun textFieldAppearance(157
input: TextFieldInput,158
theme: Theme,159
text: TextStyle,160
placeholder: TextStyle,161
fonts: FontCollection162
): TextEditAppearance {163
val style = input.style164
val palette = theme.tokens.palette165
val selection = themeTextSelection(theme)166
return TextEditAppearance(167
fonts,168
text,169
placeholder,170
style.selectionColor ?: selection.background,171
style.inactiveSelectionColor ?: selection.inactiveBackground,172
style.caretColor ?: palette.primary.background,173
style.markedColor ?: palette.primary.background,174
style.caretWidth,175
theme.tokens.motion.preference == MotionPreference.Reduced,176
selectionTextColor = selectionTextColor(177
style.selectionTextColor,178
style.selectionColor,179
selection.text180
),181
inactiveSelectionTextColor = selectionTextColor(182
style.inactiveSelectionTextColor,183
style.inactiveSelectionColor,184
selection.inactiveText185
)186
)187
}