Scoop API

scoop:scoop.ui:0.1.0

platform/desktop/text/services.scoop

Selected production source · platform/desktop/text/services.scoop

1package scoop.ui.text
3import scoop.graphics.Rect
4import scoop.graphics.text.TextRange
5import scoop.ui.*
6import scoop.window.*
8internal class TextMenuServiceAction(
9 val label: String,
10 val valid: () -> Boolean,
11 val perform: (String?) -> Unit,
12 val kind: MenuItemKind = MenuItemKind.Action
15internal fun editorSystemMenuActions(host: TextEditHost): Array<TextMenuServiceAction> {
16 val options = host.options.contextMenu
17 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.isComposing
25 )
26 return []
27 val document = host.current
28 val revision = document.revision
29 val selection = document.selection
30 val editable = !host.options.readOnly
31 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 Unit
56 }
57 val actions = ArrayList<TextMenuServiceAction>()
58 val services = host.scope.application.textServices
59 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.end
68 )
69 if (!textCheckTouches(range, selection.range))
70 continue
71 for (suggestion in finding.suggestions) {
72 val label =
73 if (suggestion.byteLength == 0L)
74 labels.deleteSelection ?: "Delete"
75 else
76 suggestion
77 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 else
101 None
102 },
103 if (editable)
104 Some({ value -> replace(selection.range, value, TextEditSource.Paste) })
105 else
106 None
107 )
108 return actions.toArray()
111internal fun selectableSystemMenuActions(host: SelectableTextHost): Array<TextMenuServiceAction> {
112 val options = host.options.contextMenu
113 if (
114 !options.systemServices ||
115 options.mode !=
116 TextContextMenuMode.PreferSystem ||
117 !host.options.copy
118 )
119 return []
120 val content = host.content
121 val selection = host.selection
122 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 else
149 None
150 },
151 None
152 )
153 return actions.toArray()
156private 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 return
167 val services = scope.application.textServices
168 val capabilities = services.capabilities
169 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 Unit
181 }
182 case None -> {}
183 }
184 }
185 )
186 )
187 if (!capabilities.services)
188 return
189 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(_) -> true
208 case None -> false
209 }
210 )
211 )
212 )
215private fun textServiceAnchor(rect: Rect): LogicalRect =
216 LogicalRect(LogicalPosition(rect.x, rect.y), LogicalSize(rect.width, rect.height))
218private fun editorServiceAnchor(host: TextEditHost): LogicalRect? {
219 val frame = host.accepted ?: return None
220 val bounds = host.area.visibleBounds ?: return None
221 if (frame.display.source.text != host.current.text)
222 return None
223 val rect = editorMenuAnchor(host, frame, bounds) ?: return None
224 return Some(textServiceAnchor(rect))
227private fun selectableServiceAnchor(host: SelectableTextHost): LogicalRect? {
228 val frame = host.inputFrame() ?: return None
229 val bounds = host.area.visibleBounds ?: return None
230 val rect = selectableMenuAnchor(host, frame, bounds) ?: return None
231 return Some(textServiceAnchor(rect))
234internal fun openEditorTextMenu(host: TextEditHost, template: TextContextMenuTemplate, placement: PopupPlacement): Boolean =
235 host.contextMenu.open(template, placement, editorSystemMenuActions(host))
237internal fun openSelectableTextMenu(host: SelectableTextHost, template: TextContextMenuTemplate, placement: PopupPlacement): Boolean =
238 host.contextMenu.open(template, placement, selectableSystemMenuActions(host))