Skip to content
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

electron-window-state #9

Open
dylang opened this issue Jul 4, 2015 · 5 comments
Open

electron-window-state #9

dylang opened this issue Jul 4, 2015 · 5 comments

Comments

@dylang
Copy link

dylang commented Jul 4, 2015

Problem

Electron does not save/restore window size and position between loads.

Solution

A library for storing and loading window state.

Some logic might be required for plugging in and unplugging from a multi-monitor setup.

Electron lets you save state in files or localStorage. I'm not sure if one is better than the other.

@sindresorhus
Copy link
Owner

👍 I could use this too. I think localStorage would be best as with the filesystem you might get permissions errors and other annoying things.

// @jprichardson

@jprichardson
Copy link

I could also use this.

This would be pretty simple with it just handled size/position. I'm not sure how to best handle multi-monitors (I don't use them anymore, so I couldn't test).

Also, it was my initial inclination to use localStorage as well, but would files be better since on the restoration of the window (new BrowserWindow) you could access the state? (i.e. access the saved state before creation as opposed to accessing it after creation and resizing/repositioning). Although, localStorage would simplify things.

@floydpink
Copy link

One of the boilerplates mentioned in awesome-electron has a module that does this:

// Simple module to help you remember the size and position of windows.
// Can be used for more than one window, just construct many
// instances of it and give each different name.

'use strict';

var app = require('app');
var jetpack = require('fs-jetpack');

module.exports = function (name, defaults) {

    var userDataDir = jetpack.cwd(app.getPath('userData'));
    var stateStoreFile = 'window-state-' + name +'.json';

    var state = userDataDir.read(stateStoreFile, 'json') || {
        width: defaults.width,
        height: defaults.height
    };

    var saveState = function (win) {
        if (!win.isMaximized() && !win.isMinimized()) {
            var position = win.getPosition();
            var size = win.getSize();
            state.x = position[0];
            state.y = position[1];
            state.width = size[0];
            state.height = size[1];
        }
        state.isMaximized = win.isMaximized();
        userDataDir.write(stateStoreFile, state, { atomic: true });
    };

    return {
        get x() { return state.x; },
        get y() { return state.y; },
        get width() { return state.width; },
        get height() { return state.height; },
        get isMaximized() { return state.isMaximized; },
        saveState: saveState
    };
};

Maybe that can be built as an npm module (without any dependencies like jetpack above) with some nice tests etc.

@mawie81
Copy link

mawie81 commented Nov 1, 2015

So I went ahead and gave this a try: https://github.com/mawie81/electron-window-state
It´s still lacking tests and I haven´t had the chance to test multiple monitor setups but at least it´s a start.

@dylang
Copy link
Author

dylang commented Nov 1, 2015

Thanks @mawie81! Heading to vacation for the week, but when I return I can try multiple monitors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants