From 9df6f67c98876398b5d8177560c790aa3c2ae369 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Sat, 8 Jun 2024 15:44:46 -0400 Subject: [PATCH 1/5] refactor: main js --- main.js | 81 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 40 insertions(+), 41 deletions(-) diff --git a/main.js b/main.js index d0d76f0a2..f22ef8ca4 100644 --- a/main.js +++ b/main.js @@ -1,18 +1,17 @@ -const { ipcMain: ipc, app, nativeTheme } = require('electron'); +const { ipcMain: ipc, app, nativeTheme } = require('electron/main'); const { menubar } = require('menubar'); const { autoUpdater } = require('electron-updater'); const { onFirstRunMaybe } = require('./first-run'); const path = require('node:path'); -require('@electron/remote/main').initialize(); - -const iconIdle = path.join( +const idleIcon = path.join( __dirname, 'assets', 'images', 'tray-idleTemplate.png', ); -const iconActive = path.join(__dirname, 'assets', 'images', 'tray-active.png'); + +const activeIcon = path.join(__dirname, 'assets', 'images', 'tray-active.png'); const browserWindowOpts = { width: 500, @@ -28,49 +27,49 @@ const browserWindowOpts = { }, }; -app.on('ready', async () => { +app.whenReady().then(async () => { await onFirstRunMaybe(); -}); -const mb = menubar({ - icon: iconIdle, - index: `file://${__dirname}/index.html`, - browserWindow: browserWindowOpts, - preloadWindow: true, - showDockIcon: false, -}); + const mb = menubar({ + icon: idleIcon, + index: `file://${__dirname}/index.html`, + browserWindow: browserWindowOpts, + preloadWindow: true, + showDockIcon: false, + }); -mb.on('ready', () => { - mb.app.setAppUserModelId('com.electron.gitify'); - mb.tray.setIgnoreDoubleClickEvents(true); + mb.on('ready', () => { + autoUpdater.checkForUpdatesAndNotify(); - mb.hideWindow(); + mb.app.setAppUserModelId('com.electron.gitify'); + mb.tray.setIgnoreDoubleClickEvents(true); - // Force the window to retrieve its previous zoom factor - mb.window.webContents.setZoomFactor(mb.window.webContents.getZoomFactor()); + mb.hideWindow(); - mb.window.webContents.on('devtools-opened', () => { - mb.window.setSize(800, 600); - mb.window.center(); - mb.window.resizable = true; - }); + // Force the window to retrieve its previous zoom factor + mb.window.webContents.setZoomFactor(mb.window.webContents.getZoomFactor()); - mb.window.webContents.on('devtools-closed', () => { - const trayBounds = mb.tray.getBounds(); - mb.window.setSize(browserWindowOpts.width, browserWindowOpts.height); - mb.positioner.move('trayCenter', trayBounds); - mb.window.resizable = false; - }); - - mb.window.webContents.on('before-input-event', (event, input) => { - if (input.key === 'Escape') { - mb.window.hide(); - event.preventDefault(); - } + mb.window.webContents.on('before-input-event', (event, input) => { + if (input.key === 'Escape') { + mb.window.hide(); + event.preventDefault(); + } + }); + + mb.window.webContents.on('devtools-opened', () => { + mb.window.setSize(800, 600); + mb.window.center(); + mb.window.resizable = true; + }); + + mb.window.webContents.on('devtools-closed', () => { + const trayBounds = mb.tray.getBounds(); + mb.window.setSize(browserWindowOpts.width, browserWindowOpts.height); + mb.positioner.move('trayCenter', trayBounds); + mb.window.resizable = false; + }); }); - autoUpdater.checkForUpdatesAndNotify(); - nativeTheme.on('updated', () => { if (nativeTheme.shouldUseDarkColors) { mb.window.webContents.send('update-native-theme', 'DARK'); @@ -90,9 +89,9 @@ mb.on('ready', () => { ipc.on('update-icon', (_, arg) => { if (!mb.tray.isDestroyed()) { if (arg === 'TrayActive') { - mb.tray.setImage(iconActive); + mb.tray.setImage(activeIcon); } else { - mb.tray.setImage(iconIdle); + mb.tray.setImage(idleIcon); } } }); From 0e204c7c4f154d659d53ed0ef69c5e5b5e6e873a Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Sat, 8 Jun 2024 18:37:01 -0400 Subject: [PATCH 2/5] refactor: main js --- main.js | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/main.js b/main.js index 26abb46c9..78de9723a 100644 --- a/main.js +++ b/main.js @@ -77,34 +77,34 @@ app.whenReady().then(async () => { mb.window.webContents.send('gitify:update-theme', 'LIGHT'); } }); -}); -ipc.handle('gitify:version', () => app.getVersion()); + ipc.handle('gitify:version', () => app.getVersion()); -ipc.on('gitify:window-show', () => mb.showWindow()); + ipc.on('gitify:window-show', () => mb.showWindow()); -ipc.on('gitify:window-hide', () => mb.hideWindow()); + ipc.on('gitify:window-hide', () => mb.hideWindow()); -ipc.on('gitify:quit', () => mb.app.quit()); + ipc.on('gitify:quit', () => mb.app.quit()); -ipc.on('gitify:icon-active', () => { - if (!mb.tray.isDestroyed()) { - mb.tray.setImage(iconActive); - } -}); + ipc.on('gitify:icon-active', () => { + if (!mb.tray.isDestroyed()) { + mb.tray.setImage(activeIcon); + } + }); -ipc.on('gitify:icon-idle', () => { - if (!mb.tray.isDestroyed()) { - mb.tray.setImage(iconIdle); - } -}); + ipc.on('gitify:icon-idle', () => { + if (!mb.tray.isDestroyed()) { + mb.tray.setImage(idleIcon); + } + }); -ipc.on('gitify:update-title', (_, title) => { - if (!mb.tray.isDestroyed()) { - mb.tray.setTitle(title); - } -}); + ipc.on('gitify:update-title', (_, title) => { + if (!mb.tray.isDestroyed()) { + mb.tray.setTitle(title); + } + }); -ipc.on('gitify:update-auto-launch', (_, settings) => { - app.setLoginItemSettings(settings); + ipc.on('gitify:update-auto-launch', (_, settings) => { + app.setLoginItemSettings(settings); + }); }); From eaad728cdfe11f6c62ad7988a3df1b6488e2db7c Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Mon, 10 Jun 2024 13:16:40 -0400 Subject: [PATCH 3/5] Merge remote-tracking branch 'origin/main' into refactor/mainjs --- main.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/main.js b/main.js index f4d2d508f..6f4ef5961 100644 --- a/main.js +++ b/main.js @@ -4,13 +4,7 @@ const { autoUpdater } = require('electron-updater'); const { onFirstRunMaybe } = require('./first-run'); const path = require('node:path'); -const idleIcon = path.join( - __dirname, - 'assets', - 'images', - 'tray-idleTemplate.png', -); - +const idleIcon = path.join(__dirname, 'assets', 'images', 'tray-idle.png'); const activeIcon = path.join(__dirname, 'assets', 'images', 'tray-active.png'); const browserWindowOpts = { From 1edb463800351205774b8d0f2f52c088573c73a0 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Mon, 10 Jun 2024 13:21:17 -0400 Subject: [PATCH 4/5] defer removal until #650 is resolved --- main.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/main.js b/main.js index 6f4ef5961..a8e6980f5 100644 --- a/main.js +++ b/main.js @@ -4,6 +4,9 @@ const { autoUpdater } = require('electron-updater'); const { onFirstRunMaybe } = require('./first-run'); const path = require('node:path'); +// TODO: Remove @electron/remote use - see #650 +require('@electron/remote/main').initialize(); + const idleIcon = path.join(__dirname, 'assets', 'images', 'tray-idle.png'); const activeIcon = path.join(__dirname, 'assets', 'images', 'tray-active.png'); From b1a407410cebcca2b8d50fa192f0cfc020be207e Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Mon, 10 Jun 2024 13:25:20 -0400 Subject: [PATCH 5/5] add comments to group sections --- main.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/main.js b/main.js index a8e6980f5..2828a4df7 100644 --- a/main.js +++ b/main.js @@ -47,11 +47,11 @@ app.whenReady().then(async () => { mb.on('ready', () => { autoUpdater.checkForUpdatesAndNotify(); - mb.app.setAppUserModelId('com.electron.gitify'); - mb.tray.setIgnoreDoubleClickEvents(true); - mb.hideWindow(); + mb.app.setAppUserModelId('com.electron.gitify'); + // Tray configuration + mb.tray.setIgnoreDoubleClickEvents(true); mb.tray.on('right-click', (_event, bounds) => { mb.tray.popUpContextMenu(contextMenu, { x: bounds.x, y: bounds.y }); }); @@ -59,6 +59,7 @@ app.whenReady().then(async () => { // Force the window to retrieve its previous zoom factor mb.window.webContents.setZoomFactor(mb.window.webContents.getZoomFactor()); + // Custom key events mb.window.webContents.on('before-input-event', (event, input) => { if (input.key === 'Escape') { mb.window.hide(); @@ -66,6 +67,7 @@ app.whenReady().then(async () => { } }); + // DevTools configuration mb.window.webContents.on('devtools-opened', () => { mb.window.setSize(800, 600); mb.window.center(); @@ -88,6 +90,9 @@ app.whenReady().then(async () => { } }); + /** + * Gitify custom IPC events + */ ipc.handle('gitify:version', () => app.getVersion()); ipc.on('gitify:window-show', () => mb.showWindow());