From 731bc7a334565b67b0f75b8b713bfb55db12e67a Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Thu, 9 May 2024 00:42:20 +0200 Subject: [PATCH] fix: `BrowserWindow.center()` should center relative to screen (#42048) --- shell/browser/native_window_views.cc | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/shell/browser/native_window_views.cc b/shell/browser/native_window_views.cc index 0528acdadabf..e18fcc133597 100644 --- a/shell/browser/native_window_views.cc +++ b/shell/browser/native_window_views.cc @@ -39,6 +39,7 @@ #include "shell/common/options_switches.h" #include "ui/aura/window_tree_host.h" #include "ui/base/hit_test.h" +#include "ui/display/screen.h" #include "ui/gfx/image/image.h" #include "ui/gfx/native_widget_types.h" #include "ui/ozone/public/ozone_platform.h" @@ -80,9 +81,9 @@ #include "shell/browser/ui/win/electron_desktop_native_widget_aura.h" #include "skia/ext/skia_utils_win.h" #include "ui/base/win/shell.h" -#include "ui/display/screen.h" #include "ui/display/win/screen_win.h" #include "ui/gfx/color_utils.h" +#include "ui/gfx/win/hwnd_util.h" #include "ui/gfx/win/msg_util.h" #endif @@ -1069,8 +1070,23 @@ ui::ZOrderLevel NativeWindowViews::GetZOrderLevel() const { return widget()->GetZOrderLevel(); } +// We previous called widget()->CenterWindow() here, but in +// Chromium CL 4916277 behavior was changed to center relative to the +// parent window if there is one. We want to keep the old behavior +// for now to avoid breaking API contract, but should consider the long +// term plan for this aligning with upstream. void NativeWindowViews::Center() { - widget()->CenterWindow(GetSize()); +#if BUILDFLAG(IS_LINUX) + auto display = + display::Screen::GetScreen()->GetDisplayNearestWindow(GetNativeWindow()); + gfx::Rect window_bounds_in_screen = display.work_area(); + window_bounds_in_screen.ClampToCenteredSize(GetSize()); + widget()->SetBounds(window_bounds_in_screen); +#else + HWND hwnd = GetAcceleratedWidget(); + gfx::Size size = display::win::ScreenWin::DIPToScreenSize(hwnd, GetSize()); + gfx::CenterAndSizeWindow(hwnd, hwnd, size); +#endif } void NativeWindowViews::Invalidate() {