screen_position_controller.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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 "ash/display/screen_position_controller.h"
6
7#include "ash/display/display_controller.h"
8#include "ash/root_window_controller.h"
9#include "ash/shell.h"
10#include "ash/shell_window_ids.h"
11#include "ash/wm/coordinate_conversion.h"
12#include "ash/wm/system_modal_container_layout_manager.h"
13#include "ash/wm/window_properties.h"
14#include "ash/wm/window_state.h"
15#include "ui/aura/client/activation_client.h"
16#include "ui/aura/client/capture_client.h"
17#include "ui/aura/client/focus_client.h"
18#include "ui/aura/root_window.h"
19#include "ui/aura/window_tracker.h"
20#include "ui/compositor/dip_util.h"
21#include "ui/gfx/display.h"
22#include "ui/gfx/screen.h"
23#include "ui/views/corewm/window_util.h"
24
25namespace ash {
26namespace {
27
28// Return true if the window or its ancestor has |kStayInSameRootWindowkey|
29// property.
30bool ShouldStayInSameRootWindow(const aura::Window* window) {
31  return window &&
32      (window->GetProperty(internal::kStayInSameRootWindowKey) ||
33       ShouldStayInSameRootWindow(window->parent()));
34}
35
36// Move all transient children to |dst_root|, including the ones in
37// the child windows and transient children of the transient children.
38void MoveAllTransientChildrenToNewRoot(const gfx::Display& display,
39                                       aura::Window* window) {
40  aura::Window* dst_root = Shell::GetInstance()->display_controller()->
41      GetRootWindowForDisplayId(display.id());
42  aura::Window::Windows transient_children =
43      views::corewm::GetTransientChildren(window);
44  for (aura::Window::Windows::iterator iter = transient_children.begin();
45       iter != transient_children.end(); ++iter) {
46    aura::Window* transient_child = *iter;
47    int container_id = transient_child->parent()->id();
48    DCHECK_GE(container_id, 0);
49    aura::Window* container = Shell::GetContainer(dst_root, container_id);
50    gfx::Rect parent_bounds_in_screen = transient_child->GetBoundsInScreen();
51    container->AddChild(transient_child);
52    transient_child->SetBoundsInScreen(parent_bounds_in_screen, display);
53
54    // Transient children may have transient children.
55    MoveAllTransientChildrenToNewRoot(display, transient_child);
56  }
57  // Move transient children of the child windows if any.
58  aura::Window::Windows children = window->children();
59  for (aura::Window::Windows::iterator iter = children.begin();
60       iter != children.end(); ++iter)
61    MoveAllTransientChildrenToNewRoot(display, *iter);
62}
63
64// Finds the root window at |location| in |window|'s coordinates and returns a
65// pair of root window and location in that root window's coordinates. The
66// function usually returns |window->GetRootWindow()|, but if the mouse pointer
67// is moved outside the |window|'s root while the mouse is captured, it returns
68// the other root window.
69std::pair<aura::Window*, gfx::Point> GetRootWindowRelativeToWindow(
70    aura::Window* window,
71    const gfx::Point& location) {
72  aura::Window* root_window = window->GetRootWindow();
73  gfx::Point location_in_root(location);
74  aura::Window::ConvertPointToTarget(window, root_window, &location_in_root);
75
76#if defined(USE_X11)
77  if (!root_window->ContainsPointInRoot(location_in_root)) {
78    // This conversion is necessary to deal with X's passive input
79    // grab while dragging window. For example, if we have two
80    // displays, say 1000x1000 (primary) and 500x500 (extended one
81    // on the right), and start dragging a window at (999, 123), and
82    // then move the pointer to the right, the pointer suddenly
83    // warps to the extended display. The destination is (0, 123) in
84    // the secondary root window's coordinates, or (1000, 123) in
85    // the screen coordinates. However, since the mouse is captured
86    // by X during drag, a weird LocatedEvent, something like (0, 1123)
87    // in the *primary* root window's coordinates, is sent to Chrome
88    // (Remember that in the native X11 world, the two root windows
89    // are always stacked vertically regardless of the display
90    // layout in Ash). We need to figure out that (0, 1123) in the
91    // primary root window's coordinates is actually (0, 123) in the
92    // extended root window's coordinates.
93
94    gfx::Point location_in_native(location_in_root);
95    root_window->GetDispatcher()->host()->ConvertPointToNativeScreen(
96        &location_in_native);
97
98    aura::Window::Windows root_windows = Shell::GetAllRootWindows();
99    for (size_t i = 0; i < root_windows.size(); ++i) {
100      aura::WindowEventDispatcher* dispatcher =
101          root_windows[i]->GetDispatcher();
102      const gfx::Rect native_bounds = dispatcher->host()->GetBounds();
103      if (native_bounds.Contains(location_in_native)) {
104        root_window = root_windows[i];
105        location_in_root = location_in_native;
106        dispatcher->host()->ConvertPointFromNativeScreen(&location_in_root);
107        break;
108      }
109    }
110  }
111#else
112  // TODO(yusukes): Support non-X11 platforms if necessary.
113#endif
114
115  return std::make_pair(root_window, location_in_root);
116}
117
118}  // namespace
119
120namespace internal {
121
122void ScreenPositionController::ConvertPointToScreen(
123    const aura::Window* window,
124    gfx::Point* point) {
125  const aura::Window* root = window->GetRootWindow();
126  aura::Window::ConvertPointToTarget(window, root, point);
127  const gfx::Point display_origin = Shell::GetScreen()->GetDisplayNearestWindow(
128      const_cast<aura::Window*>(root)).bounds().origin();
129  point->Offset(display_origin.x(), display_origin.y());
130}
131
132void ScreenPositionController::ConvertPointFromScreen(
133    const aura::Window* window,
134    gfx::Point* point) {
135  const aura::Window* root = window->GetRootWindow();
136  const gfx::Point display_origin = Shell::GetScreen()->GetDisplayNearestWindow(
137      const_cast<aura::Window*>(root)).bounds().origin();
138  point->Offset(-display_origin.x(), -display_origin.y());
139  aura::Window::ConvertPointToTarget(root, window, point);
140}
141
142void ScreenPositionController::ConvertHostPointToScreen(
143    aura::Window* root_window,
144    gfx::Point* point) {
145  aura::Window* root = root_window->GetRootWindow();
146  root->GetDispatcher()->host()->ConvertPointFromHost(point);
147  std::pair<aura::Window*, gfx::Point> pair =
148      GetRootWindowRelativeToWindow(root, *point);
149  *point = pair.second;
150  ConvertPointToScreen(pair.first, point);
151}
152
153void ScreenPositionController::SetBounds(aura::Window* window,
154                                         const gfx::Rect& bounds,
155                                         const gfx::Display& display) {
156  DCHECK_NE(-1, display.id());
157  if (!window->parent()->GetProperty(internal::kUsesScreenCoordinatesKey)) {
158    window->SetBounds(bounds);
159    return;
160  }
161
162  // Don't move a window to other root window if:
163  // a) the window is a transient window. It moves when its
164  //    transient_parent moves.
165  // b) if the window or its ancestor has kStayInSameRootWindowkey. It's
166  //    intentionally kept in the same root window even if the bounds is
167  //    outside of the display.
168  if (!views::corewm::GetTransientParent(window) &&
169      !ShouldStayInSameRootWindow(window)) {
170    aura::Window* dst_root =
171        Shell::GetInstance()->display_controller()->GetRootWindowForDisplayId(
172            display.id());
173    DCHECK(dst_root);
174    aura::Window* dst_container = NULL;
175    if (dst_root != window->GetRootWindow()) {
176      int container_id = window->parent()->id();
177      // All containers that uses screen coordinates must have valid window ids.
178      DCHECK_GE(container_id, 0);
179      // Don't move modal background.
180      if (!SystemModalContainerLayoutManager::IsModalBackground(window))
181        dst_container = Shell::GetContainer(dst_root, container_id);
182    }
183
184    if (dst_container && window->parent() != dst_container) {
185      aura::Window* focused = aura::client::GetFocusClient(window)->
186          GetFocusedWindow();
187      aura::client::ActivationClient* activation_client =
188          aura::client::GetActivationClient(window->GetRootWindow());
189      aura::Window* active = activation_client->GetActiveWindow();
190
191      aura::WindowTracker tracker;
192      if (focused)
193        tracker.Add(focused);
194      if (active && focused != active)
195        tracker.Add(active);
196
197      // Set new bounds now so that the container's layout manager
198      // can adjust the bounds if necessary.
199      gfx::Point origin = bounds.origin();
200      const gfx::Point display_origin = display.bounds().origin();
201      origin.Offset(-display_origin.x(), -display_origin.y());
202      gfx::Rect new_bounds = gfx::Rect(origin, bounds.size());
203
204      window->SetBounds(new_bounds);
205
206      dst_container->AddChild(window);
207
208      MoveAllTransientChildrenToNewRoot(display, window);
209
210      // Restore focused/active window.
211      if (tracker.Contains(focused)) {
212        aura::client::GetFocusClient(window)->FocusWindow(focused);
213        // TODO(beng): replace with GetRootWindow().
214        ash::Shell::GetInstance()->set_target_root_window(
215            focused->GetRootWindow());
216      } else if (tracker.Contains(active)) {
217        activation_client->ActivateWindow(active);
218      }
219      // TODO(oshima): We should not have to update the bounds again
220      // below in theory, but we currently do need as there is a code
221      // that assumes that the bounds will never be overridden by the
222      // layout mananger. We should have more explicit control how
223      // constraints are applied by the layout manager.
224    }
225  }
226  gfx::Point origin(bounds.origin());
227  const gfx::Point display_origin = Shell::GetScreen()->GetDisplayNearestWindow(
228      window).bounds().origin();
229  origin.Offset(-display_origin.x(), -display_origin.y());
230  window->SetBounds(gfx::Rect(origin, bounds.size()));
231}
232
233}  // internal
234}  // ash
235