Skip to content

Commit

Permalink
Merge pull request #12 from joshaber/fix-monitor-switching
Browse files Browse the repository at this point in the history
Try to keep as state as possible when the display changes
  • Loading branch information
mawie81 committed Oct 8, 2016
2 parents b6e8ea5 + 236baea commit ed741c5
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,37 @@ module.exports = function (options) {

function validateState() {
var isValid = state && (hasBounds() || state.isMaximized || state.isFullScreen);
if (!isValid) {
state = null;
return;
}

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});
var sameBounds = deepEqual(state.displayBounds, displayBounds, {strict: true});
if (!sameBounds) {
if (displayBounds.width < state.displayBounds.width) {
if (state.x > displayBounds.width) {
state.x = null;
}

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

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

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

function updateState(win) {
Expand Down Expand Up @@ -126,9 +151,7 @@ module.exports = function (options) {
}

// Check state validity
if (!validateState()) {
state = null;
}
validateState();

// Set state fallback values
state = objectAssign({
Expand Down

0 comments on commit ed741c5

Please sign in to comment.