Scoop API

scoop:scoop.ui.component:0.1.0

src/text_field/ink.scoop

Selected production source · src/text_field/ink.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.text.*
9import scoop.window.*
10import scoop.window.accessibility.*
12internal class TextFieldInk(
13 internal val props: Props<TextFieldInkProps>,
14 scope: ComponentScope
15) : Element<TextFieldDrawing>(scope) {
16 internal val fonts: FontCollection get() = props.value.fonts
17 internal val host = scope
18 .resource {
19 val value = props.value
20 TextEditHost(
21 scope,
22 value.input.state,
23 textFieldAppearance(value.input, value.theme, value.text, value.placeholder, fonts),
24 value.input.options,
25 value.onInteractionChanged
26 )
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 = scope
33 .press(padding, PressOptions(props.value.input.options.enabled)) {
34 host.requestFocus(FocusOrigin.Pointer, softwareKeyboard = true)
35 }
36 private val field = scope
37 .resource { FieldLink(scope, host.area, host.focus, semantics) { updateSemantics() } }
38 internal val scrollbarFeedback = scope
39 .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 semantics
52 ) { 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 semantics
61 ) { 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 handle
68 .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.value
100 return textFieldAppearance(value.input, value.theme, value.text, value.placeholder, fonts)
101 }
102 private fun scrollbarProps(): ViewportScrollbarOptions {
103 val value = props.value
104 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.enabled
108 )
109 }
110 public override fun update(): UiInvalidation {
111 paddingPress
112 .update(PressOptions(props.value.input.options.enabled)) {
113 host.requestFocus(FocusOrigin.Pointer, softwareKeyboard = true)
114 }
115 val value = props.value
116 val input = value.input
117 host
118 .update(
119 input.state,
120 appearance(),
121 input.options,
122 input.focus,
123 if (value.label?.position == Some(FieldLabelPosition.Inline))
124 ""
125 else
126 input.placeholder,
127 input.onEdited,
128 input.onSubmit
129 )
130 field.update(input.labelTarget, input.options.enabled)
131 updateSemantics()
132 padding.scope.enabled = input.options.enabled
133 scrollbarFeedback.update()
134 horizontal.update()
135 vertical.update()
136 painters.get(value.theme)
137 return UiInvalidation.Layout
138 }
139 private fun updateSemantics() {
140 val handle = semantics ?: return
141 if (handle.isClosed) return
142 val input = props.value.input
143 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 handle
147 .updateTextEditor(
148 host,
149 input
150 .semantics
151 .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 None
157 )
158 .copy {
159 if (name ==
160 None &&
161 relations.labelledBy.size ==
162 0L &&
163 input.label.byteLength >
164 0L
165 )
166 name = Some(input.label)
167 },
168 relations,
169 input.expansion?.setExpanded
170 )
171 }
172 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
173 TextFieldDrawing
174 > = measureTextField(this, context, constraints)
175 public override fun scene(context: SceneContext, current: TextFieldDrawing) {
176 val value = props.value
177 val geometry = current.geometry
178 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 context
186 .draw { paint ->
187 painter
188 .drawSurface(
189 paint.canvas,
190 geometry,
191 value.visual,
192 SurfaceLayer.Background
193 )
194 }
195 context
196 .place(
197 padding,
198 if (!panelPresent(value.label))
199 geometry.bounds
200 else
201 Rect(
202 geometry.bounds.x, 0.0, geometry.bounds.width,
203 geometry.bounds.y + geometry.bounds.height
204 )
205 )
206 content
207 .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 scrollbarFeedback
217 .scene(
218 local,
219 Rect(0.0, 0.0, geometry.content.width, geometry.content.height),
220 current.horizontal,
221 current.vertical
222 )
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 context
228 .draw { paint ->
229 paint
230 .canvas
231 .withState { canvas ->
232 when (current.outlineClip) {
233 case Some(val clip) -> canvas.clipPath(clip)
234 case None -> {}
235 }
236 painter
237 .drawSurface(
238 canvas,
239 geometry,
240 value.visual,
241 SurfaceLayer.Foreground
242 )
243 }
244 }
245 if (geometry.opacity < 1.0) context.restore()
246 context.children()
247 }