From 26af87362f6208b9c52727aa919e5c7a0d0b0b10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillermo=20Vay=C3=A1?= Date: Mon, 13 Apr 2020 15:50:53 +0200 Subject: [PATCH] [MM-23195] fix logic to detect if windows is outside of display boundaries (#1249) * [MM-23195] fix logic to detect if windows is outside of display boundaries * wip removal * fix stuck halfway through screens * don't jump to another display --- src/main.js | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/main.js b/src/main.js index 3c22ad6a..929a7503 100644 --- a/src/main.js +++ b/src/main.js @@ -307,8 +307,7 @@ function handleAppWindowAllClosed() { function handleAppBrowserWindowCreated(error, newWindow) { // Screen cannot be required before app is ready - const {screen} = electron; - resizeScreen(screen, newWindow); + resizeScreen(electron.screen, newWindow); } function handleAppActivate() { @@ -1131,28 +1130,29 @@ function clearAppCache() { } function isWithinDisplay(state, display) { - // given a display, check if window is within it - return (state.x > display.maxX || state.y > display.maxY || state.x < display.minX || state.y < display.minY); + const startsWithinDisplay = !(state.x > display.maxX || state.y > display.maxY || state.x < display.minX || state.y < display.minY); + if (!startsWithinDisplay) { + return false; + } + + // is half the screen within the display? + const midX = state.x + (state.width / 2); + const midY = state.y + (state.height / 2); + return !(midX > display.maxX || midY > display.maxY); } function getValidWindowPosition(state) { // Check if the previous position is out of the viewable area // (e.g. because the screen has been plugged off) const boundaries = Utils.getDisplayBoundaries(); - const isDisplayed = boundaries.reduce( - (prev, display) => { - return prev || isWithinDisplay(state, display); - }, - false); + const display = boundaries.find((boundary) => { + return isWithinDisplay(state, boundary); + }); - if (isDisplayed) { - Reflect.deleteProperty(state, 'x'); - Reflect.deleteProperty(state, 'y'); - Reflect.deleteProperty(state, 'width'); - Reflect.deleteProperty(state, 'height'); + if (typeof display === 'undefined') { + return {}; } - - return state; + return {x: state.x, y: state.y}; } function resizeScreen(screen, browserWindow) { @@ -1165,7 +1165,11 @@ function resizeScreen(screen, browserWindow) { width: size[0], height: size[1], }); - browserWindow.setPosition(validPosition.x || 0, validPosition.y || 0); + if (typeof validPosition.x !== 'undefined' || typeof validPosition.y !== 'undefined') { + browserWindow.setPosition(validPosition.x || 0, validPosition.y || 0); + } else { + browserWindow.center(); + } } browserWindow.on('restore', handle);