From 70e446feb775cf8e5c9c3402144bf268c99223f7 Mon Sep 17 00:00:00 2001 From: Arthur Dufour Date: Fri, 9 Feb 2024 21:11:01 +0100 Subject: [PATCH] feat: resizable window --- main.js | 49 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/main.js b/main.js index 56ffc84ce..7fadbaf43 100644 --- a/main.js +++ b/main.js @@ -4,7 +4,7 @@ const { autoUpdater } = require('electron-updater'); const { onFirstRunMaybe } = require('./first-run'); const path = require('path'); -require('@electron/remote/main').initialize() +require('@electron/remote/main').initialize(); app.setAppUserModelId('com.electron.gitify'); @@ -12,7 +12,7 @@ const iconIdle = path.join( __dirname, 'assets', 'images', - 'tray-idleTemplate.png' + 'tray-idleTemplate.png', ); const iconActive = path.join(__dirname, 'assets', 'images', 'tray-active.png'); @@ -21,7 +21,7 @@ const browserWindowOpts = { height: 400, minWidth: 500, minHeight: 400, - resizable: false, + resizable: true, webPreferences: { enableRemoteModule: true, overlayScrollbars: true, @@ -52,7 +52,30 @@ const menubarApp = menubar({ }); menubarApp.on('ready', () => { + // Utility function to reposition the window so it stays in the tray center + function moveToTrayCenter() { + const trayBounds = menubarApp.tray.getBounds(); + menubarApp.positioner.move('trayCenter', trayBounds); + } + + // If window has previously been resized, update its size + function restoreWindowSize() { + menubarApp.window.webContents + .executeJavaScript('localStorage.getItem("size");', true) + .then((result) => { + const size = result.split(','); + if (!result || size.length !== 2) return; + + const width = Number(size[0]); + const height = Number(size[1]); + menubarApp.window.setSize(width, height); + + moveToTrayCenter(); + }); + } + delayedHideAppIcon(); + restoreWindowSize(); menubarApp.tray.setIgnoreDoubleClickEvents(true); @@ -93,17 +116,11 @@ menubarApp.on('ready', () => { menubarApp.window.webContents.on('devtools-opened', () => { menubarApp.window.setSize(800, 600); menubarApp.window.center(); - menubarApp.window.resizable = true; }); menubarApp.window.webContents.on('devtools-closed', () => { - const trayBounds = menubarApp.tray.getBounds(); - menubarApp.window.setSize( - browserWindowOpts.width, - browserWindowOpts.height - ); - menubarApp.positioner.move('trayCenter', trayBounds); - menubarApp.window.resizable = false; + restoreWindowSize(); + moveToTrayCenter(); }); menubarApp.window.webContents.on('before-input-event', (event, input) => { @@ -112,4 +129,14 @@ menubarApp.on('ready', () => { event.preventDefault(); } }); + + menubarApp.window.on('resized', () => { + moveToTrayCenter(); + + // Store the current size in localStorage to persist across restarts + menubarApp.window.webContents.executeJavaScript( + `localStorage.setItem("size", ${JSON.stringify(menubarApp.window.getSize())});`, + true, + ); + }); });