From 431dcfc0fe59438b4d9ef4a9aeb062a7884e1d10 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Thu, 9 May 2024 10:00:24 +0200 Subject: [PATCH] fix: `BrowserWindow.center()` should center relative to screen (#42100) * fix: BrowserWindow.center() should center relative to screen Co-authored-by: Shelley Vohr * chore: feedback from review & remove test Co-authored-by: Shelley Vohr --------- Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Shelley Vohr --- 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 de3c0f2bb08..0c6b087a41d 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() {