scoop:scoop.ui:0.1.0
src/text/editor/semantics.scoop
Selected production source · src/text/editor/semantics.scoop
1
package scoop.ui.text3
import scoop.graphics.text.TextPosition4
import scoop.graphics.text.TextAffinity5
import scoop.ui.*6
import scoop.window.*7
import scoop.window.accessibility.*9
/** Publishes this mount's existing editor and routes native actions through its real transactions. */10
public fun SemanticsHandle.updateTextEditor(11
host: TextEditHost,12
properties: SemanticProperties = SemanticProperties(),13
relations: SemanticRelations = SemanticRelations(),14
setExpanded: ((Boolean) -> SemanticActionResult)? = None15
) {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.current24
val revision = model.revision25
val protectedText = properties.protectedText || host.options.protectedText26
val readOnly = properties.readOnly || host.options.readOnly27
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.isInputAllowed34
}35
val select: ((Utf8Selection) -> SemanticActionResult)? =36
if (protectedText)37
None38
else39
Some {40
selection -> if (!available())41
SemanticActionResult.Unavailable42
else43
if (host.options.protectedText)44
SemanticActionResult.Unsupported45
else46
if (!host.options.enabled || !host.area.scope.isInputAllowed)47
SemanticActionResult.Rejected48
else49
semanticEditorSelect(host, selection)50
}51
val replace: ((Utf8Range, String) -> SemanticActionResult)? =52
if (protectedText || readOnly)53
None54
else55
Some {56
range,57
text -> if (!available())58
SemanticActionResult.Unavailable59
else60
if (host.options.protectedText)61
SemanticActionResult.Unsupported62
else63
if (!writable())64
SemanticActionResult.Rejected65
else66
semanticEditorReplace(host, range, text)67
}68
val setValue: ((String) -> SemanticActionResult)? =69
if (readOnly)70
None71
else72
Some {73
text -> if (!available())74
SemanticActionResult.Unavailable75
else76
if (!writable())77
SemanticActionResult.Rejected78
else79
semanticEditorReplace(host, Utf8Range(0L, model.text.byteLength), text)80
}81
val showContextMenu: (() -> SemanticActionResult)? =82
if (host.options.contextMenu.mode == TextContextMenuMode.Disabled)83
None84
else85
Some {86
if (!available())87
SemanticActionResult.Unavailable88
else89
if (host.options.contextMenu.mode == TextContextMenuMode.Disabled)90
SemanticActionResult.Unsupported91
else92
semanticTextMenu(host.focus, host.contextMenu) {93
available() && host.showContextMenu()94
}95
}96
bindFocus(Some(host.focus))97
updateText(98
if (protectedText)99
None100
else101
Some(host.semantics),102
properties.copy {103
value =104
if (protectedText)105
None106
else107
Some(model.text)108
selection =109
if (protectedText)110
None111
else112
Some(model.selection)113
enabled = properties.enabled && host.options.enabled114
this.readOnly = readOnly115
multiline = host.options.multiline116
this.protectedText = protectedText117
cursorAffinity =118
if (!protectedText && model.cursor.affinity == TextAffinity.Upstream)119
SemanticTextAffinity.Upstream120
else121
SemanticTextAffinity.Downstream122
},123
SemanticActions(124
setValue = setValue,125
setSelection = select,126
replaceText = replace,127
showContextMenu = showContextMenu,128
revealText =129
if (protectedText)130
None131
else132
Some {133
range,134
scroll -> if (!available())135
SemanticActionResult.Unavailable136
else137
semanticEditorReveal(this, host, range, scroll)138
},139
setExpanded =140
when (setExpanded) {141
case Some(val change) -> Some {142
value -> if (!available())143
SemanticActionResult.Unavailable144
else145
if (!writable())146
SemanticActionResult.Rejected147
else148
change(value)149
}150
case None -> None151
}152
),153
relations154
)155
if (protectedText)156
host.retireSemanticDocument()157
resource.textDocument =158
if (protectedText)159
None160
else161
host.semanticTextDocument()162
}164
private fun semanticEditorReplace(165
host: TextEditHost,166
range: Utf8Range,167
text: String168
): SemanticActionResult {169
val edit =170
prepareEditorEdit(171
host.current,172
range,173
text,174
host.options.policy,175
TextEditSource.Accessibility,176
!host.options.multiline177
)178
editorStopDrag(host, false)179
host.perform({ edit.publish(host.current, false) }, endComposition = true)180
return if (edit.accepted)181
SemanticActionResult.Completed182
else183
SemanticActionResult.Rejected184
}186
private fun semanticEditorSelect(host: TextEditHost, value: Utf8Selection): SemanticActionResult {187
val model = host.current188
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
else205
None206
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 = true215
)216
return if (edit?.accepted ?: true)217
SemanticActionResult.Completed218
else219
SemanticActionResult.Rejected220
}