Save settings on windows close


#1

I try to save/load the window size using electron-settings Load work fine but I cant save setting on window close. This is my code:

const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const globalShortcut = electron.globalShortcut;
const ElectronSettings = require('electron-settings');

let mainWindow;
let settings = new ElectronSettings();

function createWindow() {

    let winWidth = parseInt(settings.get('size')[0]); //LOAD WINDOW WIDTH
    let winHeight = parseInt(settings.get('size')[1]); //LOAD WINDOW HEIGHT

    mainWindow = new BrowserWindow({
        title: 'Meow',
        width: winWidth,
        height: winHeight,
        minWidth: 500,
        minHeight: 300
    });

    mainWindow.loadURL('file://' + __dirname + '/index.html');
    mainWindow.setMenu(null);

    mainWindow.on('closed', function() {
        mainWindow = null;
    });

    mainWindow.on('close', function () {
     settings.set('size', mainWindow.getSize()); //SAVE WINDOWS SIZE
 });
}

app.on('ready', createWindow);

app.on('window-all-closed', function() {
    if (process.platform !== 'darwin') {
        app.quit();
    }
});

app.on('will-quit', function() {
    globalShortcut.unregisterAll();
});

app.on('activate', function() {
    if (mainWindow === null) {
        createWindow();
    }
});

What am I missing? Why on windows close the window size is not saved?


#2

Not sure. Look up the path name and see if the settings are in the JSON file properly. Also, if the JSON is saving properly, you’re loading it directly. So you shouldn’t need to parse it. You should just be able to say

“let winWidth = settings.size[0]; let winHeight = settings.size[1];”


#3

But when I’m using

app.on('browser-window-blur', function() {
   settings.set('size', mainWindow.getSize());
});

Settings properly saving when window lose focus.


#4

I feel like this is a typo in your example and not in your program, but if you’re calling this it should be app.on(‘ready’, createWindow());


#5

This code taken from electron-quick-start


#6

When I use node-localstorage to store values, everything is OK:

"use strict";
const electron = require('electron');
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
const globalShortcut = electron.globalShortcut;
const LocalStorage = require('node-localstorage').LocalStorage;

let mainWindow;
let localStorage = new LocalStorage('./config');

function createWindow() {

    let W = parseInt(localStorage.getItem('width'));
    let H = parseInt(localStorage.getItem('height'));
    //console.log(W, H);

    mainWindow = new BrowserWindow({
        title: 'Meow',
        width: W,
        height: H,
        minWidth: 500,
        minHeight: 300
    });

    mainWindow.loadURL('file://' + __dirname + '/index.html');
    mainWindow.setMenu(null);

    mainWindow.on('closed', function() {
        mainWindow = null;
    });

    mainWindow.on('close', function () {
        let size = mainWindow.getSize();
        localStorage.setItem('width', size[0])
        localStorage.setItem('height', size[1])
        //console.log(size)
 });
}

app.on('ready', createWindow);

app.on('window-all-closed', function() {
    if (process.platform !== 'darwin') {
        app.quit();
    }
});

app.on('will-quit', function() {
    globalShortcut.unregisterAll();
});

app.on('activate', function() {
    if (mainWindow === null) {
        createWindow();
    }
});

So isn’t problem in electron-settings package?


#7

Yes! Problem was with electron-settings, now I’m using configstore and all working fine.