fix: maximized frameless window bleeding to other monitors (#25940)

This commit is contained in:
Cheng Zhao 2020-10-16 06:05:44 +09:00 committed by GitHub
parent 1c44bb8e74
commit 20c6677a9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 61 deletions

View file

@ -6,29 +6,11 @@
#include "base/win/windows_version.h" #include "base/win/windows_version.h"
#include "shell/browser/native_window_views.h" #include "shell/browser/native_window_views.h"
#include "ui/display/win/screen_win.h"
#include "ui/views/widget/widget.h" #include "ui/views/widget/widget.h"
#include "ui/views/win/hwnd_util.h" #include "ui/views/win/hwnd_util.h"
namespace electron { namespace electron {
namespace {
gfx::Insets GetGlassInsets() {
int frame_height =
display::win::ScreenWin::GetSystemMetricsInDIP(SM_CYSIZEFRAME) +
display::win::ScreenWin::GetSystemMetricsInDIP(SM_CXPADDEDBORDER);
int frame_size =
base::win::GetVersion() < base::win::Version::WIN8
? display::win::ScreenWin::GetSystemMetricsInDIP(SM_CXSIZEFRAME)
: 0;
return gfx::Insets(frame_height, frame_size, frame_size, frame_size);
}
} // namespace
const char WinFrameView::kViewClassName[] = "WinFrameView"; const char WinFrameView::kViewClassName[] = "WinFrameView";
WinFrameView::WinFrameView() {} WinFrameView::WinFrameView() {}
@ -42,17 +24,6 @@ gfx::Rect WinFrameView::GetWindowBoundsForClientBounds(
client_bounds); client_bounds);
} }
gfx::Rect WinFrameView::GetBoundsForClientView() const {
if (window_->IsMaximized() && !window_->has_frame()) {
gfx::Insets insets = GetGlassInsets();
gfx::Rect result(width(), height());
result.Inset(insets);
return result;
} else {
return bounds();
}
}
int WinFrameView::NonClientHitTest(const gfx::Point& point) { int WinFrameView::NonClientHitTest(const gfx::Point& point) {
if (window_->has_frame()) if (window_->has_frame())
return frame_->client_view()->NonClientHitTest(point); return frame_->client_view()->NonClientHitTest(point);

View file

@ -16,7 +16,6 @@ class WinFrameView : public FramelessView {
~WinFrameView() override; ~WinFrameView() override;
// views::NonClientFrameView: // views::NonClientFrameView:
gfx::Rect GetBoundsForClientView() const override;
gfx::Rect GetWindowBoundsForClientBounds( gfx::Rect GetWindowBoundsForClientBounds(
const gfx::Rect& client_bounds) const override; const gfx::Rect& client_bounds) const override;
int NonClientHitTest(const gfx::Point& point) override; int NonClientHitTest(const gfx::Point& point) override;

View file

@ -6,10 +6,7 @@
#include "base/win/windows_version.h" #include "base/win/windows_version.h"
#include "shell/browser/ui/views/win_frame_view.h" #include "shell/browser/ui/views/win_frame_view.h"
#include "ui/base/win/hwnd_metrics.h"
#include "ui/base/win/shell.h" #include "ui/base/win/shell.h"
#include "ui/display/win/screen_win.h"
#include "ui/views/win/hwnd_util.h"
namespace electron { namespace electron {
@ -41,23 +38,25 @@ bool ElectronDesktopWindowTreeHostWin::HasNativeFrame() const {
// Since we never use chromium's titlebar implementation, we can just say // Since we never use chromium's titlebar implementation, we can just say
// that we use a native titlebar. This will disable the repaint locking when // that we use a native titlebar. This will disable the repaint locking when
// DWM composition is disabled. // DWM composition is disabled.
// See also https://github.com/electron/electron/issues/1821.
return !ui::win::IsAeroGlassEnabled(); return !ui::win::IsAeroGlassEnabled();
} }
bool ElectronDesktopWindowTreeHostWin::GetDwmFrameInsetsInPixels( bool ElectronDesktopWindowTreeHostWin::GetDwmFrameInsetsInPixels(
gfx::Insets* insets) const { gfx::Insets* insets) const {
// Set DWMFrameInsets to prevent maximized frameless window from bleeding
// into other monitors.
if (IsMaximized() && !native_window_view_->has_frame()) { if (IsMaximized() && !native_window_view_->has_frame()) {
HMONITOR monitor = ::MonitorFromWindow( // This would be equivalent to calling:
native_window_view_->GetAcceleratedWidget(), MONITOR_DEFAULTTONEAREST); // DwmExtendFrameIntoClientArea({0, 0, 0, 0});
int frame_height = display::win::ScreenWin::GetSystemMetricsForMonitor( //
monitor, SM_CYSIZEFRAME) + // which means do not extend window frame into client area. It is almost
display::win::ScreenWin::GetSystemMetricsForMonitor( // a no-op, but it can tell Windows to not extend the window frame to be
monitor, SM_CXPADDEDBORDER); // larger than current workspace.
int frame_size = base::win::GetVersion() < base::win::Version::WIN8 //
? display::win::ScreenWin::GetSystemMetricsForMonitor( // See also:
monitor, SM_CXSIZEFRAME) // https://devblogs.microsoft.com/oldnewthing/20150304-00/?p=44543
: 0; *insets = gfx::Insets();
insets->Set(frame_height, frame_size, frame_size, frame_size);
return true; return true;
} }
return false; return false;
@ -66,24 +65,20 @@ bool ElectronDesktopWindowTreeHostWin::GetDwmFrameInsetsInPixels(
bool ElectronDesktopWindowTreeHostWin::GetClientAreaInsets( bool ElectronDesktopWindowTreeHostWin::GetClientAreaInsets(
gfx::Insets* insets, gfx::Insets* insets,
HMONITOR monitor) const { HMONITOR monitor) const {
// Windows by deafult extends the maximized window slightly larger than
// current workspace, for frameless window since the standard frame has been
// removed, the client area would then be drew outside current workspace.
//
// Indenting the client area can fix this behavior.
if (IsMaximized() && !native_window_view_->has_frame()) { if (IsMaximized() && !native_window_view_->has_frame()) {
if (base::win::GetVersion() < base::win::Version::WIN8) { // The insets would be eventually passed to WM_NCCALCSIZE, which takes
// This tells Windows that most of the window is a client area, meaning // the metrics under the DPI of _main_ monitor instead of current moniotr.
// Chrome will draw it. Windows still fills in the glass bits because of //
// the // DwmExtendFrameIntoClientArea call in |UpdateDWMFrame|. // Please make sure you tested maximized frameless window under multiple
// Without this 1 pixel offset on the right and bottom: // monitors with different DPIs before changing this code.
// * windows paint in a more standard way, and const int thickness = ::GetSystemMetrics(SM_CXSIZEFRAME) +
// * we get weird black bars at the top when maximized in multiple ::GetSystemMetrics(SM_CXPADDEDBORDER);
// monitor configurations. insets->Set(thickness, thickness, thickness, thickness);
int border_thickness = 1;
insets->Set(0, 0, border_thickness, border_thickness);
} else {
const int frame_thickness = ui::GetFrameThickness(monitor);
// Reduce the Windows non-client border size because we extend the border
// into our client area in UpdateDWMFrame(). The top inset must be 0 or
// else Windows will draw a full native titlebar outside the client area.
insets->Set(0, frame_thickness, frame_thickness, frame_thickness);
}
return true; return true;
} }
return false; return false;