Merge pull request #536 from jarne/bugfix/bug-451-max-window-reset

Fix: save window maximized state
This commit is contained in:
Anoop M D 2023-10-18 23:27:55 +05:30 committed by GitHub
commit ece5ad3175
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 6 deletions

View File

@ -10,7 +10,7 @@ const registerNetworkIpc = require('./ipc/network');
const registerCollectionsIpc = require('./ipc/collection'); const registerCollectionsIpc = require('./ipc/collection');
const registerPreferencesIpc = require('./ipc/preferences'); const registerPreferencesIpc = require('./ipc/preferences');
const Watcher = require('./app/watcher'); const Watcher = require('./app/watcher');
const { loadWindowState, saveWindowState } = require('./utils/window'); const { loadWindowState, saveBounds, saveMaximized } = require('./utils/window');
const lastOpenedCollections = new LastOpenedCollections(); const lastOpenedCollections = new LastOpenedCollections();
@ -33,7 +33,7 @@ let watcher;
// Prepare the renderer once the app is ready // Prepare the renderer once the app is ready
app.on('ready', async () => { app.on('ready', async () => {
const { x, y, width, height } = loadWindowState(); const { maximized, x, y, width, height } = loadWindowState();
mainWindow = new BrowserWindow({ mainWindow = new BrowserWindow({
x, x,
@ -55,6 +55,10 @@ app.on('ready', async () => {
// autoHideMenuBar: true // autoHideMenuBar: true
}); });
if (maximized) {
mainWindow.maximize();
}
const url = isDev const url = isDev
? 'http://localhost:3000' ? 'http://localhost:3000'
: format({ : format({
@ -66,8 +70,17 @@ app.on('ready', async () => {
mainWindow.loadURL(url); mainWindow.loadURL(url);
watcher = new Watcher(); watcher = new Watcher();
mainWindow.on('resize', () => saveWindowState(mainWindow)); const handleBoundsChange = () => {
mainWindow.on('move', () => saveWindowState(mainWindow)); if (!mainWindow.isMaximized()) {
saveBounds(mainWindow);
}
};
mainWindow.on('resize', handleBoundsChange);
mainWindow.on('move', handleBoundsChange);
mainWindow.on('maximize', () => saveMaximized(true));
mainWindow.on('unmaximize', () => saveMaximized(false));
mainWindow.webContents.on('new-window', function (e, url) { mainWindow.webContents.on('new-window', function (e, url) {
e.preventDefault(); e.preventDefault();

View File

@ -3,6 +3,8 @@ const Store = require('electron-store');
const DEFAULT_WINDOW_WIDTH = 1280; const DEFAULT_WINDOW_WIDTH = 1280;
const DEFAULT_WINDOW_HEIGHT = 768; const DEFAULT_WINDOW_HEIGHT = 768;
const DEFAULT_MAXIMIZED = false;
class WindowStateStore { class WindowStateStore {
constructor() { constructor() {
this.store = new Store({ this.store = new Store({
@ -25,6 +27,14 @@ class WindowStateStore {
setBounds(bounds) { setBounds(bounds) {
this.store.set('window-bounds', bounds); this.store.set('window-bounds', bounds);
} }
getMaximized() {
return this.store.get('maximized') || DEFAULT_MAXIMIZED;
}
setMaximized(isMaximized) {
this.store.set('maximized', isMaximized);
}
} }
module.exports = WindowStateStore; module.exports = WindowStateStore;

View File

@ -7,12 +7,14 @@ const DEFAULT_WINDOW_WIDTH = 1280;
const DEFAULT_WINDOW_HEIGHT = 768; const DEFAULT_WINDOW_HEIGHT = 768;
const loadWindowState = () => { const loadWindowState = () => {
const maximized = windowStateStore.getMaximized();
const bounds = windowStateStore.getBounds(); const bounds = windowStateStore.getBounds();
const positionValid = isPositionValid(bounds); const positionValid = isPositionValid(bounds);
const sizeValid = isSizeValid(bounds); const sizeValid = isSizeValid(bounds);
return { return {
maximized,
x: bounds.x && positionValid ? bounds.x : undefined, x: bounds.x && positionValid ? bounds.x : undefined,
y: bounds.y && positionValid ? bounds.y : undefined, y: bounds.y && positionValid ? bounds.y : undefined,
width: bounds.width && sizeValid ? bounds.width : DEFAULT_WINDOW_WIDTH, width: bounds.width && sizeValid ? bounds.width : DEFAULT_WINDOW_WIDTH,
@ -20,12 +22,16 @@ const loadWindowState = () => {
}; };
}; };
const saveWindowState = (window) => { const saveBounds = (window) => {
const bounds = window.getBounds(); const bounds = window.getBounds();
windowStateStore.setBounds(bounds); windowStateStore.setBounds(bounds);
}; };
const saveMaximized = (isMaximized) => {
windowStateStore.setMaximized(isMaximized);
};
const isPositionValid = (bounds) => { const isPositionValid = (bounds) => {
const area = getArea(bounds); const area = getArea(bounds);
@ -49,5 +55,6 @@ const getArea = (bounds) => {
module.exports = { module.exports = {
loadWindowState, loadWindowState,
saveWindowState saveBounds,
saveMaximized
}; };