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