mawie81 / electron-window-state

A library to store and restore window sizes and positions for your Electron app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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:

const displayBounds = screen.getDisplayMatching(state).bounds;
const sameBounds = deepEqual(state.displayBounds, displayBounds, {strict: true});
if (!sameBounds) {
if (displayBounds.width < state.displayBounds.width) {
if (state.x > displayBounds.width) {
state.x = 0;
}
if (state.width > displayBounds.width) {
state.width = displayBounds.width;
}
}
if (displayBounds.height < state.displayBounds.height) {
if (state.y > displayBounds.height) {
state.y = 0;
}
if (state.height > displayBounds.height) {
state.height = displayBounds.height;
}
}
}
}
}

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

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):

capture

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

PR #45 proposes to fix this issue. Check it out 🤙

With the merge of #45 this issue is hopefully fixed 👍
Thx @MathieuDebit