diff --git a/electron/utils/setup.ts b/electron/utils/setup.ts index 437e21f97..39b8a4133 100644 --- a/electron/utils/setup.ts +++ b/electron/utils/setup.ts @@ -1,4 +1,4 @@ -import { app } from 'electron' +import { app, screen } from 'electron' import Store from 'electron-store' const DEFAULT_WIDTH = 1000 @@ -22,13 +22,42 @@ export const getBounds = async () => { height: DEFAULT_HEIGHT, } - const bounds = await storage.get('windowBounds') - if (bounds) { - return bounds as Electron.Rectangle - } else { + const bounds = (await storage.get('windowBounds')) as + | Electron.Rectangle + | undefined + + // If no bounds are saved, use the defaults + if (!bounds) { storage.set('windowBounds', defaultBounds) return defaultBounds } + + // Validate that the bounds are on a valid display + const displays = screen.getAllDisplays() + const isValid = displays.some((display) => { + const { x, y, width, height } = display.bounds + return ( + bounds.x >= x && + bounds.x < x + width && + bounds.y >= y && + bounds.y < y + height + ) + }) + + // If the position is valid, return the saved bounds, otherwise return default bounds + if (isValid) { + return bounds + } else { + const primaryDisplay = screen.getPrimaryDisplay() + const resetBounds = { + x: primaryDisplay.bounds.x, + y: primaryDisplay.bounds.y, + width: DEFAULT_WIDTH, + height: DEFAULT_HEIGHT, + } + storage.set('windowBounds', resetBounds) + return resetBounds + } } export const saveBounds = (bounds: Electron.Rectangle | undefined) => {