Add some checks if position/size is inside screen border

This commit is contained in:
Jarne 2023-10-07 14:51:47 +02:00
parent 4600217606
commit fab350a32d

View File

@ -1,3 +1,4 @@
const { screen } = require('electron');
const WindowStateStore = require('../store/window-state');
const windowStateStore = new WindowStateStore();
@ -8,11 +9,14 @@ const DEFAULT_WINDOW_HEIGHT = 768;
const loadWindowState = () => {
const bounds = windowStateStore.getBounds();
const positionValid = isPositionValid(bounds);
const sizeValid = isSizeValid(bounds);
return {
x: bounds.x || undefined,
y: bounds.y || undefined,
width: bounds.width || DEFAULT_WINDOW_WIDTH,
height: bounds.height || DEFAULT_WINDOW_HEIGHT
x: bounds.x && positionValid ? bounds.x : undefined,
y: bounds.y && positionValid ? bounds.y : undefined,
width: bounds.width && sizeValid ? bounds.width : DEFAULT_WINDOW_WIDTH,
height: bounds.height && sizeValid ? bounds.height : DEFAULT_WINDOW_HEIGHT
};
};
@ -22,6 +26,27 @@ const saveWindowState = (window) => {
windowStateStore.setBounds(bounds);
};
const isPositionValid = (bounds) => {
const area = getArea(bounds);
return (
bounds.x >= area.x &&
bounds.y >= area.y &&
bounds.x + bounds.width <= area.x + area.width &&
bounds.y + bounds.height <= area.y + area.height
);
};
const isSizeValid = (bounds) => {
const area = getArea(bounds);
return bounds.width <= area.width && bounds.height <= area.height;
};
const getArea = (bounds) => {
return screen.getDisplayMatching(bounds).workArea;
};
module.exports = {
loadWindowState,
saveWindowState