Skip to content

Commit

Permalink
Fix: Validate state when saved display unavailable (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuDebit authored and mawie81 committed Nov 25, 2018
1 parent 6bb0a3f commit d764cb5
Show file tree
Hide file tree
Showing 4 changed files with 1,467 additions and 1,247 deletions.
64 changes: 38 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const path = require('path');
const electron = require('electron');
const jsonfile = require('jsonfile');
const mkdirp = require('mkdirp');
const deepEqual = require('deep-equal');

module.exports = function (options) {
const app = electron.app || electron.remote.app;
Expand Down Expand Up @@ -33,6 +32,40 @@ module.exports = function (options) {
Number.isInteger(state.height) && state.height > 0;
}

function resetStateToDefault() {
const displayBounds = screen.getPrimaryDisplay().bounds;

// Reset state to default values on the primary display
state = {
width: config.defaultWidth || 800,
height: config.defaultHeight || 600,
x: 0,
y: 0,
displayBounds
};
}

function windowWithinBounds(state, bounds) {
return (
state.x >= bounds.x &&
state.y >= bounds.y &&
state.x + state.width <= bounds.x + bounds.width &&
state.y + state.height <= bounds.y + bounds.height
);
}

function ensureWindowVisibleOnSomeDisplay() {
const visible = screen.getAllDisplays().some(display => {
return windowWithinBounds(state, display.bounds);
});

if (!visible) {
// Window is partially or fully not visible now.
// Reset it to safe defaults.
return resetStateToDefault();
}
}

function validateState() {
const isValid = state && (hasBounds() || state.isMaximized || state.isFullScreen);
if (!isValid) {
Expand All @@ -41,30 +74,7 @@ module.exports = function (options) {
}

if (hasBounds() && state.displayBounds) {
// Check if the display where the window was last open is still available
const displayBounds = screen.getDisplayMatching(state).bounds;
const sameBounds = deepEqual(state.displayBounds, displayBounds, {strict: true});
if (!sameBounds) {
if (displayBounds.width < state.displayBounds.width) {
if (state.x > displayBounds.width) {
state.x = 0;
}

if (state.width > displayBounds.width) {
state.width = displayBounds.width;
}
}

if (displayBounds.height < state.displayBounds.height) {
if (state.y > displayBounds.height) {
state.y = 0;
}

if (state.height > displayBounds.height) {
state.height = displayBounds.height;
}
}
}
ensureWindowVisibleOnSomeDisplay();
}
}

Expand Down Expand Up @@ -165,10 +175,12 @@ module.exports = function (options) {
get y() { return state.y; },
get width() { return state.width; },
get height() { return state.height; },
get displayBounds() { return state.displayBounds; },
get isMaximized() { return state.isMaximized; },
get isFullScreen() { return state.isFullScreen; },
saveState,
unmanage,
manage
manage,
resetStateToDefault
};
};
Loading

0 comments on commit d764cb5

Please sign in to comment.