Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App launched outside display area when second screen disconnected #44

Closed
MathieuDebit opened this issue Nov 12, 2018 · 5 comments
Closed

Comments

@MathieuDebit
Copy link
Contributor

MathieuDebit commented Nov 12, 2018

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

@gseregni
Copy link

Same here..

@MathieuDebit
Copy link
Contributor Author

MathieuDebit commented Nov 13, 2018

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

@MathieuDebit
Copy link
Contributor Author

MathieuDebit commented Nov 13, 2018

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

@MathieuDebit
Copy link
Contributor Author

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

@mawie81
Copy link
Owner

mawie81 commented Nov 25, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants