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/workspace_controller.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/client/stacking_client.h"
19 #include "ui/aura/root_window.h"
20 #include "ui/aura/window_tracker.h"
21 #include "ui/compositor/dip_util.h"
22 #include "ui/gfx/display.h"
23 #include "ui/gfx/screen.h"
28 // Move all transient children to |dst_root|, including the ones in
29 // the child windows and transient children of the transient children.
30 void MoveAllTransientChildrenToNewRoot(const gfx::Display
& display
,
31 aura::Window
* window
) {
32 aura::RootWindow
* dst_root
= Shell::GetInstance()->display_controller()->
33 GetRootWindowForDisplayId(display
.id());
34 aura::Window::Windows transient_children
= window
->transient_children();
35 for (aura::Window::Windows::iterator iter
= transient_children
.begin();
36 iter
!= transient_children
.end(); ++iter
) {
37 aura::Window
* transient_child
= *iter
;
38 int container_id
= transient_child
->parent()->id();
39 DCHECK_GE(container_id
, 0);
40 aura::Window
* container
= Shell::GetContainer(dst_root
, container_id
);
41 gfx::Rect parent_bounds_in_screen
= transient_child
->GetBoundsInScreen();
42 container
->AddChild(transient_child
);
43 transient_child
->SetBoundsInScreen(parent_bounds_in_screen
, display
);
45 // Transient children may have transient children.
46 MoveAllTransientChildrenToNewRoot(display
, transient_child
);
48 // Move transient children of the child windows if any.
49 aura::Window::Windows children
= window
->children();
50 for (aura::Window::Windows::iterator iter
= children
.begin();
51 iter
!= children
.end(); ++iter
)
52 MoveAllTransientChildrenToNewRoot(display
, *iter
);
55 // Finds the root window at |location| in |window|'s coordinates and returns a
56 // pair of root window and location in that root window's coordinates. The
57 // function usually returns |window->GetRootWindow()|, but if the mouse pointer
58 // is moved outside the |window|'s root while the mouse is captured, it returns
59 // the other root window.
60 std::pair
<aura::RootWindow
*, gfx::Point
> GetRootWindowRelativeToWindow(
62 const gfx::Point
& location
) {
63 aura::RootWindow
* root_window
= window
->GetRootWindow();
64 gfx::Point
location_in_root(location
);
65 aura::Window::ConvertPointToTarget(window
, root_window
, &location_in_root
);
68 if (!root_window
->ContainsPointInRoot(location_in_root
)) {
69 // This conversion is necessary to deal with X's passive input
70 // grab while dragging window. For example, if we have two
71 // displays, say 1000x1000 (primary) and 500x500 (extended one
72 // on the right), and start dragging a window at (999, 123), and
73 // then move the pointer to the right, the pointer suddenly
74 // warps to the extended display. The destination is (0, 123) in
75 // the secondary root window's coordinates, or (1000, 123) in
76 // the screen coordinates. However, since the mouse is captured
77 // by X during drag, a weird LocatedEvent, something like (0, 1123)
78 // in the *primary* root window's coordinates, is sent to Chrome
79 // (Remember that in the native X11 world, the two root windows
80 // are always stacked vertically regardless of the display
81 // layout in Ash). We need to figure out that (0, 1123) in the
82 // primary root window's coordinates is actually (0, 123) in the
83 // extended root window's coordinates.
85 gfx::Point
location_in_native(location_in_root
);
86 root_window
->ConvertPointToNativeScreen(&location_in_native
);
88 Shell::RootWindowList root_windows
= Shell::GetAllRootWindows();
89 for (size_t i
= 0; i
< root_windows
.size(); ++i
) {
90 const gfx::Rect
native_bounds(
91 root_windows
[i
]->GetHostOrigin(),
92 root_windows
[i
]->GetHostSize()); // in px.
93 if (native_bounds
.Contains(location_in_native
)) {
94 root_window
= root_windows
[i
];
95 location_in_root
= location_in_native
;
96 root_window
->ConvertPointFromNativeScreen(&location_in_root
);
102 // TODO(yusukes): Support non-X11 platforms if necessary.
105 return std::make_pair(root_window
, location_in_root
);
112 void ScreenPositionController::ConvertPointToScreen(
113 const aura::Window
* window
,
115 const aura::RootWindow
* root
= window
->GetRootWindow();
116 aura::Window::ConvertPointToTarget(window
, root
, point
);
117 const gfx::Point display_origin
= Shell::GetScreen()->GetDisplayNearestWindow(
118 const_cast<aura::RootWindow
*>(root
)).bounds().origin();
119 point
->Offset(display_origin
.x(), display_origin
.y());
122 void ScreenPositionController::ConvertPointFromScreen(
123 const aura::Window
* window
,
125 const aura::RootWindow
* root
= window
->GetRootWindow();
126 const gfx::Point display_origin
= Shell::GetScreen()->GetDisplayNearestWindow(
127 const_cast<aura::RootWindow
*>(root
)).bounds().origin();
128 point
->Offset(-display_origin
.x(), -display_origin
.y());
129 aura::Window::ConvertPointToTarget(root
, window
, point
);
132 void ScreenPositionController::ConvertNativePointToScreen(
133 aura::Window
* window
,
135 std::pair
<aura::RootWindow
*, gfx::Point
> pair
=
136 GetRootWindowRelativeToWindow(
138 ui::ConvertPointToDIP(window
->GetRootWindow()->layer(), *point
));
139 *point
= pair
.second
;
140 ConvertPointToScreen(pair
.first
, point
);
143 void ScreenPositionController::SetBounds(aura::Window
* window
,
144 const gfx::Rect
& bounds
,
145 const gfx::Display
& display
) {
146 DCHECK_NE(-1, display
.id());
147 if (!window
->parent()->GetProperty(internal::kUsesScreenCoordinatesKey
)) {
148 window
->SetBounds(bounds
);
152 // Don't move a window to other root window if:
153 // a) the window is a transient window. It moves when its
154 // transient_parent moves.
155 // b) if the window has kStayInSameRootWindowkey. It's intentionally kept in
156 // the same root window even if the bounds is outside of the display.
157 if (!window
->transient_parent() &&
158 !window
->GetProperty(internal::kStayInSameRootWindowKey
)) {
159 aura::RootWindow
* dst_root
=
160 Shell::GetInstance()->display_controller()->GetRootWindowForDisplayId(
163 aura::Window
* dst_container
= NULL
;
164 if (dst_root
!= window
->GetRootWindow()) {
165 int container_id
= window
->parent()->id();
166 // All containers that uses screen coordinates must have valid window ids.
167 DCHECK_GE(container_id
, 0);
168 // Don't move modal background.
169 if (!SystemModalContainerLayoutManager::IsModalBackground(window
))
170 dst_container
= Shell::GetContainer(dst_root
, container_id
);
173 if (dst_container
&& window
->parent() != dst_container
) {
174 aura::Window
* focused
= aura::client::GetFocusClient(window
)->
176 aura::client::ActivationClient
* activation_client
=
177 aura::client::GetActivationClient(window
->GetRootWindow());
178 aura::Window
* active
= activation_client
->GetActiveWindow();
180 aura::WindowTracker tracker
;
182 tracker
.Add(focused
);
183 if (active
&& focused
!= active
)
186 if (dst_container
->id() == kShellWindowId_WorkspaceContainer
) {
188 GetRootWindowController(dst_root
)->workspace_controller()->
189 GetParentForNewWindow(window
);
192 dst_container
->AddChild(window
);
194 MoveAllTransientChildrenToNewRoot(display
, window
);
196 // Restore focused/active window.
197 if (tracker
.Contains(focused
)) {
198 aura::client::GetFocusClient(window
)->FocusWindow(focused
, NULL
);
199 } else if (tracker
.Contains(active
)) {
200 activation_client
->ActivateWindow(active
);
205 gfx::Point
origin(bounds
.origin());
206 const gfx::Point display_origin
= Shell::GetScreen()->GetDisplayNearestWindow(
207 window
).bounds().origin();
208 origin
.Offset(-display_origin
.x(), -display_origin
.y());
209 window
->SetBounds(gfx::Rect(origin
, bounds
.size()));