|
14 | 14 | const {app, globalShortcut, BrowserWindow, Menu, nativeTheme} = require('electron'); |
15 | 15 | const platformData = require('./os-checker'); |
16 | 16 |
|
17 | | -function registerGlobalShortcuts() { |
18 | | - const globalShortcuts = [ |
19 | | - {key: 'CommandOrControl+W', action: () => BrowserWindow.getFocusedWindow()?.close()}, |
20 | | - {key: 'CommandOrControl+M', action: () => BrowserWindow.getFocusedWindow()?.minimize()}, |
21 | | - {key: 'CommandOrControl+Shift+M', action: () => BrowserWindow.getFocusedWindow()?.maximize()}, |
22 | | - {key: 'CommandOrControl+Shift+F', action: () => BrowserWindow.getFocusedWindow()?.setFullScreen(true)}, |
23 | | - {key: 'CommandOrControl+Shift+G', action: () => BrowserWindow.getFocusedWindow()?.setFullScreen(false)}, |
24 | | - ]; |
| 17 | +const COMMON_SHORTCUTS = [ |
| 18 | + {key: 'CommandOrControl+W', action: win => win.close()}, |
| 19 | + {key: 'CommandOrControl+M', action: win => win.minimize()}, |
| 20 | + {key: 'CommandOrControl+Shift+M', action: win => win.maximize()}, |
| 21 | + {key: 'CommandOrControl+Shift+F', action: win => win.setFullScreen(true)}, |
| 22 | + {key: 'CommandOrControl+Shift+G', action: win => win.setFullScreen(false)}, |
| 23 | + // Edit operations |
| 24 | + {key: 'CommandOrControl+X', action: win => win.webContents.cut()}, |
| 25 | + {key: 'CommandOrControl+C', action: win => win.webContents.copy()}, |
| 26 | + {key: 'CommandOrControl+V', action: win => win.webContents.paste()}, |
| 27 | + {key: 'CommandOrControl+Z', action: win => win.webContents.undo()}, |
| 28 | + {key: 'CommandOrControl+A', action: win => win.webContents.selectAll()}, |
| 29 | + {key: 'CommandOrControl+R', action: win => win.reload()}, |
| 30 | +]; |
25 | 31 |
|
26 | | - const macShortcuts = [ |
27 | | - {key: 'CommandOrControl+Q', action: () => (BrowserWindow.getFocusedWindow() ? app.quit() : null)}, |
28 | | - {key: 'Command+Option+I', action: () => BrowserWindow.getFocusedWindow().webContents.openDevTools()}, |
29 | | - {key: 'CommandOrControl+R', action: () => BrowserWindow.getFocusedWindow()?.reload()}, |
30 | | - { |
31 | | - key: 'CommandOrControl+Shift+R', |
32 | | - action: () => BrowserWindow.getFocusedWindow()?.webContents.reloadIgnoringCache(), |
| 32 | +const MAC_SHORTCUTS = [ |
| 33 | + { |
| 34 | + key: 'CommandOrControl+Q', |
| 35 | + action: () => { |
| 36 | + const win = BrowserWindow.getFocusedWindow(); |
| 37 | + if (win) { |
| 38 | + app.quit(); |
| 39 | + } |
| 40 | + }, |
| 41 | + }, |
| 42 | + { |
| 43 | + key: 'Command+Option+I', |
| 44 | + action: () => { |
| 45 | + const win = BrowserWindow.getFocusedWindow(); |
| 46 | + win?.webContents.openDevTools(); |
33 | 47 | }, |
34 | | - ]; |
| 48 | + }, |
| 49 | +]; |
35 | 50 |
|
36 | | - const openDevShortcut = [{key: 'Control+Shift+I', action: () => BrowserWindow.getFocusedWindow().webContents.openDevTools()}]; |
| 51 | +const DEVTOOLS_SHORTCUTS = [ |
| 52 | + { |
| 53 | + key: 'Control+Shift+I', |
| 54 | + action: () => { |
| 55 | + const win = BrowserWindow.getFocusedWindow(); |
| 56 | + win?.webContents.openDevTools(); |
| 57 | + }, |
| 58 | + }, |
| 59 | +]; |
37 | 60 |
|
38 | | - globalShortcuts.forEach(({key, action}) => { |
39 | | - globalShortcut.register(key, action); |
| 61 | +function registerShortcut({key, action}) { |
| 62 | + globalShortcut.register(key, () => { |
| 63 | + const focusedWindow = BrowserWindow.getFocusedWindow(); |
| 64 | + if (!focusedWindow && action.length > 0) { |
| 65 | + // For actions that rely on a window, do nothing if there is none |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + if (action.length > 0) { |
| 70 | + // action expects a window |
| 71 | + action(focusedWindow); |
| 72 | + } else { |
| 73 | + // action handles its own window lookup |
| 74 | + action(); |
| 75 | + } |
40 | 76 | }); |
| 77 | +} |
41 | 78 |
|
42 | | - if (platformData.isMac) { |
43 | | - macShortcuts.forEach(({key, action}) => { |
44 | | - globalShortcut.register(key, action); |
45 | | - }); |
46 | | - } |
| 79 | +function registerGlobalShortcuts() { |
| 80 | + COMMON_SHORTCUTS.forEach(registerShortcut); |
47 | 81 |
|
48 | | - if (platformData.isWin) { |
49 | | - openDevShortcut.forEach(({key, action}) => { |
50 | | - globalShortcut.register(key, action); |
51 | | - }); |
| 82 | + if (platformData.isMac) { |
| 83 | + MAC_SHORTCUTS.forEach(registerShortcut); |
52 | 84 | } |
53 | 85 |
|
54 | | - if (platformData.isLinux) { |
55 | | - openDevShortcut.forEach(({key, action}) => { |
56 | | - globalShortcut.register(key, action); |
57 | | - }); |
| 86 | + if (platformData.isWin || platformData.isLinux) { |
| 87 | + DEVTOOLS_SHORTCUTS.forEach(registerShortcut); |
58 | 88 | } |
59 | 89 | } |
60 | 90 |
|
|
0 commit comments