fix ##741 window disappear when remove external screen

This commit is contained in:
qishibo 2022-09-12 20:27:31 +08:00
parent 0dc9a1abfb
commit 0a58523b33
2 changed files with 53 additions and 7 deletions

View File

@ -45,10 +45,10 @@ function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({
x: (lastWinStage.x > 0) ? lastWinStage.x : null,
y: (lastWinStage.y > 0) ? lastWinStage.y : null,
width: (lastWinStage.width > 250) ? lastWinStage.width : 1100,
height: (lastWinStage.height > 250) ? lastWinStage.height : 728,
x: lastWinStage.x,
y: lastWinStage.y,
width: lastWinStage.width,
height: lastWinStage.height,
icon: `${__dirname}/icons/icon.png`,
autoHideMenuBar: true,
webPreferences: {

View File

@ -1,9 +1,9 @@
const { app } = require('electron');
const { app, screen } = require('electron');
const path = require('path');
const fs = require('fs');
const winState = {
// {x, y, width, height}
// {x, y, width, height, maximized}
getLastState() {
let data = '{}';
@ -12,7 +12,53 @@ const winState = {
}
catch (err) {}
return this.parseJson(data);
const lastWinStage = this.parseJson(data);
const lastX = lastWinStage.x;
const lastY = lastWinStage.y;
const primary = screen.getPrimaryDisplay();
// recovery position only when app in primary screen
// if in external screens, reset position for uncaught display issues
if (
lastX < 0 || lastY < 0 ||
lastX > primary.workAreaSize.width || lastY > primary.workAreaSize.height
) {
lastWinStage.x = null;
lastWinStage.y = null;
}
// adjust extremely small window
(lastWinStage.width < 250) && (lastWinStage.width = 1100);
(lastWinStage.height < 250) && (lastWinStage.height = 728);
return lastWinStage;
// // there is some uncaught display issues when display in external screens
// // such as windows disappears even x < width
// let screenCanDisplay = false;
// const displays = screen.getAllDisplays()
// for (const display of displays) {
// const bounds = display.workArea;
// // check if there is a screen can display this position
// if (bounds.x * lastX > 0 && bounds.y * lastY > 0) {
// if (bounds.width > Math.abs(lastX) && bounds.height > Math.abs(lastY)) {
// screenCanDisplay = true;
// break;
// }
// }
// }
// let state = {...lastWinStage, x: null, y: null};
// // recovery to last position
// if (screenCanDisplay) {
// state.x = lastX;
// state.y = lastY;
// }
// return state;
},
watchClose(win) {