Skip to content

Commit

Permalink
fix: reset window state after disconnected from multi monitor (janhq#…
Browse files Browse the repository at this point in the history
  • Loading branch information
urmauur authored Oct 16, 2024
1 parent 19a60bc commit 02190c5
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions electron/utils/setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app } from 'electron'
import { app, screen } from 'electron'
import Store from 'electron-store'

const DEFAULT_WIDTH = 1000
Expand All @@ -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) => {
Expand Down

0 comments on commit 02190c5

Please sign in to comment.