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

Side by Side Diff: ui/gfx/screen_win.cc

Issue 1639623003: ScreenWin Testability and Restructuring (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase to bdd95f1 Created 4 years, 10 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 "ui/gfx/screen_win.h" 5 #include "ui/gfx/screen_win.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 #include <stdint.h>
9 8
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/hash.h"
13 #include "base/logging.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/win/win_util.h"
16 #include "ui/gfx/display.h" 9 #include "ui/gfx/display.h"
10 #include "ui/gfx/geometry/point.h"
11 #include "ui/gfx/geometry/rect.h"
12 #include "ui/gfx/win/display_manager.h"
17 #include "ui/gfx/win/dpi.h" 13 #include "ui/gfx/win/dpi.h"
14 #include "ui/gfx/win/screen_win_display.h"
18 15
19 namespace { 16 namespace {
20 17
21 MONITORINFOEX GetMonitorInfoForMonitor(HMONITOR monitor) { 18 std::vector<gfx::Display> ScreenWinDisplaysToDisplays(
22 MONITORINFOEX monitor_info; 19 const std::vector<gfx::win::ScreenWinDisplay>& screen_win_displays) {
23 ZeroMemory(&monitor_info, sizeof(MONITORINFOEX)); 20 std::vector<gfx::Display> displays;
24 monitor_info.cbSize = sizeof(monitor_info); 21 for (const auto& screen_win_display : screen_win_displays)
25 GetMonitorInfo(monitor, &monitor_info); 22 displays.push_back(screen_win_display.display());
26 return monitor_info; 23
24 return displays;
27 } 25 }
28 26
29 gfx::Display GetDisplay(const MONITORINFOEX& monitor_info) { 27 inline gfx::win::DisplayManager* GetDisplayManager() {
30 int64_t id = 28 return gfx::win::DisplayManager::GetInstance();
31 static_cast<int64_t>(base::Hash(base::WideToUTF8(monitor_info.szDevice)));
32 gfx::Rect bounds = gfx::Rect(monitor_info.rcMonitor);
33 gfx::Display display(id);
34 display.set_bounds(gfx::win::ScreenToDIPRect(bounds));
35 display.set_work_area(
36 gfx::win::ScreenToDIPRect(gfx::Rect(monitor_info.rcWork)));
37 display.SetScaleAndBounds(gfx::GetDPIScale(), bounds);
38
39 DEVMODE mode;
40 memset(&mode, 0, sizeof(DEVMODE));
41 mode.dmSize = sizeof(DEVMODE);
42 mode.dmDriverExtra = 0;
43 if (EnumDisplaySettings(monitor_info.szDevice,
44 ENUM_CURRENT_SETTINGS,
45 &mode)) {
46 switch (mode.dmDisplayOrientation) {
47 case DMDO_DEFAULT:
48 display.set_rotation(gfx::Display::ROTATE_0);
49 break;
50 case DMDO_90:
51 display.set_rotation(gfx::Display::ROTATE_90);
52 break;
53 case DMDO_180:
54 display.set_rotation(gfx::Display::ROTATE_180);
55 break;
56 case DMDO_270:
57 display.set_rotation(gfx::Display::ROTATE_270);
58 break;
59 default:
60 NOTREACHED();
61 }
62 }
63
64 return display;
65 }
66
67 BOOL CALLBACK EnumMonitorCallback(HMONITOR monitor,
68 HDC hdc,
69 LPRECT rect,
70 LPARAM data) {
71 std::vector<gfx::Display>* all_displays =
72 reinterpret_cast<std::vector<gfx::Display>*>(data);
73 DCHECK(all_displays);
74
75 MONITORINFOEX monitor_info = GetMonitorInfoForMonitor(monitor);
76 gfx::Display display = GetDisplay(monitor_info);
77 all_displays->push_back(display);
78 return TRUE;
79 }
80
81 std::vector<gfx::Display> GetDisplays() {
82 std::vector<gfx::Display> displays;
83 EnumDisplayMonitors(NULL, NULL, EnumMonitorCallback,
84 reinterpret_cast<LPARAM>(&displays));
85 return displays;
86 } 29 }
87 30
88 } // namespace 31 } // namespace
89 32
90 namespace gfx { 33 namespace gfx {
91 34
92 ScreenWin::ScreenWin() 35 ScreenWin::ScreenWin() : display_manager_(GetDisplayManager()) {
93 : singleton_hwnd_observer_(new SingletonHwndObserver( 36 display_manager_->AddObserver(this);
94 base::Bind(&ScreenWin::OnWndProc, base::Unretained(this)))),
95 displays_(GetDisplays()) {
96 } 37 }
97 38
98 ScreenWin::~ScreenWin() {} 39 ScreenWin::~ScreenWin() {
40 display_manager_->RemoveObserver(this);
41 }
99 42
100 HWND ScreenWin::GetHWNDFromNativeView(NativeView window) const { 43 HWND ScreenWin::GetHWNDFromNativeView(NativeView window) const {
101 NOTREACHED(); 44 NOTREACHED();
102 return NULL; 45 return NULL;
103 } 46 }
104 47
105 NativeWindow ScreenWin::GetNativeWindowFromHWND(HWND hwnd) const { 48 NativeWindow ScreenWin::GetNativeWindowFromHWND(HWND hwnd) const {
106 NOTREACHED(); 49 NOTREACHED();
107 return NULL; 50 return NULL;
108 } 51 }
109 52
110 gfx::Point ScreenWin::GetCursorScreenPoint() { 53 gfx::Point ScreenWin::GetCursorScreenPoint() {
111 POINT pt; 54 POINT pt;
112 GetCursorPos(&pt); 55 ::GetCursorPos(&pt);
113 gfx::Point cursor_pos_pixels(pt); 56 gfx::Point cursor_pos_pixels(pt);
114 return gfx::win::ScreenToDIPPoint(cursor_pos_pixels); 57 return gfx::win::ScreenToDIPPoint(cursor_pos_pixels);
115 } 58 }
116 59
117 gfx::NativeWindow ScreenWin::GetWindowUnderCursor() { 60 gfx::NativeWindow ScreenWin::GetWindowUnderCursor() {
118 POINT cursor_loc; 61 POINT cursor_loc;
119 HWND hwnd = GetCursorPos(&cursor_loc) ? WindowFromPoint(cursor_loc) : NULL; 62 HWND hwnd =
63 ::GetCursorPos(&cursor_loc) ? ::WindowFromPoint(cursor_loc) : NULL;
120 return GetNativeWindowFromHWND(hwnd); 64 return GetNativeWindowFromHWND(hwnd);
121 } 65 }
122 66
123 gfx::NativeWindow ScreenWin::GetWindowAtScreenPoint(const gfx::Point& point) { 67 gfx::NativeWindow ScreenWin::GetWindowAtScreenPoint(const gfx::Point& point) {
124 gfx::Point point_in_pixels = gfx::win::DIPToScreenPoint(point); 68 gfx::Point point_in_pixels = gfx::win::DIPToScreenPoint(point);
125 return GetNativeWindowFromHWND(WindowFromPoint(point_in_pixels.ToPOINT())); 69 return GetNativeWindowFromHWND(WindowFromPoint(point_in_pixels.ToPOINT()));
126 } 70 }
127 71
128 int ScreenWin::GetNumDisplays() const { 72 int ScreenWin::GetNumDisplays() const {
129 return GetSystemMetrics(SM_CMONITORS); 73 return static_cast<int>(display_manager_->GetScreenWinDisplays().size());
130 } 74 }
131 75
132 std::vector<gfx::Display> ScreenWin::GetAllDisplays() const { 76 std::vector<gfx::Display> ScreenWin::GetAllDisplays() const {
133 return displays_; 77 return ScreenWinDisplaysToDisplays(display_manager_->GetScreenWinDisplays());
134 } 78 }
135 79
136 gfx::Display ScreenWin::GetDisplayNearestWindow(gfx::NativeView window) const { 80 gfx::Display ScreenWin::GetDisplayNearestWindow(gfx::NativeView window) const {
137 HWND window_hwnd = GetHWNDFromNativeView(window); 81 HWND window_hwnd = GetHWNDFromNativeView(window);
138 if (!window_hwnd) { 82 if (!window_hwnd) {
139 // When |window| isn't rooted to a display, we should just return the 83 // When |window| isn't rooted to a display, we should just return the
140 // default display so we get some correct display information like the 84 // default display so we get some correct display information like the
141 // scaling factor. 85 // scaling factor.
142 return GetPrimaryDisplay(); 86 return GetPrimaryDisplay();
143 } 87 }
144 88 gfx::win::ScreenWinDisplay screen_win_display =
145 MONITORINFOEX monitor_info; 89 display_manager_->GetScreenWinDisplayNearestHWND(window_hwnd);
146 monitor_info.cbSize = sizeof(monitor_info); 90 return screen_win_display.display();
147 GetMonitorInfo(MonitorFromWindow(window_hwnd, MONITOR_DEFAULTTONEAREST),
148 &monitor_info);
149 return GetDisplay(monitor_info);
150 } 91 }
151 92
152 gfx::Display ScreenWin::GetDisplayNearestPoint(const gfx::Point& point) const { 93 gfx::Display ScreenWin::GetDisplayNearestPoint(const gfx::Point& point) const {
153 gfx::Point point_in_pixels = gfx::win::DIPToScreenPoint(point); 94 gfx::Point screen_point(gfx::win::DIPToScreenPoint(point));
154 POINT initial_loc = { point_in_pixels.x(), point_in_pixels.y() }; 95 gfx::win::ScreenWinDisplay screen_win_display =
155 HMONITOR monitor = MonitorFromPoint(initial_loc, MONITOR_DEFAULTTONEAREST); 96 display_manager_->GetScreenWinDisplayNearestScreenPoint(screen_point);
156 MONITORINFOEX mi; 97 return screen_win_display.display();
157 ZeroMemory(&mi, sizeof(MONITORINFOEX));
158 mi.cbSize = sizeof(mi);
159 if (monitor && GetMonitorInfo(monitor, &mi)) {
160 return GetDisplay(mi);
161 }
162 return gfx::Display();
163 } 98 }
164 99
165 gfx::Display ScreenWin::GetDisplayMatching(const gfx::Rect& match_rect) const { 100 gfx::Display ScreenWin::GetDisplayMatching(const gfx::Rect& match_rect) const {
166 RECT other_bounds_rect = match_rect.ToRECT(); 101 gfx::win::ScreenWinDisplay screen_win_display =
167 MONITORINFOEX monitor_info = GetMonitorInfoForMonitor(MonitorFromRect( 102 display_manager_->GetScreenWinDisplayNearestScreenRect(match_rect);
168 &other_bounds_rect, MONITOR_DEFAULTTONEAREST)); 103 return screen_win_display.display();
169 return GetDisplay(monitor_info);
170 } 104 }
171 105
172 gfx::Display ScreenWin::GetPrimaryDisplay() const { 106 gfx::Display ScreenWin::GetPrimaryDisplay() const {
173 MONITORINFOEX mi = GetMonitorInfoForMonitor( 107 return display_manager_->GetPrimaryScreenWinDisplay().display();
174 MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY));
175 gfx::Display display = GetDisplay(mi);
176 // TODO(kevers|girard): Test if these checks can be reintroduced for high-DIP
177 // once more of the app is DIP-aware.
178 if (GetDPIScale() == 1.0) {
179 DCHECK_EQ(GetSystemMetrics(SM_CXSCREEN), display.size().width());
180 DCHECK_EQ(GetSystemMetrics(SM_CYSCREEN), display.size().height());
181 }
182 return display;
183 } 108 }
184 109
185 void ScreenWin::AddObserver(DisplayObserver* observer) { 110 void ScreenWin::AddObserver(DisplayObserver* observer) {
186 change_notifier_.AddObserver(observer); 111 change_notifier_.AddObserver(observer);
187 } 112 }
188 113
189 void ScreenWin::RemoveObserver(DisplayObserver* observer) { 114 void ScreenWin::RemoveObserver(DisplayObserver* observer) {
190 change_notifier_.RemoveObserver(observer); 115 change_notifier_.RemoveObserver(observer);
191 } 116 }
192 117
193 void ScreenWin::OnWndProc(HWND hwnd, 118 void ScreenWin::OnDisplaysChanged(
194 UINT message, 119 const std::vector<gfx::win::ScreenWinDisplay> old_screen_win_displays,
195 WPARAM wparam, 120 const std::vector<gfx::win::ScreenWinDisplay> new_screen_win_displays) {
196 LPARAM lparam) { 121 std::vector<gfx::Display> old_displays =
197 if (message != WM_DISPLAYCHANGE) 122 ScreenWinDisplaysToDisplays(old_screen_win_displays);
198 return; 123 std::vector<gfx::Display> new_displays =
199 124 ScreenWinDisplaysToDisplays(new_screen_win_displays);
200 std::vector<gfx::Display> old_displays = displays_; 125 change_notifier_.NotifyDisplaysChanged(old_displays, new_displays);
201 displays_ = GetDisplays();
202
203 change_notifier_.NotifyDisplaysChanged(old_displays, displays_);
204 }
205
206 // static
207 std::vector<gfx::Display> ScreenWin::GetDisplaysForMonitorInfos(
208 const std::vector<MONITORINFOEX>& monitor_infos) {
209 std::vector<gfx::Display> displays;
210 for (const MONITORINFOEX& monitor_info : monitor_infos)
211 displays.push_back(GetDisplay(monitor_info));
212
213 return displays;
214 } 126 }
215 127
216 } // namespace gfx 128 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698