Skip to content

Commit

Permalink
Consider a state without bounds still valid if fullscreen of maximized
Browse files Browse the repository at this point in the history
The state will have it's coordinates and size info updated only if the
app is not in fullscreen mode or maximized at the time of the update.

This makes sense because coordinates and size are not relevant when in
fullscreen or maximized. The problem is that only the defaultWidth and
defaultHeight definitions will be read if the state in the file is not
considered valid, and to be considered valid, a state must have bounds
(x, y, width and height must be defined).

When leaving the application for the first time (no preexistent
window-store.json), if the application was in fullscreen mode or
maximized when exiting, the newly created window-store.json will not
have coordinates and size info (since the update function that is called
right before the save operation will ignore the coordinates and size, as
said before).

Because of that, when the application is opened for the second time, the
state will be considered invalid and the saved isMaximized or
isFullScreen options will be completely ignored.

This fixes that by considering the state valid if isFullScreen or
isMaximized are true, even if it has no bounds. It also adds some basic
tests to this functionality.
  • Loading branch information
mateusmedeiros committed Aug 22, 2016
1 parent a98cf0d commit 8b03536
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ module.exports = function (options) {
}

function validateState() {
var isValid = state && hasBounds();
if (isValid && state.displayBounds) {
var isValid = state && (hasBounds() || state.isMaximized || state.isFullScreen);
if (hasBounds() && state.displayBounds) {
// Check if the display where the window was last open is still available
var displayBounds = screen.getDisplayMatching(state).bounds;
isValid = deepEqual(state.displayBounds, displayBounds, {strict: true});
Expand Down
46 changes: 46 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,52 @@ test('tries to read state file from the configured source', t => {
jsonfile.readFileSync.restore();
});

test('considers the state invalid if without bounds', t => {
const jsonfile = require('jsonfile');
sinon.stub(jsonfile, 'readFileSync').returns({
width: 100
});

const state = require('./')({
defaultWidth: 200
});

t.not(state.width, 100);
jsonfile.readFileSync.restore();
});

test('considers the state valid if without bounds but isMaximized is true', t => {
const jsonfile = require('jsonfile');
sinon.stub(jsonfile, 'readFileSync').returns({
isMaximized: true,
width: 100
});

const state = require('./')({
defaultWidth: 200
});

t.true(state.isMaximized);
t.is(state.width, 100);
jsonfile.readFileSync.restore();
});

test('considers the state valid if without bounds but isFullScreen is true', t => {
const jsonfile = require('jsonfile');
sinon.stub(jsonfile, 'readFileSync').returns({
isFullScreen: true,
width: 100
});

const state = require('./')({
defaultWidth: 200
});

t.true(state.isFullScreen);
t.is(state.width, 100);
jsonfile.readFileSync.restore();
});

test('returns the defaults if the state in the file is invalid', t => {
const jsonfile = require('jsonfile');
sinon.stub(jsonfile, 'readFileSync').returns({});
Expand Down

0 comments on commit 8b03536

Please sign in to comment.