Skip to content

Commit

Permalink
Persist maximized state and handle title bar double-click (#1270)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
whitecrownclown and sindresorhus authored Mar 9, 2020
1 parent 9197321 commit dc392dc
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
14 changes: 14 additions & 0 deletions source/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,20 @@ document.addEventListener('DOMContentLoaded', async () => {
toggleVideoAutoplay();
});

// Handle title bar double-click.
window.addEventListener('dblclick', (event: Event) => {
const target = event.target as HTMLElement;
const titleBar = target.closest('._36ic._5l-3,._5742,._6-xk,._673w');

if (!titleBar) {
return;
}

ipc.callMain('titlebar-doubleclick');
}, {
passive: true
});

window.addEventListener('load', () => {
if (location.pathname.startsWith('/login')) {
const keepMeSignedInCheckbox = document.querySelector<HTMLInputElement>('#u_0_0')!;
Expand Down
7 changes: 6 additions & 1 deletion source/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type StoreType = {
y: number;
width: number;
height: number;
isMaximized: boolean;
};
menuBarMode: boolean;
showDockIcon: boolean;
Expand Down Expand Up @@ -83,13 +84,17 @@ const schema: {[Key in keyof StoreType]: Store.Schema} = {
},
height: {
type: 'number'
},
isMaximized: {
type: 'boolean'
}
},
default: {
x: undefined,
y: undefined,
width: 800,
height: 600
height: 600,
isMaximized: false
}
},
menuBarMode: {
Expand Down
45 changes: 42 additions & 3 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
BrowserWindow,
Menu,
Notification,
MenuItemConstructorOptions
MenuItemConstructorOptions,
systemPreferences
} from 'electron';
import {ipcMain} from 'electron-better-ipc';
import log from 'electron-log';
Expand Down Expand Up @@ -358,7 +359,18 @@ function createMainWindow(): BrowserWindow {
});

win.on('resize', () => {
config.set('lastWindowState', win.getNormalBounds());
const {isMaximized} = config.get('lastWindowState');
config.set('lastWindowState', {...win.getNormalBounds(), isMaximized});
});

win.on('maximize', () => {
// @ts-ignore
config.set('lastWindowState.isMaximized', true);
});

win.on('unmaximize', () => {
// @ts-ignore
config.set('lastWindowState.isMaximized', false);
});

return win;
Expand Down Expand Up @@ -455,6 +467,10 @@ function createMainWindow(): BrowserWindow {
mainWindow.hide();
tray.create(mainWindow);
} else {
if (config.get('lastWindowState').isMaximized) {
mainWindow.maximize();
}

mainWindow.show();
}

Expand Down Expand Up @@ -552,6 +568,28 @@ if (is.macos) {
});
}

function toggleMaximized(): void {
if (mainWindow.isMaximized()) {
mainWindow.unmaximize();
} else {
mainWindow.maximize();
}
}

ipcMain.answerRenderer('titlebar-doubleclick', () => {
if (is.macos) {
const doubleClickAction = systemPreferences.getUserDefault('AppleActionOnDoubleClick', 'string');

if (doubleClickAction === 'Minimize') {
mainWindow.minimize();
} else if (doubleClickAction === 'Maximize') {
toggleMaximized();
}
} else {
toggleMaximized();
}
});

app.on('activate', () => {
if (mainWindow) {
mainWindow.show();
Expand All @@ -564,7 +602,8 @@ app.on('before-quit', () => {
// Checking whether the window exists to work around an Electron race issue:
// https://github.com/sindresorhus/caprine/issues/809
if (mainWindow) {
config.set('lastWindowState', mainWindow.getNormalBounds());
const {isMaximized} = config.get('lastWindowState');
config.set('lastWindowState', {...mainWindow.getNormalBounds(), isMaximized});
}
});

Expand Down
7 changes: 6 additions & 1 deletion source/tray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ export default {
if (win.isVisible()) {
win.hide();
} else {
win.show();
if (config.get('lastWindowState').isMaximized) {
win.maximize();
win.focus();
} else {
win.show();
}

// Workaround for https://github.com/electron/electron/issues/20858
// `setAlwaysOnTop` stops working after hiding the window on KDE Plasma.
Expand Down

0 comments on commit dc392dc

Please sign in to comment.