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