Skip to content

Commit

Permalink
refactor: main js (#1207)
Browse files Browse the repository at this point in the history
* refactor: main js

* refactor: main js

* Merge remote-tracking branch 'origin/main' into refactor/mainjs

* defer removal until #650 is resolved

* add comments to group sections
  • Loading branch information
setchy authored Jun 10, 2024
1 parent 7c62dd8 commit b017186
Showing 1 changed file with 72 additions and 66 deletions.
138 changes: 72 additions & 66 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const { ipcMain: ipc, app, nativeTheme, Menu } = require('electron');
const { ipcMain: ipc, app, nativeTheme, Menu } = require('electron/main');
const { menubar } = require('menubar');
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 iconIdle = path.join(__dirname, 'assets', 'images', 'tray-idle.png');
const iconActive = path.join(__dirname, 'assets', 'images', 'tray-active.png');
const idleIcon = path.join(__dirname, 'assets', 'images', 'tray-idle.png');
const activeIcon = path.join(__dirname, 'assets', 'images', 'tray-active.png');

const browserWindowOpts = {
width: 500,
Expand All @@ -32,88 +33,93 @@ const contextMenu = Menu.buildFromTemplate([
},
]);

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,
});

mb.on('ready', () => {
mb.app.setAppUserModelId('com.electron.gitify');
mb.tray.setIgnoreDoubleClickEvents(true);

mb.hideWindow();

mb.tray.on('right-click', (_event, bounds) => {
mb.tray.popUpContextMenu(contextMenu, { x: bounds.x, y: bounds.y });
});

// Force the window to retrieve its previous zoom factor
mb.window.webContents.setZoomFactor(mb.window.webContents.getZoomFactor());

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;
const mb = menubar({
icon: idleIcon,
index: `file://${__dirname}/index.html`,
browserWindow: browserWindowOpts,
preloadWindow: true,
showDockIcon: false,
});

mb.window.webContents.on('before-input-event', (event, input) => {
if (input.key === 'Escape') {
mb.window.hide();
event.preventDefault();
}
mb.on('ready', () => {
autoUpdater.checkForUpdatesAndNotify();

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 });
});

// 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();
event.preventDefault();
}
});

// DevTools configuration
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('gitify:update-theme', 'DARK');
} else {
mb.window.webContents.send('gitify:update-theme', 'LIGHT');
}
});
});

ipc.handle('gitify:version', () => app.getVersion());
/**
* Gitify custom IPC events
*/
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);
});
});

0 comments on commit b017186

Please sign in to comment.