Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: main js #1207

Merged
merged 7 commits into from
Jun 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
setchy marked this conversation as resolved.
Show resolved Hide resolved
setchy marked this conversation as resolved.
Show resolved Hide resolved

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