screen_position_controller.cc revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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/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" 24 25namespace ash { 26namespace { 27 28// Move all transient children to |dst_root|, including the ones in 29// the child windows and transient children of the transient children. 30void 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); 44 45 // Transient children may have transient children. 46 MoveAllTransientChildrenToNewRoot(display, transient_child); 47 } 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); 53} 54 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. 60std::pair<aura::RootWindow*, gfx::Point> GetRootWindowRelativeToWindow( 61 aura::Window* window, 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); 66 67#if defined(USE_X11) 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. 84 85 gfx::Point location_in_native(location_in_root); 86 root_window->ConvertPointToNativeScreen(&location_in_native); 87 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); 97 break; 98 } 99 } 100 } 101#else 102 // TODO(yusukes): Support non-X11 platforms if necessary. 103#endif 104 105 return std::make_pair(root_window, location_in_root); 106} 107 108} // namespace 109 110namespace internal { 111 112void ScreenPositionController::ConvertPointToScreen( 113 const aura::Window* window, 114 gfx::Point* point) { 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()); 120} 121 122void ScreenPositionController::ConvertPointFromScreen( 123 const aura::Window* window, 124 gfx::Point* point) { 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); 130} 131 132void ScreenPositionController::ConvertHostPointToScreen( 133 aura::RootWindow* root_window, 134 gfx::Point* point) { 135 root_window->ConvertPointFromHost(point); 136 std::pair<aura::RootWindow*, gfx::Point> pair = 137 GetRootWindowRelativeToWindow(root_window, *point); 138 *point = pair.second; 139 ConvertPointToScreen(pair.first, point); 140} 141 142void ScreenPositionController::SetBounds(aura::Window* window, 143 const gfx::Rect& bounds, 144 const gfx::Display& display) { 145 DCHECK_NE(-1, display.id()); 146 if (!window->parent()->GetProperty(internal::kUsesScreenCoordinatesKey)) { 147 window->SetBounds(bounds); 148 return; 149 } 150 151 // Don't move a window to other root window if: 152 // a) the window is a transient window. It moves when its 153 // transient_parent moves. 154 // b) if the window has kStayInSameRootWindowkey. It's intentionally kept in 155 // the same root window even if the bounds is outside of the display. 156 if (!window->transient_parent() && 157 !window->GetProperty(internal::kStayInSameRootWindowKey)) { 158 aura::RootWindow* dst_root = 159 Shell::GetInstance()->display_controller()->GetRootWindowForDisplayId( 160 display.id()); 161 DCHECK(dst_root); 162 aura::Window* dst_container = NULL; 163 if (dst_root != window->GetRootWindow()) { 164 int container_id = window->parent()->id(); 165 // Dragging a docked window to another root window should show it floating 166 // rather than docked in another screen's dock. 167 if (container_id == kShellWindowId_DockedContainer) 168 container_id = kShellWindowId_WorkspaceContainer; 169 // All containers that uses screen coordinates must have valid window ids. 170 DCHECK_GE(container_id, 0); 171 // Don't move modal background. 172 if (!SystemModalContainerLayoutManager::IsModalBackground(window)) 173 dst_container = Shell::GetContainer(dst_root, container_id); 174 } 175 176 if (dst_container && window->parent() != dst_container) { 177 aura::Window* focused = aura::client::GetFocusClient(window)-> 178 GetFocusedWindow(); 179 aura::client::ActivationClient* activation_client = 180 aura::client::GetActivationClient(window->GetRootWindow()); 181 aura::Window* active = activation_client->GetActiveWindow(); 182 183 aura::WindowTracker tracker; 184 if (focused) 185 tracker.Add(focused); 186 if (active && focused != active) 187 tracker.Add(active); 188 189 if (dst_container->id() == kShellWindowId_WorkspaceContainer) { 190 dst_container = 191 GetRootWindowController(dst_root)->workspace_controller()-> 192 GetParentForNewWindow(window); 193 } 194 195 dst_container->AddChild(window); 196 197 MoveAllTransientChildrenToNewRoot(display, window); 198 199 // Restore focused/active window. 200 if (tracker.Contains(focused)) { 201 aura::client::GetFocusClient(window)->FocusWindow(focused); 202 } else if (tracker.Contains(active)) { 203 activation_client->ActivateWindow(active); 204 } 205 } 206 } 207 208 gfx::Point origin(bounds.origin()); 209 const gfx::Point display_origin = Shell::GetScreen()->GetDisplayNearestWindow( 210 window).bounds().origin(); 211 origin.Offset(-display_origin.x(), -display_origin.y()); 212 window->SetBounds(gfx::Rect(origin, bounds.size())); 213} 214 215} // internal 216} // ash 217