Skip to content

Commit

Permalink
feat: save window state
Browse files Browse the repository at this point in the history
open the main window in the last used position of the screen
  • Loading branch information
toriphes committed Dec 28, 2021
1 parent 0c00291 commit 8f9385d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -111,6 +111,7 @@
"electron-log": "^4.4.1",
"electron-store": "^8.0.1",
"electron-updater": "^4.3.9",
"electron-window-state": "^5.0.3",
"faker": "^5.5.3",
"marked": "^4.0.0",
"moment": "^2.29.1",
Expand Down
21 changes: 19 additions & 2 deletions src/main/main.js
Expand Up @@ -3,6 +3,7 @@
import { app, BrowserWindow, /* session, */ nativeImage, Menu } from 'electron';
import * as path from 'path';
import Store from 'electron-store';
import * as windowStateKeeper from 'electron-window-state';
import * as remoteMain from '@electron/remote/main';

import ipcHandlers from './ipc-handlers';
Expand All @@ -18,12 +19,15 @@ process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = 'true';

// global reference to mainWindow (necessary to prevent window from being garbage collected)
let mainWindow;
let mainWindowState;

async function createMainWindow () {
const icon = require('../renderer/images/logo-32.png');
const window = new BrowserWindow({
width: 1024,
height: 800,
width: mainWindowState.width,
height: mainWindowState.height,
x: mainWindowState.x,
y: mainWindowState.y,
minWidth: 900,
minHeight: 550,
title: 'Antares SQL',
Expand All @@ -41,6 +45,9 @@ async function createMainWindow () {
backgroundColor: '#1d1d1d'
});

mainWindowState.manage(window);
window.on('moved', saveWindowState);

remoteMain.enable(window.webContents);

try {
Expand Down Expand Up @@ -70,6 +77,7 @@ async function createMainWindow () {
}

window.on('closed', () => {
window.removeListener('moved', saveWindowState);
mainWindow = null;
});

Expand Down Expand Up @@ -104,6 +112,11 @@ else {

// create main BrowserWindow when electron is ready
app.on('ready', async () => {
mainWindowState = windowStateKeeper({
defaultWidth: 1024,
defaultHeight: 800
});

mainWindow = await createMainWindow();
createAppMenu();

Expand Down Expand Up @@ -160,3 +173,7 @@ function createAppMenu () {

Menu.setApplicationMenu(menu);
}

function saveWindowState () {
mainWindowState.saveState(mainWindow);
}

0 comments on commit 8f9385d

Please sign in to comment.