scoop:scoop.window:0.1.0
src/menu/description.scoop
Selected production source · src/menu/description.scoop
1
package scoop.window3
internal class PreparedMenuItem(4
val description: MenuItemDescription,5
val kind: Int,6
val checked: Int,7
val role: Int,8
val key: NativeMenuKey9
)11
internal fun prepareMenuItem(description: MenuItemDescription): PreparedMenuItem {12
for (character in description.text)13
requireWindow(character.code != 0, "menu text contains NUL")14
val kind =15
when (description.kind) {16
case Action -> 017
case Checkbox(_) -> 118
case Radio(_) -> 219
case Submenu -> 320
case Separator -> 421
case TextServices(_, _) -> {22
if (!platformTextServiceCapabilities().services)23
throw WindowException("text services menu", WindowErrorKind.Unsupported)24
525
}26
}27
if (kind >= 3) {28
when (description.shortcut) {29
case Some(_) -> throw IllegalArgumentException(30
Some("submenu and separator cannot have a shortcut")31
)32
case None -> {}33
}34
requireWindow(35
description.command == MenuCommand.Action,36
"submenu and separator cannot have a command or shortcut"37
)38
}39
if (kind == 4) {40
when (description.image) {41
case Some(_) -> throw IllegalArgumentException(Some("separator cannot have an image"))42
case None -> {}43
}44
requireWindow(description.text.byteLength == 0L, "separator cannot have text or image")45
}46
val checked =47
when (description.kind) {48
case Checkbox(val value) -> flag(value)49
case Radio(val value) -> flag(value)50
else -> 051
}52
val role =53
when (description.command) {54
case Action -> 055
case Role(val value) -> menuRoleCode(value)56
}57
val key =58
when (description.shortcut) {59
case Some(val chord) -> resolveMenuKey(chord)60
case None -> NativeMenuKey(0, 0, 0L)61
}62
return PreparedMenuItem(description, kind, checked, role, key)63
}65
internal fun menuRoleCode(role: MenuRole): Int =66
when (role) {67
case Quit -> 168
case Minimize -> 269
case Zoom -> 370
case Cut -> 471
case Copy -> 572
case Paste -> 673
case SelectAll -> 774
case Undo -> 875
case Redo -> 976
case DeleteSelection -> 1077
}79
private fun resolveMenuKey(chord: ShortcutChord): NativeMenuKey {80
val key =81
when (chord.key) {82
case Logical(LogicalKey.Character(val text)) -> {83
if (text.charLength != 1L || text.getCharAt(0L).code == 0)84
throw WindowException("native menu shortcut", WindowErrorKind.Unsupported)85
(0, text.getCharAt(0L).code)86
}87
case Logical(LogicalKey.Named(val named)) -> (1, menuNamedKey(named))88
else -> throw WindowException("native menu shortcut", WindowErrorKind.Unsupported)89
}90
val modifiers = resolvedModifiers(chord.modifiers, platformPrimaryModifier())91
val mask =92
flag(modifiers.control) +93
flag(modifiers.alt) *94
2 +95
flag(modifiers.shift) *96
4 +97
flag(modifiers.superKey) *98
8 +99
flag(modifiers.function) *100
16101
@Unsafe102
{103
val result = nativeMenuResolveKey(key._1, key._2, mask)104
checkNativeStatus(result.error, "native menu shortcut")105
return result106
}107
}109
private fun menuNamedKey(key: NamedKey): Int =110
when (key) {111
case Enter -> 1112
case Tab -> 2113
case Space -> 3114
case Backspace -> 4115
case Escape -> 5116
case Delete -> 6117
case ArrowLeft -> 7118
case ArrowRight -> 8119
case ArrowUp -> 9120
case ArrowDown -> 10121
case Home -> 11122
case End -> 12123
case PageUp -> 13124
case PageDown -> 14125
case F1 -> 21126
case F2 -> 22127
case F3 -> 23128
case F4 -> 24129
case F5 -> 25130
case F6 -> 26131
case F7 -> 27132
case F8 -> 28133
case F9 -> 29134
case F10 -> 30135
case F11 -> 31136
case F12 -> 32137
case F13 -> 33138
case F14 -> 34139
case F15 -> 35140
case F16 -> 36141
case F17 -> 37142
case F18 -> 38143
case F19 -> 39144
case F20 -> 40145
else -> throw WindowException("native menu named key", WindowErrorKind.Unsupported)146
}