-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Expand file tree
/
Copy pathAppMenu.js
More file actions
162 lines (152 loc) · 4.27 KB
/
Copy pathAppMenu.js
File metadata and controls
162 lines (152 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
const {BrowserWindow, Menu, app, nativeImage, shell} =
// $FlowFixMe[unclear-type] We have no Flow types for the Electron API.
require('electron') as any;
const {isMacOSAtLeast} = require('./utils');
export function configureAppMenu(): void {
const template = [
...(process.platform === 'darwin' ? [{role: 'appMenu'}] : []),
{
label: 'File',
submenu: [
{
label: 'Reload App',
...menuSymbol('arrow.clockwise'),
accelerator:
process.platform === 'darwin' ? 'Command+R' : 'Control+R',
click: () => invokeCommand('inspector-main.reload'),
},
{
label: 'Reload DevTools',
...menuSymbol('none'),
accelerator: process.platform === 'darwin' ? 'Option+R' : 'Alt+R',
click: () => BrowserWindow.getFocusedWindow()?.webContents.reload(),
},
{type: 'separator'},
{
label: 'Quick Open…',
...menuSymbol('doc.text.magnifyingglass'),
accelerator:
process.platform === 'darwin' ? 'Command+P' : 'Control+P',
click: () => invokeCommand('quick-open.show'),
},
{type: 'separator'},
{role: 'close'},
],
},
{
label: 'Edit',
submenu: [
{role: 'undo'},
{role: 'redo'},
{type: 'separator'},
{role: 'cut'},
{role: 'copy'},
{role: 'paste'},
{role: 'selectAll'},
],
},
{
label: 'View',
submenu: [
{
label: 'Command Palette…',
...menuSymbol('filemenu.and.selection'),
accelerator:
process.platform === 'darwin'
? 'Command+Shift+P'
: 'Control+Shift+P',
click: () => invokeCommand('quick-open.show-command-menu'),
},
{type: 'separator'},
// Enable Developer Tools only in development
...(!app.isPackaged
? [{type: 'separator'}, {role: 'toggleDevTools'}]
: []),
{type: 'separator'},
{role: 'resetZoom'},
{role: 'zoomIn'},
{role: 'zoomOut'},
{type: 'separator'},
{role: 'togglefullscreen'},
],
},
{role: 'windowMenu'},
{
role: 'help',
submenu: [
{
label: 'Keyboard Shortcuts',
...menuSymbol('keyboard'),
click: () => invokeCommand('settings.shortcuts'),
},
{type: 'separator'},
{
label: 'React Native Website',
...menuSymbol('text.rectangle.page'),
click: () => shell.openExternal('https://reactnative.dev'),
},
{
label: 'Release Notes',
...menuSymbol('none'),
click: () =>
shell.openExternal(
'https://github.com/facebook/react-native/releases',
),
},
],
},
];
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
function menuSymbol(symbolName: string): {icon?: unknown} {
if (!isMacOSAtLeast(26)) {
return {};
}
return {
icon:
symbolName === 'none'
? createMenuSpacer()
: nativeImage.createMenuSymbol(symbolName),
};
}
function createMenuSpacer() {
const size = 16;
const buf = Buffer.alloc(size * size * 4);
for (let i = 0; i < buf.length; i += 4) {
buf[i] = 0; // R
buf[i + 1] = 0; // G
buf[i + 2] = 0; // B
buf[i + 3] = 1; // A
}
const spacer = nativeImage.createFromBitmap(buf, {
width: size,
height: size,
});
// On macOS, mark as template so it lives in the same gutter as other
// template icons and adapts to light/dark menu bars.
if (process.platform === 'darwin') {
spacer.setTemplateImage(true);
}
return spacer;
}
function invokeCommand(commandId: string): void {
const win = BrowserWindow.getFocusedWindow();
win.webContents.executeJavaScript(
`(async () => {
const UI = await import('./ui/legacy/legacy.js');
return UI.ActionRegistry.ActionRegistry.instance()
.getAction(${JSON.stringify(commandId)})?.execute();
})()`,
true,
);
}