Skip to content

Commit

Permalink
fix: adjust window size in NCCALCSIZE instead of adding insets (elect…
Browse files Browse the repository at this point in the history
  • Loading branch information
brenca authored and zcbenz committed Aug 26, 2019
1 parent 080fdb3 commit f6c523d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 37 deletions.
42 changes: 24 additions & 18 deletions shell/browser/native_window_views_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,15 @@ int GetAppbarAutohideEdges(HWND hwnd) {
return edges;
}

void TriggerNCCalcSize(HWND hwnd) {
RECT rcClient;
::GetWindowRect(hwnd, &rcClient);

::SetWindowPos(hwnd, NULL, rcClient.left, rcClient.top,
rcClient.right - rcClient.left, rcClient.bottom - rcClient.top,
SWP_FRAMECHANGED);
}

} // namespace

std::set<NativeWindowViews*> NativeWindowViews::forwarding_windows_;
Expand Down Expand Up @@ -369,8 +378,12 @@ bool NativeWindowViews::PreHandleMSG(UINT message,
// https://blogs.msdn.microsoft.com/wpfsdk/2008/09/08/custom-window-chrome-in-wpf/
DefWindowProcW(GetAcceleratedWidget(), WM_NCCALCSIZE, w_param, l_param);

params->rgrc[0] = PROPOSED;
params->rgrc[1] = BEFORE;
if (last_window_state_ == ui::SHOW_STATE_MAXIMIZED) {
params->rgrc[0].top = 0;
} else {
params->rgrc[0] = PROPOSED;
params->rgrc[1] = BEFORE;
}

return true;
} else {
Expand Down Expand Up @@ -453,26 +466,15 @@ void NativeWindowViews::HandleSizeEvent(WPARAM w_param, LPARAM l_param) {
// window state and notify the user accordingly.
switch (w_param) {
case SIZE_MAXIMIZED: {
// Frameless maximized windows are size compensated by Windows for a
// border that's not actually there, so we must conter-compensate.
// https://blogs.msdn.microsoft.com/wpfsdk/2008/09/08/custom-window-chrome-in-wpf/
if (!has_frame()) {
float scale_factor = display::win::ScreenWin::GetScaleFactorForHWND(
GetAcceleratedWidget());

int border =
GetSystemMetrics(SM_CXFRAME) + GetSystemMetrics(SM_CXPADDEDBORDER);
if (!thick_frame_) {
border -= GetSystemMetrics(SM_CXBORDER);
}
root_view_->SetInsets(gfx::Insets(border).Scale(1.0f / scale_factor));
}

last_window_state_ = ui::SHOW_STATE_MAXIMIZED;
if (consecutive_moves_) {
last_normal_bounds_ = last_normal_bounds_before_move_;
}

if (!has_frame()) {
TriggerNCCalcSize(GetAcceleratedWidget());
}

NotifyWindowMaximize();
break;
}
Expand All @@ -497,8 +499,12 @@ void NativeWindowViews::HandleSizeEvent(WPARAM w_param, LPARAM l_param) {
switch (last_window_state_) {
case ui::SHOW_STATE_MAXIMIZED:
last_window_state_ = ui::SHOW_STATE_NORMAL;
root_view_->SetInsets(gfx::Insets(0));
NotifyWindowUnmaximize();

if (!has_frame()) {
TriggerNCCalcSize(GetAcceleratedWidget());
}

break;
case ui::SHOW_STATE_MINIMIZED:
if (IsFullscreen()) {
Expand Down
19 changes: 4 additions & 15 deletions shell/browser/ui/views/root_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,18 +176,14 @@ void RootView::Layout() {
return;

const auto menu_bar_bounds =
menu_bar_visible_
? gfx::Rect(insets_.left(), insets_.top(),
size().width() - insets_.width(), kMenuBarHeight)
: gfx::Rect();
menu_bar_visible_ ? gfx::Rect(0, 0, size().width(), kMenuBarHeight)
: gfx::Rect();
if (menu_bar_)
menu_bar_->SetBoundsRect(menu_bar_bounds);

window_->content_view()->SetBoundsRect(
gfx::Rect(insets_.left(),
menu_bar_visible_ ? menu_bar_bounds.bottom() : insets_.top(),
size().width() - insets_.width(),
size().height() - menu_bar_bounds.height() - insets_.height()));
gfx::Rect(0, menu_bar_visible_ ? menu_bar_bounds.bottom() : 0,
size().width(), size().height() - menu_bar_bounds.height()));
}

gfx::Size RootView::GetMinimumSize() const {
Expand Down Expand Up @@ -224,11 +220,4 @@ void RootView::UnregisterAcceleratorsWithFocusManager() {
focus_manager->UnregisterAccelerators(this);
}

void RootView::SetInsets(const gfx::Insets& insets) {
if (insets != insets_) {
insets_ = insets;
Layout();
}
}

} // namespace electron
4 changes: 0 additions & 4 deletions shell/browser/ui/views/root_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ class RootView : public views::View {
// Register/Unregister accelerators supported by the menu model.
void RegisterAcceleratorsWithFocusManager(AtomMenuModel* menu_model);
void UnregisterAcceleratorsWithFocusManager();
void SetInsets(const gfx::Insets& insets);
gfx::Insets insets() const { return insets_; }

// views::View:
void Layout() override;
Expand All @@ -59,8 +57,6 @@ class RootView : public views::View {
bool menu_bar_visible_ = false;
bool menu_bar_alt_pressed_ = false;

gfx::Insets insets_;

// Map from accelerator to menu item's command id.
accelerator_util::AcceleratorTable accelerator_table_;

Expand Down

0 comments on commit f6c523d

Please sign in to comment.