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

Unified 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: Created 4 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: ui/gfx/screen_win.cc
diff --git a/ui/gfx/screen_win.cc b/ui/gfx/screen_win.cc
index 3199f7acaacdd2b15c3dca37d09014dc418c2d00..f4fb25c41fb344ce317818c35092c2d16acddc01 100644
--- a/ui/gfx/screen_win.cc
+++ b/ui/gfx/screen_win.cc
@@ -5,97 +5,40 @@
#include "ui/gfx/screen_win.h"
#include <windows.h>
-#include <stdint.h>
-
-#include "base/bind.h"
-#include "base/bind_helpers.h"
-#include "base/hash.h"
-#include "base/logging.h"
-#include "base/strings/utf_string_conversions.h"
-#include "base/win/win_util.h"
+
#include "ui/gfx/display.h"
+#include "ui/gfx/geometry/point.h"
+#include "ui/gfx/geometry/rect.h"
+#include "ui/gfx/win/display_manager.h"
#include "ui/gfx/win/dpi.h"
+#include "ui/gfx/win/screen_win_display.h"
namespace {
-MONITORINFOEX GetMonitorInfoForMonitor(HMONITOR monitor) {
- MONITORINFOEX monitor_info;
- ZeroMemory(&monitor_info, sizeof(MONITORINFOEX));
- monitor_info.cbSize = sizeof(monitor_info);
- GetMonitorInfo(monitor, &monitor_info);
- return monitor_info;
-}
-
-gfx::Display GetDisplay(const MONITORINFOEX& monitor_info) {
- int64_t id =
- static_cast<int64_t>(base::Hash(base::WideToUTF8(monitor_info.szDevice)));
- gfx::Rect bounds = gfx::Rect(monitor_info.rcMonitor);
- gfx::Display display(id);
- display.set_bounds(gfx::win::ScreenToDIPRect(bounds));
- display.set_work_area(
- gfx::win::ScreenToDIPRect(gfx::Rect(monitor_info.rcWork)));
- display.SetScaleAndBounds(gfx::GetDPIScale(), bounds);
-
- DEVMODE mode;
- memset(&mode, 0, sizeof(DEVMODE));
- mode.dmSize = sizeof(DEVMODE);
- mode.dmDriverExtra = 0;
- if (EnumDisplaySettings(monitor_info.szDevice,
- ENUM_CURRENT_SETTINGS,
- &mode)) {
- switch (mode.dmDisplayOrientation) {
- case DMDO_DEFAULT:
- display.set_rotation(gfx::Display::ROTATE_0);
- break;
- case DMDO_90:
- display.set_rotation(gfx::Display::ROTATE_90);
- break;
- case DMDO_180:
- display.set_rotation(gfx::Display::ROTATE_180);
- break;
- case DMDO_270:
- display.set_rotation(gfx::Display::ROTATE_270);
- break;
- default:
- NOTREACHED();
- }
- }
-
- return display;
-}
-
-BOOL CALLBACK EnumMonitorCallback(HMONITOR monitor,
- HDC hdc,
- LPRECT rect,
- LPARAM data) {
- std::vector<gfx::Display>* all_displays =
- reinterpret_cast<std::vector<gfx::Display>*>(data);
- DCHECK(all_displays);
+std::vector<gfx::Display> ScreenWinDisplaysToDisplays(
+ const std::vector<gfx::win::ScreenWinDisplay>& screen_win_displays) {
+ std::vector<gfx::Display> displays;
+ for (const auto& screen_win_display : screen_win_displays)
+ displays.push_back(screen_win_display.display());
- MONITORINFOEX monitor_info = GetMonitorInfoForMonitor(monitor);
- gfx::Display display = GetDisplay(monitor_info);
- all_displays->push_back(display);
- return TRUE;
+ return displays;
}
-std::vector<gfx::Display> GetDisplays() {
- std::vector<gfx::Display> displays;
- EnumDisplayMonitors(NULL, NULL, EnumMonitorCallback,
- reinterpret_cast<LPARAM>(&displays));
- return displays;
+inline gfx::win::DisplayManager* GetDisplayManager() {
+ return gfx::win::DisplayManager::GetInstance();
}
} // namespace
namespace gfx {
-ScreenWin::ScreenWin()
- : singleton_hwnd_observer_(new SingletonHwndObserver(
- base::Bind(&ScreenWin::OnWndProc, base::Unretained(this)))),
- displays_(GetDisplays()) {
+ScreenWin::ScreenWin() : display_manager_(GetDisplayManager()) {
+ display_manager_->AddObserver(this);
}
-ScreenWin::~ScreenWin() {}
+ScreenWin::~ScreenWin() {
+ display_manager_->RemoveObserver(this);
+}
HWND ScreenWin::GetHWNDFromNativeView(NativeView window) const {
NOTREACHED();
@@ -109,14 +52,15 @@ NativeWindow ScreenWin::GetNativeWindowFromHWND(HWND hwnd) const {
gfx::Point ScreenWin::GetCursorScreenPoint() {
POINT pt;
- GetCursorPos(&pt);
+ ::GetCursorPos(&pt);
gfx::Point cursor_pos_pixels(pt);
return gfx::win::ScreenToDIPPoint(cursor_pos_pixels);
}
gfx::NativeWindow ScreenWin::GetWindowUnderCursor() {
POINT cursor_loc;
- HWND hwnd = GetCursorPos(&cursor_loc) ? WindowFromPoint(cursor_loc) : NULL;
+ HWND hwnd =
+ ::GetCursorPos(&cursor_loc) ? ::WindowFromPoint(cursor_loc) : NULL;
return GetNativeWindowFromHWND(hwnd);
}
@@ -126,11 +70,11 @@ gfx::NativeWindow ScreenWin::GetWindowAtScreenPoint(const gfx::Point& point) {
}
int ScreenWin::GetNumDisplays() const {
- return GetSystemMetrics(SM_CMONITORS);
+ return static_cast<int>(display_manager_->GetScreenWinDisplays().size());
}
std::vector<gfx::Display> ScreenWin::GetAllDisplays() const {
- return displays_;
+ return ScreenWinDisplaysToDisplays(display_manager_->GetScreenWinDisplays());
}
gfx::Display ScreenWin::GetDisplayNearestWindow(gfx::NativeView window) const {
@@ -141,45 +85,26 @@ gfx::Display ScreenWin::GetDisplayNearestWindow(gfx::NativeView window) const {
// scaling factor.
return GetPrimaryDisplay();
}
-
- MONITORINFOEX monitor_info;
- monitor_info.cbSize = sizeof(monitor_info);
- GetMonitorInfo(MonitorFromWindow(window_hwnd, MONITOR_DEFAULTTONEAREST),
- &monitor_info);
- return GetDisplay(monitor_info);
+ gfx::win::ScreenWinDisplay screen_win_display =
+ display_manager_->GetScreenWinDisplayNearestHWND(window_hwnd);
+ return screen_win_display.display();
}
gfx::Display ScreenWin::GetDisplayNearestPoint(const gfx::Point& point) const {
- gfx::Point point_in_pixels = gfx::win::DIPToScreenPoint(point);
- POINT initial_loc = { point_in_pixels.x(), point_in_pixels.y() };
- HMONITOR monitor = MonitorFromPoint(initial_loc, MONITOR_DEFAULTTONEAREST);
- MONITORINFOEX mi;
- ZeroMemory(&mi, sizeof(MONITORINFOEX));
- mi.cbSize = sizeof(mi);
- if (monitor && GetMonitorInfo(monitor, &mi)) {
- return GetDisplay(mi);
- }
- return gfx::Display();
+ gfx::Point screen_point(gfx::win::DIPToScreenPoint(point));
+ gfx::win::ScreenWinDisplay screen_win_display =
+ display_manager_->GetScreenWinDisplayNearestScreenPoint(screen_point);
+ return screen_win_display.display();
}
gfx::Display ScreenWin::GetDisplayMatching(const gfx::Rect& match_rect) const {
- RECT other_bounds_rect = match_rect.ToRECT();
- MONITORINFOEX monitor_info = GetMonitorInfoForMonitor(MonitorFromRect(
- &other_bounds_rect, MONITOR_DEFAULTTONEAREST));
- return GetDisplay(monitor_info);
+ gfx::win::ScreenWinDisplay screen_win_display =
+ display_manager_->GetScreenWinDisplayNearestScreenRect(match_rect);
+ return screen_win_display.display();
}
gfx::Display ScreenWin::GetPrimaryDisplay() const {
- MONITORINFOEX mi = GetMonitorInfoForMonitor(
- MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY));
- gfx::Display display = GetDisplay(mi);
- // TODO(kevers|girard): Test if these checks can be reintroduced for high-DIP
- // once more of the app is DIP-aware.
- if (GetDPIScale() == 1.0) {
- DCHECK_EQ(GetSystemMetrics(SM_CXSCREEN), display.size().width());
- DCHECK_EQ(GetSystemMetrics(SM_CYSCREEN), display.size().height());
- }
- return display;
+ return display_manager_->GetPrimaryScreenWinDisplay().display();
}
void ScreenWin::AddObserver(DisplayObserver* observer) {
@@ -190,27 +115,14 @@ void ScreenWin::RemoveObserver(DisplayObserver* observer) {
change_notifier_.RemoveObserver(observer);
}
-void ScreenWin::OnWndProc(HWND hwnd,
- UINT message,
- WPARAM wparam,
- LPARAM lparam) {
- if (message != WM_DISPLAYCHANGE)
- return;
-
- std::vector<gfx::Display> old_displays = displays_;
- displays_ = GetDisplays();
-
- change_notifier_.NotifyDisplaysChanged(old_displays, displays_);
-}
-
-// static
-std::vector<gfx::Display> ScreenWin::GetDisplaysForMonitorInfos(
- const std::vector<MONITORINFOEX>& monitor_infos) {
- std::vector<gfx::Display> displays;
- for (const MONITORINFOEX& monitor_info : monitor_infos)
- displays.push_back(GetDisplay(monitor_info));
-
- return displays;
+void ScreenWin::OnDisplaysChanged(
+ const std::vector<gfx::win::ScreenWinDisplay> old_screen_win_displays,
+ const std::vector<gfx::win::ScreenWinDisplay> new_screen_win_displays) {
+ std::vector<gfx::Display> old_displays =
+ ScreenWinDisplaysToDisplays(old_screen_win_displays);
+ std::vector<gfx::Display> new_displays =
+ ScreenWinDisplaysToDisplays(new_screen_win_displays);
+ change_notifier_.NotifyDisplaysChanged(old_displays, new_displays);
}
} // namespace gfx

Powered by Google App Engine
This is Rietveld 408576698