@octane-xplat/app-menu 0.0.0-stage → 0.20.0
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 +4 -2
- 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 +62 -5
- 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/dist/model.js
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import { parseAccelerator, toAppKit, toGtk, toWinUI, } from './accelerator.js';
|
|
2
|
+
const ROLES = new Set([
|
|
3
|
+
'undo',
|
|
4
|
+
'redo',
|
|
5
|
+
'cut',
|
|
6
|
+
'copy',
|
|
7
|
+
'paste',
|
|
8
|
+
'selectAll',
|
|
9
|
+
'minimize',
|
|
10
|
+
'close',
|
|
11
|
+
'quit',
|
|
12
|
+
]);
|
|
13
|
+
export const ROLE_DEFAULT_LABELS = {
|
|
14
|
+
undo: 'Undo',
|
|
15
|
+
redo: 'Redo',
|
|
16
|
+
cut: 'Cut',
|
|
17
|
+
copy: 'Copy',
|
|
18
|
+
paste: 'Paste',
|
|
19
|
+
selectAll: 'Select All',
|
|
20
|
+
minimize: 'Minimize',
|
|
21
|
+
close: 'Close',
|
|
22
|
+
quit: 'Quit',
|
|
23
|
+
};
|
|
24
|
+
/** Accelerators a role *registers* with the platform menu engine, keyed by
|
|
25
|
+
* platform. macOS registers native key equivalents for every role except
|
|
26
|
+
* quit (the application menu owns Quit ⌘Q). GTK registers close/quit only —
|
|
27
|
+
* edit-role accelerators stay widget-owned and are only displayed via the
|
|
28
|
+
* menu item `accel` attribute. Windows role entries are explicitly unsupported. */
|
|
29
|
+
const ROLE_ACCELERATORS = {
|
|
30
|
+
macos: {
|
|
31
|
+
undo: 'Mod+Z',
|
|
32
|
+
redo: 'Mod+Shift+Z',
|
|
33
|
+
cut: 'Mod+X',
|
|
34
|
+
copy: 'Mod+C',
|
|
35
|
+
paste: 'Mod+V',
|
|
36
|
+
selectAll: 'Mod+A',
|
|
37
|
+
minimize: 'Mod+M',
|
|
38
|
+
close: 'Mod+W',
|
|
39
|
+
},
|
|
40
|
+
linux: { close: 'Control+W', quit: 'Control+Q' },
|
|
41
|
+
windows: {},
|
|
42
|
+
};
|
|
43
|
+
/** Accelerators the standard macOS application menu registers regardless of
|
|
44
|
+
* the installed tree: Hide ⌘H, Hide Others ⌥⌘H, Quit ⌘Q. */
|
|
45
|
+
const MACOS_APPLICATION_MENU_ACCELERATORS = [
|
|
46
|
+
['Mod+H', 'the standard application menu (Hide)'],
|
|
47
|
+
['Mod+Alt+H', 'the standard application menu (Hide Others)'],
|
|
48
|
+
['Mod+Q', 'the standard application menu (Quit)'],
|
|
49
|
+
];
|
|
50
|
+
function fail(path, message) {
|
|
51
|
+
throw new TypeError(`${path}: ${message}`);
|
|
52
|
+
}
|
|
53
|
+
/** Canonical string identifying one effective accelerator on the target
|
|
54
|
+
* platform, so `Mod+Q` and `Command+Q` collide on macOS but `Mod+Q` and
|
|
55
|
+
* `Meta+Q` do not on Linux. */
|
|
56
|
+
function canonicalAccelerator(parsed, platform) {
|
|
57
|
+
switch (platform) {
|
|
58
|
+
case 'macos': {
|
|
59
|
+
const { keyEquivalent, modifierMask } = toAppKit(parsed);
|
|
60
|
+
return `${modifierMask}:${keyEquivalent.codePointAt(0) ?? 0}`;
|
|
61
|
+
}
|
|
62
|
+
case 'linux':
|
|
63
|
+
return toGtk(parsed);
|
|
64
|
+
case 'windows': {
|
|
65
|
+
const { key, modifiers } = toWinUI(parsed);
|
|
66
|
+
return `${modifiers}:${key}`;
|
|
67
|
+
}
|
|
68
|
+
case null:
|
|
69
|
+
return JSON.stringify(parsed);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function noteAccelerator(context, parsed, source, owner) {
|
|
73
|
+
if (!context.accelerators) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const canonical = canonicalAccelerator(parsed, context.platform);
|
|
77
|
+
const existing = context.accelerators.get(canonical);
|
|
78
|
+
if (existing !== undefined) {
|
|
79
|
+
fail(owner, `accelerator "${source}" duplicates ${existing}`);
|
|
80
|
+
}
|
|
81
|
+
context.accelerators.set(canonical, owner);
|
|
82
|
+
}
|
|
83
|
+
function normalizeChildren(entries, path, context) {
|
|
84
|
+
const siblings = new Set();
|
|
85
|
+
const items = [];
|
|
86
|
+
for (const [index, entry] of entries.entries()) {
|
|
87
|
+
const itemPath = `${path}[${index}]`;
|
|
88
|
+
if (typeof entry !== 'object' || entry === null) {
|
|
89
|
+
fail(itemPath, 'expected a menu entry object');
|
|
90
|
+
}
|
|
91
|
+
const record = entry;
|
|
92
|
+
const key = record.key;
|
|
93
|
+
if (typeof key !== 'string' || !key) {
|
|
94
|
+
fail(itemPath, 'key must be a non-empty string');
|
|
95
|
+
}
|
|
96
|
+
if (siblings.has(key)) {
|
|
97
|
+
fail(itemPath, `duplicate sibling key "${key}"`);
|
|
98
|
+
}
|
|
99
|
+
siblings.add(key);
|
|
100
|
+
switch (record.type) {
|
|
101
|
+
case 'action': {
|
|
102
|
+
const label = requireLabel(record, itemPath);
|
|
103
|
+
const accelerator = parseAcceleratorField(record, itemPath, context);
|
|
104
|
+
if (typeof record.onSelect !== 'function') {
|
|
105
|
+
fail(itemPath, 'onSelect must be a function');
|
|
106
|
+
}
|
|
107
|
+
const node = {
|
|
108
|
+
kind: 'action',
|
|
109
|
+
id: context.nextId++,
|
|
110
|
+
key,
|
|
111
|
+
label,
|
|
112
|
+
accelerator,
|
|
113
|
+
disabled: Boolean(record.isDisabled),
|
|
114
|
+
onSelect: record.onSelect,
|
|
115
|
+
};
|
|
116
|
+
context.nodes.set(node.id, node);
|
|
117
|
+
items.push(node);
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
case 'checkbox': {
|
|
121
|
+
const label = requireLabel(record, itemPath);
|
|
122
|
+
const accelerator = parseAcceleratorField(record, itemPath, context);
|
|
123
|
+
if (typeof record.value !== 'boolean') {
|
|
124
|
+
fail(itemPath, 'value must be a boolean');
|
|
125
|
+
}
|
|
126
|
+
if (typeof record.onValueChange !== 'function') {
|
|
127
|
+
fail(itemPath, 'onValueChange must be a function');
|
|
128
|
+
}
|
|
129
|
+
const node = {
|
|
130
|
+
kind: 'checkbox',
|
|
131
|
+
id: context.nextId++,
|
|
132
|
+
key,
|
|
133
|
+
label,
|
|
134
|
+
value: record.value,
|
|
135
|
+
accelerator,
|
|
136
|
+
disabled: Boolean(record.isDisabled),
|
|
137
|
+
onValueChange: record.onValueChange,
|
|
138
|
+
};
|
|
139
|
+
context.nodes.set(node.id, node);
|
|
140
|
+
items.push(node);
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
case 'separator': {
|
|
144
|
+
const node = { kind: 'separator', key };
|
|
145
|
+
// Leading/trailing/repeated separators collapse — GTK sections and
|
|
146
|
+
// the macOS quit-role omission both depend on it.
|
|
147
|
+
if (items.length === 0 || items[items.length - 1].kind === 'separator') {
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
items.push(node);
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
case 'submenu': {
|
|
154
|
+
const label = requireLabel(record, itemPath);
|
|
155
|
+
if (!Array.isArray(record.items)) {
|
|
156
|
+
fail(itemPath, 'items must be an array');
|
|
157
|
+
}
|
|
158
|
+
const node = {
|
|
159
|
+
kind: 'submenu',
|
|
160
|
+
key,
|
|
161
|
+
label,
|
|
162
|
+
items: normalizeChildren(record.items, `${itemPath}.items`, context),
|
|
163
|
+
};
|
|
164
|
+
items.push(node);
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
case 'role': {
|
|
168
|
+
if (context.platform === 'windows') {
|
|
169
|
+
fail(itemPath, 'standard app menu roles are unsupported on Windows; use an action callback');
|
|
170
|
+
}
|
|
171
|
+
const role = record.role;
|
|
172
|
+
if (typeof role !== 'string' || !ROLES.has(role)) {
|
|
173
|
+
fail(itemPath, `unknown role ${JSON.stringify(role)}`);
|
|
174
|
+
}
|
|
175
|
+
let label = ROLE_DEFAULT_LABELS[role];
|
|
176
|
+
if (record.label !== undefined) {
|
|
177
|
+
if (typeof record.label !== 'string' || !record.label) {
|
|
178
|
+
fail(itemPath, 'label must be a non-empty string');
|
|
179
|
+
}
|
|
180
|
+
label = record.label;
|
|
181
|
+
}
|
|
182
|
+
const node = {
|
|
183
|
+
kind: 'role',
|
|
184
|
+
id: context.nextId++,
|
|
185
|
+
key,
|
|
186
|
+
role: role,
|
|
187
|
+
label,
|
|
188
|
+
};
|
|
189
|
+
context.nodes.set(node.id, node);
|
|
190
|
+
const registered = context.platform
|
|
191
|
+
? ROLE_ACCELERATORS[context.platform][node.role]
|
|
192
|
+
: undefined;
|
|
193
|
+
if (registered) {
|
|
194
|
+
noteAccelerator(context, parseAccelerator(registered), registered, `${itemPath} (role "${node.role}")`);
|
|
195
|
+
}
|
|
196
|
+
items.push(node);
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
default:
|
|
200
|
+
fail(itemPath, `unknown menu entry type ${JSON.stringify(record.type)}`);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
if (items.length && items[items.length - 1].kind === 'separator') {
|
|
204
|
+
items.pop();
|
|
205
|
+
}
|
|
206
|
+
return items;
|
|
207
|
+
}
|
|
208
|
+
function requireLabel(record, path) {
|
|
209
|
+
if (typeof record.label !== 'string' || !record.label) {
|
|
210
|
+
fail(path, 'label must be a non-empty string');
|
|
211
|
+
}
|
|
212
|
+
return record.label;
|
|
213
|
+
}
|
|
214
|
+
function parseAcceleratorField(record, path, context) {
|
|
215
|
+
const source = record.accelerator;
|
|
216
|
+
if (source === undefined) {
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
if (typeof source !== 'string') {
|
|
220
|
+
fail(path, 'accelerator must be a string');
|
|
221
|
+
}
|
|
222
|
+
let parsed;
|
|
223
|
+
try {
|
|
224
|
+
parsed = parseAccelerator(source);
|
|
225
|
+
}
|
|
226
|
+
catch (error) {
|
|
227
|
+
fail(`${path}.accelerator`, error instanceof Error ? error.message : String(error));
|
|
228
|
+
}
|
|
229
|
+
noteAccelerator(context, parsed, source, path);
|
|
230
|
+
return parsed;
|
|
231
|
+
}
|
|
232
|
+
/** Validate and normalize an install/update payload into an id-addressed
|
|
233
|
+
* tree. Throws `TypeError` carrying a path (`menus[1].items[0]`) before any
|
|
234
|
+
* native write. Runs the whole pass before hosts see the tree. */
|
|
235
|
+
export function normalizeAppMenu(menus, options, platform, generation) {
|
|
236
|
+
if (!Array.isArray(menus)) {
|
|
237
|
+
fail('menus', 'must be an array of submenus');
|
|
238
|
+
}
|
|
239
|
+
const accelerators = new Map();
|
|
240
|
+
const context = { platform, nodes: new Map(), nextId: 1, accelerators };
|
|
241
|
+
if (platform === 'macos') {
|
|
242
|
+
for (const [source, owner] of MACOS_APPLICATION_MENU_ACCELERATORS) {
|
|
243
|
+
accelerators.set(canonicalAccelerator(parseAccelerator(source), platform), owner);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const normalized = [];
|
|
247
|
+
const topKeys = new Set();
|
|
248
|
+
for (const [index, menu] of menus.entries()) {
|
|
249
|
+
const path = `menus[${index}]`;
|
|
250
|
+
if (typeof menu !== 'object' ||
|
|
251
|
+
menu === null ||
|
|
252
|
+
menu.type !== 'submenu') {
|
|
253
|
+
fail(path, 'menu bar entries must be submenus');
|
|
254
|
+
}
|
|
255
|
+
const record = menu;
|
|
256
|
+
if (typeof record.key !== 'string' || !record.key) {
|
|
257
|
+
fail(path, 'key must be a non-empty string');
|
|
258
|
+
}
|
|
259
|
+
if (topKeys.has(record.key)) {
|
|
260
|
+
fail(path, `duplicate sibling key "${record.key}"`);
|
|
261
|
+
}
|
|
262
|
+
topKeys.add(record.key);
|
|
263
|
+
const label = requireLabel(record, path);
|
|
264
|
+
if (!Array.isArray(record.items)) {
|
|
265
|
+
fail(path, 'items must be an array');
|
|
266
|
+
}
|
|
267
|
+
normalized.push({
|
|
268
|
+
kind: 'submenu',
|
|
269
|
+
key: record.key,
|
|
270
|
+
label,
|
|
271
|
+
items: normalizeChildren(record.items, `${path}.items`, context),
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
// options.macos is namespaced — always validated, only consumed on macOS.
|
|
275
|
+
const applicationItems = options?.macos?.applicationItems;
|
|
276
|
+
let normalizedApplicationItems = [];
|
|
277
|
+
if (applicationItems !== undefined) {
|
|
278
|
+
if (!Array.isArray(applicationItems)) {
|
|
279
|
+
fail('options.macos.applicationItems', 'must be an array');
|
|
280
|
+
}
|
|
281
|
+
if (platform === 'macos') {
|
|
282
|
+
normalizedApplicationItems = normalizeChildren(applicationItems, 'options.macos.applicationItems', context);
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
const dormant = {
|
|
286
|
+
platform: null,
|
|
287
|
+
nodes: new Map(),
|
|
288
|
+
nextId: context.nextId,
|
|
289
|
+
accelerators: null,
|
|
290
|
+
};
|
|
291
|
+
normalizedApplicationItems = normalizeChildren(applicationItems, 'options.macos.applicationItems', dormant);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return {
|
|
295
|
+
platform,
|
|
296
|
+
generation,
|
|
297
|
+
menus: normalized,
|
|
298
|
+
applicationItems: normalizedApplicationItems,
|
|
299
|
+
nodes: context.nodes,
|
|
300
|
+
};
|
|
301
|
+
}
|
package/dist/shared.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native desktop application menu bar API — the top-level menu owned by the
|
|
3
|
+
* application (NSApp.mainMenu on macOS, the GApplication menubar on GTK4,
|
|
4
|
+
* native buttons and MenuFlyouts above Windows window content).
|
|
5
|
+
* Distinct from in-window DropdownMenu/ContextMenu surfaces and from
|
|
6
|
+
* tray/status-bar icons.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { appMenu } from '@octane-xplat/app-menu'
|
|
11
|
+
*
|
|
12
|
+
* const handle = appMenu.install([
|
|
13
|
+
* { type: 'submenu', key: 'file', label: 'File', items: [
|
|
14
|
+
* { type: 'action', key: 'new', label: 'New', accelerator: 'Mod+N', onSelect: newDocument },
|
|
15
|
+
* { type: 'separator', key: 'after-new' },
|
|
16
|
+
* { type: 'role', key: 'quit', role: 'quit' },
|
|
17
|
+
* ]},
|
|
18
|
+
* ])
|
|
19
|
+
* // Later: handle.update(nextMenus) or handle.dispose()
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@octane-xplat/app-menu",
|
|
3
|
+
"version": "0.20.0",
|
|
4
|
+
"description": "Native desktop application menu bar for macOS, Windows and GTK apps",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/octane-xplat/octane-xplat",
|
|
9
|
+
"directory": "packages/app-menu"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"types/generated",
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"type": "module",
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./types/generated/index.d.ts",
|
|
20
|
+
"macos": "./dist/index.macos.js",
|
|
21
|
+
"gtk": "./dist/index.gtk.js",
|
|
22
|
+
"windows": "./dist/index.windows.js",
|
|
23
|
+
"web": "./dist/index.web.js",
|
|
24
|
+
"browser": "./dist/index.web.js",
|
|
25
|
+
"default": "./dist/index.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsc --runExternalCode -p tsconfig.build.json --declaration false",
|
|
33
|
+
"typegen": "tsrx-typegen --target app-menu",
|
|
34
|
+
"typecheck": "node ../../scripts/typecheck.ts",
|
|
35
|
+
"test": "vitest run",
|
|
36
|
+
"pack:check": "tsrx-typegen --pack-check",
|
|
37
|
+
"prepack": "bun run build && tsrx-typegen --pack-check",
|
|
38
|
+
"test:packed": "node tests/packed-consumer.mjs",
|
|
39
|
+
"test:gtk-runtime": "node tests/gtk-runtime.mjs",
|
|
40
|
+
"test:windows-runtime": "node tests/windows-runtime.mjs"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@octane-xplat/lib": "0.20.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"jsdom": "30.1.1",
|
|
47
|
+
"tsrx-typegen": "0.6.1",
|
|
48
|
+
"typescript": "7.1.0-beta",
|
|
49
|
+
"vite": "8.3.6",
|
|
50
|
+
"vitest": "4.1.11",
|
|
51
|
+
"@tsrx/content-mapper": "0.1.2",
|
|
52
|
+
"@nativescript/core": "https://pkg.pr.new/@nativescript/core@abae298"
|
|
53
|
+
},
|
|
54
|
+
"types": "./types/generated/index.d.ts",
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"@nativescript/core": ">=9.1.4-0 <10"
|
|
57
|
+
},
|
|
58
|
+
"peerDependenciesMeta": {
|
|
59
|
+
"@nativescript/core": {
|
|
60
|
+
"optional": true
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/** Parsed accelerator: one key plus the pressed modifiers. `mod` is the
|
|
2
|
+
* platform's primary modifier (Command on macOS, Control elsewhere); `meta`
|
|
3
|
+
* is Command on macOS but the Super/Windows key elsewhere. */
|
|
4
|
+
export interface ParsedAccelerator {
|
|
5
|
+
key: string;
|
|
6
|
+
modifiers: {
|
|
7
|
+
mod: boolean;
|
|
8
|
+
ctrl: boolean;
|
|
9
|
+
alt: boolean;
|
|
10
|
+
shift: boolean;
|
|
11
|
+
meta: boolean;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export interface AppKitAccelerator {
|
|
15
|
+
keyEquivalent: string;
|
|
16
|
+
modifierMask: number;
|
|
17
|
+
}
|
|
18
|
+
export interface WinUIAccelerator {
|
|
19
|
+
key: number;
|
|
20
|
+
modifiers: number;
|
|
21
|
+
}
|
|
22
|
+
/** Parse a `'Mod+Shift+Z'`-style accelerator. `+`-separated, case-insensitive
|
|
23
|
+
* modifier tokens (`Mod`, `Ctrl`/`Control`, `Alt`/`Option`, `Shift`,
|
|
24
|
+
* `Meta`/`Cmd`/`Command`/`Super`) plus exactly one key: `A`–`Z`, `0`–`9`,
|
|
25
|
+
* `F1`–`F24`, `Enter`, `Escape`, `Tab`, `Space`, `Backspace`, `Delete`,
|
|
26
|
+
* `ArrowUp/Down/Left/Right`, `Home`, `End`, `PageUp`, `PageDown`, `Plus`, or
|
|
27
|
+
* the punctuation `` , . / ; ' [ ] \ - = ` ``. At least one non-Shift
|
|
28
|
+
* modifier is required unless the key is F1–F24 — plain letters as app
|
|
29
|
+
* accelerators would steal typing. */
|
|
30
|
+
export declare function parseAccelerator(source: string): ParsedAccelerator;
|
|
31
|
+
/** Map to AppKit `{ keyEquivalent, modifierMask }`. `mod` and `meta` are both
|
|
32
|
+
* Command here. */
|
|
33
|
+
export declare function toAppKit(parsed: ParsedAccelerator): AppKitAccelerator;
|
|
34
|
+
/** Map to a GTK accelerator string such as `<Control><Shift>z`. `mod` is
|
|
35
|
+
* Control and `meta` is Super here. */
|
|
36
|
+
export declare function toGtk(parsed: ParsedAccelerator): string;
|
|
37
|
+
/** Map to WinUI `{ key: VirtualKey, modifiers: VirtualKeyModifiers }`.
|
|
38
|
+
* `mod` is Control and `meta` is the Windows key here. */
|
|
39
|
+
export declare function toWinUI(parsed: ParsedAccelerator): WinUIAccelerator;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { AppMenuService } from './shared.js';
|
|
2
|
+
import type { AppMenuHost } from './host-types.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 declare function createAppMenuService(host: AppMenuHost): AppMenuService;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { NormalizedAppMenu } from './model.js';
|
|
2
|
+
export type AppMenuPlatform = 'macos' | 'linux' | 'windows' | null;
|
|
3
|
+
/** Native → controller callback. `generation` is the generation of the tree
|
|
4
|
+
* the native item was built from; the controller ignores stale generations,
|
|
5
|
+
* disposed handles and unknown ids. Returns whether the item is still
|
|
6
|
+
* current — hosts use it to decide whether a role item may act. */
|
|
7
|
+
export type AppMenuDispatch = (id: number, generation: number) => boolean;
|
|
8
|
+
/** One applied native menu. `update` swaps in a rebuilt tree; `dispose`
|
|
9
|
+
* tears down and restores whatever the platform had before install. */
|
|
10
|
+
export interface NativeAppMenu {
|
|
11
|
+
update(tree: NormalizedAppMenu): void;
|
|
12
|
+
dispose(): void;
|
|
13
|
+
}
|
|
14
|
+
/** Platform adapter owned by each index leaf. `install` is only reached when
|
|
15
|
+
* `supported()` is true. `defer` schedules the coalesced native
|
|
16
|
+
* update/dispose that follows a callback-triggered tree change. */
|
|
17
|
+
export interface AppMenuHost {
|
|
18
|
+
readonly platform: AppMenuPlatform;
|
|
19
|
+
supported(): boolean;
|
|
20
|
+
defer(fn: () => void): void;
|
|
21
|
+
install(tree: NormalizedAppMenu, dispatch: AppMenuDispatch): NativeAppMenu;
|
|
22
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { AppMenuService } from './shared.js';
|
|
2
|
+
export * from './shared.js';
|
|
3
|
+
/** The application menu service — install at most one menu bar per process.
|
|
4
|
+
* Import-safe: no native work happens until `install`.
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { appMenu } from '@octane-xplat/app-menu'
|
|
8
|
+
* if (appMenu.supported) {
|
|
9
|
+
* const handle = appMenu.install([
|
|
10
|
+
* { type: 'submenu', key: 'file', label: 'File', items: [] },
|
|
11
|
+
* ])
|
|
12
|
+
* }
|
|
13
|
+
* ``` */
|
|
14
|
+
export declare const appMenu: AppMenuService;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { type ParsedAccelerator } from './accelerator.js';
|
|
2
|
+
import type { AppMenuOptions, AppMenuRole, AppMenuSubmenu } from './shared.js';
|
|
3
|
+
import type { AppMenuPlatform } from './host-types.js';
|
|
4
|
+
export interface NormalizedAction {
|
|
5
|
+
kind: 'action';
|
|
6
|
+
id: number;
|
|
7
|
+
key: string;
|
|
8
|
+
label: string;
|
|
9
|
+
accelerator: ParsedAccelerator | null;
|
|
10
|
+
disabled: boolean;
|
|
11
|
+
onSelect(): void;
|
|
12
|
+
}
|
|
13
|
+
export interface NormalizedCheckbox {
|
|
14
|
+
kind: 'checkbox';
|
|
15
|
+
id: number;
|
|
16
|
+
key: string;
|
|
17
|
+
label: string;
|
|
18
|
+
value: boolean;
|
|
19
|
+
accelerator: ParsedAccelerator | null;
|
|
20
|
+
disabled: boolean;
|
|
21
|
+
onValueChange(value: boolean): void;
|
|
22
|
+
}
|
|
23
|
+
export interface NormalizedRole {
|
|
24
|
+
kind: 'role';
|
|
25
|
+
id: number;
|
|
26
|
+
key: string;
|
|
27
|
+
role: AppMenuRole;
|
|
28
|
+
label: string;
|
|
29
|
+
}
|
|
30
|
+
export interface NormalizedSeparator {
|
|
31
|
+
kind: 'separator';
|
|
32
|
+
key: string;
|
|
33
|
+
}
|
|
34
|
+
export interface NormalizedSubmenu {
|
|
35
|
+
kind: 'submenu';
|
|
36
|
+
key: string;
|
|
37
|
+
label: string;
|
|
38
|
+
items: NormalizedEntry[];
|
|
39
|
+
}
|
|
40
|
+
export type NormalizedEntry = NormalizedAction | NormalizedCheckbox | NormalizedRole | NormalizedSeparator | NormalizedSubmenu;
|
|
41
|
+
export type NormalizedActionable = NormalizedAction | NormalizedCheckbox | NormalizedRole;
|
|
42
|
+
/** Immutable id-addressed tree handed to hosts. `generation` identifies this
|
|
43
|
+
* applied tree — native callbacks pass it back through dispatch so stale
|
|
44
|
+
* items from a superseded tree can be ignored. */
|
|
45
|
+
export interface NormalizedAppMenu {
|
|
46
|
+
platform: AppMenuPlatform;
|
|
47
|
+
generation: number;
|
|
48
|
+
menus: NormalizedSubmenu[];
|
|
49
|
+
/** Normalized `options.macos.applicationItems` — consumed by the macOS
|
|
50
|
+
* host, ignored elsewhere. */
|
|
51
|
+
applicationItems: NormalizedEntry[];
|
|
52
|
+
nodes: ReadonlyMap<number, NormalizedActionable>;
|
|
53
|
+
}
|
|
54
|
+
export declare const ROLE_DEFAULT_LABELS: Record<AppMenuRole, string>;
|
|
55
|
+
/** Validate and normalize an install/update payload into an id-addressed
|
|
56
|
+
* tree. Throws `TypeError` carrying a path (`menus[1].items[0]`) before any
|
|
57
|
+
* native write. Runs the whole pass before hosts see the tree. */
|
|
58
|
+
export declare function normalizeAppMenu(menus: readonly AppMenuSubmenu[], options: AppMenuOptions | undefined, platform: AppMenuPlatform, generation: number): NormalizedAppMenu;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native desktop application menu bar API — the top-level menu owned by the
|
|
3
|
+
* application (NSApp.mainMenu on macOS, the GApplication menubar on GTK4,
|
|
4
|
+
* native buttons and MenuFlyouts above Windows window content).
|
|
5
|
+
* Distinct from in-window DropdownMenu/ContextMenu surfaces and from
|
|
6
|
+
* tray/status-bar icons.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { appMenu } from '@octane-xplat/app-menu'
|
|
11
|
+
*
|
|
12
|
+
* const handle = appMenu.install([
|
|
13
|
+
* { type: 'submenu', key: 'file', label: 'File', items: [
|
|
14
|
+
* { type: 'action', key: 'new', label: 'New', accelerator: 'Mod+N', onSelect: newDocument },
|
|
15
|
+
* { type: 'separator', key: 'after-new' },
|
|
16
|
+
* { type: 'role', key: 'quit', role: 'quit' },
|
|
17
|
+
* ]},
|
|
18
|
+
* ])
|
|
19
|
+
* // Later: handle.update(nextMenus) or handle.dispose()
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
/** Standard item behavior the platform menu engine already owns — no
|
|
23
|
+
* callback, no custom accelerator. `quit` only appears inside the automatic
|
|
24
|
+
* macOS application menu; GTK supplies its application action. Windows
|
|
25
|
+
* explicitly rejects every role; use action callbacks on that host. */
|
|
26
|
+
export type AppMenuRole = 'undo' | 'redo' | 'cut' | 'copy' | 'paste' | 'selectAll' | 'minimize' | 'close' | 'quit';
|
|
27
|
+
/** A plain menu item that runs `onSelect` when activated.
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* const open: AppMenuAction = {
|
|
31
|
+
* type: 'action', key: 'open', label: 'Open…',
|
|
32
|
+
* accelerator: 'Mod+O', onSelect: () => openFile(),
|
|
33
|
+
* }
|
|
34
|
+
* ``` */
|
|
35
|
+
export interface AppMenuAction {
|
|
36
|
+
type: 'action';
|
|
37
|
+
/** Stable identity among sibling entries; unique per parent menu. */
|
|
38
|
+
key: string;
|
|
39
|
+
label: string;
|
|
40
|
+
/** `'Mod+Shift+Z'`-style accelerator — see the package accelerator grammar. */
|
|
41
|
+
accelerator?: string;
|
|
42
|
+
isDisabled?: boolean;
|
|
43
|
+
onSelect(): void;
|
|
44
|
+
}
|
|
45
|
+
/** A checkable menu item. Controlled: activation reports the *next* value via
|
|
46
|
+
* `onValueChange(!value)`; the native check mark only moves when the app
|
|
47
|
+
* installs a new `value` through `update()`.
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* const sidebar: AppMenuCheckbox = {
|
|
51
|
+
* type: 'checkbox', key: 'sidebar', label: 'Sidebar',
|
|
52
|
+
* value: showSidebar, accelerator: 'Mod+Shift+S',
|
|
53
|
+
* onValueChange: (next) => setSidebar(next),
|
|
54
|
+
* }
|
|
55
|
+
* ``` */
|
|
56
|
+
export interface AppMenuCheckbox {
|
|
57
|
+
type: 'checkbox';
|
|
58
|
+
key: string;
|
|
59
|
+
label: string;
|
|
60
|
+
value: boolean;
|
|
61
|
+
accelerator?: string;
|
|
62
|
+
isDisabled?: boolean;
|
|
63
|
+
onValueChange(value: boolean): void;
|
|
64
|
+
}
|
|
65
|
+
/** A separator row. Leading, trailing and repeated separators collapse on
|
|
66
|
+
* every platform (GTK sections require it). */
|
|
67
|
+
export interface AppMenuSeparator {
|
|
68
|
+
type: 'separator';
|
|
69
|
+
key: string;
|
|
70
|
+
}
|
|
71
|
+
/** A nested menu. Menu-bar entries are always submenus; nested submenus are
|
|
72
|
+
* allowed inside them. Submenus cannot be disabled — GMenu has no such
|
|
73
|
+
* concept. */
|
|
74
|
+
export interface AppMenuSubmenu {
|
|
75
|
+
type: 'submenu';
|
|
76
|
+
key: string;
|
|
77
|
+
label: string;
|
|
78
|
+
items: readonly AppMenuEntry[];
|
|
79
|
+
}
|
|
80
|
+
/** A standard item the platform owns: fixed default label (overridable via
|
|
81
|
+
* `label`), fixed accelerator, native behavior. See the package role matrix
|
|
82
|
+
* for per-platform behavior.
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* const copy: AppMenuRoleItem = { type: 'role', key: 'copy', role: 'copy' }
|
|
86
|
+
* ``` */
|
|
87
|
+
export interface AppMenuRoleItem {
|
|
88
|
+
type: 'role';
|
|
89
|
+
key: string;
|
|
90
|
+
role: AppMenuRole;
|
|
91
|
+
/** Overrides the default English label. */
|
|
92
|
+
label?: string;
|
|
93
|
+
}
|
|
94
|
+
/** One menu entry — action, checkbox, separator, submenu or platform role. */
|
|
95
|
+
export type AppMenuEntry = AppMenuAction | AppMenuCheckbox | AppMenuSeparator | AppMenuSubmenu | AppMenuRoleItem;
|
|
96
|
+
/** Per-platform install options. Namespaced: other platforms ignore
|
|
97
|
+
* `options.macos` rather than erroring. */
|
|
98
|
+
export interface AppMenuOptions {
|
|
99
|
+
macos?: {
|
|
100
|
+
/** Extra entries inserted into the automatic application menu, between
|
|
101
|
+
* the About item and the Hide/Show All group. */
|
|
102
|
+
applicationItems?: readonly AppMenuEntry[];
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
/** Handle for the installed menu. `update` replaces the whole tree
|
|
106
|
+
* (validated synchronously before any native write); `dispose` restores the
|
|
107
|
+
* previous menu bar and is idempotent. */
|
|
108
|
+
export interface AppMenu {
|
|
109
|
+
update(menus: readonly AppMenuSubmenu[], options?: AppMenuOptions): void;
|
|
110
|
+
dispose(): void;
|
|
111
|
+
readonly isDisposed: boolean;
|
|
112
|
+
}
|
|
113
|
+
/** The flat capability service. `supported` is a cheap sync check evaluated
|
|
114
|
+
* on each read; `isAvailable()` is the authoritative async probe. `install`
|
|
115
|
+
* throws `UnavailabilityError('appMenu')` on absent platforms and
|
|
116
|
+
* `Error('An app menu is already installed; update or dispose it first')`
|
|
117
|
+
* while another handle is live — one owner per process.
|
|
118
|
+
* @example
|
|
119
|
+
* ```ts
|
|
120
|
+
* import { appMenu } from '@octane-xplat/app-menu'
|
|
121
|
+
* if (appMenu.supported) {
|
|
122
|
+
* const handle = appMenu.install(menus)
|
|
123
|
+
* }
|
|
124
|
+
* ``` */
|
|
125
|
+
export interface AppMenuService {
|
|
126
|
+
readonly supported: boolean;
|
|
127
|
+
isAvailable(): Promise<boolean>;
|
|
128
|
+
install(menus: readonly AppMenuSubmenu[], options?: AppMenuOptions): AppMenu;
|
|
129
|
+
}
|