From fab350a32dfed1466d6f884ca0777c248d0680ed Mon Sep 17 00:00:00 2001 From: Jarne Date: Sat, 7 Oct 2023 14:51:47 +0200 Subject: [PATCH] Add some checks if position/size is inside screen border --- packages/bruno-electron/src/utils/window.js | 33 ++++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/packages/bruno-electron/src/utils/window.js b/packages/bruno-electron/src/utils/window.js index db1aff11d..d824141d3 100644 --- a/packages/bruno-electron/src/utils/window.js +++ b/packages/bruno-electron/src/utils/window.js @@ -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