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