Scoop API

scoop:scoop.ui:0.1.0

src/text/editor/semantics.scoop

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

1package scoop.ui.text
3import scoop.graphics.text.TextPosition
4import scoop.graphics.text.TextAffinity
5import scoop.ui.*
6import scoop.window.*
7import scoop.window.accessibility.*
9/** Publishes this mount's existing editor and routes native actions through its real transactions. */
10public fun SemanticsHandle.updateTextEditor(
11 host: TextEditHost,
12 properties: SemanticProperties = SemanticProperties(),
13 relations: SemanticRelations = SemanticRelations(),
14 setExpanded: ((Boolean) -> SemanticActionResult)? = None
15) {
16 checkOpen()
17 checkSemanticsMutation()
18 host.checkOpen()
19 requireUi(
20 role == SemanticRole.TextField && owner === host.focus.node,
21 "text semantics require this mount's text field and editor"
22 )
23 val model = host.current
24 val revision = model.revision
25 val protectedText = properties.protectedText || host.options.protectedText
26 val readOnly = properties.readOnly || host.options.readOnly
27 val available = { !host.closed && host.current === model && model.revision == revision }
28 val writable =
29 {
30 host.options.enabled &&
31 !readOnly &&
32 !host.options.readOnly &&
33 host.area.scope.isInputAllowed
34 }
35 val select: ((Utf8Selection) -> SemanticActionResult)? =
36 if (protectedText)
37 None
38 else
39 Some {
40 selection -> if (!available())
41 SemanticActionResult.Unavailable
42 else
43 if (host.options.protectedText)
44 SemanticActionResult.Unsupported
45 else
46 if (!host.options.enabled || !host.area.scope.isInputAllowed)
47 SemanticActionResult.Rejected
48 else
49 semanticEditorSelect(host, selection)
50 }
51 val replace: ((Utf8Range, String) -> SemanticActionResult)? =
52 if (protectedText || readOnly)
53 None
54 else
55 Some {
56 range,
57 text -> if (!available())
58 SemanticActionResult.Unavailable
59 else
60 if (host.options.protectedText)
61 SemanticActionResult.Unsupported
62 else
63 if (!writable())
64 SemanticActionResult.Rejected
65 else
66 semanticEditorReplace(host, range, text)
67 }
68 val setValue: ((String) -> SemanticActionResult)? =
69 if (readOnly)
70 None
71 else
72 Some {
73 text -> if (!available())
74 SemanticActionResult.Unavailable
75 else
76 if (!writable())
77 SemanticActionResult.Rejected
78 else
79 semanticEditorReplace(host, Utf8Range(0L, model.text.byteLength), text)
80 }
81 val showContextMenu: (() -> SemanticActionResult)? =
82 if (host.options.contextMenu.mode == TextContextMenuMode.Disabled)
83 None
84 else
85 Some {
86 if (!available())
87 SemanticActionResult.Unavailable
88 else
89 if (host.options.contextMenu.mode == TextContextMenuMode.Disabled)
90 SemanticActionResult.Unsupported
91 else
92 semanticTextMenu(host.focus, host.contextMenu) {
93 available() && host.showContextMenu()
94 }
95 }
96 bindFocus(Some(host.focus))
97 updateText(
98 if (protectedText)
99 None
100 else
101 Some(host.semantics),
102 properties.copy {
103 value =
104 if (protectedText)
105 None
106 else
107 Some(model.text)
108 selection =
109 if (protectedText)
110 None
111 else
112 Some(model.selection)
113 enabled = properties.enabled && host.options.enabled
114 this.readOnly = readOnly
115 multiline = host.options.multiline
116 this.protectedText = protectedText
117 cursorAffinity =
118 if (!protectedText && model.cursor.affinity == TextAffinity.Upstream)
119 SemanticTextAffinity.Upstream
120 else
121 SemanticTextAffinity.Downstream
122 },
123 SemanticActions(
124 setValue = setValue,
125 setSelection = select,
126 replaceText = replace,
127 showContextMenu = showContextMenu,
128 revealText =
129 if (protectedText)
130 None
131 else
132 Some {
133 range,
134 scroll -> if (!available())
135 SemanticActionResult.Unavailable
136 else
137 semanticEditorReveal(this, host, range, scroll)
138 },
139 setExpanded =
140 when (setExpanded) {
141 case Some(val change) -> Some {
142 value -> if (!available())
143 SemanticActionResult.Unavailable
144 else
145 if (!writable())
146 SemanticActionResult.Rejected
147 else
148 change(value)
149 }
150 case None -> None
151 }
152 ),
153 relations
154 )
155 if (protectedText)
156 host.retireSemanticDocument()
157 resource.textDocument =
158 if (protectedText)
159 None
160 else
161 host.semanticTextDocument()
164private fun semanticEditorReplace(
165 host: TextEditHost,
166 range: Utf8Range,
167 text: String
168): SemanticActionResult {
169 val edit =
170 prepareEditorEdit(
171 host.current,
172 range,
173 text,
174 host.options.policy,
175 TextEditSource.Accessibility,
176 !host.options.multiline
177 )
178 editorStopDrag(host, false)
179 host.perform({ edit.publish(host.current, false) }, endComposition = true)
180 return if (edit.accepted)
181 SemanticActionResult.Completed
182 else
183 SemanticActionResult.Rejected
186private fun semanticEditorSelect(host: TextEditHost, value: Utf8Selection): SemanticActionResult {
187 val model = host.current
188 val selection = EditorSelection(TextPosition(value.anchor), TextPosition(value.cursor))
189 val edit =
190 if (model.isComposing)
191 Some(
192 finishEditorInput(
193 prepareEditorInput(
194 model,
195 TextInputBatch(None, [TextInputOperation.ClearPreedit])
196 )!!,
197 host.options.policy,
198 TextEditSource.EditCommand,
199 !host.options.multiline,
200 EditorEditKind.Separate,
201 Some(selection)
202 )
203 )
204 else
205 None
206 editorStopDrag(host, false)
207 host.perform(
208 {
209 when (edit) {
210 case Some(val prepared) -> prepared.publish(model, false)
211 case None -> model.setSelection(selection.anchor, selection.cursor)
212 }
213 },
214 endComposition = true
215 )
216 return if (edit?.accepted ?: true)
217 SemanticActionResult.Completed
218 else
219 SemanticActionResult.Rejected