Scoop API

scoop:scoop.window:0.1.0

src/menu/description.scoop

Selected production source · src/menu/description.scoop

1package scoop.window
3internal class PreparedMenuItem(
4 val description: MenuItemDescription,
5 val kind: Int,
6 val checked: Int,
7 val role: Int,
8 val key: NativeMenuKey
9)
11internal 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 -> 0
17 case Checkbox(_) -> 1
18 case Radio(_) -> 2
19 case Submenu -> 3
20 case Separator -> 4
21 case TextServices(_, _) -> {
22 if (!platformTextServiceCapabilities().services)
23 throw WindowException("text services menu", WindowErrorKind.Unsupported)
24 5
25 }
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 -> 0
51 }
52 val role =
53 when (description.command) {
54 case Action -> 0
55 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)
65internal fun menuRoleCode(role: MenuRole): Int =
66 when (role) {
67 case Quit -> 1
68 case Minimize -> 2
69 case Zoom -> 3
70 case Cut -> 4
71 case Copy -> 5
72 case Paste -> 6
73 case SelectAll -> 7
74 case Undo -> 8
75 case Redo -> 9
76 case DeleteSelection -> 10
77 }
79private 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 16
101 @Unsafe
102 {
103 val result = nativeMenuResolveKey(key._1, key._2, mask)
104 checkNativeStatus(result.error, "native menu shortcut")
105 return result
106 }
109private fun menuNamedKey(key: NamedKey): Int =
110 when (key) {
111 case Enter -> 1
112 case Tab -> 2
113 case Space -> 3
114 case Backspace -> 4
115 case Escape -> 5
116 case Delete -> 6
117 case ArrowLeft -> 7
118 case ArrowRight -> 8
119 case ArrowUp -> 9
120 case ArrowDown -> 10
121 case Home -> 11
122 case End -> 12
123 case PageUp -> 13
124 case PageDown -> 14
125 case F1 -> 21
126 case F2 -> 22
127 case F3 -> 23
128 case F4 -> 24
129 case F5 -> 25
130 case F6 -> 26
131 case F7 -> 27
132 case F8 -> 28
133 case F9 -> 29
134 case F10 -> 30
135 case F11 -> 31
136 case F12 -> 32
137 case F13 -> 33
138 case F14 -> 34
139 case F15 -> 35
140 case F16 -> 36
141 case F17 -> 37
142 case F18 -> 38
143 case F19 -> 39
144 case F20 -> 40
145 else -> throw WindowException("native menu named key", WindowErrorKind.Unsupported)
146 }