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

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

Issue 401313003: ScreenWin notifies DisplayObserver's when displays changed. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review comments Created 6 years, 5 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
« no previous file with comments | « ui/gfx/screen_win.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 8
9 #include "base/hash.h" 9 #include "base/hash.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "base/win/win_util.h" 12 #include "base/win/win_util.h"
13 #include "ui/gfx/display.h" 13 #include "ui/gfx/display.h"
14 #include "ui/gfx/win/dpi.h" 14 #include "ui/gfx/win/dpi.h"
15 15
16 namespace { 16 namespace {
17 17
18 MONITORINFOEX GetMonitorInfoForMonitor(HMONITOR monitor) { 18 MONITORINFOEX GetMonitorInfoForMonitor(HMONITOR monitor) {
19 MONITORINFOEX monitor_info; 19 MONITORINFOEX monitor_info;
20 ZeroMemory(&monitor_info, sizeof(MONITORINFOEX)); 20 ZeroMemory(&monitor_info, sizeof(MONITORINFOEX));
21 monitor_info.cbSize = sizeof(monitor_info); 21 monitor_info.cbSize = sizeof(monitor_info);
22 GetMonitorInfo(monitor, &monitor_info); 22 GetMonitorInfo(monitor, &monitor_info);
23 return monitor_info; 23 return monitor_info;
24 } 24 }
25 25
26 gfx::Display GetDisplay(MONITORINFOEX& monitor_info) { 26 gfx::Display GetDisplay(MONITORINFOEX& monitor_info) {
27 // TODO(oshima): Implement Observer.
28 int64 id = static_cast<int64>( 27 int64 id = static_cast<int64>(
29 base::Hash(base::WideToUTF8(monitor_info.szDevice))); 28 base::Hash(base::WideToUTF8(monitor_info.szDevice)));
30 gfx::Rect bounds = gfx::Rect(monitor_info.rcMonitor); 29 gfx::Rect bounds = gfx::Rect(monitor_info.rcMonitor);
31 gfx::Display display(id, bounds); 30 gfx::Display display(id, bounds);
32 display.set_work_area(gfx::Rect(monitor_info.rcWork)); 31 display.set_work_area(gfx::Rect(monitor_info.rcWork));
33 display.SetScaleAndBounds(gfx::win::GetDeviceScaleFactor(), bounds); 32 display.SetScaleAndBounds(gfx::win::GetDeviceScaleFactor(), bounds);
34 33
35 DEVMODE mode; 34 DEVMODE mode;
36 memset(&mode, 0, sizeof(DEVMODE)); 35 memset(&mode, 0, sizeof(DEVMODE));
37 mode.dmSize = sizeof(DEVMODE); 36 mode.dmSize = sizeof(DEVMODE);
(...skipping 29 matching lines...) Expand all
67 std::vector<gfx::Display>* all_displays = 66 std::vector<gfx::Display>* all_displays =
68 reinterpret_cast<std::vector<gfx::Display>*>(data); 67 reinterpret_cast<std::vector<gfx::Display>*>(data);
69 DCHECK(all_displays); 68 DCHECK(all_displays);
70 69
71 MONITORINFOEX monitor_info = GetMonitorInfoForMonitor(monitor); 70 MONITORINFOEX monitor_info = GetMonitorInfoForMonitor(monitor);
72 gfx::Display display = GetDisplay(monitor_info); 71 gfx::Display display = GetDisplay(monitor_info);
73 all_displays->push_back(display); 72 all_displays->push_back(display);
74 return TRUE; 73 return TRUE;
75 } 74 }
76 75
76 std::vector<gfx::Display> GetDisplays() {
77 std::vector<gfx::Display> displays;
78 EnumDisplayMonitors(NULL, NULL, EnumMonitorCallback,
79 reinterpret_cast<LPARAM>(&displays));
80 return displays;
81 }
82
77 } // namespace 83 } // namespace
78 84
79 namespace gfx { 85 namespace gfx {
80 86
81 ScreenWin::ScreenWin() { 87 ScreenWin::ScreenWin()
88 : displays_(GetDisplays()) {
89 SingletonHwnd::GetInstance()->AddObserver(this);
82 } 90 }
83 91
84 ScreenWin::~ScreenWin() { 92 ScreenWin::~ScreenWin() {
93 SingletonHwnd::GetInstance()->RemoveObserver(this);
85 } 94 }
86 95
87 bool ScreenWin::IsDIPEnabled() { 96 bool ScreenWin::IsDIPEnabled() {
88 return IsInHighDPIMode(); 97 return IsInHighDPIMode();
89 } 98 }
90 99
91 gfx::Point ScreenWin::GetCursorScreenPoint() { 100 gfx::Point ScreenWin::GetCursorScreenPoint() {
92 POINT pt; 101 POINT pt;
93 GetCursorPos(&pt); 102 GetCursorPos(&pt);
94 gfx::Point cursor_pos_pixels(pt); 103 gfx::Point cursor_pos_pixels(pt);
95 return gfx::win::ScreenToDIPPoint(cursor_pos_pixels); 104 return gfx::win::ScreenToDIPPoint(cursor_pos_pixels);
96 } 105 }
97 106
98 gfx::NativeWindow ScreenWin::GetWindowUnderCursor() { 107 gfx::NativeWindow ScreenWin::GetWindowUnderCursor() {
99 POINT cursor_loc; 108 POINT cursor_loc;
100 HWND hwnd = GetCursorPos(&cursor_loc) ? WindowFromPoint(cursor_loc) : NULL; 109 HWND hwnd = GetCursorPos(&cursor_loc) ? WindowFromPoint(cursor_loc) : NULL;
101 return GetNativeWindowFromHWND(hwnd); 110 return GetNativeWindowFromHWND(hwnd);
102 } 111 }
103 112
104 gfx::NativeWindow ScreenWin::GetWindowAtScreenPoint(const gfx::Point& point) { 113 gfx::NativeWindow ScreenWin::GetWindowAtScreenPoint(const gfx::Point& point) {
105 gfx::Point point_in_pixels = gfx::win::DIPToScreenPoint(point); 114 gfx::Point point_in_pixels = gfx::win::DIPToScreenPoint(point);
106 return GetNativeWindowFromHWND(WindowFromPoint(point_in_pixels.ToPOINT())); 115 return GetNativeWindowFromHWND(WindowFromPoint(point_in_pixels.ToPOINT()));
107 } 116 }
108 117
109 int ScreenWin::GetNumDisplays() const { 118 int ScreenWin::GetNumDisplays() const {
110 return GetSystemMetrics(SM_CMONITORS); 119 return GetSystemMetrics(SM_CMONITORS);
111 } 120 }
112 121
113 std::vector<gfx::Display> ScreenWin::GetAllDisplays() const { 122 std::vector<gfx::Display> ScreenWin::GetAllDisplays() const {
114 std::vector<gfx::Display> all_displays; 123 return displays_;
115 EnumDisplayMonitors(NULL, NULL, EnumMonitorCallback,
116 reinterpret_cast<LPARAM>(&all_displays));
117 return all_displays;
118 } 124 }
119 125
120 gfx::Display ScreenWin::GetDisplayNearestWindow(gfx::NativeView window) const { 126 gfx::Display ScreenWin::GetDisplayNearestWindow(gfx::NativeView window) const {
121 HWND window_hwnd = GetHWNDFromNativeView(window); 127 HWND window_hwnd = GetHWNDFromNativeView(window);
122 if (!window_hwnd) { 128 if (!window_hwnd) {
123 // When |window| isn't rooted to a display, we should just return the 129 // When |window| isn't rooted to a display, we should just return the
124 // default display so we get some correct display information like the 130 // default display so we get some correct display information like the
125 // scaling factor. 131 // scaling factor.
126 return GetPrimaryDisplay(); 132 return GetPrimaryDisplay();
127 } 133 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 // TODO(kevers|girard): Test if these checks can be reintroduced for high-DIP 165 // TODO(kevers|girard): Test if these checks can be reintroduced for high-DIP
160 // once more of the app is DIP-aware. 166 // once more of the app is DIP-aware.
161 if (!(IsInHighDPIMode() || IsHighDPIEnabled())) { 167 if (!(IsInHighDPIMode() || IsHighDPIEnabled())) {
162 DCHECK_EQ(GetSystemMetrics(SM_CXSCREEN), display.size().width()); 168 DCHECK_EQ(GetSystemMetrics(SM_CXSCREEN), display.size().width());
163 DCHECK_EQ(GetSystemMetrics(SM_CYSCREEN), display.size().height()); 169 DCHECK_EQ(GetSystemMetrics(SM_CYSCREEN), display.size().height());
164 } 170 }
165 return display; 171 return display;
166 } 172 }
167 173
168 void ScreenWin::AddObserver(DisplayObserver* observer) { 174 void ScreenWin::AddObserver(DisplayObserver* observer) {
169 // TODO(oshima): crbug.com/122863. 175 change_notifier_.AddObserver(observer);
170 } 176 }
171 177
172 void ScreenWin::RemoveObserver(DisplayObserver* observer) { 178 void ScreenWin::RemoveObserver(DisplayObserver* observer) {
173 // TODO(oshima): crbug.com/122863. 179 change_notifier_.RemoveObserver(observer);
180 }
181
182 void ScreenWin::OnWndProc(HWND hwnd,
183 UINT message,
184 WPARAM wparam,
185 LPARAM lparam) {
186 if (message != WM_DISPLAYCHANGE)
187 return;
188
189 std::vector<gfx::Display> old_displays = displays_;
190 displays_ = GetDisplays();
191
192 change_notifier_.NotifyDisplaysChanged(old_displays, displays_);
174 } 193 }
175 194
176 HWND ScreenWin::GetHWNDFromNativeView(NativeView window) const { 195 HWND ScreenWin::GetHWNDFromNativeView(NativeView window) const {
177 NOTREACHED(); 196 NOTREACHED();
178 return NULL; 197 return NULL;
179 } 198 }
180 199
181 NativeWindow ScreenWin::GetNativeWindowFromHWND(HWND hwnd) const { 200 NativeWindow ScreenWin::GetNativeWindowFromHWND(HWND hwnd) const {
182 NOTREACHED(); 201 NOTREACHED();
183 return NULL; 202 return NULL;
184 } 203 }
185 204
186 } // namespace gfx 205 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/screen_win.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698