| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/views/frame/minimize_button_metrics_win.h" | 5 #include "chrome/browser/ui/views/frame/minimize_button_metrics_win.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/i18n/rtl.h" | 8 #include "base/i18n/rtl.h" |
| 9 #include "dwmapi.h" | |
| 10 #include "ui/base/win/shell.h" | 9 #include "ui/base/win/shell.h" |
| 11 #include "ui/display/win/dpi.h" | 10 #include "ui/display/win/dpi.h" |
| 12 #include "ui/display/win/screen_win.h" | |
| 13 | 11 |
| 14 namespace { | 12 namespace { |
| 15 | 13 |
| 16 using display::win::ScreenWin; | 14 int GetMinimizeButtonOffsetForWindow(HWND hwnd) { |
| 15 // The WM_GETTITLEBARINFOEX message can fail if we are not active/visible. By |
| 16 // fail we get a location of 0; the return status code is always the same and |
| 17 // similarly the state never seems to change (titlebar_info.rgstate). |
| 18 TITLEBARINFOEX titlebar_info = {0}; |
| 19 titlebar_info.cbSize = sizeof(TITLEBARINFOEX); |
| 20 SendMessage(hwnd, WM_GETTITLEBARINFOEX, 0, |
| 21 reinterpret_cast<WPARAM>(&titlebar_info)); |
| 17 | 22 |
| 18 int GetMinimizeButtonOffsetForWindow(HWND hwnd, bool was_activated) { | 23 if (titlebar_info.rgrect[2].left == titlebar_info.rgrect[2].right || |
| 19 bool dwm_button_pos = false; | 24 (titlebar_info.rgstate[2] & (STATE_SYSTEM_INVISIBLE | |
| 20 POINT minimize_button_corner = {0}; | 25 STATE_SYSTEM_OFFSCREEN | |
| 21 RECT button_bounds = {0}; | 26 STATE_SYSTEM_UNAVAILABLE))) { |
| 22 if (SUCCEEDED(DwmGetWindowAttribute(hwnd, DWMWA_CAPTION_BUTTON_BOUNDS, | 27 return 0; |
| 23 &button_bounds, sizeof(button_bounds)))) { | |
| 24 if (button_bounds.left != button_bounds.right) { | |
| 25 // This converts the button coordinate into screen coordinates | |
| 26 // thus, ensuring that the identity switcher is placed in the | |
| 27 // same location as before. | |
| 28 RECT window_bounds = {0}; | |
| 29 if (GetWindowRect(hwnd, &window_bounds)) { | |
| 30 minimize_button_corner = {button_bounds.left + window_bounds.left, 0}; | |
| 31 dwm_button_pos = true; | |
| 32 } | |
| 33 } | |
| 34 } | |
| 35 if (!dwm_button_pos) { | |
| 36 // Fallback to using the message for the titlebar info only if the above | |
| 37 // code fails. It can fail if DWM is disabled globally or only for the | |
| 38 // given HWND. The WM_GETTITLEBARINFOEX message can fail if we are not | |
| 39 // active/visible. By fail we get a location of 0; the return status | |
| 40 // code is always the same and similarly the state never seems to change | |
| 41 // (titlebar_info.rgstate). | |
| 42 TITLEBARINFOEX titlebar_info = {0}; | |
| 43 titlebar_info.cbSize = sizeof(TITLEBARINFOEX); | |
| 44 SendMessage(hwnd, WM_GETTITLEBARINFOEX, 0, | |
| 45 reinterpret_cast<WPARAM>(&titlebar_info)); | |
| 46 | |
| 47 // Under DWM WM_GETTITLEBARINFOEX won't return the right thing until after | |
| 48 // WM_NCACTIVATE (maybe it returns classic values?). In an attempt to | |
| 49 // return a consistant value we cache the last value across instances and | |
| 50 // use it until we get the activate. | |
| 51 if (!was_activated || | |
| 52 titlebar_info.rgrect[2].left == titlebar_info.rgrect[2].right || | |
| 53 (titlebar_info.rgstate[2] & | |
| 54 (STATE_SYSTEM_INVISIBLE | STATE_SYSTEM_OFFSCREEN | | |
| 55 STATE_SYSTEM_UNAVAILABLE))) { | |
| 56 return 0; | |
| 57 } | |
| 58 minimize_button_corner = {titlebar_info.rgrect[2].left, 0}; | |
| 59 } | 28 } |
| 60 | 29 |
| 61 // WM_GETTITLEBARINFOEX returns rects in screen coordinates in pixels. | 30 // WM_GETTITLEBARINFOEX returns rects in screen coordinates in pixels. |
| 62 // DWMNA_CAPTION_BUTTON_BOUNDS is in window (not client) coordinates, | 31 // We need to convert the minimize button corner offset to DIP before |
| 63 // but it has been converted to screen coordinates above. We need to | 32 // returning it. |
| 64 // convert the minimize button corner offset to DIP before returning it. | 33 POINT minimize_button_corner = { titlebar_info.rgrect[2].left, 0 }; |
| 65 MapWindowPoints(HWND_DESKTOP, hwnd, &minimize_button_corner, 1); | 34 MapWindowPoints(HWND_DESKTOP, hwnd, &minimize_button_corner, 1); |
| 66 gfx::Point pixel_point = {minimize_button_corner.x, 0}; | 35 return minimize_button_corner.x / display::win::GetDPIScale(); |
| 67 gfx::Point dip_point = ScreenWin::ClientToDIPPoint(hwnd, pixel_point); | |
| 68 return dip_point.x(); | |
| 69 } | 36 } |
| 70 | 37 |
| 71 } // namespace | 38 } // namespace |
| 72 | 39 |
| 73 // static | 40 // static |
| 74 int MinimizeButtonMetrics::last_cached_minimize_button_x_delta_ = 0; | 41 int MinimizeButtonMetrics::last_cached_minimize_button_x_delta_ = 0; |
| 75 | 42 |
| 76 MinimizeButtonMetrics::MinimizeButtonMetrics() | 43 MinimizeButtonMetrics::MinimizeButtonMetrics() |
| 77 : hwnd_(nullptr), | 44 : hwnd_(nullptr), |
| 78 cached_minimize_button_x_delta_(last_cached_minimize_button_x_delta_), | 45 cached_minimize_button_x_delta_(last_cached_minimize_button_x_delta_), |
| 79 was_activated_(false) { | 46 was_activated_(false) { |
| 80 } | 47 } |
| 81 | 48 |
| 82 MinimizeButtonMetrics::~MinimizeButtonMetrics() { | 49 MinimizeButtonMetrics::~MinimizeButtonMetrics() { |
| 83 } | 50 } |
| 84 | 51 |
| 85 void MinimizeButtonMetrics::Init(HWND hwnd) { | 52 void MinimizeButtonMetrics::Init(HWND hwnd) { |
| 86 DCHECK(!hwnd_); | 53 DCHECK(!hwnd_); |
| 87 hwnd_ = hwnd; | 54 hwnd_ = hwnd; |
| 88 } | 55 } |
| 89 | 56 |
| 90 void MinimizeButtonMetrics::OnHWNDActivated() { | 57 void MinimizeButtonMetrics::OnHWNDActivated() { |
| 91 was_activated_ = true; | 58 was_activated_ = true; |
| 92 // NOTE: we don't cache here as it seems only after the activate is the | 59 // NOTE: we don't cache here as it seems only after the activate is the value |
| 93 // value correct. | 60 // correct. |
| 94 } | 61 } |
| 95 | 62 |
| 96 int MinimizeButtonMetrics::GetMinimizeButtonOffsetX() const { | 63 int MinimizeButtonMetrics::GetMinimizeButtonOffsetX() const { |
| 97 if (!ui::win::IsAeroGlassEnabled() || cached_minimize_button_x_delta_ == 0) { | 64 // Under DWM WM_GETTITLEBARINFOEX won't return the right thing until after |
| 65 // WM_NCACTIVATE (maybe it returns classic values?). In an attempt to return a |
| 66 // consistant value we cache the last value across instances and use it until |
| 67 // we get the activate. |
| 68 if (was_activated_ || !ui::win::IsAeroGlassEnabled() || |
| 69 cached_minimize_button_x_delta_ == 0) { |
| 98 const int minimize_button_offset = GetAndCacheMinimizeButtonOffsetX(); | 70 const int minimize_button_offset = GetAndCacheMinimizeButtonOffsetX(); |
| 99 if (minimize_button_offset > 0) | 71 if (minimize_button_offset > 0) |
| 100 return minimize_button_offset; | 72 return minimize_button_offset; |
| 101 } | 73 } |
| 102 | 74 |
| 103 // If we fail to get the minimize button offset via the WM_GETTITLEBARINFOEX | 75 // If we fail to get the minimize button offset via the WM_GETTITLEBARINFOEX |
| 104 // message or DwmGetWindowAttribute then calculate and return this via the | 76 // message then calculate and return this via the |
| 105 // cached_minimize_button_x_delta_ member value. Please see | 77 // cached_minimize_button_x_delta_ member value. Please see |
| 106 // CacheMinimizeButtonDelta() for more details. | 78 // CacheMinimizeButtonDelta() for more details. |
| 107 DCHECK(cached_minimize_button_x_delta_); | 79 DCHECK(cached_minimize_button_x_delta_); |
| 108 | 80 |
| 109 if (base::i18n::IsRTL()) | 81 if (base::i18n::IsRTL()) |
| 110 return cached_minimize_button_x_delta_; | 82 return cached_minimize_button_x_delta_; |
| 111 | 83 |
| 112 RECT client_rect = {0}; | 84 RECT client_rect = {0}; |
| 113 GetClientRect(hwnd_, &client_rect); | 85 GetClientRect(hwnd_, &client_rect); |
| 114 return client_rect.right - cached_minimize_button_x_delta_; | 86 return client_rect.right - cached_minimize_button_x_delta_; |
| 115 } | 87 } |
| 116 | 88 |
| 117 int MinimizeButtonMetrics::GetAndCacheMinimizeButtonOffsetX() const { | 89 int MinimizeButtonMetrics::GetAndCacheMinimizeButtonOffsetX() const { |
| 118 const int minimize_button_offset = | 90 const int minimize_button_offset = GetMinimizeButtonOffsetForWindow(hwnd_); |
| 119 GetMinimizeButtonOffsetForWindow(hwnd_, was_activated_); | |
| 120 if (minimize_button_offset <= 0) | 91 if (minimize_button_offset <= 0) |
| 121 return 0; | 92 return 0; |
| 122 | 93 |
| 123 if (base::i18n::IsRTL()) { | 94 if (base::i18n::IsRTL()) { |
| 124 cached_minimize_button_x_delta_ = minimize_button_offset; | 95 cached_minimize_button_x_delta_ = minimize_button_offset; |
| 125 } else { | 96 } else { |
| 126 RECT client_rect = {0}; | 97 RECT client_rect = {0}; |
| 127 GetClientRect(hwnd_, &client_rect); | 98 GetClientRect(hwnd_, &client_rect); |
| 128 cached_minimize_button_x_delta_ = | 99 cached_minimize_button_x_delta_ = |
| 129 client_rect.right - minimize_button_offset; | 100 client_rect.right - minimize_button_offset; |
| 130 } | 101 } |
| 131 last_cached_minimize_button_x_delta_ = cached_minimize_button_x_delta_; | 102 last_cached_minimize_button_x_delta_ = cached_minimize_button_x_delta_; |
| 132 return minimize_button_offset; | 103 return minimize_button_offset; |
| 133 } | 104 } |
| OLD | NEW |