Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only reset window position if it is out of bounds across multiple monitors and allow out of bounds option #69

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
53 changes: 41 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -45,21 +46,49 @@ 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 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 (currentPointCount >= validPointCount) {
break;
}
}

if (currentPointCount < validPointCount) {
// Window is partially or fully not visible now.
// Reset it to safe defaults.
return resetStateToDefault();
Expand Down