scoop:scoop.ui:0.1.0
platform/desktop/text/services.scoop
Selected production source · platform/desktop/text/services.scoop
1
package scoop.ui.text3
import scoop.graphics.Rect4
import scoop.graphics.text.TextRange5
import scoop.ui.*6
import scoop.window.*8
internal class TextMenuServiceAction(9
val label: String,10
val valid: () -> Boolean,11
val perform: (String?) -> Unit,12
val kind: MenuItemKind = MenuItemKind.Action13
)15
internal fun editorSystemMenuActions(host: TextEditHost): Array<TextMenuServiceAction> {16
val options = host.options.contextMenu17
if (18
!options.systemServices ||19
options.mode !=20
TextContextMenuMode.PreferSystem ||21
!host.copyAllowed ||22
host.options.protectedText ||23
host.options.projection.masked ||24
host.current.isComposing25
)26
return []27
val document = host.current28
val revision = document.revision29
val selection = document.selection30
val editable = !host.options.readOnly31
val valid =32
{33
!host.closed &&34
host.scope.isMounted &&35
host.current ===36
document &&37
document.revision ==38
revision &&39
document.selection ==40
selection &&41
host.options.readOnly ==42
!editable &&43
host.options.contextMenu ==44
options &&45
host.copyAllowed &&46
!host.options.protectedText &&47
!host.options.projection.masked &&48
host.contextMenu.eligible()49
}50
val replace =51
{52
range: Utf8Range, text: String, source: TextEditSource ->53
if (valid() && editable && !host.options.readOnly)54
host.perform({ editorApplyEdit(host, range, text, source) }, endComposition = true)55
Unit56
}57
val actions = ArrayList<TextMenuServiceAction>()58
val services = host.scope.application.textServices59
val labels = options.labels ?: TextContextMenuLabels()60
if (editable) {61
val context = textCheckingContext(document.text, selection.range)62
try {63
for (finding in services.checkText(context.text)) {64
val range =65
Utf8Range(66
context.start + finding.range.start,67
context.start + finding.range.end68
)69
if (!textCheckTouches(range, selection.range))70
continue71
for (suggestion in finding.suggestions) {72
val label =73
if (suggestion.byteLength == 0L)74
labels.deleteSelection ?: "Delete"75
else76
suggestion77
actions.add(78
TextMenuServiceAction(79
label,80
{ valid() && !host.options.readOnly },81
{ _ -> replace(range, suggestion, TextEditSource.EditCommand) }82
)83
)84
}85
}86
} catch (_: WindowException) {87
// Standard editing remains available when the native checker declines.88
}89
}90
val selected = document.text.utf8Slice(selection.range)91
appendReadTextServices(92
actions,93
host.scope,94
selected,95
options,96
valid,97
{98
if (valid())99
editorServiceAnchor(host)100
else101
None102
},103
if (editable)104
Some({ value -> replace(selection.range, value, TextEditSource.Paste) })105
else106
None107
)108
return actions.toArray()109
}111
internal fun selectableSystemMenuActions(host: SelectableTextHost): Array<TextMenuServiceAction> {112
val options = host.options.contextMenu113
if (114
!options.systemServices ||115
options.mode !=116
TextContextMenuMode.PreferSystem ||117
!host.options.copy118
)119
return []120
val content = host.content121
val selection = host.selection122
val text = content.toPlainText(TextRange(selection.range.start, selection.range.end))123
val valid =124
{125
!host.closed &&126
host.scope.isMounted &&127
host.options.copy &&128
host.options.contextMenu ==129
options &&130
host.selection ==131
selection &&132
host.content.text ==133
content.text &&134
host.content.toPlainText() ==135
content.toPlainText() &&136
host.contextMenu.eligible()137
}138
val actions = ArrayList<TextMenuServiceAction>()139
appendReadTextServices(140
actions,141
host.scope,142
text,143
options,144
valid,145
{146
if (valid())147
selectableServiceAnchor(host)148
else149
None150
},151
None152
)153
return actions.toArray()154
}156
private fun appendReadTextServices(157
actions: ArrayList<TextMenuServiceAction>,158
scope: ComponentScope,159
text: String,160
options: TextContextMenuOptions,161
valid: () -> Boolean,162
anchor: () -> LogicalRect?,163
replace: ((String) -> Unit)?164
) {165
if (text.byteLength == 0L)166
return167
val services = scope.application.textServices168
val capabilities = services.capabilities169
val labels = options.labels ?: TextContextMenuLabels()170
if (capabilities.dictionary)171
actions.add(172
TextMenuServiceAction(173
labels.lookUp ?: "Look Up",174
valid,175
{176
_ -> if (valid())177
when (val rect = anchor()) {178
case Some(val value) -> {179
scope.nativeWindow.showTextDefinition(text, value);180
Unit181
}182
case None -> {}183
}184
}185
)186
)187
if (!capabilities.services)188
return189
actions.add(190
TextMenuServiceAction(191
labels.services ?: "Services",192
valid,193
{194
result -> if (valid())195
when (replace) {196
case Some(val apply) -> when (result) {197
case Some(val value) -> if (value != text)198
apply(value)199
case None -> {}200
}201
case None -> {}202
}203
},204
MenuItemKind.TextServices(205
text,206
when (replace) {207
case Some(_) -> true208
case None -> false209
}210
)211
)212
)213
}215
private fun textServiceAnchor(rect: Rect): LogicalRect =216
LogicalRect(LogicalPosition(rect.x, rect.y), LogicalSize(rect.width, rect.height))218
private fun editorServiceAnchor(host: TextEditHost): LogicalRect? {219
val frame = host.accepted ?: return None220
val bounds = host.area.visibleBounds ?: return None221
if (frame.display.source.text != host.current.text)222
return None223
val rect = editorMenuAnchor(host, frame, bounds) ?: return None224
return Some(textServiceAnchor(rect))225
}227
private fun selectableServiceAnchor(host: SelectableTextHost): LogicalRect? {228
val frame = host.inputFrame() ?: return None229
val bounds = host.area.visibleBounds ?: return None230
val rect = selectableMenuAnchor(host, frame, bounds) ?: return None231
return Some(textServiceAnchor(rect))232
}234
internal fun openEditorTextMenu(host: TextEditHost, template: TextContextMenuTemplate, placement: PopupPlacement): Boolean =235
host.contextMenu.open(template, placement, editorSystemMenuActions(host))237
internal fun openSelectableTextMenu(host: SelectableTextHost, template: TextContextMenuTemplate, placement: PopupPlacement): Boolean =238
host.contextMenu.open(template, placement, selectableSystemMenuActions(host))