scoop:scoop.window:0.1.0
src/menu/text_template.scoop
Selected production source · src/menu/text_template.scoop
1
package scoop.window3
/** Explicit application translations override corresponding platform titles. */4
public 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? = None14
)16
public enum TextContextMenuSource {17
Standard, SystemTemplate18
}20
/** A display description with no native ownership or implicit edit target. */21
public enum TextContextMenuEntry {22
Command(val command: TextEditCommand, val label: String, val shortcut: ShortcutChord?),23
Separator24
}26
public class TextContextMenuTemplate internal constructor(27
public val entries: Array<TextContextMenuEntry>,28
public val source: TextContextMenuSource,29
public val selectWordOutsideSelection: Boolean30
)32
/** Reads a platform template without creating an editor, menu session or text document. */33
public fun ActiveApplication.textContextMenuTemplate(34
preferSystem: Boolean = true,35
labels: TextContextMenuLabels? = None36
): TextContextMenuTemplate {37
owner.requireRunning("read text context menu template")38
val template = platformTextContextMenuTemplate(preferSystem)39
val overrides = labels ?: return template40
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
shortcut47
)48
case Separator -> TextContextMenuEntry.Separator49
}50
}51
return TextContextMenuTemplate(entries, template.source, template.selectWordOutsideSelection)52
}54
/** Preserves the physical button event while recognizing the platform's auxiliary click. */55
public 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.superKey66
)68
internal 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.Separator75
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
false86
)87
}89
internal 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
else96
"z"97
case Cut -> "x"98
case Copy -> "c"99
case Paste -> "v"100
case SelectAll -> "a"101
else -> return None102
}103
return Some(104
ShortcutChord(105
ShortcutKey.Logical(LogicalKey.Character(key)),106
ShortcutModifiers(primary = true, shift = command == TextEditCommand.Redo && !redoWithY)107
)108
)109
}111
private fun textMenuLabel(command: TextEditCommand, labels: TextContextMenuLabels): String? =112
when (command) {113
case Undo -> labels.undo114
case Redo -> labels.redo115
case Cut -> labels.cut116
case Copy -> labels.copy117
case Paste -> labels.paste118
case DeleteSelection -> labels.deleteSelection119
case SelectAll -> labels.selectAll120
else -> None121
}