Scoop API

scoop:scoop.ui:0.1.0

src/text/editor/drag_edit.scoop

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

1package scoop.ui.text
3import scoop.graphics.text.*
4import scoop.ui.*
5import scoop.window.*
7/** One prepared source, passed by reference only to destinations in the same process. */
8internal class EditorTextTransfer(
9 val editor: TextEditorState,
10 val range: Utf8Range,
11 val effects: DragEffects,
12 private val sourceReady: () -> Boolean
13) {
14 private val revision = editor.contentRevision
15 val text = editor.text.utf8Slice(range)
16 var moved = false
17 val canMove: Boolean get() =
18 !moved && editor.contentRevision == revision && !editor.isComposing && sourceReady()
21internal fun prepareEditorMove(
22 source: EditorTextTransfer,
23 position: Long,
24 policy: TextEditPolicy,
25 singleLine: Boolean = false
26): EditorPreparedEdit? {
27 if (!source.canMove)
28 return None
29 val editor = source.editor
30 requireUi(editorScalar(editor.text.toByteArray(), position), "invalid text drop position")
31 val range = source.range
32 if (position >= range.start && position <= range.end)
33 return Some(
34 EditorPreparedEdit(
35 editor.storage.value,
36 None,
37 EditorHistoryChange.Append([], false, false),
38 true
39 )
40 )
41 val destination =
42 if (position > range.end)
43 position - (range.end - range.start)
44 else
45 position
46 val raw =
47 prepareEditorInput(
48 editor,
49 TextInputBatch(
50 Some(editor.revision),
51 [
52 TextInputOperation.Commit("", TextReplacement.Range(range)),
53 TextInputOperation.Commit(
54 source.text,
55 TextReplacement.Range(Utf8Range(destination, destination))
56 )
57 ]
58 )
59 )!!
60 val edit =
61 finishEditorInput(
62 raw,
63 policy,
64 TextEditSource.DragDrop,
65 singleLine,
66 EditorEditKind.Separate,
67 Some(
68 EditorSelection(
69 TextPosition(destination),
70 TextPosition(destination + source.text.byteLength)
71 )
72 )
73 )
74 return completeEditorTransfer(raw, edit)
77/** Removal applies command policy once; a transformation cannot delete unrelated content. */
78internal fun prepareEditorDragRemoval(
79 source: EditorTextTransfer,
80 policy: TextEditPolicy,
81 singleLine: Boolean = false
82): EditorPreparedEdit? {
83 if (!source.canMove)
84 return None
85 val editor = source.editor
86 val raw =
87 prepareEditorInput(
88 editor,
89 TextInputBatch(
90 Some(editor.revision),
91 [TextInputOperation.Commit("", TextReplacement.Range(source.range))]
92 )
93 )!!
94 return completeEditorTransfer(
95 raw,
96 finishEditorInput(
97 raw,
98 policy,
99 TextEditSource.EditCommand,
100 singleLine,
101 EditorEditKind.Separate
102 )
103 )
106internal fun completeEditorTransfer(
107 raw: EditorPreparedInput,
108 edit: EditorPreparedEdit
109): EditorPreparedEdit? =
110 if (edit.accepted && edit.value.input.surroundingText == raw.current.input.surroundingText)
111 Some(edit)
112 else
113 None