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 "base/win/windows_version.h" | |
10 #include "dwmapi.h" | |
9 #include "ui/base/win/shell.h" | 11 #include "ui/base/win/shell.h" |
10 #include "ui/display/win/dpi.h" | 12 #include "ui/display/win/dpi.h" |
13 #include "ui/display/win/screen_win.h" | |
11 | 14 |
12 namespace { | 15 namespace { |
13 | 16 |
14 int GetMinimizeButtonOffsetForWindow(HWND hwnd) { | 17 // These constants were determined by manually adding various offsets |
15 // The WM_GETTITLEBARINFOEX message can fail if we are not active/visible. By | 18 // until the identity switcher was placed at the same location as before. |
16 // fail we get a location of 0; the return status code is always the same and | 19 // When a new or updated OS version is released, a new constant may need |
17 // similarly the state never seems to change (titlebar_info.rgstate). | 20 // to be added to this list and GetDefaultButtonBoundsOffset() is updated. |
18 TITLEBARINFOEX titlebar_info = {0}; | 21 const int kWin7ButtonBoundsPositionOffset = 1; |
19 titlebar_info.cbSize = sizeof(TITLEBARINFOEX); | 22 const int kWin8ButtonBoundsPositionOffset = 10; |
20 SendMessage(hwnd, WM_GETTITLEBARINFOEX, 0, | 23 const int kWin10ButtonBoundsPositionOffset = 6; |
21 reinterpret_cast<WPARAM>(&titlebar_info)); | 24 const int kInvalidOffset = (int)0x80000000; |
sky
2016/05/12 16:04:10
static_cast<int>
kylix_rd
2016/05/12 16:17:36
Done.
| |
22 | 25 |
23 if (titlebar_info.rgrect[2].left == titlebar_info.rgrect[2].right || | 26 using base::win::GetVersion; |
24 (titlebar_info.rgstate[2] & (STATE_SYSTEM_INVISIBLE | | 27 using display::win::ScreenWin; |
25 STATE_SYSTEM_OFFSCREEN | | |
26 STATE_SYSTEM_UNAVAILABLE))) { | |
27 return 0; | |
28 } | |
29 | 28 |
30 // WM_GETTITLEBARINFOEX returns rects in screen coordinates in pixels. | 29 int GetDefaultButtonBoundsOffset() { |
31 // We need to convert the minimize button corner offset to DIP before | 30 if (GetVersion() >= base::win::VERSION_WIN10) |
32 // returning it. | 31 return kWin10ButtonBoundsPositionOffset; |
33 POINT minimize_button_corner = { titlebar_info.rgrect[2].left, 0 }; | 32 else if (GetVersion() >= base::win::VERSION_WIN8) |
sky
2016/05/12 16:04:10
nit: no else after a return (see style guide).
kylix_rd
2016/05/12 16:17:36
Done.
| |
34 MapWindowPoints(HWND_DESKTOP, hwnd, &minimize_button_corner, 1); | 33 return kWin8ButtonBoundsPositionOffset; |
35 return minimize_button_corner.x / display::win::GetDPIScale(); | 34 else |
35 return kWin7ButtonBoundsPositionOffset; | |
36 } | 36 } |
37 | 37 |
38 } // namespace | 38 } // namespace |
39 | 39 |
40 // static | 40 // static |
41 int MinimizeButtonMetrics::last_cached_minimize_button_x_delta_ = 0; | 41 int MinimizeButtonMetrics::last_cached_minimize_button_x_delta_ = 0; |
42 | 42 |
43 // static | |
44 int MinimizeButtonMetrics::button_bounds_position_offset_ = kInvalidOffset; | |
45 | |
43 MinimizeButtonMetrics::MinimizeButtonMetrics() | 46 MinimizeButtonMetrics::MinimizeButtonMetrics() |
44 : hwnd_(nullptr), | 47 : hwnd_(nullptr), |
45 cached_minimize_button_x_delta_(last_cached_minimize_button_x_delta_), | 48 cached_minimize_button_x_delta_(last_cached_minimize_button_x_delta_), |
46 was_activated_(false) { | 49 was_activated_(false) { |
47 } | 50 } |
48 | 51 |
49 MinimizeButtonMetrics::~MinimizeButtonMetrics() { | 52 MinimizeButtonMetrics::~MinimizeButtonMetrics() { |
50 } | 53 } |
51 | 54 |
52 void MinimizeButtonMetrics::Init(HWND hwnd) { | 55 void MinimizeButtonMetrics::Init(HWND hwnd) { |
53 DCHECK(!hwnd_); | 56 DCHECK(!hwnd_); |
54 hwnd_ = hwnd; | 57 hwnd_ = hwnd; |
55 } | 58 } |
56 | 59 |
57 void MinimizeButtonMetrics::OnHWNDActivated() { | 60 void MinimizeButtonMetrics::OnHWNDActivated() { |
58 was_activated_ = true; | 61 was_activated_ = true; |
59 // NOTE: we don't cache here as it seems only after the activate is the value | 62 // NOTE: we don't cache here as it seems only after the activate is the value |
60 // correct. | 63 // correct. |
61 } | 64 } |
62 | 65 |
66 // This function attempts to calculate the odd and varying difference | |
67 // between the results of DwmGetWindowAttribute with the | |
68 // DWMWA_CAPTION_BUTTON_BOUNDS flag and the information from the | |
69 // WM_GETTITLEBARINFOEX message. It will return an empirically determined | |
70 // offset until the window has been activated and the message returns | |
71 // valid rectangles. | |
72 int MinimizeButtonMetrics::GetButtonBoundsPositionOffset( | |
73 const RECT& button_bounds, | |
74 const RECT& window_bounds) const { | |
75 if (button_bounds_position_offset_ == kInvalidOffset) { | |
76 if (!was_activated_) | |
77 return GetDefaultButtonBoundsOffset(); | |
78 TITLEBARINFOEX info = {0}; | |
79 info.cbSize = sizeof(info); | |
80 SendMessage(hwnd_, WM_GETTITLEBARINFOEX, 0, | |
81 reinterpret_cast<LPARAM>(&info)); | |
82 if (info.rgrect[2].right == info.rgrect[2].left || | |
83 (info.rgstate[2] & (STATE_SYSTEM_INVISIBLE | STATE_SYSTEM_OFFSCREEN | | |
84 STATE_SYSTEM_UNAVAILABLE))) | |
85 return GetDefaultButtonBoundsOffset(); | |
86 button_bounds_position_offset_ = | |
87 info.rgrect[2].left - (button_bounds.left + window_bounds.left); | |
88 } | |
89 return button_bounds_position_offset_; | |
90 } | |
91 | |
92 int MinimizeButtonMetrics::GetMinimizeButtonOffsetForWindow() const { | |
93 bool dwm_button_pos = false; | |
94 POINT minimize_button_corner = {0}; | |
95 RECT button_bounds = {0}; | |
96 if (SUCCEEDED(DwmGetWindowAttribute(hwnd_, DWMWA_CAPTION_BUTTON_BOUNDS, | |
97 &button_bounds, sizeof(button_bounds)))) { | |
98 if (button_bounds.left != button_bounds.right) { | |
99 // This converts the button coordinate into screen coordinates | |
100 // thus, ensuring that the identity switcher is placed in the | |
101 // same location as before. An additional constant is added because | |
102 // there is a difference between the caption button bounds and | |
103 // the values obtained through WM_GETTITLEBARINFOEX. This difference | |
104 // varies between OS versions, and no metric describing this difference | |
105 // has been located. | |
106 RECT window_bounds = {0}; | |
107 if (GetWindowRect(hwnd_, &window_bounds)) { | |
108 int offset = | |
109 GetButtonBoundsPositionOffset(button_bounds, window_bounds); | |
110 minimize_button_corner = { | |
111 button_bounds.left + window_bounds.left + offset, 0}; | |
112 dwm_button_pos = true; | |
113 } | |
114 } | |
115 } | |
116 if (!dwm_button_pos) { | |
117 // Fallback to using the message for the titlebar info only if the above | |
118 // code fails. It can fail if DWM is disabled globally or only for the | |
119 // given HWND. The WM_GETTITLEBARINFOEX message can fail if we are not | |
120 // active/visible. By fail we get a location of 0; the return status | |
121 // code is always the same and similarly the state never seems to change | |
122 // (titlebar_info.rgstate). | |
123 TITLEBARINFOEX titlebar_info = {0}; | |
124 titlebar_info.cbSize = sizeof(TITLEBARINFOEX); | |
125 SendMessage(hwnd_, WM_GETTITLEBARINFOEX, 0, | |
126 reinterpret_cast<WPARAM>(&titlebar_info)); | |
127 | |
128 // Under DWM WM_GETTITLEBARINFOEX won't return the right thing until after | |
129 // WM_NCACTIVATE (maybe it returns classic values?). In an attempt to | |
130 // return a consistant value we cache the last value across instances and | |
131 // use it until we get the activate. | |
132 if (titlebar_info.rgrect[2].left == titlebar_info.rgrect[2].right || | |
133 (titlebar_info.rgstate[2] & | |
134 (STATE_SYSTEM_INVISIBLE | STATE_SYSTEM_OFFSCREEN | | |
135 STATE_SYSTEM_UNAVAILABLE))) | |
136 return 0; | |
137 minimize_button_corner = {titlebar_info.rgrect[2].left, 0}; | |
138 } | |
139 | |
140 // WM_GETTITLEBARINFOEX returns rects in screen coordinates in pixels. | |
141 // DWMNA_CAPTION_BUTTON_BOUNDS is in window (not client) coordinates, | |
142 // but it has been converted to screen coordinates above. We need to | |
143 // convert the minimize button corner offset to DIP before returning it. | |
144 MapWindowPoints(HWND_DESKTOP, hwnd_, &minimize_button_corner, 1); | |
145 gfx::Point pixel_point = {minimize_button_corner.x, 0}; | |
146 gfx::Point dip_point = ScreenWin::ClientToDIPPoint(hwnd_, pixel_point); | |
147 return dip_point.x(); | |
148 } | |
149 | |
63 int MinimizeButtonMetrics::GetMinimizeButtonOffsetX() const { | 150 int MinimizeButtonMetrics::GetMinimizeButtonOffsetX() const { |
64 // Under DWM WM_GETTITLEBARINFOEX won't return the right thing until after | 151 // 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 | 152 // 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 | 153 // consistant value we cache the last value across instances and use it until |
67 // we get the activate. | 154 // we get the activate. |
68 if (was_activated_ || !ui::win::IsAeroGlassEnabled() || | 155 if (was_activated_ || !ui::win::IsAeroGlassEnabled() || |
69 cached_minimize_button_x_delta_ == 0) { | 156 cached_minimize_button_x_delta_ == 0) { |
70 const int minimize_button_offset = GetAndCacheMinimizeButtonOffsetX(); | 157 const int minimize_button_offset = GetAndCacheMinimizeButtonOffsetX(); |
71 if (minimize_button_offset > 0) | 158 if (minimize_button_offset > 0) |
72 return minimize_button_offset; | 159 return minimize_button_offset; |
73 } | 160 } |
74 | 161 |
75 // If we fail to get the minimize button offset via the WM_GETTITLEBARINFOEX | 162 // If we fail to get the minimize button offset via the WM_GETTITLEBARINFOEX |
76 // message then calculate and return this via the | 163 // message then calculate and return this via the |
77 // cached_minimize_button_x_delta_ member value. Please see | 164 // cached_minimize_button_x_delta_ member value. Please see |
78 // CacheMinimizeButtonDelta() for more details. | 165 // CacheMinimizeButtonDelta() for more details. |
79 DCHECK(cached_minimize_button_x_delta_); | 166 DCHECK(cached_minimize_button_x_delta_); |
80 | 167 |
81 if (base::i18n::IsRTL()) | 168 if (base::i18n::IsRTL()) |
82 return cached_minimize_button_x_delta_; | 169 return cached_minimize_button_x_delta_; |
83 | 170 |
84 RECT client_rect = {0}; | 171 RECT client_rect = {0}; |
85 GetClientRect(hwnd_, &client_rect); | 172 GetClientRect(hwnd_, &client_rect); |
86 return client_rect.right - cached_minimize_button_x_delta_; | 173 return client_rect.right - cached_minimize_button_x_delta_; |
87 } | 174 } |
88 | 175 |
89 int MinimizeButtonMetrics::GetAndCacheMinimizeButtonOffsetX() const { | 176 int MinimizeButtonMetrics::GetAndCacheMinimizeButtonOffsetX() const { |
90 const int minimize_button_offset = GetMinimizeButtonOffsetForWindow(hwnd_); | 177 const int minimize_button_offset = GetMinimizeButtonOffsetForWindow(); |
91 if (minimize_button_offset <= 0) | 178 if (minimize_button_offset <= 0) |
92 return 0; | 179 return 0; |
93 | 180 |
94 if (base::i18n::IsRTL()) { | 181 if (base::i18n::IsRTL()) { |
95 cached_minimize_button_x_delta_ = minimize_button_offset; | 182 cached_minimize_button_x_delta_ = minimize_button_offset; |
96 } else { | 183 } else { |
97 RECT client_rect = {0}; | 184 RECT client_rect = {0}; |
98 GetClientRect(hwnd_, &client_rect); | 185 GetClientRect(hwnd_, &client_rect); |
99 cached_minimize_button_x_delta_ = | 186 cached_minimize_button_x_delta_ = |
100 client_rect.right - minimize_button_offset; | 187 client_rect.right - minimize_button_offset; |
101 } | 188 } |
102 last_cached_minimize_button_x_delta_ = cached_minimize_button_x_delta_; | 189 last_cached_minimize_button_x_delta_ = cached_minimize_button_x_delta_; |
103 return minimize_button_offset; | 190 return minimize_button_offset; |
104 } | 191 } |
OLD | NEW |