Scoop API

scoop:scoop.ui:0.1.0

src/text/editor/state.scoop

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

1package scoop.ui.text
3import scoop.graphics.text.*
4import scoop.ui.*
5import scoop.window.*
6import scoop.window.accessibility.SemanticTextDocument
8/** Observable plain text and edit history, independent of any mounted editor. */
9public class TextEditorState public constructor(
10 text: String = "",
11 selection: Utf8Selection? = None
12) {
13 internal val storage = state(editorInitial(text, selection))
14 internal val history = EditorHistory()
15 internal var composition: EditorComposition? = None
16 internal var contentRevision = 0L
17 private var activeHost: TextEditHost? = None
18 internal var inputStarter: TextInputStarter? = None
19 internal val semanticDocuments = ArrayList<SemanticTextDocument>()
21 internal fun checkHost(host: TextEditHost) {
22 storage.checkMutation()
23 when (activeHost) {
24 case Some(val value) -> requireUi(
25 value === host,
26 "text state already has an active editor"
27 )
28 case None -> {}
29 }
30 }
31 internal fun attachHost(host: TextEditHost) {
32 checkHost(host)
33 inputStarter?.cancel()
34 activeHost = Some(host)
35 }
36 internal fun ownsHost(host: TextEditHost): Boolean =
37 when (activeHost) {
38 case Some(val value) -> value === host
39 case None -> false
40 }
41 internal fun detachHost(host: TextEditHost): Boolean {
42 storage.checkMutation()
43 if (!ownsHost(host))
44 return false
45 activeHost = None
46 return true
47 }
49 /** Observes the document while excluding an active composition's temporary text. */
50 public val committedText: String get() {
51 val current = storage.value
52 return (composition?.baseline ?: current).input.surroundingText
53 }
55 public val inputState: TextInputState get() = storage.value.input
56 public val text: String get() = inputState.surroundingText
57 public val selection: Utf8Selection get() = inputState.selection
58 public val markedRange: Utf8Range? get() = inputState.markedRange
59 public val revision: Long get() = inputState.revision
60 public val isComposing: Boolean get() = markedRange != None
61 public val anchor: TextPosition get() = storage.value.endpoints.anchor
62 public val cursor: TextPosition get() = storage.value.endpoints.cursor
63 public val canUndo: Boolean get() {
64 storage.value
65 return isComposing || history.undoDelta != None
66 }
67 public val canRedo: Boolean get() {
68 storage.value
69 return history.redoDelta != None
70 }
72 public fun setSelection(value: Utf8Selection): Boolean =
73 setSelection(TextPosition(value.anchor), TextPosition(value.cursor))
75 public fun setSelection(anchor: TextPosition, cursor: TextPosition): Boolean {
76 storage.checkMutation()
77 val previous = storage.value
78 val input =
79 TextInputState(
80 previous.input.surroundingText,
81 Utf8Selection(anchor.byteOffset, cursor.byteOffset)
82 )
83 input.validate()
84 val edits = endingComposition(previous)
85 return publish(
86 EditorValue(input, anchor.affinity, cursor.affinity),
87 None,
88 EditorHistoryChange.Append(edits.toArray(), true, true)
89 )
90 }
92 public fun replace(range: Utf8Range, text: String): Boolean =
93 edit(range, text, EditorEditKind.Separate)
95 internal fun edit(range: Utf8Range, text: String, kind: EditorEditKind): Boolean {
96 storage.checkMutation()
97 val previous = storage.value
98 val next =
99 EditorValue(
100 editorVersion(previous, 0L).input.apply(
101 TextInputBatch(
102 None,
103 [TextInputOperation.Commit(text, TextReplacement.Range(range))]
104 )
105 )
106 )
107 val edits = endingComposition(previous)
108 appendEditorDelta(edits, previous, next, kind)
109 val boundary = isComposing || kind == EditorEditKind.Separate
110 return publish(next, None, EditorHistoryChange.Append(edits.toArray(), boundary, boundary))
111 }
113 public fun replaceAll(text: String, selection: Utf8Selection? = None): Boolean {
114 storage.checkMutation()
115 return publish(editorInitial(text, selection), None, EditorHistoryChange.Reset)
116 }
118 public fun applyInput(
119 batch: TextInputBatch,
120 policy: TextEditPolicy = textEditPolicy(),
121 source: TextEditSource = TextEditSource.NativeInput
122 ): Boolean = applyEditorInput(this, batch, policy = policy, source = source)
124 public fun applyEdit(
125 range: Utf8Range,
126 text: String,
127 policy: TextEditPolicy,
128 source: TextEditSource = TextEditSource.Programmatic
129 ): Boolean = prepareEditorEdit(this, range, text, policy, source).publish(this, false)
131 public fun cancelComposition(): Boolean {
132 storage.checkMutation()
133 val baseline = composition?.baseline ?: return false
134 return publish(baseline, None, EditorHistoryChange.Break)
135 }
137 public fun undo(): Boolean {
138 storage.checkMutation()
139 val value = composition?.baseline ?: storage.value
140 when (history.undoDelta) {
141 case Some(val delta) -> return publish(
142 replayEditorDelta(value, delta, true),
143 None,
144 EditorHistoryChange.Undo
145 )
146 case None -> return if (isComposing)
147 publish(value, None, EditorHistoryChange.Break)
148 else
149 false
150 }
151 }
153 public fun redo(): Boolean {
154 storage.checkMutation()
155 val delta = history.redoDelta ?: return false
156 return publish(
157 replayEditorDelta(composition?.baseline ?: storage.value, delta, false),
158 None,
159 EditorHistoryChange.Redo
160 )
161 }
163 public fun breakUndoGroup() {
164 storage.checkMutation()
165 history.breakGroup()
166 }
168 private fun endingComposition(previous: EditorValue): ArrayList<EditorDelta> {
169 val edits = ArrayList<EditorDelta>()
170 when (composition) {
171 case Some(val baseline) -> appendEditorDelta(
172 edits,
173 baseline.baseline,
174 previous,
175 EditorEditKind.Separate
176 )
177 case None -> {}
178 }
179 return edits
180 }
182 /** Preparation validates first; publication reuses the transaction's mutation guard. */
183 internal fun publish(
184 next: EditorValue,
185 baseline: EditorComposition?,
186 change: EditorHistoryChange,
187 forceRevision: Boolean = false,
188 mapping: EditorOffsetMap? = None
189 ): Boolean {
190 val previous = storage.value
191 val a = previous.input
192 val b = next.input
193 activeHost?.checkDocument((baseline?.baseline ?: next).input.surroundingText)
194 val revise =
195 forceRevision ||
196 a.surroundingText !=
197 b.surroundingText ||
198 a.selection !=
199 b.selection ||
200 a.markedRange !=
201 b.markedRange
202 requireUi(!revise || a.revision < 9223372036854775807L, "text revision overflow")
203 val value =
204 editorVersion(
205 next,
206 a.revision +
207 if (revise)
208 1L
209 else
210 0L
211 )
212 val compositionChanged =
213 when (baseline) {
214 case Some(val nextComposition) -> when (composition) {
215 case Some(val previousComposition) -> nextComposition !== previousComposition
216 case None -> true
217 }
218 case None -> when (composition) {
219 case Some(_) -> true
220 case None -> false
221 }
222 }
223 val changed = value != previous || compositionChanged
224 mapEditorSemanticDocuments(this, previous, value, change, mapping)
225 val changedHistory = history.apply(change)
226 composition = baseline
227 if (a.surroundingText != b.surroundingText)
228 contentRevision = value.input.revision
229 if (changed || changedHistory)
230 storage.publish(value)
231 if (changed) {
232 activeHost?.modelChanged()
233 inputStarter?.modelChanged()
234 }
235 return changed || changedHistory
236 }