Skip to content

Commit

Permalink
fix: BrowserWindow.center() should center relative to screen (#42101)
Browse files Browse the repository at this point in the history
* fix: BrowserWindow.center() should center relative to screen

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* chore: feedback from review & remove test

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
  • Loading branch information
trop[bot] and codebytere committed May 9, 2024
1 parent cdf508a commit 12bc4dc
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions shell/browser/native_window_views.cc
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 12bc4dc

Please sign in to comment.