Scoop API

scoop:scoop.ui.component:0.1.0

src/otp/ink.scoop

Selected production source · src/otp/ink.scoop

1package scoop.ui.component
3import scoop.charset.graphemeBoundaries
4import scoop.graphics.*
5import scoop.graphics.text.*
6import scoop.ui.*
7import scoop.ui.style.*
8import scoop.ui.text.*
9import scoop.window.TextInputPurpose
10import scoop.window.accessibility.*
12internal class OtpInk(internal val props: Props<OtpInkProps>, scope: ComponentScope): Element<
13 OtpDrawing
14>(scope) {
15 private val projection = textProjection()
16 internal val host =
17 scope.resource {
18 val value = props.value
19 TextEditHost(
20 scope,
21 value.input.state,
22 otpAppearance(value.input, value.theme, value.text, value.fonts),
23 otpOptions(value.input, projection),
24 value.onInteractionChanged
25 )
26 }
27 private val semantics = createComponentSemantics(scope, SemanticRole.TextField)
28 private val field =
29 scope.resource { FieldLink(scope, host.area, host.focus, semantics) { updateSemantics() } }
30 private val painters = scope.resource { ThemePainterCache() }
32 init {
33 scope.routeChildren(host.area)
34 scope.routeSemanticsChildren(semantics)
35 }
37 public override fun update(): UiInvalidation {
38 val input = props.value.input
39 host.update(
40 input.state,
41 otpAppearance(input, props.value.theme, props.value.text, props.value.fonts),
42 otpOptions(input, projection),
43 input.focus,
44 onEdited = { state -> props.value.input.onEdited(state) },
45 onCommitted =
46 {
47 state ->
48 val current = props.value.input
49 if (
50 state ===
51 current.state &&
52 graphemeBoundaries(state.text).size -
53 1L ==
54 current.length
55 )
56 current.onComplete(state)
57 }
58 )
59 field.update(input.labelTarget, input.enabled)
60 updateSemantics()
61 painters.get(props.value.theme)
62 return UiInvalidation.Layout
63 }
64 private fun updateSemantics() {
65 val handle = semantics ?: return
66 if (handle.isClosed)
67 return
68 val input = props.value.input
69 handle.bindReference(input.semantics.reference)
70 handle.updateTextEditor(
71 host,
72 input.semantics.properties(
73 required = field.isRequired(handle),
74 invalid = input.invalid
75 ),
76 field.relations(handle, input.semantics.relations)
77 )
78 }
79 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
80 OtpDrawing
81 > = measureOtp(this, context, constraints)
82 public override fun scene(context: SceneContext, current: OtpDrawing) {
83 val value = props.value
84 when (semantics) {
85 case Some(val handle) -> context.semantics(
86 handle,
87 Rect(0.0, 0.0, context.size.width, context.size.height)
88 )
89 case None -> {}
90 }
91 val painter = painters.get(value.theme)
92 val adapter = host
93 val offset = current.editor.scrollSnapshot.offset
94 if (!value.input.enabled)
95 context.saveLayer(value.theme.tokens.disabledOpacity)
96 context.clipRect(Rect(0.0, 0.0, context.size.width, context.size.height))
97 context.translate(current.origin.x, current.origin.y)
98 context.draw {
99 paint ->
100 paint.canvas.translate(-offset.x, -offset.y)
101 for (slot in current.slots)
102 painter.drawSurface(
103 paint.canvas,
104 slot.geometry,
105 slot.visual,
106 SurfaceLayer.Background
107 )
108 }
109 adapter.scene(context, current.editor)
110 context.withState<Unit> {
111 content ->
112 val viewport = current.editor.scrollSnapshot.viewport
113 content.clipRect(Rect(0.0, -current.origin.y, viewport.width, content.size.height))
114 content.translate(-offset.x, -offset.y)
115 content.children()
116 }
117 context.draw {
118 paint ->
119 paint.canvas.translate(-offset.x, -offset.y)
120 for (index in 0L ..< current.slots.size)
121 if (index != current.active)
122 painter.drawSurface(
123 paint.canvas,
124 current.slots[index].geometry,
125 current.slots[index].visual,
126 SurfaceLayer.Foreground
127 )
128 val active = current.slots[current.active]
129 painter.drawSurface(
130 paint.canvas,
131 active.geometry,
132 active.visual,
133 SurfaceLayer.Foreground
134 )
135 }
136 if (!value.input.enabled)
137 context.restore()
138 }
141internal fun otpAppearance(
142 input: InputOtpProps,
143 theme: Theme,
144 text: TextStyle,
145 fonts: FontCollection
146): TextEditAppearance {
147 val style = input.style
148 val colors = theme.tokens.palette
149 val selection = themeTextSelection(theme)
150 return TextEditAppearance(
151 fonts,
152 text,
153 text,
154 style.selectionColor ?: selection.background,
155 style.inactiveSelectionColor ?: selection.inactiveBackground,
156 style.caretColor ?: colors.background.foreground,
157 style.markedColor ?: colors.primary.background,
158 style.caretWidth,
159 theme.tokens.motion.preference == MotionPreference.Reduced,
160 selectionTextColor =
161 selectionTextColor(style.selectionTextColor, style.selectionColor, selection.text),
162 inactiveSelectionTextColor =
163 selectionTextColor(
164 style.inactiveSelectionTextColor,
165 style.inactiveSelectionColor,
166 selection.inactiveText
167 )
168 )
171internal fun otpOptions(input: InputOtpProps, projection: TextProjection): TextEditOptions =
172 TextEditOptions(
173 readOnly = input.readOnly,
174 enabled = input.enabled,
175 submit = TextSubmitKeys.None,
176 caretBlink = input.caretBlink,
177 policy = otpPolicy(input.pattern, input.length),
178 projection = projection,
179 contextMenu = input.contextMenu,
180 protectedText = input.protectedText,
181 acceptDrops = input.acceptDrops,
182 drag = input.drag,
183 touch = input.touch,
184 touchScroll = input.touchScroll,
185 scrollPhysics = input.scrollPhysics,
186 inputPurpose =
187 input.inputPurpose ?:
188 when (input.pattern) {
189 case Digits -> TextInputPurpose.Digits
190 else -> TextInputPurpose.Normal
191 },
192 softwareKeyboard = input.softwareKeyboard,
193 primarySelection = input.primarySelection,
194 automaticTextReplacement = input.automaticTextReplacement
195 )