Skip to content

Commit

Permalink
Refactor Windows Scaling Functions from gfx::win to display::win::Scr…
Browse files Browse the repository at this point in the history
…eenWin

Scaling functions in a multi-DPI world in Windows only make sense in the context
of a screen, so the scaling functions are moving there.

This also prepares the functions to accept an HWND, providing a context to
determine the scaling factor. The argument is currently ignored to keep the
same behavior as today.

Also fixed an error in TooltipWin where a screen point was passed into
Screen::GetDisplayNearestPoint, which expects a DIP point.

BUG=426656

Review URL: https://codereview.chromium.org/1875733005

Cr-Commit-Position: refs/heads/master@{#387643}
  • Loading branch information
robliao authored and Commit bot committed Apr 15, 2016
1 parent a794ae4 commit 30f57c3
Show file tree
Hide file tree
Showing 27 changed files with 206 additions and 97 deletions.
1 change: 1 addition & 0 deletions chrome/browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ source_set("ui") {
"//ui/accessibility",
"//ui/base",
"//ui/content_accelerators",
"//ui/display",
"//ui/events",
"//ui/events:gesture_detection",
"//ui/gfx",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "chrome/browser/ui/views/tabs/tab_strip.h"
#include "chrome/common/chrome_constants.h"
#include "ui/base/theme_provider.h"
#include "ui/gfx/win/dpi.h"
#include "ui/display/win/screen_win.h"
#include "ui/views/controls/menu/native_menu_win.h"

namespace {
Expand Down Expand Up @@ -285,7 +285,8 @@ MARGINS BrowserDesktopWindowTreeHostWin::GetDWMFrameMargins() const {
if (!browser_view_->IsFullscreen()) {
gfx::Rect tabstrip_bounds(
browser_frame_->GetBoundsForTabStrip(browser_view_->tabstrip()));
tabstrip_bounds = gfx::win::DIPToScreenRect(tabstrip_bounds);
tabstrip_bounds =
display::win::ScreenWin::DIPToClientRect(GetHWND(), tabstrip_bounds);
margins.cyTopHeight = tabstrip_bounds.bottom();

// On pre-Win 10, we need to offset the DWM frame into the toolbar so that
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/ui/views/tabs/tab_strip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
#include "ui/views/window/non_client_view.h"

#if defined(OS_WIN)
#include "ui/gfx/win/dpi.h"
#include "ui/display/win/screen_win.h"
#include "ui/gfx/win/hwnd_util.h"
#include "ui/views/widget/monitor_win.h"
#include "ui/views/win/hwnd_util.h"
Expand Down Expand Up @@ -345,7 +345,7 @@ void NewTabButton::OnMouseReleased(const ui::MouseEvent& event) {
if (event.IsOnlyRightMouseButton()) {
gfx::Point point = event.location();
views::View::ConvertPointToScreen(this, &point);
point = gfx::win::DIPToScreenPoint(point);
point = display::win::ScreenWin::DIPToScreenPoint(point);
bool destroyed = false;
destroyed_ = &destroyed;
gfx::ShowSystemMenuAtPoint(views::HWNDForView(this), point);
Expand Down
6 changes: 3 additions & 3 deletions chrome/browser/ui/views/tabs/window_finder_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#include "base/win/scoped_gdi_object.h"
#include "base/win/windows_version.h"
#include "ui/aura/window.h"
#include "ui/display/win/screen_win.h"
#include "ui/gfx/screen.h"
#include "ui/gfx/win/dpi.h"
#include "ui/views/widget/desktop_aura/desktop_window_tree_host_win.h"
#include "ui/views/win/hwnd_util.h"

Expand Down Expand Up @@ -126,7 +126,7 @@ class TopMostFinder : public BaseWindowFinder {
target_(window),
is_top_most_(false),
tmp_region_(CreateRectRgn(0, 0, 0, 0)) {
screen_loc_ = gfx::win::DIPToScreenPoint(screen_loc);
screen_loc_ = display::win::ScreenWin::DIPToScreenPoint(screen_loc);
EnumWindows(WindowCallbackProc, as_lparam());
}

Expand Down Expand Up @@ -203,7 +203,7 @@ class LocalProcessWindowFinder : public BaseWindowFinder {
CHECK(SUCCEEDED(virtual_desktop_manager_.CreateInstance(
__uuidof(VirtualDesktopManager))));
}
screen_loc_ = gfx::win::DIPToScreenPoint(screen_loc);
screen_loc_ = display::win::ScreenWin::DIPToScreenPoint(screen_loc);
EnumThreadWindows(GetCurrentThreadId(), WindowCallbackProc, as_lparam());
}

Expand Down
1 change: 1 addition & 0 deletions chrome/chrome_browser_ui.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -2911,6 +2911,7 @@
'../ui/accessibility/accessibility.gyp:accessibility',
'../ui/base/ui_base.gyp:ui_base',
'../ui/content_accelerators/ui_content_accelerators.gyp:ui_content_accelerators',
'../ui/display/display.gyp:display',
'../ui/events/events.gyp:events',
'../ui/events/events.gyp:gesture_detection',
'../ui/gfx/gfx.gyp:gfx',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#include "base/logging.h"
#include "content/browser/renderer_host/input/web_input_event_util.h"
#include "ui/display/win/screen_win.h"
#include "ui/events/blink/blink_event_util.h"
#include "ui/events/event_utils.h"
#include "ui/gfx/win/dpi.h"

using blink::WebInputEvent;
using blink::WebKeyboardEvent;
Expand Down Expand Up @@ -163,7 +163,7 @@ WebMouseEvent WebMouseEventBuilder::Build(HWND hwnd,
ClientToScreen(hwnd, &global_point);

// We need to convert the global point back to DIP before using it.
gfx::Point dip_global_point = gfx::win::ScreenToDIPPoint(
gfx::Point dip_global_point = display::win::ScreenWin::ScreenToDIPPoint(
gfx::Point(global_point.x, global_point.y));

result.globalX = dip_global_point.x();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
#include "ui/base/view_prop.h"
#include "ui/base/win/internal_constants.h"
#include "ui/base/win/window_event_target.h"
#include "ui/display/win/screen_win.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/win/direct_manipulation.h"
#include "ui/gfx/win/dpi.h"

namespace content {

Expand Down Expand Up @@ -85,7 +85,8 @@ void LegacyRenderWidgetHostHWND::Hide() {
}

void LegacyRenderWidgetHostHWND::SetBounds(const gfx::Rect& bounds) {
gfx::Rect bounds_in_pixel = gfx::win::DIPToScreenRect(bounds);
gfx::Rect bounds_in_pixel = display::win::ScreenWin::DIPToClientRect(hwnd(),
bounds);
::SetWindowPos(hwnd(), NULL, bounds_in_pixel.x(), bounds_in_pixel.y(),
bounds_in_pixel.width(), bounds_in_pixel.height(),
SWP_NOREDRAW);
Expand Down
6 changes: 3 additions & 3 deletions content/browser/renderer_host/render_widget_host_view_aura.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@
#include "ui/base/win/hidden_window.h"
#include "ui/display/win/screen_win.h"
#include "ui/gfx/gdi_util.h"
#include "ui/gfx/win/dpi.h"
#endif

#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
Expand Down Expand Up @@ -1018,7 +1017,8 @@ bool RenderWidgetHostViewAura::UsesNativeWindowFrame() const {

void RenderWidgetHostViewAura::UpdateMouseLockRegion() {
RECT window_rect =
gfx::win::DIPToScreenRect(window_->GetBoundsInScreen()).ToRECT();
gfx::Screen::GetScreen()->DIPToScreenRectInWindow(
window_, window_->GetBoundsInScreen()).ToRECT();
::ClipCursor(&window_rect);
}

Expand Down Expand Up @@ -1112,7 +1112,7 @@ gfx::Rect RenderWidgetHostViewAura::GetBoundsInRootWindow() {
}
}

bounds = gfx::win::ScreenToDIPRect(bounds);
bounds = gfx::Screen::GetScreen()->ScreenToDIPRectInWindow(top_level, bounds);
#endif

return bounds;
Expand Down
1 change: 1 addition & 0 deletions ui/app_list/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ component("app_list") {
"//ui/base",
"//ui/base/ime",
"//ui/compositor",
"//ui/display",
"//ui/events",
"//ui/gfx",
"//ui/gfx/geometry",
Expand Down
1 change: 1 addition & 0 deletions ui/app_list/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ include_rules = [
"+ui/accessibility",
"+ui/aura",
"+ui/base",
"+ui/display",
"+ui/compositor",
"+ui/events",
"+ui/gfx",
Expand Down
1 change: 1 addition & 0 deletions ui/app_list/app_list.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'../base/ui_base.gyp:ui_base',
'../base/ime/ui_base_ime.gyp:ui_base_ime',
'../compositor/compositor.gyp:compositor',
'../display/display.gyp:display',
'../events/events.gyp:events_base',
'../gfx/gfx.gyp:gfx',
'../gfx/gfx.gyp:gfx_geometry',
Expand Down
6 changes: 4 additions & 2 deletions ui/app_list/views/apps_grid_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
#include "ui/base/dragdrop/drop_target_win.h"
#include "ui/base/dragdrop/os_exchange_data.h"
#include "ui/base/dragdrop/os_exchange_data_provider_win.h"
#include "ui/gfx/win/dpi.h"
#include "ui/display/win/screen_win.h"
#endif

namespace app_list {
Expand Down Expand Up @@ -338,7 +338,9 @@ class SynchronousDrag : public ui::DragSourceWin {
GetCursorPos(&p);
ScreenToClient(GetGridViewHWND(), &p);
gfx::Point grid_view_pt(p.x, p.y);
grid_view_pt = gfx::win::ScreenToDIPPoint(grid_view_pt);
grid_view_pt =
display::win::ScreenWin::ClientToDIPPoint(GetGridViewHWND(),
grid_view_pt);
views::View::ConvertPointFromWidget(grid_view_, &grid_view_pt);
return grid_view_pt;
}
Expand Down
1 change: 1 addition & 0 deletions ui/base/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include_rules = [
"+net",
"+skia/ext",
"+third_party/skia",
"+ui/display",
"+ui/events",
"+ui/gfx",
"+ui/resources/grit/ui_resources.h",
Expand Down
1 change: 1 addition & 0 deletions ui/base/ime/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ component("ime") {
"//net",
"//third_party/icu",
"//ui/base",
"//ui/display",
"//ui/events",
"//ui/gfx",
"//ui/gfx/geometry",
Expand Down
10 changes: 7 additions & 3 deletions ui/base/ime/input_method_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
#include "ui/base/ime/text_input_client.h"
#include "ui/base/ime/win/tsf_input_scope.h"
#include "ui/base/ui_base_switches.h"
#include "ui/display/win/screen_win.h"
#include "ui/events/event.h"
#include "ui/events/event_constants.h"
#include "ui/events/event_utils.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/gfx/win/dpi.h"
#include "ui/gfx/win/hwnd_util.h"

namespace {
Expand Down Expand Up @@ -234,7 +234,9 @@ void InputMethodWin::OnCaretBoundsChanged(const TextInputClient* client) {
// Tentatively assume that the returned value is DIP (Density Independent
// Pixel). See the comment in text_input_client.h and http://crbug.com/360334.
const gfx::Rect dip_screen_bounds(GetTextInputClient()->GetCaretBounds());
const gfx::Rect screen_bounds = gfx::win::DIPToScreenRect(dip_screen_bounds);
const gfx::Rect screen_bounds =
display::win::ScreenWin::DIPToScreenRect(toplevel_window_handle_,
dip_screen_bounds);

HWND attached_window = toplevel_window_handle_;
// TODO(ime): see comment in TextInputClient::GetCaretBounds(), this
Expand Down Expand Up @@ -615,7 +617,9 @@ LRESULT InputMethodWin::OnQueryCharPosition(IMECHARPOSITION* char_positon) {
return 0;
dip_rect = client->GetCaretBounds();
}
const gfx::Rect rect = gfx::win::DIPToScreenRect(dip_rect);
const gfx::Rect rect =
display::win::ScreenWin::DIPToScreenRect(toplevel_window_handle_,
dip_rect);

char_positon->pt.x = rect.x();
char_positon->pt.y = rect.y();
Expand Down
1 change: 1 addition & 0 deletions ui/base/ime/ui_base_ime.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
'../../../third_party/icu/icu.gyp:icui18n',
'../../../third_party/icu/icu.gyp:icuuc',
'../../../url/url.gyp:url_lib',
'../../display/display.gyp:display',
'../../events/events.gyp:dom_keycode_converter',
'../../events/events.gyp:events',
'../../events/events.gyp:events_base',
Expand Down
75 changes: 72 additions & 3 deletions ui/display/win/screen_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
#include "ui/display/win/screen_win_display.h"
#include "ui/gfx/display.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/point_conversions.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/size_conversions.h"
#include "ui/gfx/win/dpi.h"

namespace display {
Expand Down Expand Up @@ -75,6 +78,72 @@ ScreenWin::ScreenWin() {

ScreenWin::~ScreenWin() = default;

// static
gfx::Point ScreenWin::ScreenToDIPPoint(const gfx::Point& pixel_point) {
return ScaleToFlooredPoint(pixel_point, 1.0f / gfx::GetDPIScale());
}

// static
gfx::Point ScreenWin::DIPToScreenPoint(const gfx::Point& dip_point) {
return ScaleToFlooredPoint(dip_point, gfx::GetDPIScale());
}

// static
gfx::Point ScreenWin::ClientToDIPPoint(HWND hwnd,
const gfx::Point& client_point) {
// TODO(robliao): Get the scale factor from |hwnd|.
return ScreenToDIPPoint(client_point);
}

// static
gfx::Point ScreenWin::DIPToClientPoint(HWND hwnd, const gfx::Point& dip_point) {
// TODO(robliao): Get the scale factor from |hwnd|.
return DIPToScreenPoint(dip_point);
}

// static
gfx::Rect ScreenWin::ScreenToDIPRect(HWND hwnd, const gfx::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).
return gfx::Rect(ScreenToDIPPoint(pixel_bounds.origin()),
ScreenToDIPSize(hwnd, pixel_bounds.size()));
}

// static
gfx::Rect ScreenWin::DIPToScreenRect(HWND hwnd, const gfx::Rect& dip_bounds) {
// See comment in ScreenToDIPRect for why we calculate size like this.
return gfx::Rect(DIPToScreenPoint(dip_bounds.origin()),
DIPToScreenSize(hwnd, dip_bounds.size()));
}

// static
gfx::Rect ScreenWin::ClientToDIPRect(HWND hwnd, const gfx::Rect& pixel_bounds) {
return ScreenToDIPRect(hwnd, pixel_bounds);
}

// static
gfx::Rect ScreenWin::DIPToClientRect(HWND hwnd, const gfx::Rect& dip_bounds) {
return DIPToScreenRect(hwnd, dip_bounds);
}

// static
gfx::Size ScreenWin::ScreenToDIPSize(HWND hwnd,
const gfx::Size& size_in_pixels) {
// Always ceil sizes. Otherwise we may be leaving off part of the bounds.
// TODO(robliao): Get the scale factor from |hwnd|.
return ScaleToCeiledSize(size_in_pixels, 1.0f / gfx::GetDPIScale());
}

// static
gfx::Size ScreenWin::DIPToScreenSize(HWND hwnd, const gfx::Size& dip_size) {
// Always ceil sizes. Otherwise we may be leaving off part of the bounds.
// TODO(robliao): Get the scale factor from |hwnd|.
return ScaleToCeiledSize(dip_size, gfx::GetDPIScale());
}

HWND ScreenWin::GetHWNDFromNativeView(gfx::NativeView window) const {
NOTREACHED();
return nullptr;
Expand All @@ -89,7 +158,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() {
Expand All @@ -100,7 +169,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()));
}

Expand All @@ -126,7 +195,7 @@ gfx::Display ScreenWin::GetDisplayNearestWindow(gfx::NativeView window) const {
}

gfx::Display ScreenWin::GetDisplayNearestPoint(const gfx::Point& point) const {
gfx::Point screen_point(gfx::win::DIPToScreenPoint(point));
gfx::Point screen_point(DIPToScreenPoint(point));
ScreenWinDisplay screen_win_display =
GetScreenWinDisplayNearestScreenPoint(screen_point);
return screen_win_display.display();
Expand Down
Loading

0 comments on commit 30f57c3

Please sign in to comment.