From 993eed92ed285f5a629d9eea023139ae5af72f6d Mon Sep 17 00:00:00 2001 From: Joshua Matthews Date: Tue, 27 Apr 2021 16:10:54 -0500 Subject: [PATCH 1/3] Update index.js Add logic to only reset the window if the window is out of bounds of a view in a multi-monitor supported way. --- index.js | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 97491eb..930f384 100644 --- a/index.js +++ b/index.js @@ -45,21 +45,45 @@ module.exports = function (options) { }; } - function windowWithinBounds(bounds) { + function newPoint(x, y) { + return { + x, + y, + }; + } + + function pointWithinBounds(point, 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 - ); + point.x >= bounds.x && + point.y >= bounds.y && + point.x <= bounds.x + bounds.width && + point.y <= bounds.y + bounds.height + ) } function ensureWindowVisibleOnSomeDisplay() { - const visible = screen.getAllDisplays().some(display => { - return windowWithinBounds(display.bounds); - }); - - if (!visible) { + const points = []; + points.push(newPoint(state.x, state.y)); + points.push(newPoint(state.x, state.y + state.height)); + points.push(newPoint(state.x + state.width, state.y)); + points.push(newPoint(state.x + state.width, state.y + state.height)); + const displays = screen.getAllDisplays(); + const length = displays.length; + let valid_point_count = 0; + for (let i = 0; i < length; i++) { + const curr_display = displays[i]; + const point_length = points.length; + for (let j = 0; j < point_length; j++) { + if (pointWithinBounds(points[j], curr_display.bounds)) { + valid_point_count++; + } + } + if (valid_point_count === 4) { + break; + } + } + + if (valid_point_count !== 4) { // Window is partially or fully not visible now. // Reset it to safe defaults. return resetStateToDefault(); From 6e33ae858d6f248eb18ea05eff364ddca424bf9b Mon Sep 17 00:00:00 2001 From: Joshua Matthews Date: Wed, 28 Apr 2021 10:16:19 -0500 Subject: [PATCH 2/3] Support out of bounds option Allow the user to configure the window state to be out of bounds. (all but one corner out of view) --- index.js | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 930f384..3178b6d 100644 --- a/index.js +++ b/index.js @@ -16,7 +16,8 @@ module.exports = function (options) { file: 'window-state.json', path: app.getPath('userData'), maximize: true, - fullScreen: true + fullScreen: true, + outOfBounds: false }, options); const fullStoreFileName = path.join(config.path, config.file); @@ -68,22 +69,26 @@ module.exports = function (options) { points.push(newPoint(state.x + state.width, state.y)); points.push(newPoint(state.x + state.width, state.y + state.height)); const displays = screen.getAllDisplays(); - const length = displays.length; - let valid_point_count = 0; - for (let i = 0; i < length; i++) { - const curr_display = displays[i]; - const point_length = points.length; - for (let j = 0; j < point_length; j++) { - if (pointWithinBounds(points[j], curr_display.bounds)) { - valid_point_count++; + const displayLength = displays.length; + let currentPointCount = 0; + let validPointCount = 4; + if (config.outOfBounds) { + validPointCount = 1; + } + for (let i = 0; i < displayLength; i++) { + const currDisplay = displays[i]; + const pointLength = points.length; + for (let j = 0; j < pointLength; j++) { + if (pointWithinBounds(points[j], currDisplay.bounds)) { + currentPointCount++; } } - if (valid_point_count === 4) { + if (currentPointCount >= validPointCount) { break; } } - if (valid_point_count !== 4) { + if (currentPointCount < validPointCount) { // Window is partially or fully not visible now. // Reset it to safe defaults. return resetStateToDefault(); From 98feced519fe2061fa14d87d746c5e038ca444da Mon Sep 17 00:00:00 2001 From: Joshua Matthews Date: Wed, 28 Apr 2021 11:16:37 -0500 Subject: [PATCH 3/3] Update index.d.ts --- index.d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.d.ts b/index.d.ts index bdd51eb..944c4e4 100644 --- a/index.d.ts +++ b/index.d.ts @@ -15,6 +15,8 @@ declare namespace windowStateKeeper { file?: string; /** Should we automatically maximize the window, if it was last closed maximized. Defaults to `true`. */ maximize?: boolean; + /** Allow the window to go out of bounds as long as one corner is still in view. */ + outOfBounds?: boolean; } interface State {