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