Scoop API

scoop:scoop.ui:0.1.0

src/text/editor/events.scoop

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

1package scoop.ui.text
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.ui.*
6import scoop.window.*
8internal fun editorInteraction(host: TextEditHost, focused: Boolean, hovered: Boolean) {
9 val next =
10 TextEditInteraction(
11 focused && host.options.enabled,
12 hovered && host.options.enabled,
13 host.area.isFocusVisible && host.options.enabled
14 )
15 host.publishInteraction(next)
18internal fun editorHandle(host: TextEditHost, context: RoutedEventContext, event: InputEvent) {
19 if (host.closed)
20 return
21 if (context.phase == RoutedPhase.Notification) {
22 when (event) {
23 case FocusChanged(val value) -> editorFocused(host, value)
24 case FocusVisibilityChanged(_) -> editorInteraction(
25 host,
26 host.area.isFocusActive,
27 host.visual.value.hovered
28 )
29 case TextInput(val value) -> editorNative(host, value)
30 case Pointer(InputPointerEvent.Entered(_)) -> editorInteraction(
31 host,
32 host.area.isFocusActive,
33 true
34 )
35 case Pointer(InputPointerEvent.Left(_)) -> editorInteraction(
36 host,
37 host.area.isFocusActive,
38 false
39 )
40 case Pointer(InputPointerEvent.Cancelled(_, _)) -> {
41 host.dataSource?.cancelCandidate()
42 editorStopDrag(host, false)
43 }
44 case Contact(InputContactEvent.Cancelled(_, _, _)) -> host.dataSource?.cancelCandidate()
45 else -> {}
46 }
47 return
48 }
49 if (context.phase != RoutedPhase.Default || !host.options.enabled)
50 return
51 when (event) {
52 case Contact(InputContactEvent.Began(_)) -> host.revealNeeded = false
53 case Pointer(
54 InputPointerEvent.Button(val pointer, val button, ElementState.Pressed, _)
55 ) -> {
56 if (isContextMenuClick(button, pointer.state.modifiers)) {
57 if (editorContextMenu(host, Some(pointer.state), context.localPosition))
58 context.consume()
59 return
60 }
61 if (button == MouseButton.Middle && editorPastePrimary(host, context.localPosition)) {
62 context.consume()
63 return
64 }
65 }
66 case Keyboard(val key) -> {
67 if (textMenuKey(key) && host.showContextMenu()) {
68 context.consume()
69 return
70 }
71 }
72 else -> {}
73 }
74 if (host.dataSource?.handle(context, event) ?: false) {
75 context.consume()
76 return
77 }
78 when (event) {
79 case TextCommand(val command) -> if (host.execute(command))
80 context.consume()
81 case Pointer(
82 InputPointerEvent.Button(val pointer, MouseButton.Left, val state, val count)
83 ) -> {
84 if (state == ElementState.Pressed)
85 editorPress(host, pointer, context.localPosition, count)
86 else
87 editorStopDrag(host, true)
88 context.consume()
89 }
90 case Pointer(InputPointerEvent.Moved(val pointer)) -> {
91 if (editorDragging(host)) {
92 editorDragMove(host, pointer)
93 context.consume()
94 }
95 }
96 case Pointer(InputPointerEvent.Wheel(_, _, val phase, val momentum)) -> {
97 host.revealNeeded = false
98 host.viewport.consumeScroll(
99 context,
100 host.appearance.textStyle.fontSize,
101 editorTouchAxes(host.options),
102 phase = phase,
103 momentumPhase = momentum
104 )
105 }
106 case Keyboard(val key) -> {
107 if (!host.area.isFocusActive || key.state != ElementState.Pressed || key.synthetic)
108 return
109 var handled = false
110 if (key.textCommands.size > 0L) {
111 for (command in key.textCommands) {
112 if (
113 host.closed ||
114 !host.options.enabled ||
115 !host.area.isFocusActive ||
116 !host.area.scope.isInputAllowed
117 )
118 break
119 if (editorCommand(host, command, Some(key)))
120 handled = true
121 }
122 }
123 if (handled) {
124 editorStopDrag(host, false)
125 context.consume()
126 }
127 }
128 else -> {}
129 }
132private fun editorFocused(host: TextEditHost, active: Boolean) {
133 if (active) {
134 host.current.attachHost(host)
135 host.current.breakUndoGroup()
136 host.invalidate(cancelContacts = false)
137 host.publishNative()
138 } else {
139 host.keyboard.deactivate()
140 host.dataSource?.cancelCandidate()
141 host.contextMenu.cancel()
142 host.retireConnection()
143 editorStopDrag(host, false)
144 host.blink.cancel()
145 host.revealNeeded = false
146 if (host.current.detachHost(host)) {
147 host.current.breakUndoGroup()
148 host.perform({ host.current.cancelComposition() })
149 }
150 }
151 editorInteraction(host, active, host.visual.value.hovered)
154private fun editorNative(host: TextEditHost, event: TextInputEvent) {
155 when (event) {
156 case Enabled(val connection) -> {
157 host.connection = connection
158 host.sessionEnabled = true
159 host.current.inputStarter?.take(host, connection)
160 host.publishNative()
161 }
162 case Disabled(val connection) -> {
163 if (host.connection != None && host.connection != connection)
164 return
165 host.connection = None
166 host.keyboard.connectionClosed()
167 host.sessionEnabled = false
168 host.retiring = false
169 if (host.current.ownsHost(host))
170 host.perform({ host.current.cancelComposition() })
171 }
172 case Edit(val batch) -> {
173 if (
174 !host.sessionEnabled ||
175 host.retiring ||
176 !host.area.isFocusActive ||
177 !host.options.enabled ||
178 host.options.readOnly
179 )
180 return
181 when (batch.origin) {
182 case Some(val origin) -> if (host.connection != Some(origin.connection))
183 return
184 case None -> {}
185 }
186 editorStopDrag(host, false)
187 var invalid = false
188 val raw =
189 try {
190 prepareEditorInput(host.current, batch)
191 } catch (failure: IllegalArgumentException) {
192 invalid = true
193 None
194 } catch (failure: IllegalStateException) {
195 invalid = true
196 None
197 }
198 if (invalid) {
199 host.retireConnection()
200 host.perform({ host.current.cancelComposition() })
201 return
202 }
203 val before = host.current.text
204 val beforeCommitted = host.current.committedText
205 host.performing = true
206 val changed =
207 try {
208 when (raw) {
209 case Some(val prepared) -> finishEditorInput(
210 prepared,
211 host.options.policy,
212 TextEditSource.NativeInput,
213 !host.options.multiline,
214 replacements = editorNativeReplacements(host, prepared)
215 ).publish(host.current, true)
216 case None -> false
217 }
218 } finally {
219 host.performing = false
220 }
221 if (changed)
222 host.invalidate()
223 host.refreshMenuCommands()
224 host.publishNative(batch.origin)
225 host.notifyEdited(before, beforeCommitted)
226 }
227 }
230internal fun editorFinish(host: TextEditHost, event: InputEvent) {
231 if (host.closed)
232 return
233 when (event) {
234 case Pointer(
235 InputPointerEvent.Button(_, MouseButton.Left, ElementState.Released, _)
236 ) -> editorStopDrag(host, true)
237 else -> {}
238 }