scoop:scoop.ui:0.1.0
src/text/editor/events.scoop
Selected production source · src/text/editor/events.scoop
1
package scoop.ui.text3
import scoop.graphics.*4
import scoop.surface.*5
import scoop.ui.*6
import scoop.window.*8
internal 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.enabled14
)15
host.publishInteraction(next)16
}18
internal fun editorHandle(host: TextEditHost, context: RoutedEventContext, event: InputEvent) {19
if (host.closed)20
return21
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.hovered28
)29
case TextInput(val value) -> editorNative(host, value)30
case Pointer(InputPointerEvent.Entered(_)) -> editorInteraction(31
host,32
host.area.isFocusActive,33
true34
)35
case Pointer(InputPointerEvent.Left(_)) -> editorInteraction(36
host,37
host.area.isFocusActive,38
false39
)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
return48
}49
if (context.phase != RoutedPhase.Default || !host.options.enabled)50
return51
when (event) {52
case Contact(InputContactEvent.Began(_)) -> host.revealNeeded = false53
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
return60
}61
if (button == MouseButton.Middle && editorPastePrimary(host, context.localPosition)) {62
context.consume()63
return64
}65
}66
case Keyboard(val key) -> {67
if (textMenuKey(key) && host.showContextMenu()) {68
context.consume()69
return70
}71
}72
else -> {}73
}74
if (host.dataSource?.handle(context, event) ?: false) {75
context.consume()76
return77
}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
else87
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 = false98
host.viewport.consumeScroll(99
context,100
host.appearance.textStyle.fontSize,101
editorTouchAxes(host.options),102
phase = phase,103
momentumPhase = momentum104
)105
}106
case Keyboard(val key) -> {107
if (!host.area.isFocusActive || key.state != ElementState.Pressed || key.synthetic)108
return109
var handled = false110
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.isInputAllowed117
)118
break119
if (editorCommand(host, command, Some(key)))120
handled = true121
}122
}123
if (handled) {124
editorStopDrag(host, false)125
context.consume()126
}127
}128
else -> {}129
}130
}132
private 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 = false146
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)152
}154
private fun editorNative(host: TextEditHost, event: TextInputEvent) {155
when (event) {156
case Enabled(val connection) -> {157
host.connection = connection158
host.sessionEnabled = true159
host.current.inputStarter?.take(host, connection)160
host.publishNative()161
}162
case Disabled(val connection) -> {163
if (host.connection != None && host.connection != connection)164
return165
host.connection = None166
host.keyboard.connectionClosed()167
host.sessionEnabled = false168
host.retiring = false169
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.readOnly179
)180
return181
when (batch.origin) {182
case Some(val origin) -> if (host.connection != Some(origin.connection))183
return184
case None -> {}185
}186
editorStopDrag(host, false)187
var invalid = false188
val raw =189
try {190
prepareEditorInput(host.current, batch)191
} catch (failure: IllegalArgumentException) {192
invalid = true193
None194
} catch (failure: IllegalStateException) {195
invalid = true196
None197
}198
if (invalid) {199
host.retireConnection()200
host.perform({ host.current.cancelComposition() })201
return202
}203
val before = host.current.text204
val beforeCommitted = host.current.committedText205
host.performing = true206
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 -> false217
}218
} finally {219
host.performing = false220
}221
if (changed)222
host.invalidate()223
host.refreshMenuCommands()224
host.publishNative(batch.origin)225
host.notifyEdited(before, beforeCommitted)226
}227
}228
}230
internal fun editorFinish(host: TextEditHost, event: InputEvent) {231
if (host.closed)232
return233
when (event) {234
case Pointer(235
InputPointerEvent.Button(_, MouseButton.Left, ElementState.Released, _)236
) -> editorStopDrag(host, true)237
else -> {}238
}239
}