scoop:scoop.ui.component:0.1.0
src/text_field/ink.scoop
Selected production source · src/text_field/ink.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.text.*9
import scoop.window.*10
import scoop.window.accessibility.*12
internal class TextFieldInk(13
internal val props: Props<TextFieldInkProps>,14
scope: ComponentScope15
) : Element<TextFieldDrawing>(scope) {16
internal val fonts: FontCollection get() = props.value.fonts17
internal val host = scope18
.resource {19
val value = props.value20
TextEditHost(21
scope,22
value.input.state,23
textFieldAppearance(value.input, value.theme, value.text, value.placeholder, fonts),24
value.input.options,25
value.onInteractionChanged26
)27
}28
internal val lineHeight = TextFieldLineHeight()29
internal val labelHeight = TextFieldLineHeight()30
private val semantics = createComponentSemantics(scope, SemanticRole.TextField)31
private val padding = scope.createArea()32
private val paddingPress = scope33
.press(padding, PressOptions(props.value.input.options.enabled)) {34
host.requestFocus(FocusOrigin.Pointer, softwareKeyboard = true)35
}36
private val field = scope37
.resource { FieldLink(scope, host.area, host.focus, semantics) { updateSemantics() } }38
internal val scrollbarFeedback = scope39
.resource {40
ScrollbarFeedback(scope, { host.scroll }, { scrollbarProps() }, host.area) {41
feedbackChanged()42
}43
}44
private val horizontal = ScrollBarControl(45
scope,46
{ host.scroll },47
{ scrollbarProps() },48
host.area,49
Orientation.Horizontal,50
scrollbarFeedback,51
semantics52
) { handle, orientation -> updateBar(handle, orientation) }53
private val vertical = ScrollBarControl(54
scope,55
{ host.scroll },56
{ scrollbarProps() },57
host.area,58
Orientation.Vertical,59
scrollbarFeedback,60
semantics61
) { handle, orientation -> updateBar(handle, orientation) }62
private val painters = scope.resource { ThemePainterCache() }63
private val content = scope.resource { StylePainter() }65
private fun updateBar(handle: SemanticsHandle, orientation: Orientation) {66
val value = scrollbarProps()67
handle68
.updateScrollBar(69
host,70
scrollSemanticOrientation(orientation),71
value.lineStep,72
scrollBarProperties(value, orientation)73
)74
}76
init {77
scope.routeChildren(host.area)78
scope.routeSemanticsChildren(semantics)79
padding.scope.parent = Some(host.area.scope)80
padding.scope.defaultAction = { context, event ->81
when (event) {82
case Pointer(83
InputPointerEvent.Button(_, MouseButton.Left, ElementState.Pressed, _)84
) -> {85
host.focus.handle.requestFocus(FocusOrigin.Pointer)86
context.consume()87
}89
else -> {}90
}91
}92
}93
private fun feedbackChanged() {94
horizontal.feedbackChanged()95
vertical.feedbackChanged()96
if (scope.isMounted) scope.requestFrame()97
}98
private fun appearance(): TextEditAppearance {99
val value = props.value100
return textFieldAppearance(value.input, value.theme, value.text, value.placeholder, fonts)101
}102
private fun scrollbarProps(): ViewportScrollbarOptions {103
val value = props.value104
return ViewportScrollbarOptions(105
value.theme, if (value.input.options.wrap) ScrollAxes.Vertical else ScrollAxes.Both,106
value.input.scrollbars, value.input.style.scrollbar, value.input.scrollbarBehavior,107
value.text.fontSize, value.input.options.enabled108
)109
}110
public override fun update(): UiInvalidation {111
paddingPress112
.update(PressOptions(props.value.input.options.enabled)) {113
host.requestFocus(FocusOrigin.Pointer, softwareKeyboard = true)114
}115
val value = props.value116
val input = value.input117
host118
.update(119
input.state,120
appearance(),121
input.options,122
input.focus,123
if (value.label?.position == Some(FieldLabelPosition.Inline))124
""125
else126
input.placeholder,127
input.onEdited,128
input.onSubmit129
)130
field.update(input.labelTarget, input.options.enabled)131
updateSemantics()132
padding.scope.enabled = input.options.enabled133
scrollbarFeedback.update()134
horizontal.update()135
vertical.update()136
painters.get(value.theme)137
return UiInvalidation.Layout138
}139
private fun updateSemantics() {140
val handle = semantics ?: return141
if (handle.isClosed) return142
val input = props.value.input143
val description = if (input.description.byteLength == 0L) None else Some(input.description)144
handle.bindReference(input.semantics.reference)145
val relations = field.relations(handle, input.semantics.relations)146
handle147
.updateTextEditor(148
host,149
input150
.semantics151
.properties(152
description = description,153
required = field.isRequired(handle),154
invalid = input.invalid,155
expanded = input.expansion?.expanded,156
error = if (input.invalid) description else None157
)158
.copy {159
if (name ==160
None &&161
relations.labelledBy.size ==162
0L &&163
input.label.byteLength >164
0L165
)166
name = Some(input.label)167
},168
relations,169
input.expansion?.setExpanded170
)171
}172
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<173
TextFieldDrawing174
> = measureTextField(this, context, constraints)175
public override fun scene(context: SceneContext, current: TextFieldDrawing) {176
val value = props.value177
val geometry = current.geometry178
when (semantics) {179
case Some(val handle) -> context.semantics(handle, geometry.bounds)180
case None -> {}181
}182
val painter = painters.get(value.theme)183
if (geometry.opacity < 1.0) context.saveLayer(geometry.opacity)184
if (value.input.chrome == TextFieldChrome.Framed)185
context186
.draw { paint ->187
painter188
.drawSurface(189
paint.canvas,190
geometry,191
value.visual,192
SurfaceLayer.Background193
)194
}195
context196
.place(197
padding,198
if (!panelPresent(value.label))199
geometry.bounds200
else201
Rect(202
geometry.bounds.x, 0.0, geometry.bounds.width,203
geometry.bounds.y + geometry.bounds.height204
)205
)206
content207
.scene(context, geometry.bounds, geometry.contentStyle) { local ->208
local.translate(geometry.content.x, geometry.content.y)209
local.save()210
try {211
local.translate(current.viewport.x, current.viewport.y)212
host.scene(local, current.editor)213
} finally {214
local.restore()215
}216
scrollbarFeedback217
.scene(218
local,219
Rect(0.0, 0.0, geometry.content.width, geometry.content.height),220
current.horizontal,221
current.vertical222
)223
horizontal.scene(local, current.editor.scrollSnapshot, current.horizontal, painter)224
vertical.scene(local, current.editor.scrollSnapshot, current.vertical, painter)225
}226
if (value.input.chrome == TextFieldChrome.Framed)227
context228
.draw { paint ->229
paint230
.canvas231
.withState { canvas ->232
when (current.outlineClip) {233
case Some(val clip) -> canvas.clipPath(clip)234
case None -> {}235
}236
painter237
.drawSurface(238
canvas,239
geometry,240
value.visual,241
SurfaceLayer.Foreground242
)243
}244
}245
if (geometry.opacity < 1.0) context.restore()246
context.children()247
}248
}