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