Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / ash / wm / drag_window_resizer.cc
blobf6a714c28b0cd93267fe9c2c0661cbb84b08ef35
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_enabled(true);
61 shell->mouse_cursor_filter()->HideSharedEdgeIndicator();
62 if (instance_ == this)
63 instance_ = NULL;
66 // static
67 DragWindowResizer* DragWindowResizer::Create(
68 WindowResizer* next_window_resizer,
69 wm::WindowState* window_state) {
70 return new DragWindowResizer(next_window_resizer, window_state);
73 void DragWindowResizer::Drag(const gfx::Point& location, int event_flags) {
74 base::WeakPtr<DragWindowResizer> resizer(weak_ptr_factory_.GetWeakPtr());
75 next_window_resizer_->Drag(location, event_flags);
77 if (!resizer)
78 return;
80 last_mouse_location_ = location;
81 // Show a phantom window for dragging in another root window.
82 if (HasSecondaryRootWindows()) {
83 gfx::Point location_in_screen = location;
84 ::wm::ConvertPointToScreen(GetTarget()->parent(), &location_in_screen);
85 const bool in_original_root =
86 wm::GetRootWindowAt(location_in_screen) == GetTarget()->GetRootWindow();
87 UpdateDragWindow(GetTarget()->bounds(), in_original_root);
88 } else {
89 drag_window_controllers_.clear();
93 void DragWindowResizer::CompleteDrag() {
94 next_window_resizer_->CompleteDrag();
96 GetTarget()->layer()->SetOpacity(details().initial_opacity);
97 drag_window_controllers_.clear();
99 // Check if the destination is another display.
100 gfx::Point last_mouse_location_in_screen = last_mouse_location_;
101 ::wm::ConvertPointToScreen(GetTarget()->parent(),
102 &last_mouse_location_in_screen);
103 gfx::Screen* screen = Shell::GetScreen();
104 const gfx::Display dst_display =
105 screen->GetDisplayNearestPoint(last_mouse_location_in_screen);
107 if (dst_display.id() !=
108 screen->GetDisplayNearestWindow(GetTarget()->GetRootWindow()).id()) {
109 // Adjust the size and position so that it doesn't exceed the size of
110 // work area.
111 const gfx::Size& size = dst_display.work_area().size();
112 gfx::Rect bounds = GetTarget()->bounds();
113 if (bounds.width() > size.width()) {
114 int diff = bounds.width() - size.width();
115 bounds.set_x(bounds.x() + diff / 2);
116 bounds.set_width(size.width());
118 if (bounds.height() > size.height())
119 bounds.set_height(size.height());
121 gfx::Rect dst_bounds =
122 ScreenUtil::ConvertRectToScreen(GetTarget()->parent(), bounds);
124 // Adjust the position so that the cursor is on the window.
125 if (!dst_bounds.Contains(last_mouse_location_in_screen)) {
126 if (last_mouse_location_in_screen.x() < dst_bounds.x())
127 dst_bounds.set_x(last_mouse_location_in_screen.x());
128 else if (last_mouse_location_in_screen.x() > dst_bounds.right())
129 dst_bounds.set_x(
130 last_mouse_location_in_screen.x() - dst_bounds.width());
132 ash::wm::AdjustBoundsToEnsureMinimumWindowVisibility(
133 dst_display.bounds(), &dst_bounds);
135 GetTarget()->SetBoundsInScreen(dst_bounds, dst_display);
139 void DragWindowResizer::RevertDrag() {
140 next_window_resizer_->RevertDrag();
142 drag_window_controllers_.clear();
143 GetTarget()->layer()->SetOpacity(details().initial_opacity);
146 DragWindowResizer::DragWindowResizer(WindowResizer* next_window_resizer,
147 wm::WindowState* window_state)
148 : WindowResizer(window_state),
149 next_window_resizer_(next_window_resizer),
150 weak_ptr_factory_(this) {
151 // The pointer should be confined in one display during resizing a window
152 // because the window cannot span two displays at the same time anyway. The
153 // exception is window/tab dragging operation. During that operation,
154 // |mouse_warp_mode_| should be set to WARP_DRAG so that the user could move a
155 // window/tab to another display.
156 MouseCursorEventFilter* mouse_cursor_filter =
157 Shell::GetInstance()->mouse_cursor_filter();
158 mouse_cursor_filter->set_mouse_warp_enabled(ShouldAllowMouseWarp());
159 if (ShouldAllowMouseWarp())
160 mouse_cursor_filter->ShowSharedEdgeIndicator(GetTarget()->GetRootWindow());
161 instance_ = this;
164 void DragWindowResizer::UpdateDragWindow(const gfx::Rect& bounds,
165 bool in_original_root) {
166 if (details().window_component != HTCAPTION || !ShouldAllowMouseWarp())
167 return;
169 // It's available. Show a phantom window on the display if needed.
170 aura::Window::Windows other_roots =
171 GetOtherRootWindows(GetTarget()->GetRootWindow());
172 size_t drag_window_controller_count = 0;
173 for (size_t i = 0; i < other_roots.size(); ++i) {
174 aura::Window* another_root = other_roots[i];
175 const gfx::Rect root_bounds_in_screen(another_root->GetBoundsInScreen());
176 const gfx::Rect bounds_in_screen =
177 ScreenUtil::ConvertRectToScreen(GetTarget()->parent(), bounds);
178 gfx::Rect bounds_in_another_root =
179 gfx::IntersectRects(root_bounds_in_screen, bounds_in_screen);
180 const float fraction_in_another_window =
181 (bounds_in_another_root.width() * bounds_in_another_root.height()) /
182 static_cast<float>(bounds.width() * bounds.height());
184 if (fraction_in_another_window > 0) {
185 if (drag_window_controllers_.size() < ++drag_window_controller_count)
186 drag_window_controllers_.resize(drag_window_controller_count);
187 ScopedVector<DragWindowController>::reference drag_window_controller =
188 drag_window_controllers_.back();
189 if (!drag_window_controller) {
190 drag_window_controller = new DragWindowController(GetTarget());
191 // Always show the drag phantom on the |another_root| window.
192 drag_window_controller->SetDestinationDisplay(
193 Shell::GetScreen()->GetDisplayNearestWindow(another_root));
194 drag_window_controller->Show();
195 } else {
196 // No animation.
197 drag_window_controller->SetBounds(bounds_in_screen);
199 const float phantom_opacity =
200 !in_original_root ? 1 : (kMaxOpacity * fraction_in_another_window);
201 const float window_opacity =
202 in_original_root ? 1
203 : (kMaxOpacity * (1 - fraction_in_another_window));
204 drag_window_controller->SetOpacity(phantom_opacity);
205 GetTarget()->layer()->SetOpacity(window_opacity);
206 } else {
207 GetTarget()->layer()->SetOpacity(1.0f);
211 // If we have more drag window controllers allocated than needed, release the
212 // excess controllers by shrinking the vector |drag_window_controller_|.
213 DCHECK_GE(drag_window_controllers_.size(), drag_window_controller_count);
214 if (drag_window_controllers_.size() > drag_window_controller_count)
215 drag_window_controllers_.resize(drag_window_controller_count);
218 bool DragWindowResizer::ShouldAllowMouseWarp() {
219 return details().window_component == HTCAPTION &&
220 !::wm::GetTransientParent(GetTarget()) &&
221 wm::IsWindowUserPositionable(GetTarget());
224 } // namespace ash