Scoop API

scoop:scoop.window:0.1.0

src/menu/text_template.scoop

Selected production source · src/menu/text_template.scoop

1package scoop.window
3/** Explicit application translations override corresponding platform titles. */
4public struct TextContextMenuLabels(
5 val undo: String? = None,
6 val redo: String? = None,
7 val cut: String? = None,
8 val copy: String? = None,
9 val paste: String? = None,
10 val deleteSelection: String? = None,
11 val selectAll: String? = None,
12 val lookUp: String? = None,
13 val services: String? = None
16public enum TextContextMenuSource {
17 Standard, SystemTemplate
20/** A display description with no native ownership or implicit edit target. */
21public enum TextContextMenuEntry {
22 Command(val command: TextEditCommand, val label: String, val shortcut: ShortcutChord?),
23 Separator
26public class TextContextMenuTemplate internal constructor(
27 public val entries: Array<TextContextMenuEntry>,
28 public val source: TextContextMenuSource,
29 public val selectWordOutsideSelection: Boolean
32/** Reads a platform template without creating an editor, menu session or text document. */
33public fun ActiveApplication.textContextMenuTemplate(
34 preferSystem: Boolean = true,
35 labels: TextContextMenuLabels? = None
36): TextContextMenuTemplate {
37 owner.requireRunning("read text context menu template")
38 val template = platformTextContextMenuTemplate(preferSystem)
39 val overrides = labels ?: return template
40 val entries =
41 Array<TextContextMenuEntry>(template.entries.size) {
42 index -> when (val entry = template.entries[index]) {
43 case Command(val command, val label, val shortcut) -> TextContextMenuEntry.Command(
44 command,
45 textMenuLabel(command, overrides) ?: label,
46 shortcut
47 )
48 case Separator -> TextContextMenuEntry.Separator
49 }
50 }
51 return TextContextMenuTemplate(entries, template.source, template.selectWordOutsideSelection)
54/** Preserves the physical button event while recognizing the platform's auxiliary click. */
55public fun isContextMenuClick(button: MouseButton, modifiers: Modifiers): Boolean =
56 button ==
57 MouseButton.Right ||
58 (
59 platformPrimaryModifier() ==
60 PrimaryModifier.Super &&
61 button ==
62 MouseButton.Left &&
63 modifiers.control &&
64 !modifiers.alt &&
65 !modifiers.superKey
66 )
68internal fun standardTextContextMenuTemplate(redoWithY: Boolean): TextContextMenuTemplate {
69 val roles = [8, 9, 0, 4, 5, 6, 10, 0, 7]
70 val labels = ["Undo", "Redo", "", "Cut", "Copy", "Paste", "Delete", "", "Select All"]
71 return TextContextMenuTemplate(
72 Array<TextContextMenuEntry>(roles.size) {
73 index -> if (roles[index] == 0)
74 TextContextMenuEntry.Separator
75 else {
76 val command = menuTextCommand(roles[index])
77 TextContextMenuEntry.Command(
78 command,
79 labels[index],
80 textMenuShortcut(command, redoWithY)
81 )
82 }
83 },
84 TextContextMenuSource.Standard,
85 false
86 )
89internal fun textMenuShortcut(command: TextEditCommand, redoWithY: Boolean): ShortcutChord? {
90 val key =
91 when (command) {
92 case Undo -> "z"
93 case Redo -> if (redoWithY)
94 "y"
95 else
96 "z"
97 case Cut -> "x"
98 case Copy -> "c"
99 case Paste -> "v"
100 case SelectAll -> "a"
101 else -> return None
102 }
103 return Some(
104 ShortcutChord(
105 ShortcutKey.Logical(LogicalKey.Character(key)),
106 ShortcutModifiers(primary = true, shift = command == TextEditCommand.Redo && !redoWithY)
107 )
108 )
111private fun textMenuLabel(command: TextEditCommand, labels: TextContextMenuLabels): String? =
112 when (command) {
113 case Undo -> labels.undo
114 case Redo -> labels.redo
115 case Cut -> labels.cut
116 case Copy -> labels.copy
117 case Paste -> labels.paste
118 case DeleteSelection -> labels.deleteSelection
119 case SelectAll -> labels.selectAll
120 else -> None
121 }