Scoop API

scoop:scoop.ui:0.1.0

src/text/editor/start.scoop

Selected production source · src/text/editor/start.scoop

1package scoop.ui.text
3import scoop.surface.*
4import scoop.ui.*
5import scoop.window.*
7/** Policy for native text received before the actual editor takes focus. */
8public class TextInputStartOptions public constructor(
9 public val policy: TextEditPolicy = textEditPolicy(),
10 public val multiline: Boolean = false
13/** Starts a normal editor from a browsing area's first native commit or composition. */
14public class TextInputStarter internal constructor(
15 private val scope: ComponentScope,
16 private val area: InputArea,
17 enabled: Boolean,
18 onStart: (TextEditorState) -> TextInputStartOptions?
19): Closeable {
20 private val window = scope.nativeWindow
21 private val pending = state<TextEditorState?>(None)
22 private var enabled = enabled
23 private var receive = onStart
24 private var closed = false
25 private var document: TextEditorState? = None
26 private var configuration: TextInputStartOptions? = None
27 private var target: TextEditHost? = None
28 private var connection: TextInputConnection? = None
29 private var sessionEnabled = false
30 private var retiring = false
31 private var performing = false
32 private val events =
33 area.scope.addEventHandler {
34 context,
35 event -> if (!closed && context.phase == RoutedPhase.Notification) {
36 when (event) {
37 case TextInput(val input) -> native(input)
38 case FocusChanged(false) -> reset(false, true)
39 else -> {}
40 }
41 }
42 }
44 init {
45 area.textInput = enabled
46 }
48 /** The shared state while the real editor is still waiting to take focus. */
49 public val pendingState: TextEditorState? get() = pending.value
51 public fun update(enabled: Boolean, onStart: (TextEditorState) -> TextInputStartOptions?) {
52 pending.checkMutation()
53 requireUi(!closed && !area.isClosed, "text input starter is closed")
54 receive = onStart
55 this.enabled = enabled
56 if (!enabled)
57 cancel()
58 area.textInput = enabled
59 }
61 /** Retires the browsing connection, including a queued first batch with no pending state. */
62 public fun cancel() {
63 pending.checkMutation()
64 if (closed || retiring || !sessionEnabled)
65 return
66 reset(true, true)
67 }
69 private fun owns(value: TextEditorState): Boolean =
70 when (pending.value) {
71 case Some(val current) -> current === value
72 case None -> false
73 }
75 internal fun bind(host: TextEditHost) {
76 if (closed || !owns(host.current))
77 return
78 when (target) {
79 case Some(val previous) -> requireUi(
80 previous === host || previous.closed,
81 "text start already has an editor"
82 )
83 case None -> {}
84 }
85 if (
86 !host.options.enabled || host.options.readOnly || !host.area.continueTextInputFrom(area)
87 ) {
88 cancel()
89 return
90 }
91 target = Some(host)
92 }
94 internal fun detach(host: TextEditHost) {
95 when (target) {
96 case Some(val current) -> if (current === host)
97 cancel()
98 case None -> {}
99 }
100 }
102 internal fun take(host: TextEditHost, value: TextInputConnection?) {
103 when (target) {
104 case Some(val current) -> if (
105 current === host && value != None && value == connection && host.area.isFocusActive
106 )
107 reset(false, false)
108 case None -> {}
109 }
110 }
112 internal fun modelChanged() {
113 if (!performing)
114 cancel()
115 }
117 private fun reset(restart: Boolean, cancelComposition: Boolean) {
118 val previous = document
119 document = None
120 configuration = None
121 target = None
122 pending.value = None
123 when (previous) {
124 case Some(val value) -> when (value.inputStarter) {
125 case Some(val owner) -> if (owner === this) {
126 value.inputStarter = None
127 if (cancelComposition)
128 value.cancelComposition()
129 }
130 case None -> {}
131 }
132 case None -> {}
133 }
134 if (restart && !area.isClosed)
135 retiring = area.restartTextInput()
136 }
138 private fun native(event: TextInputEvent) {
139 when (event) {
140 case Enabled(val value) -> {
141 connection = value
142 sessionEnabled = true
143 retiring = false
144 document = Some(TextEditorState())
145 publish()
146 }
147 case Disabled(val value) -> {
148 if (connection != None && connection != value)
149 return
150 connection = None
151 sessionEnabled = false
152 retiring = false
153 reset(false, true)
154 }
155 case Edit(val batch) -> input(batch)
156 }
157 }
159 private fun publish(after: TextInputOrigin? = None) {
160 if (closed || retiring || !sessionEnabled || !area.isFocusActive)
161 return
162 val value = document ?: return
163 when (connection) {
164 case Some(val native) -> {
165 window.publishTextInputState(native, value.inputState, after);
166 Unit
167 }
168 case None -> {}
169 }
170 }
172 private fun input(batch: TextInputBatch) {
173 if (
174 !enabled ||
175 !sessionEnabled ||
176 retiring ||
177 !area.isFocusActive ||
178 !area.scope.isInputAllowed
179 )
180 return
181 when (batch.origin) {
182 case Some(val origin) -> if (connection != Some(origin.connection))
183 return
184 case None -> {}
185 }
186 val value = document ?: return
187 var invalid = false
188 val prepared =
189 try {
190 prepareEditorInput(value, batch)
191 } catch (error: IllegalArgumentException) {
192 invalid = true;
193 None
194 } catch (error: IllegalStateException) {
195 invalid = true;
196 None
197 }
198 if (invalid) {
199 reset(true, true)
200 return
201 }
202 val raw = prepared ?: return
203 val started =
204 when (configuration) {
205 case Some(_) -> true
206 case None -> false
207 }
208 if (!started && startsText(batch)) {
209 pending.value = Some(value)
210 value.inputStarter = Some(this)
211 val before = value.inputState
212 val next =
213 try {
214 receive(value)
215 } catch (error: Throwable) {
216 reset(true, true)
217 throw error
218 }
219 if (closed || !owns(value))
220 return
221 requireUi(value.inputState == before, "text start callback changed the native document")
222 when (next) {
223 case Some(val options) -> configuration = Some(options)
224 case None -> {
225 reset(true, true);
226 return
227 }
228 }
229 }
230 val options = configuration ?: TextInputStartOptions()
231 performing = true
232 try {
233 finishEditorInput(
234 raw,
235 options.policy,
236 TextEditSource.NativeInput,
237 !options.multiline
238 ).publish(value, true)
239 } finally {
240 performing = false
241 }
242 area.setCaret(None, Some(value.revision))
243 publish(batch.origin)
244 }
246 public override fun close() {
247 pending.checkMutation()
248 if (closed)
249 return
250 closed = true
251 var failure = cleanupUi(None) { reset(false, true) }
252 failure = cleanupUi(failure) { events.close() }
253 if (!area.isClosed)
254 failure = cleanupUi(failure) { area.textInput = false }
255 throwUi(failure)
256 }
259private fun startsText(batch: TextInputBatch): Boolean {
260 for (operation in batch.operations) {
261 when (operation) {
262 case Commit(val text, _) -> {
263 if (text != "")
264 return true
265 }
266 case Preedit(val text, _, _) -> {
267 if (text != "")
268 return true
269 }
270 else -> {}
271 }
272 }
273 return false
276/** Borrows this mount's browsing area; the supplied state must be adopted by a normal editor. */
277public fun ComponentScope.textInputStarter(
278 area: InputArea,
279 enabled: Boolean = true,
280 onStart: (TextEditorState) -> TextInputStartOptions?
281): TextInputStarter {
282 requireBehaviorArea(area)
283 requireUi(area.focusable && !area.textInput, "text starter requires an unmanaged browsing area")
284 return resource { TextInputStarter(this, area, enabled, onStart) }