fix: reset window state after disconnected from multi monitor (#3797)

This commit is contained in:
Faisal Amir 2024-10-16 09:48:23 +07:00 committed by GitHub
parent 19a60bc973
commit 02190c5cbd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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) => {