Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ash / display / screen_position_controller.cc
blob9503fd448c02605415238f18d372f80dc709c3b0
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.
5 #include "ash/display/screen_position_controller.h"
7 #include "ash/display/window_tree_host_manager.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/capture_client.h"
16 #include "ui/aura/client/focus_client.h"
17 #include "ui/aura/window.h"
18 #include "ui/aura/window_event_dispatcher.h"
19 #include "ui/aura/window_tracker.h"
20 #include "ui/aura/window_tree_host.h"
21 #include "ui/compositor/dip_util.h"
22 #include "ui/gfx/display.h"
23 #include "ui/gfx/screen.h"
24 #include "ui/wm/core/window_util.h"
25 #include "ui/wm/public/activation_client.h"
27 namespace ash {
28 namespace {
30 // Return true if the window or its ancestor has |kStayInSameRootWindowkey|
31 // property.
32 bool ShouldStayInSameRootWindow(const aura::Window* window) {
33 return window && (window->GetProperty(kStayInSameRootWindowKey) ||
34 ShouldStayInSameRootWindow(window->parent()));
37 // Move all transient children to |dst_root|, including the ones in
38 // the child windows and transient children of the transient children.
39 void MoveAllTransientChildrenToNewRoot(const gfx::Display& display,
40 aura::Window* window) {
41 aura::Window* dst_root = Shell::GetInstance()
42 ->window_tree_host_manager()
43 ->GetRootWindowForDisplayId(display.id());
44 aura::Window::Windows transient_children =
45 ::wm::GetTransientChildren(window);
46 for (aura::Window::Windows::iterator iter = transient_children.begin();
47 iter != transient_children.end(); ++iter) {
48 aura::Window* transient_child = *iter;
49 int container_id = transient_child->parent()->id();
50 DCHECK_GE(container_id, 0);
51 aura::Window* container = Shell::GetContainer(dst_root, container_id);
52 gfx::Rect parent_bounds_in_screen = transient_child->GetBoundsInScreen();
53 container->AddChild(transient_child);
54 transient_child->SetBoundsInScreen(parent_bounds_in_screen, display);
56 // Transient children may have transient children.
57 MoveAllTransientChildrenToNewRoot(display, transient_child);
59 // Move transient children of the child windows if any.
60 aura::Window::Windows children = window->children();
61 for (aura::Window::Windows::iterator iter = children.begin();
62 iter != children.end(); ++iter)
63 MoveAllTransientChildrenToNewRoot(display, *iter);
66 } // namespace
68 // static
69 void ScreenPositionController::ConvertHostPointToRelativeToRootWindow(
70 aura::Window* root_window,
71 const aura::Window::Windows& root_windows,
72 gfx::Point* point,
73 aura::Window** target_root) {
74 DCHECK(!root_window->parent());
75 gfx::Point point_in_root(*point);
76 root_window->GetHost()->ConvertPointFromHost(&point_in_root);
78 #if defined(USE_X11) || defined(USE_OZONE)
79 gfx::Rect host_bounds(root_window->GetHost()->GetBounds().size());
80 if (!host_bounds.Contains(*point)) {
81 // This conversion is necessary to deal with X's passive input
82 // grab while dragging window. For example, if we have two
83 // displays, say 1000x1000 (primary) and 500x500 (extended one
84 // on the right), and start dragging a window at (999, 123), and
85 // then move the pointer to the right, the pointer suddenly
86 // warps to the extended display. The destination is (0, 123) in
87 // the secondary root window's coordinates, or (1000, 123) in
88 // the screen coordinates. However, since the mouse is captured
89 // by X during drag, a weird LocatedEvent, something like (0, 1123)
90 // in the *primary* root window's coordinates, is sent to Chrome
91 // (Remember that in the native X11 world, the two root windows
92 // are always stacked vertically regardless of the display
93 // layout in Ash). We need to figure out that (0, 1123) in the
94 // primary root window's coordinates is actually (0, 123) in the
95 // extended root window's coordinates.
97 // For now Ozone works in a similar manner as X11. Transitioning from one
98 // display's coordinate system to anothers may cause events in the
99 // primary's coordinate system which fall in the extended display.
101 gfx::Point location_in_native(point_in_root);
103 root_window->GetHost()->ConvertPointToNativeScreen(&location_in_native);
105 for (size_t i = 0; i < root_windows.size(); ++i) {
106 aura::WindowTreeHost* host = root_windows[i]->GetHost();
107 const gfx::Rect native_bounds = host->GetBounds();
108 if (native_bounds.Contains(location_in_native)) {
109 *target_root = root_windows[i];
110 *point = location_in_native;
111 host->ConvertPointFromNativeScreen(point);
112 return;
116 #endif
117 *target_root = root_window;
118 *point = point_in_root;
121 void ScreenPositionController::ConvertPointToScreen(
122 const aura::Window* window,
123 gfx::Point* point) {
124 const aura::Window* root = window->GetRootWindow();
125 aura::Window::ConvertPointToTarget(window, root, point);
126 const gfx::Point display_origin = Shell::GetScreen()->GetDisplayNearestWindow(
127 const_cast<aura::Window*>(root)).bounds().origin();
128 point->Offset(display_origin.x(), display_origin.y());
131 void ScreenPositionController::ConvertPointFromScreen(
132 const aura::Window* window,
133 gfx::Point* point) {
134 const aura::Window* root = window->GetRootWindow();
135 const gfx::Point display_origin = Shell::GetScreen()->GetDisplayNearestWindow(
136 const_cast<aura::Window*>(root)).bounds().origin();
137 point->Offset(-display_origin.x(), -display_origin.y());
138 aura::Window::ConvertPointToTarget(root, window, point);
141 void ScreenPositionController::ConvertHostPointToScreen(
142 aura::Window* root_window,
143 gfx::Point* point) {
144 aura::Window* root = root_window->GetRootWindow();
145 aura::Window* target_root = nullptr;
146 ConvertHostPointToRelativeToRootWindow(root, Shell::GetAllRootWindows(),
147 point, &target_root);
148 ConvertPointToScreen(target_root, point);
151 void ScreenPositionController::SetBounds(aura::Window* window,
152 const gfx::Rect& bounds,
153 const gfx::Display& display) {
154 DCHECK_NE(-1, display.id());
155 if (!window->parent()->GetProperty(kUsesScreenCoordinatesKey)) {
156 window->SetBounds(bounds);
157 return;
160 // Don't move a window to other root window if:
161 // a) the window is a transient window. It moves when its
162 // transient_parent moves.
163 // b) if the window or its ancestor has kStayInSameRootWindowkey. It's
164 // intentionally kept in the same root window even if the bounds is
165 // outside of the display.
166 if (!::wm::GetTransientParent(window) &&
167 !ShouldStayInSameRootWindow(window)) {
168 aura::Window* dst_root = Shell::GetInstance()
169 ->window_tree_host_manager()
170 ->GetRootWindowForDisplayId(display.id());
171 DCHECK(dst_root);
172 aura::Window* dst_container = NULL;
173 if (dst_root != window->GetRootWindow()) {
174 int container_id = window->parent()->id();
175 // All containers that uses screen coordinates must have valid window ids.
176 DCHECK_GE(container_id, 0);
177 // Don't move modal background.
178 if (!SystemModalContainerLayoutManager::IsModalBackground(window))
179 dst_container = Shell::GetContainer(dst_root, container_id);
182 if (dst_container && window->parent() != dst_container) {
183 aura::Window* focused = aura::client::GetFocusClient(window)->
184 GetFocusedWindow();
185 aura::client::ActivationClient* activation_client =
186 aura::client::GetActivationClient(window->GetRootWindow());
187 aura::Window* active = activation_client->GetActiveWindow();
189 aura::WindowTracker tracker;
190 if (focused)
191 tracker.Add(focused);
192 if (active && focused != active)
193 tracker.Add(active);
195 // Set new bounds now so that the container's layout manager
196 // can adjust the bounds if necessary.
197 gfx::Point origin = bounds.origin();
198 const gfx::Point display_origin = display.bounds().origin();
199 origin.Offset(-display_origin.x(), -display_origin.y());
200 gfx::Rect new_bounds = gfx::Rect(origin, bounds.size());
202 window->SetBounds(new_bounds);
204 dst_container->AddChild(window);
206 MoveAllTransientChildrenToNewRoot(display, window);
208 // Restore focused/active window.
209 if (tracker.Contains(focused)) {
210 aura::client::GetFocusClient(window)->FocusWindow(focused);
211 // TODO(beng): replace with GetRootWindow().
212 ash::Shell::GetInstance()->set_target_root_window(
213 focused->GetRootWindow());
214 } else if (tracker.Contains(active)) {
215 activation_client->ActivateWindow(active);
217 // TODO(oshima): We should not have to update the bounds again
218 // below in theory, but we currently do need as there is a code
219 // that assumes that the bounds will never be overridden by the
220 // layout mananger. We should have more explicit control how
221 // constraints are applied by the layout manager.
224 gfx::Point origin(bounds.origin());
225 const gfx::Point display_origin = Shell::GetScreen()->GetDisplayNearestWindow(
226 window).bounds().origin();
227 origin.Offset(-display_origin.x(), -display_origin.y());
228 window->SetBounds(gfx::Rect(origin, bounds.size()));
231 } // namespace ash