@octane-xplat/app-menu 0.0.0-nightly.gf55b7b9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -0
- package/dist/accelerator.js +272 -0
- package/dist/controller.js +135 -0
- package/dist/host-types.js +1 -0
- package/dist/host.gtk.js +183 -0
- package/dist/host.js +15 -0
- package/dist/host.macos.js +154 -0
- package/dist/host.web.js +14 -0
- package/dist/host.windows.js +197 -0
- package/dist/index.gtk.js +15 -0
- package/dist/index.js +15 -0
- package/dist/index.macos.js +15 -0
- package/dist/index.web.js +15 -0
- package/dist/index.windows.js +15 -0
- package/dist/model.js +301 -0
- package/dist/shared.js +22 -0
- package/package.json +63 -0
- package/types/generated/.tsrx-typegen-manifest.json +12 -0
- package/types/generated/accelerator.d.ts +39 -0
- package/types/generated/controller.d.ts +6 -0
- package/types/generated/host-types.d.ts +22 -0
- package/types/generated/host.d.ts +2 -0
- package/types/generated/index.d.ts +14 -0
- package/types/generated/model.d.ts +58 -0
- package/types/generated/shared.d.ts +129 -0
package/README.md
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
const MODIFIER_TOKENS = {
|
|
2
|
+
mod: 'mod',
|
|
3
|
+
ctrl: 'ctrl',
|
|
4
|
+
control: 'ctrl',
|
|
5
|
+
alt: 'alt',
|
|
6
|
+
option: 'alt',
|
|
7
|
+
shift: 'shift',
|
|
8
|
+
meta: 'meta',
|
|
9
|
+
cmd: 'meta',
|
|
10
|
+
command: 'meta',
|
|
11
|
+
super: 'meta',
|
|
12
|
+
};
|
|
13
|
+
const NAMED_KEYS = new Set([
|
|
14
|
+
'enter',
|
|
15
|
+
'escape',
|
|
16
|
+
'tab',
|
|
17
|
+
'space',
|
|
18
|
+
'backspace',
|
|
19
|
+
'delete',
|
|
20
|
+
'arrowup',
|
|
21
|
+
'arrowdown',
|
|
22
|
+
'arrowleft',
|
|
23
|
+
'arrowright',
|
|
24
|
+
'home',
|
|
25
|
+
'end',
|
|
26
|
+
'pageup',
|
|
27
|
+
'pagedown',
|
|
28
|
+
'plus',
|
|
29
|
+
]);
|
|
30
|
+
const CANONICAL_NAMED = {
|
|
31
|
+
enter: 'Enter',
|
|
32
|
+
escape: 'Escape',
|
|
33
|
+
tab: 'Tab',
|
|
34
|
+
space: 'Space',
|
|
35
|
+
backspace: 'Backspace',
|
|
36
|
+
delete: 'Delete',
|
|
37
|
+
arrowup: 'ArrowUp',
|
|
38
|
+
arrowdown: 'ArrowDown',
|
|
39
|
+
arrowleft: 'ArrowLeft',
|
|
40
|
+
arrowright: 'ArrowRight',
|
|
41
|
+
home: 'Home',
|
|
42
|
+
end: 'End',
|
|
43
|
+
pageup: 'PageUp',
|
|
44
|
+
pagedown: 'PageDown',
|
|
45
|
+
plus: 'Plus',
|
|
46
|
+
};
|
|
47
|
+
const PUNCTUATION = new Set([',', '.', '/', ';', "'", '[', ']', '\\', '-', '=', '`']);
|
|
48
|
+
function fail(message) {
|
|
49
|
+
throw new TypeError(message);
|
|
50
|
+
}
|
|
51
|
+
/** Parse a `'Mod+Shift+Z'`-style accelerator. `+`-separated, case-insensitive
|
|
52
|
+
* modifier tokens (`Mod`, `Ctrl`/`Control`, `Alt`/`Option`, `Shift`,
|
|
53
|
+
* `Meta`/`Cmd`/`Command`/`Super`) plus exactly one key: `A`–`Z`, `0`–`9`,
|
|
54
|
+
* `F1`–`F24`, `Enter`, `Escape`, `Tab`, `Space`, `Backspace`, `Delete`,
|
|
55
|
+
* `ArrowUp/Down/Left/Right`, `Home`, `End`, `PageUp`, `PageDown`, `Plus`, or
|
|
56
|
+
* the punctuation `` , . / ; ' [ ] \ - = ` ``. At least one non-Shift
|
|
57
|
+
* modifier is required unless the key is F1–F24 — plain letters as app
|
|
58
|
+
* accelerators would steal typing. */
|
|
59
|
+
export function parseAccelerator(source) {
|
|
60
|
+
if (typeof source !== 'string' || !source.trim()) {
|
|
61
|
+
fail('accelerator must be a non-empty string');
|
|
62
|
+
}
|
|
63
|
+
const tokens = source.split('+').map((token) => token.trim());
|
|
64
|
+
const modifiers = { mod: false, ctrl: false, alt: false, shift: false, meta: false };
|
|
65
|
+
let key = null;
|
|
66
|
+
for (const token of tokens) {
|
|
67
|
+
const lower = token.toLowerCase();
|
|
68
|
+
const modifier = MODIFIER_TOKENS[lower];
|
|
69
|
+
if (modifier) {
|
|
70
|
+
modifiers[modifier] = true;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (!token) {
|
|
74
|
+
fail(`invalid accelerator "${source}" — empty key token`);
|
|
75
|
+
}
|
|
76
|
+
let candidate = null;
|
|
77
|
+
if (/^[a-z]$/i.test(token)) {
|
|
78
|
+
candidate = token.toUpperCase();
|
|
79
|
+
}
|
|
80
|
+
else if (/^[0-9]$/.test(token)) {
|
|
81
|
+
candidate = token;
|
|
82
|
+
}
|
|
83
|
+
else if (/^f([1-9]|1[0-9]|2[0-4])$/i.test(token)) {
|
|
84
|
+
candidate = token.toUpperCase();
|
|
85
|
+
}
|
|
86
|
+
else if (NAMED_KEYS.has(lower)) {
|
|
87
|
+
candidate = CANONICAL_NAMED[lower];
|
|
88
|
+
}
|
|
89
|
+
else if (token.length === 1 && PUNCTUATION.has(token)) {
|
|
90
|
+
candidate = token;
|
|
91
|
+
}
|
|
92
|
+
if (candidate === null) {
|
|
93
|
+
fail(`invalid accelerator "${source}" — unknown key "${token}"`);
|
|
94
|
+
}
|
|
95
|
+
if (key !== null) {
|
|
96
|
+
fail(`invalid accelerator "${source}" — more than one key`);
|
|
97
|
+
}
|
|
98
|
+
key = candidate;
|
|
99
|
+
}
|
|
100
|
+
if (key === null) {
|
|
101
|
+
fail(`invalid accelerator "${source}" — a key is required`);
|
|
102
|
+
}
|
|
103
|
+
const hasModifier = modifiers.mod || modifiers.ctrl || modifiers.alt || modifiers.meta;
|
|
104
|
+
const isFunctionKey = /^F(?:[1-9]|1[0-9]|2[0-4])$/.test(key);
|
|
105
|
+
if (!hasModifier && !isFunctionKey) {
|
|
106
|
+
fail(`invalid accelerator "${source}" — requires a non-Shift modifier (plain keys are reserved for typing)`);
|
|
107
|
+
}
|
|
108
|
+
return { key, modifiers };
|
|
109
|
+
}
|
|
110
|
+
const NS_SHIFT = 1 << 17;
|
|
111
|
+
const NS_CONTROL = 1 << 18;
|
|
112
|
+
const NS_OPTION = 1 << 19;
|
|
113
|
+
const NS_COMMAND = 1 << 20;
|
|
114
|
+
const APPKIT_KEYS = {
|
|
115
|
+
ArrowUp: 0xf700,
|
|
116
|
+
ArrowDown: 0xf701,
|
|
117
|
+
ArrowLeft: 0xf702,
|
|
118
|
+
ArrowRight: 0xf703,
|
|
119
|
+
Delete: 0xf728,
|
|
120
|
+
Home: 0xf729,
|
|
121
|
+
End: 0xf72b,
|
|
122
|
+
PageUp: 0xf72c,
|
|
123
|
+
PageDown: 0xf72d,
|
|
124
|
+
Backspace: 0x08,
|
|
125
|
+
Enter: 0x0d,
|
|
126
|
+
Escape: 0x1b,
|
|
127
|
+
Tab: 0x09,
|
|
128
|
+
Space: 0x20,
|
|
129
|
+
Plus: '+'.charCodeAt(0),
|
|
130
|
+
};
|
|
131
|
+
/** Map to AppKit `{ keyEquivalent, modifierMask }`. `mod` and `meta` are both
|
|
132
|
+
* Command here. */
|
|
133
|
+
export function toAppKit(parsed) {
|
|
134
|
+
let modifierMask = 0;
|
|
135
|
+
if (parsed.modifiers.shift) {
|
|
136
|
+
modifierMask |= NS_SHIFT;
|
|
137
|
+
}
|
|
138
|
+
if (parsed.modifiers.ctrl) {
|
|
139
|
+
modifierMask |= NS_CONTROL;
|
|
140
|
+
}
|
|
141
|
+
if (parsed.modifiers.alt) {
|
|
142
|
+
modifierMask |= NS_OPTION;
|
|
143
|
+
}
|
|
144
|
+
if (parsed.modifiers.mod || parsed.modifiers.meta) {
|
|
145
|
+
modifierMask |= NS_COMMAND;
|
|
146
|
+
}
|
|
147
|
+
const code = APPKIT_KEYS[parsed.key];
|
|
148
|
+
const keyEquivalent = code !== undefined
|
|
149
|
+
? String.fromCharCode(code)
|
|
150
|
+
: /^F\d+$/.test(parsed.key)
|
|
151
|
+
? String.fromCharCode(0xf704 + Number(parsed.key.slice(1)) - 1)
|
|
152
|
+
: parsed.key.length === 1
|
|
153
|
+
? // Always lowercase — AppKit matches keyEquivalent against the
|
|
154
|
+
// event's charactersIgnoringModifiers (lowercase for letters);
|
|
155
|
+
// an uppercase letter is unreachable by key events.
|
|
156
|
+
parsed.key.toLowerCase()
|
|
157
|
+
: parsed.key;
|
|
158
|
+
return { keyEquivalent, modifierMask };
|
|
159
|
+
}
|
|
160
|
+
const GTK_KEYS = {
|
|
161
|
+
Enter: 'Return',
|
|
162
|
+
Escape: 'Escape',
|
|
163
|
+
Tab: 'Tab',
|
|
164
|
+
Space: 'space',
|
|
165
|
+
Backspace: 'BackSpace',
|
|
166
|
+
Delete: 'Delete',
|
|
167
|
+
ArrowUp: 'Up',
|
|
168
|
+
ArrowDown: 'Down',
|
|
169
|
+
ArrowLeft: 'Left',
|
|
170
|
+
ArrowRight: 'Right',
|
|
171
|
+
Home: 'Home',
|
|
172
|
+
End: 'End',
|
|
173
|
+
PageUp: 'Page_Up',
|
|
174
|
+
PageDown: 'Page_Down',
|
|
175
|
+
Plus: 'plus',
|
|
176
|
+
',': 'comma',
|
|
177
|
+
'.': 'period',
|
|
178
|
+
'/': 'slash',
|
|
179
|
+
';': 'semicolon',
|
|
180
|
+
"'": 'apostrophe',
|
|
181
|
+
'[': 'bracketleft',
|
|
182
|
+
']': 'bracketright',
|
|
183
|
+
'\\': 'backslash',
|
|
184
|
+
'-': 'minus',
|
|
185
|
+
'=': 'equal',
|
|
186
|
+
'`': 'grave',
|
|
187
|
+
};
|
|
188
|
+
/** Map to a GTK accelerator string such as `<Control><Shift>z`. `mod` is
|
|
189
|
+
* Control and `meta` is Super here. */
|
|
190
|
+
export function toGtk(parsed) {
|
|
191
|
+
let accel = '';
|
|
192
|
+
if (parsed.modifiers.ctrl || parsed.modifiers.mod) {
|
|
193
|
+
accel += '<Control>';
|
|
194
|
+
}
|
|
195
|
+
if (parsed.modifiers.shift) {
|
|
196
|
+
accel += '<Shift>';
|
|
197
|
+
}
|
|
198
|
+
if (parsed.modifiers.alt) {
|
|
199
|
+
accel += '<Alt>';
|
|
200
|
+
}
|
|
201
|
+
if (parsed.modifiers.meta) {
|
|
202
|
+
accel += '<Super>';
|
|
203
|
+
}
|
|
204
|
+
const named = GTK_KEYS[parsed.key];
|
|
205
|
+
accel += named ?? (parsed.key.length === 1 ? parsed.key.toLowerCase() : parsed.key);
|
|
206
|
+
return accel;
|
|
207
|
+
}
|
|
208
|
+
const WINUI_CONTROL = 1;
|
|
209
|
+
const WINUI_MENU = 2;
|
|
210
|
+
const WINUI_SHIFT = 4;
|
|
211
|
+
const WINUI_WINDOWS = 8;
|
|
212
|
+
const WINUI_KEYS = {
|
|
213
|
+
Backspace: 8,
|
|
214
|
+
Tab: 9,
|
|
215
|
+
Enter: 13,
|
|
216
|
+
Escape: 27,
|
|
217
|
+
Space: 32,
|
|
218
|
+
PageUp: 33,
|
|
219
|
+
PageDown: 34,
|
|
220
|
+
End: 35,
|
|
221
|
+
Home: 36,
|
|
222
|
+
ArrowLeft: 37,
|
|
223
|
+
ArrowUp: 38,
|
|
224
|
+
ArrowRight: 39,
|
|
225
|
+
ArrowDown: 40,
|
|
226
|
+
Delete: 46,
|
|
227
|
+
';': 186,
|
|
228
|
+
Plus: 187,
|
|
229
|
+
'=': 187,
|
|
230
|
+
',': 188,
|
|
231
|
+
'-': 189,
|
|
232
|
+
'.': 190,
|
|
233
|
+
'/': 191,
|
|
234
|
+
'`': 192,
|
|
235
|
+
'[': 219,
|
|
236
|
+
'\\': 220,
|
|
237
|
+
']': 221,
|
|
238
|
+
"'": 222,
|
|
239
|
+
};
|
|
240
|
+
/** Map to WinUI `{ key: VirtualKey, modifiers: VirtualKeyModifiers }`.
|
|
241
|
+
* `mod` is Control and `meta` is the Windows key here. */
|
|
242
|
+
export function toWinUI(parsed) {
|
|
243
|
+
let modifiers = 0;
|
|
244
|
+
if (parsed.modifiers.ctrl || parsed.modifiers.mod) {
|
|
245
|
+
modifiers |= WINUI_CONTROL;
|
|
246
|
+
}
|
|
247
|
+
if (parsed.modifiers.alt) {
|
|
248
|
+
modifiers |= WINUI_MENU;
|
|
249
|
+
}
|
|
250
|
+
if (parsed.modifiers.shift) {
|
|
251
|
+
modifiers |= WINUI_SHIFT;
|
|
252
|
+
}
|
|
253
|
+
if (parsed.modifiers.meta) {
|
|
254
|
+
modifiers |= WINUI_WINDOWS;
|
|
255
|
+
}
|
|
256
|
+
let key = WINUI_KEYS[parsed.key];
|
|
257
|
+
if (key === undefined) {
|
|
258
|
+
if (/^[A-Z]$/.test(parsed.key)) {
|
|
259
|
+
key = parsed.key.charCodeAt(0);
|
|
260
|
+
}
|
|
261
|
+
else if (/^[0-9]$/.test(parsed.key)) {
|
|
262
|
+
key = parsed.key.charCodeAt(0);
|
|
263
|
+
}
|
|
264
|
+
else if (/^F\d+$/.test(parsed.key)) {
|
|
265
|
+
key = 112 + Number(parsed.key.slice(1)) - 1;
|
|
266
|
+
}
|
|
267
|
+
else {
|
|
268
|
+
fail(`no WinUI VirtualKey for "${parsed.key}"`);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return { key, modifiers };
|
|
272
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { UnavailabilityError } from '@octane-xplat/lib/capability';
|
|
2
|
+
import { normalizeAppMenu } from './model.js';
|
|
3
|
+
/** Single-owner controller over the host adapter: validates payloads,
|
|
4
|
+
* assigns tree generations, gates native dispatch, and defers native
|
|
5
|
+
* updates/teardown that land inside a running callback. */
|
|
6
|
+
export function createAppMenuService(host) {
|
|
7
|
+
let slot = null;
|
|
8
|
+
let generation = 0;
|
|
9
|
+
let dispatching = 0;
|
|
10
|
+
// 'update' applies slot.tree (latest wins — already swapped logically);
|
|
11
|
+
// 'dispose' tears down retired and always supersedes a pending update.
|
|
12
|
+
let deferred = null;
|
|
13
|
+
let deferredScheduled = false;
|
|
14
|
+
let retired = null;
|
|
15
|
+
function flushDeferred() {
|
|
16
|
+
deferredScheduled = false;
|
|
17
|
+
const op = deferred;
|
|
18
|
+
deferred = null;
|
|
19
|
+
if (op === 'dispose') {
|
|
20
|
+
const native = retired;
|
|
21
|
+
retired = null;
|
|
22
|
+
native?.dispose();
|
|
23
|
+
}
|
|
24
|
+
else if (op === 'update') {
|
|
25
|
+
slot?.native.update(slot.tree);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function requestDeferred(kind) {
|
|
29
|
+
if (kind === 'dispose' || deferred !== 'dispose') {
|
|
30
|
+
deferred = kind;
|
|
31
|
+
}
|
|
32
|
+
if (!deferredScheduled) {
|
|
33
|
+
deferredScheduled = true;
|
|
34
|
+
host.defer(flushDeferred);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const dispatch = (id, gen) => {
|
|
38
|
+
const current = slot;
|
|
39
|
+
if (!current || gen !== current.tree.generation) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
const node = current.tree.nodes.get(id);
|
|
43
|
+
if (!node) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
if (node.kind === 'role') {
|
|
47
|
+
// Host-handled — current is all the host needs to know.
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
dispatching++;
|
|
51
|
+
try {
|
|
52
|
+
if (node.kind === 'action') {
|
|
53
|
+
node.onSelect();
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
node.onValueChange(!node.value);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
console.error('[app-menu] menu action failed', error);
|
|
61
|
+
}
|
|
62
|
+
finally {
|
|
63
|
+
dispatching--;
|
|
64
|
+
}
|
|
65
|
+
return true;
|
|
66
|
+
};
|
|
67
|
+
function readSupported() {
|
|
68
|
+
try {
|
|
69
|
+
return Boolean(host.supported());
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return {
|
|
76
|
+
get supported() {
|
|
77
|
+
return readSupported();
|
|
78
|
+
},
|
|
79
|
+
isAvailable() {
|
|
80
|
+
return Promise.resolve().then(readSupported);
|
|
81
|
+
},
|
|
82
|
+
install(menus, options) {
|
|
83
|
+
if (!readSupported()) {
|
|
84
|
+
throw new UnavailabilityError('appMenu');
|
|
85
|
+
}
|
|
86
|
+
// A deferred teardown/update is a pending native write — settle it
|
|
87
|
+
// before claiming the slot.
|
|
88
|
+
flushDeferred();
|
|
89
|
+
if (slot) {
|
|
90
|
+
throw new Error('An app menu is already installed; update or dispose it first');
|
|
91
|
+
}
|
|
92
|
+
const tree = normalizeAppMenu(menus, options, host.platform, ++generation);
|
|
93
|
+
// host.install throwing leaves the slot free for the next attempt.
|
|
94
|
+
const native = host.install(tree, dispatch);
|
|
95
|
+
slot = { native, tree };
|
|
96
|
+
let disposed = false;
|
|
97
|
+
const handle = {
|
|
98
|
+
get isDisposed() {
|
|
99
|
+
return disposed;
|
|
100
|
+
},
|
|
101
|
+
update(nextMenus, nextOptions) {
|
|
102
|
+
if (disposed) {
|
|
103
|
+
throw new Error('App menu is disposed');
|
|
104
|
+
}
|
|
105
|
+
const next = normalizeAppMenu(nextMenus, nextOptions, host.platform, ++generation);
|
|
106
|
+
// Logical swap is immediate — old-tree callbacks die on the
|
|
107
|
+
// generation check; the native rebuild may defer.
|
|
108
|
+
slot.tree = next;
|
|
109
|
+
if (dispatching > 0) {
|
|
110
|
+
requestDeferred('update');
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
slot.native.update(next);
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
dispose() {
|
|
117
|
+
if (disposed) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
disposed = true;
|
|
121
|
+
const native = slot.native;
|
|
122
|
+
slot = null;
|
|
123
|
+
if (dispatching > 0) {
|
|
124
|
+
retired = native;
|
|
125
|
+
requestDeferred('dispose');
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
native.dispose();
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
return handle;
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/host.gtk.js
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import Gtk from 'gi://Gtk?version=4.0';
|
|
2
|
+
import Gio from 'gi://Gio';
|
|
3
|
+
import GLib from 'gi://GLib';
|
|
4
|
+
import { UnavailabilityError } from '@octane-xplat/lib/capability';
|
|
5
|
+
import { toGtk } from './accelerator.js';
|
|
6
|
+
// Edit roles stay widget-owned: the menu item only shows the accelerator
|
|
7
|
+
// (registered on the item's `accel` attribute, never on the app) and
|
|
8
|
+
// activation forwards to the focused widget's built-in action.
|
|
9
|
+
const EDIT_ROLE_TARGETS = new Map([
|
|
10
|
+
['undo', { widgetAction: 'text.undo', accel: '<Control>z' }],
|
|
11
|
+
['redo', { widgetAction: 'text.redo', accel: '<Control><Shift>z' }],
|
|
12
|
+
['cut', { widgetAction: 'clipboard.cut', accel: '<Control>x' }],
|
|
13
|
+
['copy', { widgetAction: 'clipboard.copy', accel: '<Control>c' }],
|
|
14
|
+
['paste', { widgetAction: 'clipboard.paste', accel: '<Control>v' }],
|
|
15
|
+
['selectAll', { widgetAction: 'selection.select-all', accel: '<Control>a' }],
|
|
16
|
+
]);
|
|
17
|
+
// Roles that act on the app/window directly. Their accelerators are real
|
|
18
|
+
// app-level registrations, so model.ts counts them in collision checks.
|
|
19
|
+
const WINDOW_ROLE_ACCELERATORS = {
|
|
20
|
+
close: '<Control>w',
|
|
21
|
+
quit: '<Control>q',
|
|
22
|
+
};
|
|
23
|
+
let instanceCounter = 0;
|
|
24
|
+
function application() {
|
|
25
|
+
const app = Gio.Application.get_default();
|
|
26
|
+
return app instanceof Gtk.Application ? app : null;
|
|
27
|
+
}
|
|
28
|
+
function requireApplication() {
|
|
29
|
+
const app = application();
|
|
30
|
+
if (!app) {
|
|
31
|
+
throw new UnavailabilityError('appMenu');
|
|
32
|
+
}
|
|
33
|
+
return app;
|
|
34
|
+
}
|
|
35
|
+
/** GTK menu mnemonics treat `_` as an accelerator marker — escape it. */
|
|
36
|
+
function escapeLabel(label) {
|
|
37
|
+
return label.replace(/_/g, '__');
|
|
38
|
+
}
|
|
39
|
+
export const host = {
|
|
40
|
+
platform: 'linux',
|
|
41
|
+
supported() {
|
|
42
|
+
try {
|
|
43
|
+
return application() !== null;
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
defer(fn) {
|
|
50
|
+
GLib.idle_add(GLib.PRIORITY_DEFAULT, () => {
|
|
51
|
+
fn();
|
|
52
|
+
return GLib.SOURCE_REMOVE;
|
|
53
|
+
});
|
|
54
|
+
},
|
|
55
|
+
install(tree, dispatch) {
|
|
56
|
+
const app = requireApplication();
|
|
57
|
+
const previous = app.get_menubar();
|
|
58
|
+
// Unique action names per applied tree let update() register the new
|
|
59
|
+
// generation before retiring the old — the menubar never references a
|
|
60
|
+
// removed action, and node ids restart at 1 in every normalized tree.
|
|
61
|
+
const instance = ++instanceCounter;
|
|
62
|
+
function roleBehavior(role) {
|
|
63
|
+
if (role === 'quit') {
|
|
64
|
+
app.quit();
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const window = app.get_active_window();
|
|
68
|
+
if (role === 'close') {
|
|
69
|
+
window?.close();
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (role === 'minimize') {
|
|
73
|
+
window?.minimize();
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const target = EDIT_ROLE_TARGETS.get(role);
|
|
77
|
+
if (target) {
|
|
78
|
+
window?.get_focus()?.activate_action(target.widgetAction, null);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function registerAction(node, generation, names) {
|
|
82
|
+
const name = `xplat-menu-${instance}-${generation}-${node.id}`;
|
|
83
|
+
let action;
|
|
84
|
+
if (node.kind === 'checkbox') {
|
|
85
|
+
// Stateful so GMenu renders a checkmark; the 'activate' handler
|
|
86
|
+
// reports the intent without touching state — `update()` owns it.
|
|
87
|
+
action = Gio.SimpleAction.new_stateful(name, null, GLib.Variant.new_boolean(node.value));
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
action = Gio.SimpleAction.new(name, null);
|
|
91
|
+
}
|
|
92
|
+
action.connect('activate', () => {
|
|
93
|
+
if (!dispatch(node.id, generation)) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
if (node.kind === 'role') {
|
|
97
|
+
roleBehavior(node.role);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
action.set_enabled(node.kind === 'role' ? true : !node.disabled);
|
|
101
|
+
app.add_action(action);
|
|
102
|
+
names.push(name);
|
|
103
|
+
const accel = node.kind === 'role'
|
|
104
|
+
? WINDOW_ROLE_ACCELERATORS[node.role]
|
|
105
|
+
: node.accelerator
|
|
106
|
+
? toGtk(node.accelerator)
|
|
107
|
+
: undefined;
|
|
108
|
+
if (accel) {
|
|
109
|
+
app.set_accels_for_action(`app.${name}`, [accel]);
|
|
110
|
+
}
|
|
111
|
+
return name;
|
|
112
|
+
}
|
|
113
|
+
function buildMenu(items, generation, names) {
|
|
114
|
+
const menu = new Gio.Menu();
|
|
115
|
+
// Separators become GMenu sections — normalized trees already collapse
|
|
116
|
+
// leading/trailing/repeated separators.
|
|
117
|
+
const sections = [new Gio.Menu()];
|
|
118
|
+
for (const node of items) {
|
|
119
|
+
if (node.kind === 'separator') {
|
|
120
|
+
sections.push(new Gio.Menu());
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
const section = sections[sections.length - 1];
|
|
124
|
+
if (node.kind === 'submenu') {
|
|
125
|
+
section.append_submenu(escapeLabel(node.label), buildMenu(node.items, generation, names));
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
const name = registerAction(node, generation, names);
|
|
129
|
+
const target = node.kind === 'role' ? EDIT_ROLE_TARGETS.get(node.role) : undefined;
|
|
130
|
+
if (target) {
|
|
131
|
+
const item = Gio.MenuItem.new(escapeLabel(node.label), `app.${name}`);
|
|
132
|
+
item.set_attribute_value('accel', GLib.Variant.new_string(target.accel));
|
|
133
|
+
section.append_item(item);
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
section.append(escapeLabel(node.label), `app.${name}`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
for (const section of sections) {
|
|
140
|
+
menu.append_section(null, section);
|
|
141
|
+
}
|
|
142
|
+
return menu;
|
|
143
|
+
}
|
|
144
|
+
function build(current) {
|
|
145
|
+
const names = [];
|
|
146
|
+
const menubar = new Gio.Menu();
|
|
147
|
+
for (const submenu of current.menus) {
|
|
148
|
+
menubar.append_submenu(escapeLabel(submenu.label), buildMenu(submenu.items, current.generation, names));
|
|
149
|
+
}
|
|
150
|
+
return { menu: menubar, names };
|
|
151
|
+
}
|
|
152
|
+
function retire(old) {
|
|
153
|
+
for (const name of old.names) {
|
|
154
|
+
app.set_accels_for_action(`app.${name}`, []);
|
|
155
|
+
app.remove_action(name);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
let current = build(tree);
|
|
159
|
+
app.set_menubar(current.menu);
|
|
160
|
+
const native = {
|
|
161
|
+
update(next) {
|
|
162
|
+
const old = current;
|
|
163
|
+
const built = build(next);
|
|
164
|
+
app.set_menubar(built.menu);
|
|
165
|
+
current = built;
|
|
166
|
+
retire(old);
|
|
167
|
+
},
|
|
168
|
+
dispose() {
|
|
169
|
+
if (!current) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
const old = current;
|
|
173
|
+
current = null;
|
|
174
|
+
// Restore the pre-install menubar only when ours is still installed.
|
|
175
|
+
if (app.get_menubar() === old.menu) {
|
|
176
|
+
app.set_menubar(previous);
|
|
177
|
+
}
|
|
178
|
+
retire(old);
|
|
179
|
+
},
|
|
180
|
+
};
|
|
181
|
+
return native;
|
|
182
|
+
},
|
|
183
|
+
};
|
package/dist/host.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { UnavailabilityError } from '@octane-xplat/lib/capability';
|
|
2
|
+
// Default leaf — iOS, Android and unknown hosts have no application menu bar
|
|
3
|
+
// (iPadOS UIMenuBuilder is not exposed to this runtime).
|
|
4
|
+
export const host = {
|
|
5
|
+
platform: null,
|
|
6
|
+
supported() {
|
|
7
|
+
return false;
|
|
8
|
+
},
|
|
9
|
+
defer(fn) {
|
|
10
|
+
setTimeout(fn, 0);
|
|
11
|
+
},
|
|
12
|
+
install() {
|
|
13
|
+
throw new UnavailabilityError('appMenu');
|
|
14
|
+
},
|
|
15
|
+
};
|