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" | 9 #include "base/win/windows_version.h" |
10 #include "dwmapi.h" | 10 #include "dwmapi.h" |
11 #include "ui/base/win/shell.h" | 11 #include "ui/base/win/shell.h" |
12 #include "ui/display/win/dpi.h" | 12 #include "ui/display/win/dpi.h" |
13 #include "ui/display/win/screen_win.h" | 13 #include "ui/display/win/screen_win.h" |
14 #include "ui/gfx/geometry/point.h" | 14 #include "ui/gfx/geometry/point.h" |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 // These constants were determined by manually adding various offsets | 18 // These constants were determined by manually adding various offsets |
19 // until the identity switcher was placed at the same location as before. | 19 // until the identity switcher was placed at the same location as before. |
20 // When a new or updated OS version is released, a new constant may need | 20 // When a new or updated OS version is released, a new constant may need |
21 // to be added to this list and GetDefaultButtonBoundsOffset() is updated. | 21 // to be added to this list and GetDefaultButtonBoundsOffset() is updated. |
22 const int kWin7ButtonBoundsPositionOffset = 1; | 22 const int kWin7ButtonBoundsPositionOffset = 1; |
23 const int kWin8ButtonBoundsPositionOffset = 10; | 23 const int kWin8ButtonBoundsPositionOffset = 10; |
24 const int kWin10ButtonBoundsPositionOffset = 6; | 24 const int kWin10ButtonBoundsPositionOffset = 6; |
25 const int kInvalidOffset = static_cast<int>(0x80000000); | 25 const int kInvalidOffset = static_cast<int>(0x80000000); |
26 | 26 |
27 // These constant were determined manually by testing on Windows 7, 8 and 10. | |
28 const int kWin7ButtonHeight = 20; | |
29 const int kWin8ButtonHeight = 20; | |
30 const int kWin10ButtonHeight = 28; | |
31 const int kInvalidHeight = static_cast<int>(0x80000000); | |
32 | |
27 using base::win::GetVersion; | 33 using base::win::GetVersion; |
28 using display::win::ScreenWin; | 34 using display::win::ScreenWin; |
29 | 35 |
30 int GetDefaultButtonBoundsOffset() { | 36 int GetDefaultButtonBoundsOffset() { |
31 if (GetVersion() >= base::win::VERSION_WIN10) | 37 if (GetVersion() >= base::win::VERSION_WIN10) |
32 return kWin10ButtonBoundsPositionOffset; | 38 return kWin10ButtonBoundsPositionOffset; |
33 if (GetVersion() >= base::win::VERSION_WIN8) | 39 if (GetVersion() >= base::win::VERSION_WIN8) |
34 return kWin8ButtonBoundsPositionOffset; | 40 return kWin8ButtonBoundsPositionOffset; |
35 return kWin7ButtonBoundsPositionOffset; | 41 return kWin7ButtonBoundsPositionOffset; |
36 } | 42 } |
37 | 43 |
44 int GetDefaultButtonHeight() { | |
45 if (GetVersion() >= base::win::VERSION_WIN10) | |
46 return kWin10ButtonHeight; | |
47 if (GetVersion() >= base::win::VERSION_WIN8) | |
48 return kWin8ButtonHeight; | |
49 return kWin7ButtonHeight; | |
50 } | |
51 | |
38 } // namespace | 52 } // namespace |
39 | 53 |
40 // static | 54 // static |
41 int MinimizeButtonMetrics::last_cached_minimize_button_x_delta_ = 0; | 55 int MinimizeButtonMetrics::last_cached_minimize_button_x_delta_ = 0; |
42 | 56 |
43 // static | 57 // static |
44 int MinimizeButtonMetrics::button_bounds_position_offset_ = kInvalidOffset; | 58 int MinimizeButtonMetrics::button_bounds_position_offset_ = kInvalidOffset; |
45 | 59 |
60 // static | |
61 int MinimizeButtonMetrics::caption_button_height_ = kInvalidHeight; | |
62 | |
46 MinimizeButtonMetrics::MinimizeButtonMetrics() | 63 MinimizeButtonMetrics::MinimizeButtonMetrics() |
47 : hwnd_(nullptr), | 64 : hwnd_(nullptr), |
48 cached_minimize_button_x_delta_(last_cached_minimize_button_x_delta_), | 65 cached_minimize_button_x_delta_(last_cached_minimize_button_x_delta_), |
49 was_activated_(false) { | 66 was_activated_(false) { |
50 } | 67 } |
51 | 68 |
52 MinimizeButtonMetrics::~MinimizeButtonMetrics() { | 69 MinimizeButtonMetrics::~MinimizeButtonMetrics() { |
53 } | 70 } |
54 | 71 |
55 void MinimizeButtonMetrics::Init(HWND hwnd) { | 72 void MinimizeButtonMetrics::Init(HWND hwnd) { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
116 if (!dwm_button_pos) { | 133 if (!dwm_button_pos) { |
117 // Fallback to using the message for the titlebar info only if the above | 134 // 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 | 135 // 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 | 136 // 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 | 137 // 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 | 138 // code is always the same and similarly the state never seems to change |
122 // (titlebar_info.rgstate). | 139 // (titlebar_info.rgstate). |
123 TITLEBARINFOEX titlebar_info = {0}; | 140 TITLEBARINFOEX titlebar_info = {0}; |
124 titlebar_info.cbSize = sizeof(TITLEBARINFOEX); | 141 titlebar_info.cbSize = sizeof(TITLEBARINFOEX); |
125 SendMessage(hwnd_, WM_GETTITLEBARINFOEX, 0, | 142 SendMessage(hwnd_, WM_GETTITLEBARINFOEX, 0, |
126 reinterpret_cast<WPARAM>(&titlebar_info)); | 143 reinterpret_cast<LPARAM>(&titlebar_info)); |
127 | 144 |
128 // Under DWM WM_GETTITLEBARINFOEX won't return the right thing until after | 145 // Under DWM WM_GETTITLEBARINFOEX won't return the right thing until after |
129 // WM_NCACTIVATE (maybe it returns classic values?). In an attempt to | 146 // WM_NCACTIVATE (maybe it returns classic values?). In an attempt to |
130 // return a consistant value we cache the last value across instances and | 147 // return a consistant value we cache the last value across instances and |
131 // use it until we get the activate. | 148 // use it until we get the activate. |
132 if (titlebar_info.rgrect[2].left == titlebar_info.rgrect[2].right || | 149 if (titlebar_info.rgrect[2].left == titlebar_info.rgrect[2].right || |
133 (titlebar_info.rgstate[2] & | 150 (titlebar_info.rgstate[2] & |
134 (STATE_SYSTEM_INVISIBLE | STATE_SYSTEM_OFFSCREEN | | 151 (STATE_SYSTEM_INVISIBLE | STATE_SYSTEM_OFFSCREEN | |
135 STATE_SYSTEM_UNAVAILABLE))) | 152 STATE_SYSTEM_UNAVAILABLE))) |
136 return 0; | 153 return 0; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
182 cached_minimize_button_x_delta_ = minimize_button_offset; | 199 cached_minimize_button_x_delta_ = minimize_button_offset; |
183 } else { | 200 } else { |
184 RECT client_rect = {0}; | 201 RECT client_rect = {0}; |
185 GetClientRect(hwnd_, &client_rect); | 202 GetClientRect(hwnd_, &client_rect); |
186 cached_minimize_button_x_delta_ = | 203 cached_minimize_button_x_delta_ = |
187 client_rect.right - minimize_button_offset; | 204 client_rect.right - minimize_button_offset; |
188 } | 205 } |
189 last_cached_minimize_button_x_delta_ = cached_minimize_button_x_delta_; | 206 last_cached_minimize_button_x_delta_ = cached_minimize_button_x_delta_; |
190 return minimize_button_offset; | 207 return minimize_button_offset; |
191 } | 208 } |
209 | |
210 int MinimizeButtonMetrics::GetCaptionButtonHeight() const { | |
211 // It's hard to get the height of the caption buttons exactly right for all | |
212 // resolutions/DPI settings. http://crbug.com/668278 tracks pixel-precise | |
213 // positioning for this. (According to robliao@ and bsep@ the solution is | |
214 // probably GetSystemMetricsForDpi.) GetSystemMetrics provides a good | |
215 // approximation - it seems to be no more than 1px off in practice. | |
Peter Kasting
2017/04/26 01:04:23
This comment confuses me a bit because it's not cl
emx
2017/04/26 14:19:26
OK, Mihai and I looked into this together and I've
| |
216 | |
217 // Note that the following methods do not return the results the caller | |
218 // expects with DPI scaling settings other than 100%: | |
219 // DwmGetWindowAttribute(hwnd_, DWMWA_CAPTION_BUTTON_BOUNDS, ...) | |
220 // SendMessage(hwnd_, WM_GETTITLEBARINFOEX, ...) | |
221 // display::win::ScreenWin::GetSystemMetricsInDIP(SM_CYSIZE) | |
222 // these all return (height * scale factor) e.g. 56 at 200% | |
Peter Kasting
2017/04/26 01:04:23
This doesn't seem right, reading the code. GetSys
emx
2017/04/26 14:19:26
Yes, you're right, I got GetSystemMetricsForHwnd a
| |
223 // display::win::ScreenWin::GetSystemMetricsForHwnd(hwnd_, SM_CYSIZE) | |
224 // returns (height / by primary monitor scale factor) e.g. 22 for 125% | |
225 | |
226 if (caption_button_height_ == kInvalidHeight) { | |
227 int button_height_from_sysmetrics = GetSystemMetrics(SM_CYSIZE); | |
228 if (button_height_from_sysmetrics == 0) | |
229 return GetDefaultButtonHeight(); | |
230 caption_button_height_ = button_height_from_sysmetrics; | |
231 } | |
232 | |
233 return caption_button_height_; | |
234 } | |
OLD | NEW |