Scoop API

scoop:scoop.ui:0.1.0

src/text/editor/commands.scoop

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

1package scoop.ui.text
3import scoop.window.*
4import scoop.graphics.text.*
6internal fun editorCommand(host: TextEditHost, command: TextEditCommand, key: KeyEvent?): Boolean {
7 val state = host.current
8 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.height
18 )
19 host.perform({ editorSetSelection(host, result.selection) }, endComposition = true)
20 host.invalidate()
21 host.preferredX = result.preferredX
22 }
23 case Delete(val deletion) -> {
24 if (host.options.readOnly)
25 return true
26 val range = editorProjectedDelete(editorInputText(host), state.selection, deletion)
27 val kind =
28 when (deletion) {
29 case Backward -> EditorEditKind.Backspace
30 case Forward -> EditorEditKind.Delete
31 else -> EditorEditKind.Separate
32 }
33 host.perform(
34 { editorApplyEdit(host, range, "", TextEditSource.EditCommand, kind) },
35 endComposition = true
36 )
37 }
38 case DeleteSelection -> {
39 if (host.options.readOnly)
40 return true
41 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 = true
47 )
48 case None -> {}
49 }
50 }
51 case Copy -> {
52 if (!host.copyAllowed)
53 return true
54 val range = state.selection.range
55 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 true
61 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 true
74 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.Paste
83 )
84 },
85 endComposition = true
86 )
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 = true
99 )
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 } else
115 if (host.options.multiline && !host.options.readOnly)
116 host.perform(
117 {
118 editorApplyEdit(
119 host,
120 state.selection.range,
121 "\n",
122 TextEditSource.EditCommand
123 )
124 },
125 endComposition = true
126 )
127 }
128 case InsertTab -> {
129 if (host.options.tab == TextTabBehavior.Navigate)
130 return false
131 if (!host.options.readOnly)
132 host.perform(
133 {
134 editorApplyEdit(
135 host,
136 state.selection.range,
137 "\t",
138 TextEditSource.EditCommand
139 )
140 },
141 endComposition = true
142 )
143 }
144 case InsertBacktab -> return false
145 case Cancel -> {
146 if (!state.isComposing)
147 return false
148 host.perform({ state.cancelComposition() }, endComposition = true)
149 }
150 }
151 return true
154private fun editorSubmits(options: TextEditOptions, key: KeyEvent?): Boolean {
155 val event = key ?: return options.submit == TextSubmitKeys.Enter
156 val modifiers =
157 when (options.submit) {
158 case None -> return false
159 case Enter -> ShortcutModifiers()
160 case PrimaryEnter -> ShortcutModifiers(primary = true)
161 }
162 return ShortcutChord(ShortcutKey.Logical(LogicalKey.Named(NamedKey.Enter)), modifiers).matches(
163 event
164 )
167/** Cut and menu deletion prepare the same edit before any native side effect. */
168internal fun prepareEditorSelectionRemoval(
169 state: TextEditorState,
170 policy: TextEditPolicy,
171 singleLine: Boolean
172): EditorPreparedEdit? {
173 val range = state.selection.range
174 if (range.start == range.end)
175 return None
176 return Some(prepareEditorEdit(state, range, "", policy, TextEditSource.EditCommand, singleLine))
179internal fun editorApplyEdit(
180 host: TextEditHost,
181 range: Utf8Range,
182 text: String,
183 source: TextEditSource,
184 kind: EditorEditKind = EditorEditKind.Separate
185): Boolean =
186 prepareEditorEdit(
187 host.current,
188 range,
189 text,
190 host.options.policy,
191 source,
192 !host.options.multiline,
193 kind
194 ).publish(host.current, false)
196internal fun editorSetSelection(host: TextEditHost, selection: EditorSelection): Boolean {
197 val state = host.current
198 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)