electron / electron

:electron: Build cross-platform desktop apps with JavaScript, HTML, and CSS
https://electronjs.org
MIT License
112.33k stars 15.02k forks source link

[Bug]: Dialog Window is not centered if moved to differnt screen #42194

Closed martbrugg closed 2 weeks ago

martbrugg commented 2 weeks ago

Preflight Checklist

Electron Version

30.0.5

What operating system are you using?

Windows

Operating System Version

Windows 11

What arch are you using?

x64

Last Known Working Electron version

30.0.3

Expected Behavior

If i move the window with win.setPosition to the current screen and call win.center() aftwards I expect the window to be centered in this screen.

const { app, BrowserWindow } = require("electron");
const { screen } = require("electron/main");

const createWindow = () => {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
  });

  win.loadFile("index.html");
};

app.whenReady().then(() => {
  createWindow();

 //after primary window is shown i move it to a second screen an wait for the second window to appear
  setTimeout(() => {
    createDialogWindow();
  }, 10000);
});

const createDialogWindow = () => {
  const win = new BrowserWindow({
    width: 400,
    height: 300,
  });

  const cursor = screen.getCursorScreenPoint();
  const currentScreen = screen.getDisplayNearestPoint(cursor);
  win.setPosition(currentScreen.workArea.x, currentScreen.workArea.y, false);
  win.center();

  win.loadFile("index.html");
};

Actual Behavior

Dialog Window ist positioned in the left top corner of the screen.

image

Testcase Gist URL

Example https://gist.github.com/martbrugg/d553aabb9751853bb9188c8808b31bb2

Additional Information

the provided code example works till version 30.0.3. Starting with V3.0.4 this issue appears