scoop:scoop.ui:0.1.0
src/text/editor/commands.scoop
Selected production source · src/text/editor/commands.scoop
1
package scoop.ui.text3
import scoop.window.*4
import scoop.graphics.text.*6
internal fun editorCommand(host: TextEditHost, command: TextEditCommand, key: KeyEvent?): Boolean {7
val state = host.current8
when (command) {9
case Move(val movement, val extend) -> {10
val result =11
editorProjectedMove(12
editorInputText(host),13
state.storage.value.endpoints,14
movement,15
extend,16
host.preferredX,17
host.scroll.snapshot.visibleSize.height18
)19
host.perform({ editorSetSelection(host, result.selection) }, endComposition = true)20
host.invalidate()21
host.preferredX = result.preferredX22
}23
case Delete(val deletion) -> {24
if (host.options.readOnly)25
return true26
val range = editorProjectedDelete(editorInputText(host), state.selection, deletion)27
val kind =28
when (deletion) {29
case Backward -> EditorEditKind.Backspace30
case Forward -> EditorEditKind.Delete31
else -> EditorEditKind.Separate32
}33
host.perform(34
{ editorApplyEdit(host, range, "", TextEditSource.EditCommand, kind) },35
endComposition = true36
)37
}38
case DeleteSelection -> {39
if (host.options.readOnly)40
return true41
when (42
prepareEditorSelectionRemoval(state, host.options.policy, !host.options.multiline)43
) {44
case Some(val prepared) -> host.perform(45
{ prepared.publish(state, false) },46
endComposition = true47
)48
case None -> {}49
}50
}51
case Copy -> {52
if (!host.copyAllowed)53
return true54
val range = state.selection.range55
if (range.start != range.end)56
host.window.clipboard.writeText(state.text.utf8Slice(range))57
}58
case Cut -> {59
if (host.options.readOnly || !host.copyAllowed)60
return true61
when (62
prepareEditorSelectionRemoval(state, host.options.policy, !host.options.multiline)63
) {64
case Some(val prepared) -> if (prepared.accepted) {65
host.window.clipboard.writeText(state.text.utf8Slice(state.selection.range))66
host.perform({ prepared.publish(state, false) }, endComposition = true)67
}68
case None -> {}69
}70
}71
case Paste -> {72
if (host.options.readOnly)73
return true74
when (host.window.clipboard.readText()) {75
case Some(val value) -> {76
host.perform(77
{78
editorApplyEdit(79
host,80
state.selection.range,81
value,82
TextEditSource.Paste83
)84
},85
endComposition = true86
)87
}88
case None -> {}89
}90
}91
case SelectAll -> host.perform(92
{93
editorSetSelection(94
host,95
EditorSelection(TextPosition(0L), TextPosition(state.text.byteLength))96
)97
},98
endComposition = true99
)100
case Undo -> {101
if (!host.options.readOnly)102
host.perform({ state.undo() }, endComposition = true)103
}104
case Redo -> {105
if (!host.options.readOnly)106
host.perform({ state.redo() }, endComposition = true)107
}108
case InsertNewline -> {109
if (editorSubmits(host.options, key)) {110
if (!(key?.isRepeat ?: false)) {111
state.breakUndoGroup()112
host.submitted(state)113
}114
} else115
if (host.options.multiline && !host.options.readOnly)116
host.perform(117
{118
editorApplyEdit(119
host,120
state.selection.range,121
"\n",122
TextEditSource.EditCommand123
)124
},125
endComposition = true126
)127
}128
case InsertTab -> {129
if (host.options.tab == TextTabBehavior.Navigate)130
return false131
if (!host.options.readOnly)132
host.perform(133
{134
editorApplyEdit(135
host,136
state.selection.range,137
"\t",138
TextEditSource.EditCommand139
)140
},141
endComposition = true142
)143
}144
case InsertBacktab -> return false145
case Cancel -> {146
if (!state.isComposing)147
return false148
host.perform({ state.cancelComposition() }, endComposition = true)149
}150
}151
return true152
}154
private fun editorSubmits(options: TextEditOptions, key: KeyEvent?): Boolean {155
val event = key ?: return options.submit == TextSubmitKeys.Enter156
val modifiers =157
when (options.submit) {158
case None -> return false159
case Enter -> ShortcutModifiers()160
case PrimaryEnter -> ShortcutModifiers(primary = true)161
}162
return ShortcutChord(ShortcutKey.Logical(LogicalKey.Named(NamedKey.Enter)), modifiers).matches(163
event164
)165
}167
/** Cut and menu deletion prepare the same edit before any native side effect. */168
internal fun prepareEditorSelectionRemoval(169
state: TextEditorState,170
policy: TextEditPolicy,171
singleLine: Boolean172
): EditorPreparedEdit? {173
val range = state.selection.range174
if (range.start == range.end)175
return None176
return Some(prepareEditorEdit(state, range, "", policy, TextEditSource.EditCommand, singleLine))177
}179
internal fun editorApplyEdit(180
host: TextEditHost,181
range: Utf8Range,182
text: String,183
source: TextEditSource,184
kind: EditorEditKind = EditorEditKind.Separate185
): Boolean =186
prepareEditorEdit(187
host.current,188
range,189
text,190
host.options.policy,191
source,192
!host.options.multiline,193
kind194
).publish(host.current, false)196
internal fun editorSetSelection(host: TextEditHost, selection: EditorSelection): Boolean {197
val state = host.current198
if (!state.isComposing)199
return state.setSelection(selection.anchor, selection.cursor)200
val raw = prepareEditorInput(state, TextInputBatch(None, [TextInputOperation.ClearPreedit]))!!201
return finishEditorInput(202
raw,203
host.options.policy,204
TextEditSource.EditCommand,205
!host.options.multiline,206
EditorEditKind.Separate,207
Some(selection)208
).publish(state, false)209
}