Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ash / display / screen_position_controller.cc
bloba64fb4b85497e422ac4329cbb3be209f444b51f6
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/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/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()->display_controller()->
42 GetRootWindowForDisplayId(display.id());
43 aura::Window::Windows transient_children =
44 ::wm::GetTransientChildren(window);
45 for (aura::Window::Windows::iterator iter = transient_children.begin();
46 iter != transient_children.end(); ++iter) {
47 aura::Window* transient_child = *iter;
48 int container_id = transient_child->parent()->id();
49 DCHECK_GE(container_id, 0);
50 aura::Window* container = Shell::GetContainer(dst_root, container_id);
51 gfx::Rect parent_bounds_in_screen = transient_child->GetBoundsInScreen();
52 container->AddChild(transient_child);
53 transient_child->SetBoundsInScreen(parent_bounds_in_screen, display);
55 // Transient children may have transient children.
56 MoveAllTransientChildrenToNewRoot(display, transient_child);
58 // Move transient children of the child windows if any.
59 aura::Window::Windows children = window->children();
60 for (aura::Window::Windows::iterator iter = children.begin();
61 iter != children.end(); ++iter)
62 MoveAllTransientChildrenToNewRoot(display, *iter);
65 } // namespace
67 // static
68 void ScreenPositionController::ConvertHostPointToRelativeToRootWindow(
69 aura::Window* root_window,
70 const aura::Window::Windows& root_windows,
71 gfx::Point* point,
72 aura::Window** target_root) {
73 DCHECK(!root_window->parent());
74 gfx::Point point_in_root(*point);
75 root_window->GetHost()->ConvertPointFromHost(&point_in_root);
77 *target_root = root_window;
78 *point = point_in_root;
80 #if defined(USE_X11) || defined(USE_OZONE)
81 if (!root_window->GetHost()->GetBounds().Contains(*point)) {
82 // This conversion is necessary to deal with X's passive input
83 // grab while dragging window. For example, if we have two
84 // displays, say 1000x1000 (primary) and 500x500 (extended one
85 // on the right), and start dragging a window at (999, 123), and
86 // then move the pointer to the right, the pointer suddenly
87 // warps to the extended display. The destination is (0, 123) in
88 // the secondary root window's coordinates, or (1000, 123) in
89 // the screen coordinates. However, since the mouse is captured
90 // by X during drag, a weird LocatedEvent, something like (0, 1123)
91 // in the *primary* root window's coordinates, is sent to Chrome
92 // (Remember that in the native X11 world, the two root windows
93 // are always stacked vertically regardless of the display
94 // layout in Ash). We need to figure out that (0, 1123) in the
95 // primary root window's coordinates is actually (0, 123) in the
96 // extended root window's coordinates.
98 // For now Ozone works in a similar manner as X11. Transitioning from one
99 // display's coordinate system to anothers may cause events in the
100 // primary's coordinate system which fall in the extended display.
102 gfx::Point location_in_native(point_in_root);
104 root_window->GetHost()->ConvertPointToNativeScreen(&location_in_native);
106 for (size_t i = 0; i < root_windows.size(); ++i) {
107 aura::WindowTreeHost* host = root_windows[i]->GetHost();
108 const gfx::Rect native_bounds = host->GetBounds();
109 if (native_bounds.Contains(location_in_native)) {
110 *target_root = root_windows[i];
111 *point = location_in_native;
112 host->ConvertPointFromNativeScreen(point);
113 break;
117 #else
118 NOTIMPLEMENTED();
119 #endif
122 void 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());
132 void 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);
142 void ScreenPositionController::ConvertHostPointToScreen(
143 aura::Window* root_window,
144 gfx::Point* point) {
145 aura::Window* root = root_window->GetRootWindow();
146 aura::Window* target_root = nullptr;
147 ConvertHostPointToRelativeToRootWindow(root, Shell::GetAllRootWindows(),
148 point, &target_root);
149 ConvertPointToScreen(target_root, point);
152 void ScreenPositionController::SetBounds(aura::Window* window,
153 const gfx::Rect& bounds,
154 const gfx::Display& display) {
155 DCHECK_NE(-1, display.id());
156 if (!window->parent()->GetProperty(kUsesScreenCoordinatesKey)) {
157 window->SetBounds(bounds);
158 return;
161 // Don't move a window to other root window if:
162 // a) the window is a transient window. It moves when its
163 // transient_parent moves.
164 // b) if the window or its ancestor has kStayInSameRootWindowkey. It's
165 // intentionally kept in the same root window even if the bounds is
166 // outside of the display.
167 if (!::wm::GetTransientParent(window) &&
168 !ShouldStayInSameRootWindow(window)) {
169 aura::Window* dst_root =
170 Shell::GetInstance()->display_controller()->GetRootWindowForDisplayId(
171 display.id());
172 DCHECK(dst_root);
173 aura::Window* dst_container = NULL;
174 if (dst_root != window->GetRootWindow()) {
175 int container_id = window->parent()->id();
176 // All containers that uses screen coordinates must have valid window ids.
177 DCHECK_GE(container_id, 0);
178 // Don't move modal background.
179 if (!SystemModalContainerLayoutManager::IsModalBackground(window))
180 dst_container = Shell::GetContainer(dst_root, container_id);
183 if (dst_container && window->parent() != dst_container) {
184 aura::Window* focused = aura::client::GetFocusClient(window)->
185 GetFocusedWindow();
186 aura::client::ActivationClient* activation_client =
187 aura::client::GetActivationClient(window->GetRootWindow());
188 aura::Window* active = activation_client->GetActiveWindow();
190 aura::WindowTracker tracker;
191 if (focused)
192 tracker.Add(focused);
193 if (active && focused != active)
194 tracker.Add(active);
196 // Set new bounds now so that the container's layout manager
197 // can adjust the bounds if necessary.
198 gfx::Point origin = bounds.origin();
199 const gfx::Point display_origin = display.bounds().origin();
200 origin.Offset(-display_origin.x(), -display_origin.y());
201 gfx::Rect new_bounds = gfx::Rect(origin, bounds.size());
203 window->SetBounds(new_bounds);
205 dst_container->AddChild(window);
207 MoveAllTransientChildrenToNewRoot(display, window);
209 // Restore focused/active window.
210 if (tracker.Contains(focused)) {
211 aura::client::GetFocusClient(window)->FocusWindow(focused);
212 // TODO(beng): replace with GetRootWindow().
213 ash::Shell::GetInstance()->set_target_root_window(
214 focused->GetRootWindow());
215 } else if (tracker.Contains(active)) {
216 activation_client->ActivateWindow(active);
218 // TODO(oshima): We should not have to update the bounds again
219 // below in theory, but we currently do need as there is a code
220 // that assumes that the bounds will never be overridden by the
221 // layout mananger. We should have more explicit control how
222 // constraints are applied by the layout manager.
225 gfx::Point origin(bounds.origin());
226 const gfx::Point display_origin = Shell::GetScreen()->GetDisplayNearestWindow(
227 window).bounds().origin();
228 origin.Offset(-display_origin.x(), -display_origin.y());
229 window->SetBounds(gfx::Rect(origin, bounds.size()));
232 } // namespace ash