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
3// found in the LICENSE file.
4
5#include "ui/views/widget/desktop_aura/desktop_screen_position_client.h"
6
7#include "ui/aura/root_window.h"
8#include "ui/gfx/display.h"
9#include "ui/gfx/point_conversions.h"
10#include "ui/gfx/screen.h"
11#include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
12
13namespace views {
14
15namespace {
16
17gfx::Point GetOrigin(const aura::RootWindow* root_window) {
18  gfx::Point origin_in_pixels = root_window->GetHostOrigin();
19  aura::RootWindow* window = const_cast<aura::RootWindow*>(root_window);
20  float scale = gfx::Screen::GetScreenFor(window)->
21       GetDisplayNearestWindow(window).device_scale_factor();
22  return gfx::ToFlooredPoint(
23      gfx::ScalePoint(origin_in_pixels, 1 / scale));
24}
25
26// Returns true if bounds passed to window are treated as though they are in
27// screen coordinates.
28bool PositionWindowInScreenCoordinates(aura::Window* window) {
29  if (window->type() == aura::client::WINDOW_TYPE_POPUP ||
30      window->type() == aura::client::WINDOW_TYPE_TOOLTIP)
31    return true;
32
33  Widget* widget = Widget::GetWidgetForNativeView(window);
34  return widget && widget->is_top_level();
35}
36
37}  // namespace
38
39DesktopScreenPositionClient::DesktopScreenPositionClient() {
40}
41
42DesktopScreenPositionClient::~DesktopScreenPositionClient() {
43}
44
45void DesktopScreenPositionClient::ConvertPointToScreen(
46    const aura::Window* window, gfx::Point* point) {
47  const aura::RootWindow* root_window = window->GetRootWindow();
48  aura::Window::ConvertPointToTarget(window, root_window, point);
49  gfx::Point origin = GetOrigin(root_window);
50  point->Offset(origin.x(), origin.y());
51}
52
53void DesktopScreenPositionClient::ConvertPointFromScreen(
54    const aura::Window* window, gfx::Point* point) {
55  const aura::RootWindow* root_window = window->GetRootWindow();
56  gfx::Point origin = GetOrigin(root_window);
57  point->Offset(-origin.x(), -origin.y());
58  aura::Window::ConvertPointToTarget(root_window, window, point);
59}
60
61void DesktopScreenPositionClient::ConvertHostPointToScreen(
62    aura::RootWindow* window, gfx::Point* point) {
63  ConvertPointToScreen(window, point);
64}
65
66void DesktopScreenPositionClient::SetBounds(
67    aura::Window* window,
68    const gfx::Rect& bounds,
69    const gfx::Display& display) {
70  // TODO: Use the 3rd parameter, |display|.
71  aura::RootWindow* root = window->GetRootWindow();
72
73  if (PositionWindowInScreenCoordinates(window)) {
74    // The caller expects windows we consider "embedded" to be placed in the
75    // screen coordinate system. So we need to offset the root window's
76    // position (which is in screen coordinates) from these bounds.
77
78    gfx::Point origin = bounds.origin();
79    aura::Window::ConvertPointToTarget(window->parent(), root, &origin);
80
81    gfx::Point host_origin = GetOrigin(root);
82    origin.Offset(-host_origin.x(), -host_origin.y());
83    window->SetBounds(gfx::Rect(origin, bounds.size()));
84    return;
85  }
86
87  DesktopNativeWidgetAura* desktop_native_widget =
88      DesktopNativeWidgetAura::ForWindow(window);
89  if (desktop_native_widget) {
90    root->SetHostBounds(bounds);
91    // Setting bounds of root resizes |window|.
92  } else {
93    window->SetBounds(bounds);
94  }
95}
96
97}  // namespace views
98