App launched outside display area when second screen disconnected
MathieuDebit opened this issue · comments
About
This bug has a reproduction step on Windows 10.
Mac OSX is also affected but seems to automatically correct wrong display positioning.
No clear info on Linux.
I know this issue was reported in multiple issues but this one tends to gather everything necessary to fix it.
Related: #1, #4, #27, #31, #38, #43.
Reproduction Steps
Here is a repo to reproduce the bug: MathieuDebit/electron-quick-start#3
Same here..
The algorithm that checks if the window can be displayed within display bounds is in the validateState()
method:
electron-window-state/index.js
Lines 45 to 69 in 6bb0a3f
Here is an explanation of what this is doing:
- Retrieve the display that most closely intersects the saved bounds, and get its bounds
- if the retrieved bounds is not deeply equal to the saved bounds:
- if the retrieved screen size is smaller than the saved screen size:
- if the saved window position values are bigger than the retrieved screen size:
- set the window position values to 0
- if the saved window size is bigger than the retrieved screen size:
- set the window size to the retrieved screen size
- if the saved window position values are bigger than the retrieved screen size:
- if the retrieved screen size is smaller than the saved screen size:
This logic has drawbacks because it only takes into accounts positive values. Therefore it only works with this exact dual screen setup (with the window previously positioned in the second screen which is bigger than the primary one):
Mac OSX is handling the window positioning differently than Windows. This difference can be simply tested by manually setting the main window position in a basic electron-quick-start example.
With these options:
mainWindow = new BrowserWindow({
'x': -100,
'y': 0,
'width': 800,
'height': 600,
});
the window in Windows will be 100px off the main screen and partly in the second screen, while on Mac OSX the window will be fully on the main screen. You can't position a window between two screens in OSX.
We can understand that Windows has an extended desktop while Mac OSX has separate ones.
Actually electron-boilerplate
checks if the window is within a display bounds. See electron-boilerplate:src/helpers/window.js.
Microsoft VSCode seems to have an interesting implementation too. See vscode:src/vs/code/electron-main/window.ts
With the merge of #45 this issue is hopefully fixed 👍
Thx @MathieuDebit