| Index: ui/gfx/screen_win.cc
|
| diff --git a/ui/gfx/screen_win.cc b/ui/gfx/screen_win.cc
|
| index 3199f7acaacdd2b15c3dca37d09014dc418c2d00..86bfc3fc29df48e5ba1192c2ca93b402ece0f8bf 100644
|
| --- a/ui/gfx/screen_win.cc
|
| +++ b/ui/gfx/screen_win.cc
|
| @@ -5,97 +5,160 @@
|
| #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/win/dpi.h"
|
| +#include "ui/gfx/geometry/insets.h"
|
| +#include "ui/gfx/geometry/point.h"
|
| +#include "ui/gfx/geometry/point_conversions.h"
|
| +#include "ui/gfx/geometry/rect.h"
|
| +#include "ui/gfx/geometry/size.h"
|
| +#include "ui/gfx/geometry/vector2d.h"
|
| +#include "ui/gfx/win/display_info.h"
|
| +#include "ui/gfx/win/display_manager.h"
|
| +#include "ui/gfx/win/rect_util.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;
|
| +// Returns a point in |to_origin|'s coordinates and position scaled by
|
| +// |scale_factor|.
|
| +gfx::Point ScalePointRelative(const gfx::Point& from_origin,
|
| + const gfx::Point& to_origin,
|
| + const float scale_factor,
|
| + const gfx::Point& point) {
|
| + gfx::Vector2d from_origin_vector(from_origin.x(), from_origin.y());
|
| + gfx::Vector2d to_origin_vector(to_origin.x(), to_origin.y());
|
| + gfx::Point scaled_relative_point(
|
| + gfx::ScaleToFlooredPoint(point - from_origin_vector, scale_factor));
|
| + return scaled_relative_point + to_origin_vector;
|
| }
|
|
|
| -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> ToVectorOfDisplays(
|
| + 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);
|
| +}
|
| +
|
| +// static
|
| +Point ScreenWin::ScreenToDIPPoint(const Point& pixel_point) {
|
| + gfx::win::DisplayManager* display_manager = GetDisplayManager();
|
| + gfx::win::ScreenWinDisplay screen_win_display =
|
| + display_manager->GetScreenWinDisplayNearestScreenPoint(pixel_point);
|
| + Display display = screen_win_display.display();
|
| + return ScalePointRelative(screen_win_display.physical_bounds().origin(),
|
| + display.bounds().origin(),
|
| + 1.0f / display.device_scale_factor(),
|
| + pixel_point);
|
| +}
|
| +
|
| +// static
|
| +Point ScreenWin::DIPToScreenPoint(const Point& dip_point) {
|
| + gfx::win::DisplayManager* display_manager = GetDisplayManager();
|
| + gfx::win::ScreenWinDisplay screen_win_display =
|
| + display_manager->GetScreenWinDisplayNearestDIPPoint(dip_point);
|
| + Display display = screen_win_display.display();
|
| + return ScalePointRelative(display.bounds().origin(),
|
| + screen_win_display.physical_bounds().origin(),
|
| + display.device_scale_factor(),
|
| + dip_point);
|
| +}
|
| +
|
| +// static
|
| +Point ScreenWin::ClientToDIPPoint(HWND hwnd, const Point& client_point) {
|
| + gfx::win::DisplayManager* display_manager = GetDisplayManager();
|
| + float scale_factor = display_manager->GetScaleFactorForHWND(hwnd);
|
| + return ScaleToFlooredPoint(client_point, 1.0f / scale_factor);
|
| +}
|
| +
|
| +// static
|
| +Point ScreenWin::DIPToClientPoint(HWND hwnd, const Point& dip_point) {
|
| + gfx::win::DisplayManager* display_manager = GetDisplayManager();
|
| + float scale_factor = display_manager->GetScaleFactorForHWND(hwnd);
|
| + return ScaleToFlooredPoint(dip_point, scale_factor);
|
| +}
|
| +
|
| +// static
|
| +Rect ScreenWin::ScreenToDIPRect(HWND hwnd, const Rect& pixel_bounds) {
|
| + // It's important we scale the origin and size separately. If we instead
|
| + // calculated the size from the floored origin and ceiled right the size could
|
| + // vary depending upon where the two points land. That would cause problems
|
| + // for the places this code is used (in particular mapping from native window
|
| + // bounds to DIPs).
|
| + if (hwnd) {
|
| + return Rect(ScreenToDIPPoint(pixel_bounds.origin()),
|
| + ScreenToDIPSize(hwnd, pixel_bounds.size()));
|
| + }
|
| + gfx::win::DisplayManager* display_manager = GetDisplayManager();
|
| + gfx::win::ScreenWinDisplay screen_win_display =
|
| + display_manager->GetScreenWinDisplayNearestScreenRect(pixel_bounds);
|
| + float scale_factor = screen_win_display.display().device_scale_factor();
|
| + return Rect(ScreenToDIPPoint(pixel_bounds.origin()),
|
| + ScaleToCeiledSize(pixel_bounds.size(), 1.0f / scale_factor));
|
| +}
|
| +
|
| +// static
|
| +Rect ScreenWin::DIPToScreenRect(HWND hwnd, const Rect& dip_bounds) {
|
| + // See comment in ScreenToDIPRect for why we calculate size like this.
|
| + if (hwnd) {
|
| + return Rect(DIPToScreenPoint(dip_bounds.origin()),
|
| + DIPToScreenSize(hwnd, dip_bounds.size()));
|
| + }
|
| + gfx::win::DisplayManager* display_manager = GetDisplayManager();
|
| + gfx::win::ScreenWinDisplay screen_win_display =
|
| + display_manager->GetScreenWinDisplayNearestDIPRect(dip_bounds);
|
| + float scale_factor = screen_win_display.display().device_scale_factor();
|
| + return Rect(DIPToScreenPoint(dip_bounds.origin()),
|
| + ScaleToCeiledSize(dip_bounds.size(), scale_factor));
|
| +}
|
| +
|
| +// static
|
| +Rect ScreenWin::ClientToDIPRect(HWND hwnd, const Rect& pixel_bounds) {
|
| + // See comment in ScreenToDIPRect for why we calculate size like this.
|
| + return Rect(ClientToDIPPoint(hwnd, pixel_bounds.origin()),
|
| + ScreenToDIPSize(hwnd, pixel_bounds.size()));
|
| +}
|
| +
|
| +// static
|
| +Rect ScreenWin::DIPToClientRect(HWND hwnd, const Rect& dip_bounds) {
|
| + // See comment in ScreenToDIPRect for why we calculate size like this.
|
| + return Rect(DIPToClientPoint(hwnd, dip_bounds.origin()),
|
| + DIPToScreenSize(hwnd, dip_bounds.size()));
|
| +}
|
| +
|
| +// static
|
| +Size ScreenWin::ScreenToDIPSize(HWND hwnd, const Size& size_in_pixels) {
|
| + gfx::win::DisplayManager* display_manager = GetDisplayManager();
|
| + float scale_factor = display_manager->GetScaleFactorForHWND(hwnd);
|
| + // Always ceil sizes. Otherwise we may be leaving off part of the bounds.
|
| + return ScaleToCeiledSize(size_in_pixels, 1.0f / scale_factor);
|
| +}
|
| +
|
| +// static
|
| +Size ScreenWin::DIPToScreenSize(HWND hwnd, const Size& dip_size) {
|
| + gfx::win::DisplayManager* display_manager = GetDisplayManager();
|
| + float scale_factor = display_manager->GetScaleFactorForHWND(hwnd);
|
| + // Always ceil sizes. Otherwise we may be leaving off part of the bounds.
|
| + return ScaleToCeiledSize(dip_size, scale_factor);
|
| +}
|
|
|
| HWND ScreenWin::GetHWNDFromNativeView(NativeView window) const {
|
| NOTREACHED();
|
| @@ -111,7 +174,7 @@ gfx::Point ScreenWin::GetCursorScreenPoint() {
|
| POINT pt;
|
| GetCursorPos(&pt);
|
| gfx::Point cursor_pos_pixels(pt);
|
| - return gfx::win::ScreenToDIPPoint(cursor_pos_pixels);
|
| + return ScreenToDIPPoint(cursor_pos_pixels);
|
| }
|
|
|
| gfx::NativeWindow ScreenWin::GetWindowUnderCursor() {
|
| @@ -121,7 +184,7 @@ gfx::NativeWindow ScreenWin::GetWindowUnderCursor() {
|
| }
|
|
|
| gfx::NativeWindow ScreenWin::GetWindowAtScreenPoint(const gfx::Point& point) {
|
| - gfx::Point point_in_pixels = gfx::win::DIPToScreenPoint(point);
|
| + gfx::Point point_in_pixels = DIPToScreenPoint(point);
|
| return GetNativeWindowFromHWND(WindowFromPoint(point_in_pixels.ToPOINT()));
|
| }
|
|
|
| @@ -130,7 +193,7 @@ int ScreenWin::GetNumDisplays() const {
|
| }
|
|
|
| std::vector<gfx::Display> ScreenWin::GetAllDisplays() const {
|
| - return displays_;
|
| + return ToVectorOfDisplays(display_manager_->GetScreenWinDisplays());
|
| }
|
|
|
| gfx::Display ScreenWin::GetDisplayNearestWindow(gfx::NativeView window) const {
|
| @@ -141,45 +204,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(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 +234,26 @@ 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_);
|
| +gfx::Rect ScreenWin::ScreenToDIPRectInWindow(
|
| + NativeView view, const gfx::Rect& screen_rect) const {
|
| + HWND hwnd = view ? GetHWNDFromNativeView(view) : NULL;
|
| + return ScreenWin::ScreenToDIPRect(hwnd, screen_rect);
|
| }
|
|
|
| -// 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));
|
| +gfx::Rect ScreenWin::DIPToScreenRectInWindow(NativeView view,
|
| + const gfx::Rect& dip_rect) const {
|
| + HWND hwnd = view ? GetHWNDFromNativeView(view) : NULL;
|
| + return ScreenWin::DIPToScreenRect(hwnd, dip_rect);
|
| +}
|
|
|
| - 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 =
|
| + ToVectorOfDisplays(old_screen_win_displays);
|
| + std::vector<gfx::Display> new_displays =
|
| + ToVectorOfDisplays(new_screen_win_displays);
|
| + change_notifier_.NotifyDisplaysChanged(old_displays, new_displays);
|
| }
|
|
|
| } // namespace gfx
|
|
|