Skip to content

fix: win.center() sets the correct size and position on frameless windows#51732

Merged
jkleinsc merged 1 commit into
electron:mainfrom
mitchchn:mitch/fix-center
Jun 8, 2026
Merged

fix: win.center() sets the correct size and position on frameless windows#51732
jkleinsc merged 1 commit into
electron:mainfrom
mitchchn:mitch/fix-center

Conversation

@mitchchn

@mitchchn mitchchn commented May 21, 2026

Copy link
Copy Markdown
Member

Description of Change

Fixes #51731.

Calling win.center() caused frameless windows with insets to shrink on both Windows and Linux. Additionally, on Windows, frameless windows were not centered correctly on launch or when calling win.center().

This PR corrects both the shrinking and the positioning by introducing a shared implementation for NativeWindowViews::Center() for Linux and Windows. It is based on the existing Linux codepath but has been updated to use SetBounds(), which properly handles insets, instead of the lower level widget()->SetBounds(). On Windows, it replaces the call to gfx::CenterAndSizeWindow(size), which places windows too high if they have asymmetric insets.

While CenterAndSizeWindow could be patched upstream (this is an approach I have taken for other APIs) I looked at its implementation and do not see a benefit in having a divergent path for Windows in this instance. The upstream API is not cross-platform and is not used much even on Windows, and Electron isn't getting anything for free that it doesn't already have to support to position windows correctly. It is possible that I'm missing something, but I have done my best to test this simplification thoroughly.

With the new shared implementation, centering on both platforms is now accurate (at least at 100% DPI) and does not change the window's size. I was also able to get rid of an extra call to set bounds on init now that Center() has an improved implementation.

I added new tests for both initial window bounds and win.center() for framed and frameless windows. I also verified that frameless and framed windows are centered in the exact size and position on Windows as in 41.2.0 (prior to the insets changes).

(On Wayland, win.center() is still effectively a no-op, as the compositor will not honour the move. However, the window will no longer shrink if it is called.)

Backports to 41, 42, 43.

Checklist

Release Notes

Notes: Fixed an issue on Linux and Windows where frameless windows would shrink when calling win.center(). Additionally, fixed an issue where frameless windows would appear slightly off-center on Windows.

@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label May 21, 2026
@mitchchn mitchchn force-pushed the mitch/fix-center branch from 33de011 to f7e3a46 Compare May 21, 2026 17:44
@mitchchn

Copy link
Copy Markdown
Member Author

/request-review @jkleinsc, @ckerr

@mitchchn

Copy link
Copy Markdown
Member Author

/request-review @mlaurencin

@mitchchn mitchchn force-pushed the mitch/fix-center branch 6 times, most recently from e312d90 to f8124af Compare May 25, 2026 17:32
@mitchchn

Copy link
Copy Markdown
Member Author

Marked tests as passing because the macOS ARM64 failures are unrelated (#51776).

@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label May 28, 2026
@mitchchn mitchchn force-pushed the mitch/fix-center branch from f8124af to 55800bc Compare May 30, 2026 20:22
@mitchchn mitchchn force-pushed the mitch/fix-center branch from 55800bc to 0629bd8 Compare May 30, 2026 21:08
@mlaurencin mlaurencin added target/41-x-y PR should also be added to the "41-x-y" branch. target/42-x-y PR should also be added to the "42-x-y" branch. target/43-x-y PR should also be added to the "43-x-y" branch. semver/patch backwards-compatible bug fixes labels Jun 3, 2026

@mlaurencin mlaurencin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM on Windows

@jkleinsc jkleinsc merged commit d4d8a18 into electron:main Jun 8, 2026
84 checks passed
@release-clerk

release-clerk Bot commented Jun 8, 2026

Copy link
Copy Markdown

Release Notes Persisted

Fixed an issue on Linux and Windows where frameless windows would shrink when calling win.center(). Additionally, fixed an issue where frameless windows would appear slightly off-center on Windows.

@trop

trop Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "41-x-y", please check out #51919

@trop

trop Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "43-x-y", please check out #51920

@trop trop Bot removed the target/41-x-y PR should also be added to the "41-x-y" branch. label Jun 8, 2026
@trop

trop Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

I have automatically backported this PR to "42-x-y", please check out #51921

@trop trop Bot added in-flight/43-x-y in-flight/42-x-y merged/42-x-y PR was merged to the "42-x-y" branch. merged/43-x-y PR was merged to the "43-x-y" branch. merged/41-x-y PR was merged to the "41-x-y" branch. and removed target/43-x-y PR should also be added to the "43-x-y" branch. target/42-x-y PR should also be added to the "42-x-y" branch. in-flight/42-x-y in-flight/43-x-y in-flight/41-x-y labels Jun 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged/41-x-y PR was merged to the "41-x-y" branch. merged/42-x-y PR was merged to the "42-x-y" branch. merged/43-x-y PR was merged to the "43-x-y" branch. semver/patch backwards-compatible bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

win.center() shrinks frameless windows on Linux and Windows

3 participants