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