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/wm/drag_window_resizer.h"
7 #include "ash/display/mouse_cursor_event_filter.h"
8 #include "ash/screen_ash.h"
10 #include "ash/wm/coordinate_conversion.h"
11 #include "ash/wm/drag_window_controller.h"
12 #include "ash/wm/property_util.h"
13 #include "ui/aura/client/aura_constants.h"
14 #include "ui/aura/env.h"
15 #include "ui/aura/root_window.h"
16 #include "ui/aura/window.h"
17 #include "ui/aura/window_delegate.h"
18 #include "ui/base/hit_test.h"
19 #include "ui/base/ui_base_types.h"
20 #include "ui/gfx/screen.h"
27 // The maximum opacity of the drag phantom window.
28 const float kMaxOpacity
= 0.8f
;
30 // Returns true if Ash has more than one root window.
31 bool HasSecondaryRootWindow() {
32 return Shell::GetAllRootWindows().size() > 1;
35 // When there are two root windows, returns one of the root windows which is not
36 // |root_window|. Returns NULL if only one root window exists.
37 aura::RootWindow
* GetAnotherRootWindow(aura::RootWindow
* root_window
) {
38 Shell::RootWindowList root_windows
= Shell::GetAllRootWindows();
39 if (root_windows
.size() < 2)
41 DCHECK_EQ(2U, root_windows
.size());
42 if (root_windows
[0] == root_window
)
43 return root_windows
[1];
44 return root_windows
[0];
49 DragWindowResizer::~DragWindowResizer() {
50 Shell
* shell
= Shell::GetInstance();
51 shell
->mouse_cursor_filter()->set_mouse_warp_mode(
52 MouseCursorEventFilter::WARP_ALWAYS
);
53 shell
->mouse_cursor_filter()->HideSharedEdgeIndicator();
60 DragWindowResizer
* DragWindowResizer::Create(WindowResizer
* next_window_resizer
,
62 const gfx::Point
& location
,
63 int window_component
) {
64 Details
details(window
, location
, window_component
);
65 return details
.is_resizable
?
66 new DragWindowResizer(next_window_resizer
, details
) : NULL
;
69 void DragWindowResizer::Drag(const gfx::Point
& location
, int event_flags
) {
70 bool destroyed
= false;
71 destroyed_
= &destroyed
;
72 next_window_resizer_
->Drag(location
, event_flags
);
74 // TODO(flackr): Refactor the way WindowResizer calls into other window
75 // resizers to avoid the awkward pattern here for checking if
76 // next_window_resizer_ destroys the resizer object.
80 last_mouse_location_
= location
;
82 // Show a phantom window for dragging in another root window.
83 if (HasSecondaryRootWindow()) {
84 gfx::Point location_in_screen
= location
;
85 wm::ConvertPointToScreen(GetTarget()->parent(), &location_in_screen
);
86 const bool in_original_root
=
87 wm::GetRootWindowAt(location_in_screen
) == GetTarget()->GetRootWindow();
88 UpdateDragWindow(GetTarget()->bounds(), in_original_root
);
90 drag_window_controller_
.reset();
94 void DragWindowResizer::CompleteDrag(int event_flags
) {
95 next_window_resizer_
->CompleteDrag(event_flags
);
97 GetTarget()->layer()->SetOpacity(details_
.initial_opacity
);
98 drag_window_controller_
.reset();
100 // Check if the destination is another display.
101 gfx::Point last_mouse_location_in_screen
= last_mouse_location_
;
102 wm::ConvertPointToScreen(GetTarget()->parent(),
103 &last_mouse_location_in_screen
);
104 gfx::Screen
* screen
= Shell::GetScreen();
105 const gfx::Display dst_display
=
106 screen
->GetDisplayNearestPoint(last_mouse_location_in_screen
);
108 if (dst_display
.id() !=
109 screen
->GetDisplayNearestWindow(GetTarget()->GetRootWindow()).id()) {
110 const gfx::Rect dst_bounds
=
111 ScreenAsh::ConvertRectToScreen(GetTarget()->parent(),
112 GetTarget()->bounds());
113 GetTarget()->SetBoundsInScreen(dst_bounds
, dst_display
);
117 void DragWindowResizer::RevertDrag() {
118 next_window_resizer_
->RevertDrag();
120 drag_window_controller_
.reset();
121 GetTarget()->layer()->SetOpacity(details_
.initial_opacity
);
124 aura::Window
* DragWindowResizer::GetTarget() {
125 return next_window_resizer_
->GetTarget();
128 DragWindowResizer::DragWindowResizer(WindowResizer
* next_window_resizer
,
129 const Details
& details
)
130 : next_window_resizer_(next_window_resizer
),
133 // The pointer should be confined in one display during resizing a window
134 // because the window cannot span two displays at the same time anyway. The
135 // exception is window/tab dragging operation. During that operation,
136 // |mouse_warp_mode_| should be set to WARP_DRAG so that the user could move a
137 // window/tab to another display.
138 MouseCursorEventFilter
* mouse_cursor_filter
=
139 Shell::GetInstance()->mouse_cursor_filter();
140 mouse_cursor_filter
->set_mouse_warp_mode(
141 ShouldAllowMouseWarp() ?
142 MouseCursorEventFilter::WARP_DRAG
: MouseCursorEventFilter::WARP_NONE
);
143 if (ShouldAllowMouseWarp()) {
144 mouse_cursor_filter
->ShowSharedEdgeIndicator(
145 details
.window
->GetRootWindow());
149 void DragWindowResizer::UpdateDragWindow(const gfx::Rect
& bounds
,
150 bool in_original_root
) {
151 if (details_
.window_component
!= HTCAPTION
|| !ShouldAllowMouseWarp())
154 // It's available. Show a phantom window on the display if needed.
155 aura::RootWindow
* another_root
=
156 GetAnotherRootWindow(GetTarget()->GetRootWindow());
157 const gfx::Rect
root_bounds_in_screen(another_root
->GetBoundsInScreen());
158 const gfx::Rect bounds_in_screen
=
159 ScreenAsh::ConvertRectToScreen(GetTarget()->parent(), bounds
);
160 gfx::Rect bounds_in_another_root
=
161 gfx::IntersectRects(root_bounds_in_screen
, bounds_in_screen
);
162 const float fraction_in_another_window
=
163 (bounds_in_another_root
.width() * bounds_in_another_root
.height()) /
164 static_cast<float>(bounds
.width() * bounds
.height());
166 if (fraction_in_another_window
> 0) {
167 if (!drag_window_controller_
) {
168 drag_window_controller_
.reset(
169 new DragWindowController(GetTarget()));
170 // Always show the drag phantom on the |another_root| window.
171 drag_window_controller_
->SetDestinationDisplay(
172 Shell::GetScreen()->GetDisplayMatching(
173 another_root
->GetBoundsInScreen()));
174 drag_window_controller_
->Show();
177 drag_window_controller_
->SetBounds(bounds_in_screen
);
179 const float phantom_opacity
=
180 !in_original_root
? 1 : (kMaxOpacity
* fraction_in_another_window
);
181 const float window_opacity
=
182 in_original_root
? 1 : (kMaxOpacity
* (1 - fraction_in_another_window
));
183 drag_window_controller_
->SetOpacity(phantom_opacity
);
184 GetTarget()->layer()->SetOpacity(window_opacity
);
186 drag_window_controller_
.reset();
187 GetTarget()->layer()->SetOpacity(1.0f
);
191 bool DragWindowResizer::ShouldAllowMouseWarp() {
192 return (details_
.window_component
== HTCAPTION
) &&
193 !GetTarget()->transient_parent() &&
194 (GetTarget()->type() == aura::client::WINDOW_TYPE_NORMAL
||
195 GetTarget()->type() == aura::client::WINDOW_TYPE_PANEL
);
198 } // namespace internal