Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(246)

Side by Side Diff: chrome/browser/ui/views/frame/minimize_button_metrics_win.cc

Issue 2833363002: Add GetCaptionButtonHeightInDIPs() method (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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);
Peter Kasting 2017/04/24 23:00:17 Nit: This is a bizarre choice. Why not just -1?
emx 2017/04/25 11:57:40 I agree that it's a bizarre choice, but I wanted t
Peter Kasting 2017/04/26 01:04:23 It's theoretically valid, but the only real constr
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::button_bounds_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
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
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::GetMinimizeButtonHeight() const {
211 // It's hard to get the height of the caption buttons exactly right for all
212 // resolutions/DPI settings. GetSystemMetricsForDpi is probaly the real
213 // solution [http://crbug.com/668278], but GetSystemMetrics provides a good
Peter Kasting 2017/04/24 23:00:17 Bug 668278 is a tracking bug, it's not about addin
emx 2017/04/25 11:57:40 robliao@ and bsep@ told me that GetSystemMetricsFo
214 // approximation for now.
215
216 // Note that the following methods do not return the correct result with DPI
217 // scaling settings other than 100%:
Peter Kasting 2017/04/24 23:00:17 In what way are they incorrect?
emx 2017/04/25 11:57:40 More accurately, they're not what the caller expec
218 // DwmGetWindowAttribute(hwnd_, DWMWA_CAPTION_BUTTON_BOUNDS, ...)
219 // SendMessage(hwnd_, WM_GETTITLEBARINFOEX, ...)
220 // display::win::ScreenWin::GetSystemMetricsInDIP(SM_CYSIZE)
221 // display::win::ScreenWin::GetSystemMetricsForHwnd(hwnd_, SM_CYSIZE)
222
223 if (button_bounds_height_ == kInvalidHeight) {
224 int button_height_from_sysmetrics = GetSystemMetrics(SM_CYSIZE);
225 if (button_height_from_sysmetrics == 0) {
Peter Kasting 2017/04/24 23:00:17 When can this happen? This needs to be explained.
emx 2017/04/25 11:57:40 I don't know when it can happen. MSDN does not exp
Peter Kasting 2017/04/26 01:04:23 Most other places in code that we call GetSystemMe
226 DLOG(ERROR) << "GetSystemMetrics(SM_CYSIZE) error " << ::GetLastError();
Peter Kasting 2017/04/24 23:00:17 Avoid logging.
emx 2017/04/25 11:57:40 Logging removed.
227 return GetDefaultButtonHeight();
228 }
229 button_bounds_height_ = button_height_from_sysmetrics;
230 }
231
232 return button_bounds_height_;
233 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698