Put WeakPtrFactory member last in USBEventRouter
[chromium-blink-merge.git] / ash / wm / drag_window_resizer.cc
blob1cb72e26ff1389817c3cae86ba2f76a69cf1e285
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_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"
26 namespace ash {
27 namespace {
29 // The maximum opacity of the drag phantom window.
30 const float kMaxOpacity = 0.8f;
32 // Returns true if Ash has more than one root window.
33 bool HasSecondaryRootWindows() {
34 return Shell::GetAllRootWindows().size() > 1;
37 // When there are more than one root windows, returns all root windows which are
38 // not |root_window|. Returns an empty vector if only one root window exists.
39 aura::Window::Windows GetOtherRootWindows(aura::Window* root_window) {
40 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
41 aura::Window::Windows other_root_windows;
42 if (root_windows.size() < 2)
43 return other_root_windows;
44 for (size_t i = 0; i < root_windows.size(); ++i) {
45 if (root_windows[i] != root_window)
46 other_root_windows.push_back(root_windows[i]);
48 return other_root_windows;
51 } // namespace
53 // static
54 DragWindowResizer* DragWindowResizer::instance_ = NULL;
56 DragWindowResizer::~DragWindowResizer() {
57 if (window_state_)
58 window_state_->DeleteDragDetails();
59 Shell* shell = Shell::GetInstance();
60 shell->mouse_cursor_filter()->set_mouse_warp_mode(
61 MouseCursorEventFilter::WARP_ALWAYS);
62 shell->mouse_cursor_filter()->HideSharedEdgeIndicator();
63 if (instance_ == this)
64 instance_ = NULL;
67 // static
68 DragWindowResizer* DragWindowResizer::Create(
69 WindowResizer* next_window_resizer,
70 wm::WindowState* window_state) {
71 return new DragWindowResizer(next_window_resizer, window_state);
74 void DragWindowResizer::Drag(const gfx::Point& location, int event_flags) {
75 base::WeakPtr<DragWindowResizer> resizer(weak_ptr_factory_.GetWeakPtr());
76 next_window_resizer_->Drag(location, event_flags);
78 if (!resizer)
79 return;
81 last_mouse_location_ = location;
82 // Show a phantom window for dragging in another root window.
83 if (HasSecondaryRootWindows()) {
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);
89 } else {
90 drag_window_controllers_.clear();
94 void DragWindowResizer::CompleteDrag() {
95 next_window_resizer_->CompleteDrag();
97 GetTarget()->layer()->SetOpacity(details().initial_opacity);
98 drag_window_controllers_.clear();
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 // Adjust the size and position so that it doesn't exceed the size of
111 // work area.
112 const gfx::Size& size = dst_display.work_area().size();
113 gfx::Rect bounds = GetTarget()->bounds();
114 if (bounds.width() > size.width()) {
115 int diff = bounds.width() - size.width();
116 bounds.set_x(bounds.x() + diff / 2);
117 bounds.set_width(size.width());
119 if (bounds.height() > size.height())
120 bounds.set_height(size.height());
122 gfx::Rect dst_bounds =
123 ScreenUtil::ConvertRectToScreen(GetTarget()->parent(), bounds);
125 // Adjust the position so that the cursor is on the window.
126 if (!dst_bounds.Contains(last_mouse_location_in_screen)) {
127 if (last_mouse_location_in_screen.x() < dst_bounds.x())
128 dst_bounds.set_x(last_mouse_location_in_screen.x());
129 else if (last_mouse_location_in_screen.x() > dst_bounds.right())
130 dst_bounds.set_x(
131 last_mouse_location_in_screen.x() - dst_bounds.width());
133 ash::wm::AdjustBoundsToEnsureMinimumWindowVisibility(
134 dst_display.bounds(), &dst_bounds);
136 GetTarget()->SetBoundsInScreen(dst_bounds, dst_display);
140 void DragWindowResizer::RevertDrag() {
141 next_window_resizer_->RevertDrag();
143 drag_window_controllers_.clear();
144 GetTarget()->layer()->SetOpacity(details().initial_opacity);
147 DragWindowResizer::DragWindowResizer(WindowResizer* next_window_resizer,
148 wm::WindowState* window_state)
149 : WindowResizer(window_state),
150 next_window_resizer_(next_window_resizer),
151 weak_ptr_factory_(this) {
152 // The pointer should be confined in one display during resizing a window
153 // because the window cannot span two displays at the same time anyway. The
154 // exception is window/tab dragging operation. During that operation,
155 // |mouse_warp_mode_| should be set to WARP_DRAG so that the user could move a
156 // window/tab to another display.
157 MouseCursorEventFilter* mouse_cursor_filter =
158 Shell::GetInstance()->mouse_cursor_filter();
159 mouse_cursor_filter->set_mouse_warp_mode(
160 ShouldAllowMouseWarp() ?
161 MouseCursorEventFilter::WARP_DRAG : MouseCursorEventFilter::WARP_NONE);
162 if (ShouldAllowMouseWarp())
163 mouse_cursor_filter->ShowSharedEdgeIndicator(GetTarget()->GetRootWindow());
164 instance_ = this;
167 void DragWindowResizer::UpdateDragWindow(const gfx::Rect& bounds,
168 bool in_original_root) {
169 if (details().window_component != HTCAPTION || !ShouldAllowMouseWarp())
170 return;
172 // It's available. Show a phantom window on the display if needed.
173 aura::Window::Windows other_roots =
174 GetOtherRootWindows(GetTarget()->GetRootWindow());
175 size_t drag_window_controller_count = 0;
176 for (size_t i = 0; i < other_roots.size(); ++i) {
177 aura::Window* another_root = other_roots[i];
178 const gfx::Rect root_bounds_in_screen(another_root->GetBoundsInScreen());
179 const gfx::Rect bounds_in_screen =
180 ScreenUtil::ConvertRectToScreen(GetTarget()->parent(), bounds);
181 gfx::Rect bounds_in_another_root =
182 gfx::IntersectRects(root_bounds_in_screen, bounds_in_screen);
183 const float fraction_in_another_window =
184 (bounds_in_another_root.width() * bounds_in_another_root.height()) /
185 static_cast<float>(bounds.width() * bounds.height());
187 if (fraction_in_another_window > 0) {
188 if (drag_window_controllers_.size() < ++drag_window_controller_count)
189 drag_window_controllers_.resize(drag_window_controller_count);
190 ScopedVector<DragWindowController>::reference drag_window_controller =
191 drag_window_controllers_.back();
192 if (!drag_window_controller) {
193 drag_window_controller = new DragWindowController(GetTarget());
194 // Always show the drag phantom on the |another_root| window.
195 drag_window_controller->SetDestinationDisplay(
196 Shell::GetScreen()->GetDisplayNearestWindow(another_root));
197 drag_window_controller->Show();
198 } else {
199 // No animation.
200 drag_window_controller->SetBounds(bounds_in_screen);
202 const float phantom_opacity =
203 !in_original_root ? 1 : (kMaxOpacity * fraction_in_another_window);
204 const float window_opacity =
205 in_original_root ? 1
206 : (kMaxOpacity * (1 - fraction_in_another_window));
207 drag_window_controller->SetOpacity(phantom_opacity);
208 GetTarget()->layer()->SetOpacity(window_opacity);
209 } else {
210 GetTarget()->layer()->SetOpacity(1.0f);
214 // If we have more drag window controllers allocated than needed, release the
215 // excess controllers by shrinking the vector |drag_window_controller_|.
216 DCHECK_GE(drag_window_controllers_.size(), drag_window_controller_count);
217 if (drag_window_controllers_.size() > drag_window_controller_count)
218 drag_window_controllers_.resize(drag_window_controller_count);
221 bool DragWindowResizer::ShouldAllowMouseWarp() {
222 return (details().window_component == HTCAPTION) &&
223 !::wm::GetTransientParent(GetTarget()) &&
224 (GetTarget()->type() == ui::wm::WINDOW_TYPE_NORMAL ||
225 GetTarget()->type() == ui::wm::WINDOW_TYPE_PANEL);
228 } // namespace ash