|
1 | 1 | const {
|
2 |
| - ipcMain: ipc, |
3 |
| - app, |
4 |
| - nativeTheme, |
5 |
| - globalShortcut, |
6 |
| - Menu, |
7 |
| -} = require('electron/main'); |
8 |
| -const { menubar } = require('menubar'); |
9 |
| -const { autoUpdater } = require('electron-updater'); |
10 |
| -const { onFirstRunMaybe } = require('./first-run'); |
11 |
| -const path = require('node:path'); |
| 2 | + ipcMain: ipc, |
| 3 | + app, |
| 4 | + nativeTheme, |
| 5 | + globalShortcut, |
| 6 | + Menu, |
| 7 | +} = require("electron/main"); |
| 8 | +const { menubar } = require("menubar"); |
| 9 | +const { autoUpdater } = require("electron-updater"); |
| 10 | +const { onFirstRunMaybe } = require("./first-run"); |
| 11 | +const path = require("node:path"); |
12 | 12 |
|
13 | 13 | // TODO: Remove @electron/remote use - see #650
|
14 |
| -require('@electron/remote/main').initialize(); |
| 14 | +require("@electron/remote/main").initialize(); |
15 | 15 |
|
16 | 16 | const idleIcon = path.resolve(
|
17 |
| - `${__dirname}/../../assets/images/tray-idleTemplate.png`, |
| 17 | + `${__dirname}/../../assets/images/tray-idleTemplate.png`, |
18 | 18 | );
|
19 | 19 | const activeIcon = path.resolve(
|
20 |
| - `${__dirname}/../../assets/images/tray-active.png`, |
| 20 | + `${__dirname}/../../assets/images/tray-active.png`, |
21 | 21 | );
|
22 | 22 |
|
23 | 23 | const browserWindowOpts = {
|
24 |
| - width: 500, |
25 |
| - height: 400, |
26 |
| - minWidth: 500, |
27 |
| - minHeight: 400, |
28 |
| - resizable: false, |
29 |
| - webPreferences: { |
30 |
| - enableRemoteModule: true, |
31 |
| - nodeIntegration: true, |
32 |
| - contextIsolation: false, |
33 |
| - }, |
| 24 | + width: 500, |
| 25 | + height: 400, |
| 26 | + minWidth: 500, |
| 27 | + minHeight: 400, |
| 28 | + resizable: false, |
| 29 | + webPreferences: { |
| 30 | + enableRemoteModule: true, |
| 31 | + nodeIntegration: true, |
| 32 | + contextIsolation: false, |
| 33 | + }, |
34 | 34 | };
|
35 | 35 |
|
| 36 | +const mb = menubar({ |
| 37 | + icon: idleIcon, |
| 38 | + index: `file://${__dirname}/index.html`, |
| 39 | + browserWindow: browserWindowOpts, |
| 40 | + preloadWindow: true, |
| 41 | + showDockIcon: false, |
| 42 | +}); |
| 43 | + |
36 | 44 | const contextMenu = Menu.buildFromTemplate([
|
37 |
| - { |
38 |
| - role: 'reload', |
39 |
| - }, |
40 |
| - { |
41 |
| - role: 'toggleDevTools', |
42 |
| - }, |
43 |
| - { type: 'separator' }, |
44 |
| - { |
45 |
| - label: 'Quit', |
46 |
| - click: () => { |
47 |
| - app.quit(); |
48 |
| - }, |
49 |
| - }, |
| 45 | + { |
| 46 | + role: "reload", |
| 47 | + }, |
| 48 | + { |
| 49 | + role: "toggleDevTools", |
| 50 | + }, |
| 51 | + { type: "separator" }, |
| 52 | + { |
| 53 | + label: "Check for updates", |
| 54 | + click: () => { |
| 55 | + autoUpdater.checkForUpdatesAndNotify(); |
| 56 | + }, |
| 57 | + }, |
| 58 | + { |
| 59 | + label: "Quit", |
| 60 | + click: () => { |
| 61 | + app.quit(); |
| 62 | + }, |
| 63 | + }, |
50 | 64 | ]);
|
51 | 65 |
|
52 | 66 | app.whenReady().then(async () => {
|
53 |
| - await onFirstRunMaybe(); |
54 |
| - |
55 |
| - const mb = menubar({ |
56 |
| - icon: idleIcon, |
57 |
| - index: `file://${__dirname}/index.html`, |
58 |
| - browserWindow: browserWindowOpts, |
59 |
| - preloadWindow: true, |
60 |
| - showDockIcon: false, |
61 |
| - }); |
62 |
| - |
63 |
| - mb.on('ready', () => { |
64 |
| - autoUpdater.checkForUpdatesAndNotify(); |
65 |
| - |
66 |
| - mb.app.setAppUserModelId('com.electron.gitify'); |
67 |
| - |
68 |
| - // Tray configuration |
69 |
| - mb.tray.setIgnoreDoubleClickEvents(true); |
70 |
| - mb.tray.on('right-click', (_event, bounds) => { |
71 |
| - mb.tray.popUpContextMenu(contextMenu, { x: bounds.x, y: bounds.y }); |
72 |
| - }); |
73 |
| - |
74 |
| - // Custom key events |
75 |
| - mb.window.webContents.on('before-input-event', (event, input) => { |
76 |
| - if (input.key === 'Escape') { |
77 |
| - mb.window.hide(); |
78 |
| - event.preventDefault(); |
79 |
| - } |
80 |
| - }); |
81 |
| - |
82 |
| - // DevTools configuration |
83 |
| - mb.window.webContents.on('devtools-opened', () => { |
84 |
| - mb.window.setSize(800, 600); |
85 |
| - mb.window.center(); |
86 |
| - mb.window.resizable = true; |
87 |
| - mb.window.setAlwaysOnTop(true); |
88 |
| - }); |
89 |
| - |
90 |
| - mb.window.webContents.on('devtools-closed', () => { |
91 |
| - const trayBounds = mb.tray.getBounds(); |
92 |
| - mb.window.setSize(browserWindowOpts.width, browserWindowOpts.height); |
93 |
| - mb.positioner.move('trayCenter', trayBounds); |
94 |
| - mb.window.resizable = false; |
95 |
| - }); |
96 |
| - }); |
97 |
| - |
98 |
| - nativeTheme.on('updated', () => { |
99 |
| - if (nativeTheme.shouldUseDarkColors) { |
100 |
| - mb.window.webContents.send('gitify:update-theme', 'DARK'); |
101 |
| - } else { |
102 |
| - mb.window.webContents.send('gitify:update-theme', 'LIGHT'); |
103 |
| - } |
104 |
| - }); |
105 |
| - |
106 |
| - /** |
107 |
| - * Gitify custom IPC events |
108 |
| - */ |
109 |
| - ipc.handle('gitify:version', () => app.getVersion()); |
110 |
| - |
111 |
| - ipc.on('gitify:window-show', () => mb.showWindow()); |
112 |
| - |
113 |
| - ipc.on('gitify:window-hide', () => mb.hideWindow()); |
114 |
| - |
115 |
| - ipc.on('gitify:quit', () => mb.app.quit()); |
116 |
| - |
117 |
| - ipc.on('gitify:icon-active', () => { |
118 |
| - if (!mb.tray.isDestroyed()) { |
119 |
| - mb.tray.setImage(activeIcon); |
120 |
| - } |
121 |
| - }); |
122 |
| - |
123 |
| - ipc.on('gitify:icon-idle', () => { |
124 |
| - if (!mb.tray.isDestroyed()) { |
125 |
| - mb.tray.setImage(idleIcon); |
126 |
| - } |
127 |
| - }); |
128 |
| - |
129 |
| - ipc.on('gitify:update-title', (_, title) => { |
130 |
| - if (!mb.tray.isDestroyed()) { |
131 |
| - mb.tray.setTitle(title); |
132 |
| - } |
133 |
| - }); |
134 |
| - |
135 |
| - ipc.on( |
136 |
| - 'gitify:update-keyboard-shortcut', |
137 |
| - (_, { enabled, keyboardShortcut }) => { |
138 |
| - if (!enabled) { |
139 |
| - globalShortcut.unregister(keyboardShortcut); |
140 |
| - return; |
141 |
| - } |
142 |
| - |
143 |
| - globalShortcut.register(keyboardShortcut, () => { |
144 |
| - if (mb.window.isVisible()) { |
145 |
| - mb.hideWindow(); |
146 |
| - } else { |
147 |
| - mb.showWindow(); |
148 |
| - } |
149 |
| - }); |
150 |
| - }, |
151 |
| - ); |
152 |
| - |
153 |
| - ipc.on('gitify:update-auto-launch', (_, settings) => { |
154 |
| - app.setLoginItemSettings(settings); |
155 |
| - }); |
| 67 | + await onFirstRunMaybe(); |
| 68 | + |
| 69 | + mb.on("ready", () => { |
| 70 | + autoUpdater.checkForUpdatesAndNotify(); |
| 71 | + |
| 72 | + mb.app.setAppUserModelId("com.electron.gitify"); |
| 73 | + |
| 74 | + // Tray configuration |
| 75 | + mb.tray.setIgnoreDoubleClickEvents(true); |
| 76 | + mb.tray.on("right-click", (_event, bounds) => { |
| 77 | + mb.tray.popUpContextMenu(contextMenu, { x: bounds.x, y: bounds.y }); |
| 78 | + }); |
| 79 | + |
| 80 | + // Custom key events |
| 81 | + mb.window.webContents.on("before-input-event", (event, input) => { |
| 82 | + if (input.key === "Escape") { |
| 83 | + mb.window.hide(); |
| 84 | + event.preventDefault(); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + // DevTools configuration |
| 89 | + mb.window.webContents.on("devtools-opened", () => { |
| 90 | + mb.window.setSize(800, 600); |
| 91 | + mb.window.center(); |
| 92 | + mb.window.resizable = true; |
| 93 | + mb.window.setAlwaysOnTop(true); |
| 94 | + }); |
| 95 | + |
| 96 | + mb.window.webContents.on("devtools-closed", () => { |
| 97 | + const trayBounds = mb.tray.getBounds(); |
| 98 | + mb.window.setSize(browserWindowOpts.width, browserWindowOpts.height); |
| 99 | + mb.positioner.move("trayCenter", trayBounds); |
| 100 | + mb.window.resizable = false; |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + nativeTheme.on("updated", () => { |
| 105 | + if (nativeTheme.shouldUseDarkColors) { |
| 106 | + mb.window.webContents.send("gitify:update-theme", "DARK"); |
| 107 | + } else { |
| 108 | + mb.window.webContents.send("gitify:update-theme", "LIGHT"); |
| 109 | + } |
| 110 | + }); |
| 111 | + |
| 112 | + autoUpdater.on("checking-for-update", () => { |
| 113 | + console.log("Checking for update..."); |
| 114 | + // TODO - display the update status in the UI |
| 115 | + }); |
| 116 | + autoUpdater.on("update-available", (info) => { |
| 117 | + console.log("Update available."); |
| 118 | + // TODO - display the update status in the UI |
| 119 | + }); |
| 120 | + autoUpdater.on("update-not-available", (info) => { |
| 121 | + console.log("Update not available."); |
| 122 | + // TODO - display the update status in the UI |
| 123 | + }); |
| 124 | + |
| 125 | + /** |
| 126 | + * Gitify custom IPC events |
| 127 | + */ |
| 128 | + ipc.handle("gitify:version", () => app.getVersion()); |
| 129 | + |
| 130 | + ipc.on("gitify:window-show", () => mb.showWindow()); |
| 131 | + |
| 132 | + ipc.on("gitify:window-hide", () => mb.hideWindow()); |
| 133 | + |
| 134 | + ipc.on("gitify:quit", () => mb.app.quit()); |
| 135 | + |
| 136 | + ipc.on("gitify:icon-active", () => { |
| 137 | + if (!mb.tray.isDestroyed()) { |
| 138 | + mb.tray.setImage(activeIcon); |
| 139 | + } |
| 140 | + }); |
| 141 | + |
| 142 | + ipc.on("gitify:icon-idle", () => { |
| 143 | + if (!mb.tray.isDestroyed()) { |
| 144 | + mb.tray.setImage(idleIcon); |
| 145 | + } |
| 146 | + }); |
| 147 | + |
| 148 | + ipc.on("gitify:update-title", (_, title) => { |
| 149 | + if (!mb.tray.isDestroyed()) { |
| 150 | + mb.tray.setTitle(title); |
| 151 | + } |
| 152 | + }); |
| 153 | + |
| 154 | + ipc.on( |
| 155 | + "gitify:update-keyboard-shortcut", |
| 156 | + (_, { enabled, keyboardShortcut }) => { |
| 157 | + if (!enabled) { |
| 158 | + globalShortcut.unregister(keyboardShortcut); |
| 159 | + return; |
| 160 | + } |
| 161 | + |
| 162 | + globalShortcut.register(keyboardShortcut, () => { |
| 163 | + if (mb.window.isVisible()) { |
| 164 | + mb.hideWindow(); |
| 165 | + } else { |
| 166 | + mb.showWindow(); |
| 167 | + } |
| 168 | + }); |
| 169 | + }, |
| 170 | + ); |
| 171 | + |
| 172 | + ipc.on("gitify:update-auto-launch", (_, settings) => { |
| 173 | + app.setLoginItemSettings(settings); |
| 174 | + }); |
156 | 175 | });
|
0 commit comments