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/wm/drag_window_resizer.h" 6 7#include "ash/display/mouse_cursor_event_filter.h" 8#include "ash/screen_util.h" 9#include "ash/shell.h" 10#include "ash/wm/coordinate_conversion.h" 11#include "ash/wm/drag_window_controller.h" 12#include "ash/wm/window_state.h" 13#include "ash/wm/window_util.h" 14#include "base/memory/weak_ptr.h" 15#include "ui/aura/client/aura_constants.h" 16#include "ui/aura/env.h" 17#include "ui/aura/window.h" 18#include "ui/aura/window_delegate.h" 19#include "ui/aura/window_event_dispatcher.h" 20#include "ui/base/hit_test.h" 21#include "ui/base/ui_base_types.h" 22#include "ui/gfx/screen.h" 23#include "ui/wm/core/coordinate_conversion.h" 24#include "ui/wm/core/window_util.h" 25 26namespace ash { 27namespace { 28 29// The maximum opacity of the drag phantom window. 30const float kMaxOpacity = 0.8f; 31 32// Returns true if Ash has more than one root window. 33bool HasSecondaryRootWindow() { 34 return Shell::GetAllRootWindows().size() > 1; 35} 36 37// When there are two root windows, returns one of the root windows which is not 38// |root_window|. Returns NULL if only one root window exists. 39aura::Window* GetAnotherRootWindow(aura::Window* root_window) { 40 aura::Window::Windows root_windows = Shell::GetAllRootWindows(); 41 if (root_windows.size() < 2) 42 return NULL; 43 DCHECK_EQ(2U, root_windows.size()); 44 if (root_windows[0] == root_window) 45 return root_windows[1]; 46 return root_windows[0]; 47} 48 49} // namespace 50 51// static 52DragWindowResizer* DragWindowResizer::instance_ = NULL; 53 54DragWindowResizer::~DragWindowResizer() { 55 if (window_state_) 56 window_state_->DeleteDragDetails(); 57 Shell* shell = Shell::GetInstance(); 58 shell->mouse_cursor_filter()->set_mouse_warp_mode( 59 MouseCursorEventFilter::WARP_ALWAYS); 60 shell->mouse_cursor_filter()->HideSharedEdgeIndicator(); 61 if (instance_ == this) 62 instance_ = NULL; 63} 64 65// static 66DragWindowResizer* DragWindowResizer::Create( 67 WindowResizer* next_window_resizer, 68 wm::WindowState* window_state) { 69 return new DragWindowResizer(next_window_resizer, window_state); 70} 71 72void DragWindowResizer::Drag(const gfx::Point& location, int event_flags) { 73 base::WeakPtr<DragWindowResizer> resizer(weak_ptr_factory_.GetWeakPtr()); 74 next_window_resizer_->Drag(location, event_flags); 75 76 if (!resizer) 77 return; 78 79 last_mouse_location_ = location; 80 // Show a phantom window for dragging in another root window. 81 if (HasSecondaryRootWindow()) { 82 gfx::Point location_in_screen = location; 83 ::wm::ConvertPointToScreen(GetTarget()->parent(), &location_in_screen); 84 const bool in_original_root = 85 wm::GetRootWindowAt(location_in_screen) == GetTarget()->GetRootWindow(); 86 UpdateDragWindow(GetTarget()->bounds(), in_original_root); 87 } else { 88 drag_window_controller_.reset(); 89 } 90} 91 92void DragWindowResizer::CompleteDrag() { 93 next_window_resizer_->CompleteDrag(); 94 95 GetTarget()->layer()->SetOpacity(details().initial_opacity); 96 drag_window_controller_.reset(); 97 98 // Check if the destination is another display. 99 gfx::Point last_mouse_location_in_screen = last_mouse_location_; 100 ::wm::ConvertPointToScreen(GetTarget()->parent(), 101 &last_mouse_location_in_screen); 102 gfx::Screen* screen = Shell::GetScreen(); 103 const gfx::Display dst_display = 104 screen->GetDisplayNearestPoint(last_mouse_location_in_screen); 105 106 if (dst_display.id() != 107 screen->GetDisplayNearestWindow(GetTarget()->GetRootWindow()).id()) { 108 // Adjust the size and position so that it doesn't exceed the size of 109 // work area. 110 const gfx::Size& size = dst_display.work_area().size(); 111 gfx::Rect bounds = GetTarget()->bounds(); 112 if (bounds.width() > size.width()) { 113 int diff = bounds.width() - size.width(); 114 bounds.set_x(bounds.x() + diff / 2); 115 bounds.set_width(size.width()); 116 } 117 if (bounds.height() > size.height()) 118 bounds.set_height(size.height()); 119 120 gfx::Rect dst_bounds = 121 ScreenUtil::ConvertRectToScreen(GetTarget()->parent(), bounds); 122 123 // Adjust the position so that the cursor is on the window. 124 if (!dst_bounds.Contains(last_mouse_location_in_screen)) { 125 if (last_mouse_location_in_screen.x() < dst_bounds.x()) 126 dst_bounds.set_x(last_mouse_location_in_screen.x()); 127 else if (last_mouse_location_in_screen.x() > dst_bounds.right()) 128 dst_bounds.set_x( 129 last_mouse_location_in_screen.x() - dst_bounds.width()); 130 } 131 ash::wm::AdjustBoundsToEnsureMinimumWindowVisibility( 132 dst_display.bounds(), &dst_bounds); 133 134 GetTarget()->SetBoundsInScreen(dst_bounds, dst_display); 135 } 136} 137 138void DragWindowResizer::RevertDrag() { 139 next_window_resizer_->RevertDrag(); 140 141 drag_window_controller_.reset(); 142 GetTarget()->layer()->SetOpacity(details().initial_opacity); 143} 144 145DragWindowResizer::DragWindowResizer(WindowResizer* next_window_resizer, 146 wm::WindowState* window_state) 147 : WindowResizer(window_state), 148 next_window_resizer_(next_window_resizer), 149 weak_ptr_factory_(this) { 150 // The pointer should be confined in one display during resizing a window 151 // because the window cannot span two displays at the same time anyway. The 152 // exception is window/tab dragging operation. During that operation, 153 // |mouse_warp_mode_| should be set to WARP_DRAG so that the user could move a 154 // window/tab to another display. 155 MouseCursorEventFilter* mouse_cursor_filter = 156 Shell::GetInstance()->mouse_cursor_filter(); 157 mouse_cursor_filter->set_mouse_warp_mode( 158 ShouldAllowMouseWarp() ? 159 MouseCursorEventFilter::WARP_DRAG : MouseCursorEventFilter::WARP_NONE); 160 if (ShouldAllowMouseWarp()) 161 mouse_cursor_filter->ShowSharedEdgeIndicator(GetTarget()->GetRootWindow()); 162 instance_ = this; 163} 164 165void DragWindowResizer::UpdateDragWindow(const gfx::Rect& bounds, 166 bool in_original_root) { 167 if (details().window_component != HTCAPTION || !ShouldAllowMouseWarp()) 168 return; 169 170 // It's available. Show a phantom window on the display if needed. 171 aura::Window* another_root = 172 GetAnotherRootWindow(GetTarget()->GetRootWindow()); 173 const gfx::Rect root_bounds_in_screen(another_root->GetBoundsInScreen()); 174 const gfx::Rect bounds_in_screen = 175 ScreenUtil::ConvertRectToScreen(GetTarget()->parent(), bounds); 176 gfx::Rect bounds_in_another_root = 177 gfx::IntersectRects(root_bounds_in_screen, bounds_in_screen); 178 const float fraction_in_another_window = 179 (bounds_in_another_root.width() * bounds_in_another_root.height()) / 180 static_cast<float>(bounds.width() * bounds.height()); 181 182 if (fraction_in_another_window > 0) { 183 if (!drag_window_controller_) { 184 drag_window_controller_.reset( 185 new DragWindowController(GetTarget())); 186 // Always show the drag phantom on the |another_root| window. 187 drag_window_controller_->SetDestinationDisplay( 188 Shell::GetScreen()->GetDisplayNearestWindow(another_root)); 189 drag_window_controller_->Show(); 190 } else { 191 // No animation. 192 drag_window_controller_->SetBounds(bounds_in_screen); 193 } 194 const float phantom_opacity = 195 !in_original_root ? 1 : (kMaxOpacity * fraction_in_another_window); 196 const float window_opacity = 197 in_original_root ? 1 : (kMaxOpacity * (1 - fraction_in_another_window)); 198 drag_window_controller_->SetOpacity(phantom_opacity); 199 GetTarget()->layer()->SetOpacity(window_opacity); 200 } else { 201 drag_window_controller_.reset(); 202 GetTarget()->layer()->SetOpacity(1.0f); 203 } 204} 205 206bool DragWindowResizer::ShouldAllowMouseWarp() { 207 return (details().window_component == HTCAPTION) && 208 !::wm::GetTransientParent(GetTarget()) && 209 (GetTarget()->type() == ui::wm::WINDOW_TYPE_NORMAL || 210 GetTarget()->type() == ui::wm::WINDOW_TYPE_PANEL); 211} 212 213} // namespace ash 214