Skip to content

Commit

Permalink
fix: Fix the wrong window position or size due to the window pos is n…
Browse files Browse the repository at this point in the history
…ot inside the screen
  • Loading branch information
ci010 committed Nov 16, 2024
1 parent 506a0e2 commit 65adf0d
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion xmcl-electron-app/main/utils/windowSizeTracker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InstalledAppManifest } from '@xmcl/runtime-api'
import { BrowserWindow } from 'electron'
import { BrowserWindow, screen } from 'electron'
import { readFile, writeFile } from 'fs-extra'
import debounce from 'lodash.debounce'
import { join } from 'path'
Expand Down Expand Up @@ -30,6 +30,15 @@ export function createWindowTracker(app: LauncherApp, role: string, man: Install
return Math.max(this.height || 0, min)
},
}
function isInsideScreen(x: number, y: number, width: number, height: number) {
const displays = screen.getAllDisplays()
for (const display of displays) {
if (x >= display.bounds.x && y >= display.bounds.y && x + width <= display.bounds.x + display.bounds.width && y + height <= display.bounds.y + display.bounds.height) {
return true
}
}
return false
}
async function getConfig() {
const configData = await readFile(configPath, 'utf-8').then((v) => JSON.parse(v)).catch(() => ({
width: -1,
Expand All @@ -45,6 +54,10 @@ export function createWindowTracker(app: LauncherApp, role: string, man: Install
y: typeof configData.y === 'number' ? configData.y as number : null,
maximized: !!configData.maximized,
}
if (newConfig.x !== null && newConfig.y !== null && !isInsideScreen(newConfig.x, newConfig.y, newConfig.width, newConfig.height)) {
newConfig.x = null
newConfig.y = null
}
Object.assign(config, newConfig)
return config
}
Expand Down

0 comments on commit 65adf0d

Please sign in to comment.