@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.
@@ -0,0 +1,154 @@
1
+ import { lookupGlobal, UnavailabilityError } from '@octane-xplat/lib/capability';
2
+ import { parseAccelerator, toAppKit } from './accelerator.js';
3
+ const NS_OPTION = 1 << 19;
4
+ const NS_COMMAND = 1 << 20;
5
+ /** Role → nil-target selector and registered key equivalent on macOS. `quit`
6
+ * is absent: outside the automatic application menu it is omitted, and the
7
+ * app menu owns Quit ⌘Q. */
8
+ const ROLE_ITEMS = {
9
+ undo: { selector: 'undo:', accelerator: 'Mod+Z' },
10
+ redo: { selector: 'redo:', accelerator: 'Mod+Shift+Z' },
11
+ cut: { selector: 'cut:', accelerator: 'Mod+X' },
12
+ copy: { selector: 'copy:', accelerator: 'Mod+C' },
13
+ paste: { selector: 'paste:', accelerator: 'Mod+V' },
14
+ selectAll: { selector: 'selectAll:', accelerator: 'Mod+A' },
15
+ minimize: { selector: 'performMiniaturize:', accelerator: 'Mod+M' },
16
+ close: { selector: 'performClose:', accelerator: 'Mod+W' },
17
+ };
18
+ const ROLE_KEY_EQUIVALENTS = new Map();
19
+ for (const role of Object.keys(ROLE_ITEMS)) {
20
+ ROLE_KEY_EQUIVALENTS.set(role, toAppKit(parseAccelerator(ROLE_ITEMS[role].accelerator)));
21
+ }
22
+ function applicationName() {
23
+ const name = lookupGlobal(['NSProcessInfo', 'processInfo', 'processName']);
24
+ return typeof name === 'string' && name ? name : 'App';
25
+ }
26
+ function convertEntry(node, dispatch, generation) {
27
+ switch (node.kind) {
28
+ case 'separator':
29
+ return { type: 'separator' };
30
+ case 'submenu':
31
+ return {
32
+ type: 'submenu',
33
+ title: node.label,
34
+ children: convertItems(node.items, dispatch, generation),
35
+ };
36
+ case 'action': {
37
+ const accel = node.accelerator ? toAppKit(node.accelerator) : null;
38
+ return {
39
+ type: 'item',
40
+ title: node.label,
41
+ keyEquivalent: accel?.keyEquivalent ?? '',
42
+ modifierMask: accel?.modifierMask ?? 0,
43
+ enabled: !node.disabled,
44
+ state: 0,
45
+ onSelect: () => {
46
+ dispatch(node.id, generation);
47
+ },
48
+ };
49
+ }
50
+ case 'checkbox': {
51
+ const accel = node.accelerator ? toAppKit(node.accelerator) : null;
52
+ return {
53
+ type: 'item',
54
+ title: node.label,
55
+ keyEquivalent: accel?.keyEquivalent ?? '',
56
+ modifierMask: accel?.modifierMask ?? 0,
57
+ enabled: !node.disabled,
58
+ state: node.value ? 1 : 0,
59
+ onSelect: () => {
60
+ dispatch(node.id, generation);
61
+ },
62
+ };
63
+ }
64
+ case 'role': {
65
+ if (node.role === 'quit') {
66
+ return null;
67
+ }
68
+ const role = ROLE_ITEMS[node.role];
69
+ const accel = ROLE_KEY_EQUIVALENTS.get(node.role);
70
+ return {
71
+ type: 'item',
72
+ title: node.label,
73
+ keyEquivalent: accel.keyEquivalent,
74
+ modifierMask: accel.modifierMask,
75
+ enabled: true,
76
+ state: 0,
77
+ selector: role.selector,
78
+ };
79
+ }
80
+ }
81
+ }
82
+ function convertItems(items, dispatch, generation) {
83
+ const nodes = [];
84
+ for (const item of items) {
85
+ const node = convertEntry(item, dispatch, generation);
86
+ if (node === null) {
87
+ continue;
88
+ }
89
+ // Quit-role omission can strand separators — collapse again here.
90
+ if (node.type === 'separator' &&
91
+ (nodes.length === 0 || nodes[nodes.length - 1].type === 'separator')) {
92
+ continue;
93
+ }
94
+ nodes.push(node);
95
+ }
96
+ if (nodes.length && nodes[nodes.length - 1].type === 'separator') {
97
+ nodes.pop();
98
+ }
99
+ return nodes;
100
+ }
101
+ function item(title, selector, keyEquivalent = '', modifierMask = 0) {
102
+ return { type: 'item', title, keyEquivalent, modifierMask, enabled: true, state: 0, selector };
103
+ }
104
+ /** The standard application menu always leads the bar: About, separator,
105
+ * `options.macos.applicationItems` (+ separator), Hide ⌘H, Hide Others ⌥⌘H,
106
+ * Show All, separator, Quit ⌘Q. */
107
+ function applicationMenu(tree, dispatch) {
108
+ const name = applicationName();
109
+ const children = [item(`About ${name}`, 'orderFrontStandardAboutPanel:')];
110
+ const extras = convertItems(tree.applicationItems, dispatch, tree.generation);
111
+ if (extras.length) {
112
+ children.push({ type: 'separator' }, ...extras);
113
+ }
114
+ children.push({ type: 'separator' }, item(`Hide ${name}`, 'hide:', 'h', NS_COMMAND), item('Hide Others', 'hideOtherApplications:', 'h', NS_COMMAND | NS_OPTION), item('Show All', 'unhideAllApplications:'), { type: 'separator' }, item(`Quit ${name}`, 'terminate:', 'q', NS_COMMAND));
115
+ return { type: 'submenu', title: name, children };
116
+ }
117
+ function convertTree(tree, dispatch) {
118
+ const nodes = [applicationMenu(tree, dispatch)];
119
+ for (const menu of tree.menus) {
120
+ nodes.push({
121
+ type: 'submenu',
122
+ title: menu.label,
123
+ children: convertItems(menu.items, dispatch, tree.generation),
124
+ });
125
+ }
126
+ return nodes;
127
+ }
128
+ export const host = {
129
+ platform: 'macos',
130
+ supported() {
131
+ return typeof lookupGlobal(['__xplatAppKit', 'installApplicationMenu']) === 'function';
132
+ },
133
+ defer(fn) {
134
+ setTimeout(fn, 0);
135
+ },
136
+ install(tree, dispatch) {
137
+ const installer = lookupGlobal(['__xplatAppKit', 'installApplicationMenu']);
138
+ if (typeof installer !== 'function') {
139
+ throw new UnavailabilityError('appMenu');
140
+ }
141
+ const installed = installer(convertTree(tree, dispatch));
142
+ if (!installed) {
143
+ throw new UnavailabilityError('appMenu');
144
+ }
145
+ return {
146
+ update(next) {
147
+ installed.update(convertTree(next, dispatch));
148
+ },
149
+ dispose() {
150
+ installed.dispose();
151
+ },
152
+ };
153
+ },
154
+ };
@@ -0,0 +1,14 @@
1
+ import { UnavailabilityError } from '@octane-xplat/lib/capability';
2
+ // Browsers and webview runtimes expose no application menu bar.
3
+ export const host = {
4
+ platform: null,
5
+ supported() {
6
+ return false;
7
+ },
8
+ defer(fn) {
9
+ setTimeout(fn, 0);
10
+ },
11
+ install() {
12
+ throw new UnavailabilityError('appMenu');
13
+ },
14
+ };
@@ -0,0 +1,197 @@
1
+ import { findCurrentWindow } from '@nativescript/core/application/window-helper.windows';
2
+ import { UnavailabilityError } from '@octane-xplat/lib/capability';
3
+ import { toWinUI } from './accelerator.js';
4
+ // The runtime owns these globals. Keep the projection boundary local and named.
5
+ const projection = () => globalThis;
6
+ const window = () => findCurrentWindow() ?? undefined;
7
+ function validate(tree) {
8
+ // The pinned projection has no safe responder-chain equivalent or reliable
9
+ // application Exit. Reject roles before touching the installed native tree.
10
+ for (const node of tree.nodes.values()) {
11
+ if (node.kind === 'role') {
12
+ throw new Error(`App menu role "${node.role}" is unsupported on Windows; use an action callback`);
13
+ }
14
+ }
15
+ }
16
+ export const host = {
17
+ platform: 'windows',
18
+ supported() {
19
+ return Boolean(window()?.Content &&
20
+ projection().Microsoft?.UI.Xaml.Controls.MenuFlyout &&
21
+ projection().NSWinRT?.asDelegate);
22
+ },
23
+ defer(fn) {
24
+ setTimeout(fn, 0);
25
+ },
26
+ install(tree, dispatch) {
27
+ const owner = window();
28
+ const { Microsoft, NSWinRT } = projection();
29
+ if (!owner?.Content || !Microsoft || !NSWinRT) {
30
+ throw new UnavailabilityError('appMenu');
31
+ }
32
+ validate(tree);
33
+ const ui = Microsoft.UI.Xaml;
34
+ const C = ui.Controls;
35
+ const previous = owner.Content;
36
+ const wrapper = new C.Grid();
37
+ const top = new C.RowDefinition();
38
+ top.Height = ui.GridLengthHelper.Auto;
39
+ const rest = new C.RowDefinition();
40
+ rest.Height = ui.GridLengthHelper.FromValueAndType(1, ui.GridUnitType.Star);
41
+ wrapper.RowDefinitions.Append(top);
42
+ wrapper.RowDefinitions.Append(rest);
43
+ let disposed = false;
44
+ function build(next) {
45
+ validate(next);
46
+ let active = true;
47
+ const cleanups = [];
48
+ const bar = new C.StackPanel();
49
+ bar.Orientation = C.Orientation.Horizontal;
50
+ const accelerators = [];
51
+ const alive = () => active && !disposed && owner.Content === wrapper && window() === owner;
52
+ const click = (callback) => NSWinRT.asDelegate('Microsoft.UI.Xaml.RoutedEventHandler', callback);
53
+ const append = (items, entries) => {
54
+ for (const node of entries) {
55
+ if (node.kind === 'separator') {
56
+ items.Append(new C.MenuFlyoutSeparator());
57
+ }
58
+ else if (node.kind === 'submenu') {
59
+ const sub = new C.MenuFlyoutSubItem();
60
+ sub.Text = node.label;
61
+ append(sub.Items, node.items);
62
+ items.Append(sub);
63
+ }
64
+ else if (node.kind !== 'role') {
65
+ const item = node.kind === 'checkbox' ? new C.ToggleMenuFlyoutItem() : new C.MenuFlyoutItem();
66
+ item.Text = node.label;
67
+ item.IsEnabled = !node.disabled;
68
+ if (node.kind === 'checkbox') {
69
+ item.IsChecked = node.value;
70
+ }
71
+ const invoke = () => {
72
+ if (node.kind === 'checkbox') {
73
+ item.IsChecked = node.value;
74
+ }
75
+ return alive() && !node.disabled && dispatch(node.id, next.generation);
76
+ };
77
+ item.Click = click(() => {
78
+ invoke();
79
+ });
80
+ cleanups.push(() => {
81
+ item.Click = null;
82
+ });
83
+ if (node.accelerator) {
84
+ const keys = toWinUI(node.accelerator);
85
+ // Display on the item; register separately on the content root so
86
+ // the shortcut works without opening the flyout first.
87
+ const display = new ui.Input.KeyboardAccelerator();
88
+ display.Key = keys.key;
89
+ display.Modifiers = keys.modifiers;
90
+ display.Invoked = NSWinRT.asDelegate('Windows.Foundation.TypedEventHandler`2<Microsoft.UI.Xaml.Input.KeyboardAccelerator,Microsoft.UI.Xaml.Input.KeyboardAcceleratorInvokedEventArgs>', (_sender, args) => {
91
+ if (invoke()) {
92
+ args.Handled = true;
93
+ }
94
+ });
95
+ item.KeyboardAccelerators.Append(display);
96
+ const root = new ui.Input.KeyboardAccelerator();
97
+ root.Key = keys.key;
98
+ root.Modifiers = keys.modifiers;
99
+ root.Invoked = NSWinRT.asDelegate('Windows.Foundation.TypedEventHandler`2<Microsoft.UI.Xaml.Input.KeyboardAccelerator,Microsoft.UI.Xaml.Input.KeyboardAcceleratorInvokedEventArgs>', (_sender, args) => {
100
+ if (invoke()) {
101
+ args.Handled = true;
102
+ }
103
+ });
104
+ accelerators.push(root);
105
+ cleanups.push(() => {
106
+ display.Invoked = null;
107
+ root.Invoked = null;
108
+ item.KeyboardAccelerators.Clear();
109
+ });
110
+ }
111
+ items.Append(item);
112
+ }
113
+ }
114
+ };
115
+ const retire = () => {
116
+ active = false;
117
+ for (const cleanup of cleanups.reverse()) {
118
+ cleanup();
119
+ }
120
+ };
121
+ try {
122
+ for (const menu of next.menus) {
123
+ const button = new C.Button();
124
+ button.Content = menu.label;
125
+ const flyout = new C.MenuFlyout();
126
+ cleanups.push(() => {
127
+ flyout.Hide();
128
+ button.Click = null;
129
+ C.Primitives.FlyoutBase.SetAttachedFlyout(button, null);
130
+ });
131
+ append(flyout.Items, menu.items);
132
+ C.Primitives.FlyoutBase.SetAttachedFlyout(button, flyout);
133
+ button.Click = click(() => {
134
+ if (alive()) {
135
+ C.Primitives.FlyoutBase.ShowAttachedFlyout(button);
136
+ }
137
+ });
138
+ bar.Children.Append(button);
139
+ }
140
+ C.Grid.SetRow(bar, 0);
141
+ return { bar, accelerators, retire };
142
+ }
143
+ catch (error) {
144
+ retire();
145
+ throw error;
146
+ }
147
+ }
148
+ let current = build(tree);
149
+ // WinUI elements cannot have two parents. Detach before reparenting and
150
+ // restore if installation fails. The content keeps its original Grid.Row.
151
+ // We wrap it in its own grid rather than changing its attached properties.
152
+ const content = new C.Grid();
153
+ C.Grid.SetRow(content, 1);
154
+ try {
155
+ owner.Content = null;
156
+ content.Children.Append(previous);
157
+ wrapper.Children.Append(current.bar);
158
+ wrapper.Children.Append(content);
159
+ for (const accelerator of current.accelerators) {
160
+ wrapper.KeyboardAccelerators.Append(accelerator);
161
+ }
162
+ owner.Content = wrapper;
163
+ }
164
+ catch (error) {
165
+ current.retire();
166
+ content.Children.Clear();
167
+ owner.Content = previous;
168
+ throw error;
169
+ }
170
+ return {
171
+ update(next) {
172
+ const replacement = build(next);
173
+ wrapper.KeyboardAccelerators.Clear();
174
+ wrapper.Children.SetAt(0, replacement.bar);
175
+ for (const accelerator of replacement.accelerators) {
176
+ wrapper.KeyboardAccelerators.Append(accelerator);
177
+ }
178
+ const old = current;
179
+ current = replacement;
180
+ old.retire();
181
+ },
182
+ dispose() {
183
+ if (disposed) {
184
+ return;
185
+ }
186
+ disposed = true;
187
+ current.retire();
188
+ wrapper.KeyboardAccelerators.Clear();
189
+ wrapper.Children.Clear();
190
+ content.Children.Clear();
191
+ if (owner.Content === wrapper) {
192
+ owner.Content = previous;
193
+ }
194
+ },
195
+ };
196
+ },
197
+ };
@@ -0,0 +1,15 @@
1
+ import { createAppMenuService } from './controller.js';
2
+ import { host } from './host.gtk.js';
3
+ export * from './shared.js';
4
+ /** The application menu service — install at most one menu bar per process.
5
+ * Import-safe: no native work happens until `install`.
6
+ * @example
7
+ * ```ts
8
+ * import { appMenu } from '@octane-xplat/app-menu'
9
+ * if (appMenu.supported) {
10
+ * const handle = appMenu.install([
11
+ * { type: 'submenu', key: 'file', label: 'File', items: [] },
12
+ * ])
13
+ * }
14
+ * ``` */
15
+ export const appMenu = createAppMenuService(host);
package/dist/index.js ADDED
@@ -0,0 +1,15 @@
1
+ import { createAppMenuService } from './controller.js';
2
+ import { host } from './host.js';
3
+ export * from './shared.js';
4
+ /** The application menu service — install at most one menu bar per process.
5
+ * Import-safe: no native work happens until `install`.
6
+ * @example
7
+ * ```ts
8
+ * import { appMenu } from '@octane-xplat/app-menu'
9
+ * if (appMenu.supported) {
10
+ * const handle = appMenu.install([
11
+ * { type: 'submenu', key: 'file', label: 'File', items: [] },
12
+ * ])
13
+ * }
14
+ * ``` */
15
+ export const appMenu = createAppMenuService(host);
@@ -0,0 +1,15 @@
1
+ import { createAppMenuService } from './controller.js';
2
+ import { host } from './host.macos.js';
3
+ export * from './shared.js';
4
+ /** The application menu service — install at most one menu bar per process.
5
+ * Import-safe: no native work happens until `install`.
6
+ * @example
7
+ * ```ts
8
+ * import { appMenu } from '@octane-xplat/app-menu'
9
+ * if (appMenu.supported) {
10
+ * const handle = appMenu.install([
11
+ * { type: 'submenu', key: 'file', label: 'File', items: [] },
12
+ * ])
13
+ * }
14
+ * ``` */
15
+ export const appMenu = createAppMenuService(host);
@@ -0,0 +1,15 @@
1
+ import { createAppMenuService } from './controller.js';
2
+ import { host } from './host.web.js';
3
+ export * from './shared.js';
4
+ /** The application menu service — install at most one menu bar per process.
5
+ * Import-safe: no native work happens until `install`.
6
+ * @example
7
+ * ```ts
8
+ * import { appMenu } from '@octane-xplat/app-menu'
9
+ * if (appMenu.supported) {
10
+ * const handle = appMenu.install([
11
+ * { type: 'submenu', key: 'file', label: 'File', items: [] },
12
+ * ])
13
+ * }
14
+ * ``` */
15
+ export const appMenu = createAppMenuService(host);
@@ -0,0 +1,15 @@
1
+ import { createAppMenuService } from './controller.js';
2
+ import { host } from './host.windows.js';
3
+ export * from './shared.js';
4
+ /** The application menu service — install at most one menu bar per process.
5
+ * Import-safe: no native work happens until `install`.
6
+ * @example
7
+ * ```ts
8
+ * import { appMenu } from '@octane-xplat/app-menu'
9
+ * if (appMenu.supported) {
10
+ * const handle = appMenu.install([
11
+ * { type: 'submenu', key: 'file', label: 'File', items: [] },
12
+ * ])
13
+ * }
14
+ * ``` */
15
+ export const appMenu = createAppMenuService(host);