Skip to content

feat: resizable window #749

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

Closed
wants to merge 1 commit into from
Closed
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
49 changes: 38 additions & 11 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ 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');

const iconIdle = path.join(
__dirname,
'assets',
'images',
'tray-idleTemplate.png'
'tray-idleTemplate.png',
);
const iconActive = path.join(__dirname, 'assets', 'images', 'tray-active.png');

Expand All @@ -21,7 +21,7 @@ const browserWindowOpts = {
height: 400,
minWidth: 500,
minHeight: 400,
resizable: false,
resizable: true,
webPreferences: {
enableRemoteModule: true,
overlayScrollbars: true,
Expand Down Expand Up @@ -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() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do the function definitions need to be in the ready scope? Couldn't they be at the top level scope? Would make for cleaner code that's easier to refactor.

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

Expand Down Expand Up @@ -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) => {
Expand All @@ -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,
);
});
});