|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +const {BrowserWindow, Menu, app, nativeImage, shell} = |
| 12 | + // $FlowFixMe[unclear-type] We have no Flow types for the Electron API. |
| 13 | + require('electron') as any; |
| 14 | + |
| 15 | +const {isMacOSAtLeast} = require('./utils'); |
| 16 | + |
| 17 | +export function configureAppMenu(): void { |
| 18 | + const template = [ |
| 19 | + ...(process.platform === 'darwin' ? [{role: 'appMenu'}] : []), |
| 20 | + { |
| 21 | + label: 'File', |
| 22 | + submenu: [ |
| 23 | + { |
| 24 | + label: 'Reload App', |
| 25 | + accelerator: |
| 26 | + process.platform === 'darwin' ? 'Command+R' : 'Control+R', |
| 27 | + click: () => invokeCommand('inspector-main.reload'), |
| 28 | + }, |
| 29 | + { |
| 30 | + label: 'Reload DevTools', |
| 31 | + accelerator: process.platform === 'darwin' ? 'Option+R' : 'Alt+R', |
| 32 | + click: () => BrowserWindow.getFocusedWindow()?.webContents.reload(), |
| 33 | + }, |
| 34 | + {type: 'separator'}, |
| 35 | + { |
| 36 | + label: 'Quick Open…', |
| 37 | + ...menuSymbol('doc.text.magnifyingglass'), |
| 38 | + accelerator: |
| 39 | + process.platform === 'darwin' ? 'Command+P' : 'Control+P', |
| 40 | + click: () => invokeCommand('quick-open.show'), |
| 41 | + }, |
| 42 | + {type: 'separator'}, |
| 43 | + {role: 'close'}, |
| 44 | + ], |
| 45 | + }, |
| 46 | + { |
| 47 | + label: 'Edit', |
| 48 | + submenu: [ |
| 49 | + {role: 'undo'}, |
| 50 | + {role: 'redo'}, |
| 51 | + {type: 'separator'}, |
| 52 | + {role: 'cut'}, |
| 53 | + {role: 'copy'}, |
| 54 | + {role: 'paste'}, |
| 55 | + {role: 'selectAll'}, |
| 56 | + ], |
| 57 | + }, |
| 58 | + { |
| 59 | + label: 'View', |
| 60 | + submenu: [ |
| 61 | + { |
| 62 | + label: 'Command Palette…', |
| 63 | + ...menuSymbol('filemenu.and.selection'), |
| 64 | + accelerator: |
| 65 | + process.platform === 'darwin' |
| 66 | + ? 'Command+Shift+P' |
| 67 | + : 'Control+Shift+P', |
| 68 | + click: () => invokeCommand('quick-open.show-command-menu'), |
| 69 | + }, |
| 70 | + // Enable Developer Tools only in development |
| 71 | + ...(!app.isPackaged |
| 72 | + ? [{type: 'separator'}, {role: 'toggleDevTools'}] |
| 73 | + : []), |
| 74 | + {type: 'separator'}, |
| 75 | + {role: 'resetZoom'}, |
| 76 | + {role: 'zoomIn'}, |
| 77 | + {role: 'zoomOut'}, |
| 78 | + {type: 'separator'}, |
| 79 | + {role: 'togglefullscreen'}, |
| 80 | + ], |
| 81 | + }, |
| 82 | + {role: 'windowMenu'}, |
| 83 | + { |
| 84 | + role: 'help', |
| 85 | + submenu: [ |
| 86 | + { |
| 87 | + label: 'Keyboard Shortcuts', |
| 88 | + ...menuSymbol('keyboard'), |
| 89 | + click: () => invokeCommand('settings.shortcuts'), |
| 90 | + }, |
| 91 | + {type: 'separator'}, |
| 92 | + { |
| 93 | + label: 'React Native Website', |
| 94 | + click: () => shell.openExternal('https://reactnative.dev'), |
| 95 | + }, |
| 96 | + { |
| 97 | + label: 'Release Notes', |
| 98 | + click: () => |
| 99 | + shell.openExternal( |
| 100 | + 'https://github.com/facebook/react-native/releases', |
| 101 | + ), |
| 102 | + }, |
| 103 | + ], |
| 104 | + }, |
| 105 | + ]; |
| 106 | + const menu = Menu.buildFromTemplate(template); |
| 107 | + Menu.setApplicationMenu(menu); |
| 108 | +} |
| 109 | + |
| 110 | +function menuSymbol(symbolName: string): {icon?: unknown} { |
| 111 | + if (!isMacOSAtLeast(26)) { |
| 112 | + return {}; |
| 113 | + } |
| 114 | + return { |
| 115 | + icon: nativeImage.createMenuSymbol(symbolName), |
| 116 | + }; |
| 117 | +} |
| 118 | + |
| 119 | +function invokeCommand(commandId: string): void { |
| 120 | + const win = BrowserWindow.getFocusedWindow(); |
| 121 | + win?.webContents.executeJavaScript( |
| 122 | + `(async () => { |
| 123 | + const UI = await import('./ui/legacy/legacy.js'); |
| 124 | + return UI.ActionRegistry.ActionRegistry.instance() |
| 125 | + .getAction(${JSON.stringify(commandId)})?.execute(); |
| 126 | + })()`, |
| 127 | + true, |
| 128 | + ); |
| 129 | +} |
0 commit comments