Disable TabDragController tests that fail with a real compositor.
[chromium-blink-merge.git] / ash / wm / window_resizer.cc
blob5569646fb1f9bff18e15f5edd1b2770cb84af01b
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/window_resizer.h"
7 #include "ash/screen_util.h"
8 #include "ash/shell.h"
9 #include "ash/shell_window_ids.h"
10 #include "ash/wm/coordinate_conversion.h"
11 #include "ash/wm/dock/docked_window_layout_manager.h"
12 #include "ash/wm/window_state.h"
13 #include "ash/wm/window_util.h"
14 #include "ui/aura/client/aura_constants.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/compositor/scoped_layer_animation_settings.h"
21 #include "ui/gfx/display.h"
22 #include "ui/gfx/screen.h"
24 namespace ash {
26 namespace {
28 // Returns true for resize components along the right edge, where a drag in
29 // positive x will make the window larger.
30 bool IsRightEdge(int window_component) {
31 return window_component == HTTOPRIGHT ||
32 window_component == HTRIGHT ||
33 window_component == HTBOTTOMRIGHT ||
34 window_component == HTGROWBOX;
37 } // namespace
39 // static
40 const int WindowResizer::kBoundsChange_None = 0;
41 // static
42 const int WindowResizer::kBoundsChange_Repositions = 1;
43 // static
44 const int WindowResizer::kBoundsChange_Resizes = 2;
46 // static
47 const int WindowResizer::kBoundsChangeDirection_None = 0;
48 // static
49 const int WindowResizer::kBoundsChangeDirection_Horizontal = 1;
50 // static
51 const int WindowResizer::kBoundsChangeDirection_Vertical = 2;
53 WindowResizer::WindowResizer() {
56 WindowResizer::WindowResizer(wm::WindowState* window_state)
57 : window_state_(window_state) {
58 DCHECK(window_state_->drag_details());
61 WindowResizer::~WindowResizer() {
64 // static
65 int WindowResizer::GetBoundsChangeForWindowComponent(int component) {
66 int bounds_change = WindowResizer::kBoundsChange_None;
67 switch (component) {
68 case HTTOPLEFT:
69 case HTTOP:
70 case HTTOPRIGHT:
71 case HTLEFT:
72 case HTBOTTOMLEFT:
73 bounds_change |= WindowResizer::kBoundsChange_Repositions |
74 WindowResizer::kBoundsChange_Resizes;
75 break;
76 case HTCAPTION:
77 bounds_change |= WindowResizer::kBoundsChange_Repositions;
78 break;
79 case HTRIGHT:
80 case HTBOTTOMRIGHT:
81 case HTBOTTOM:
82 case HTGROWBOX:
83 bounds_change |= WindowResizer::kBoundsChange_Resizes;
84 break;
85 default:
86 break;
88 return bounds_change;
91 //static
92 int WindowResizer::GetPositionChangeDirectionForWindowComponent(
93 int window_component) {
94 int pos_change_direction = WindowResizer::kBoundsChangeDirection_None;
95 switch (window_component) {
96 case HTTOPLEFT:
97 case HTBOTTOMRIGHT:
98 case HTGROWBOX:
99 case HTCAPTION:
100 pos_change_direction |=
101 WindowResizer::kBoundsChangeDirection_Horizontal |
102 WindowResizer::kBoundsChangeDirection_Vertical;
103 break;
104 case HTTOP:
105 case HTTOPRIGHT:
106 case HTBOTTOM:
107 pos_change_direction |= WindowResizer::kBoundsChangeDirection_Vertical;
108 break;
109 case HTBOTTOMLEFT:
110 case HTRIGHT:
111 case HTLEFT:
112 pos_change_direction |= WindowResizer::kBoundsChangeDirection_Horizontal;
113 break;
114 default:
115 break;
117 return pos_change_direction;
120 gfx::Rect WindowResizer::CalculateBoundsForDrag(
121 const gfx::Point& passed_location) {
122 if (!details().is_resizable)
123 return details().initial_bounds_in_parent;
125 gfx::Point location = passed_location;
126 int delta_x = location.x() - details().initial_location_in_parent.x();
127 int delta_y = location.y() - details().initial_location_in_parent.y();
129 AdjustDeltaForTouchResize(&delta_x, &delta_y);
131 // The minimize size constraint may limit how much we change the window
132 // position. For example, dragging the left edge to the right should stop
133 // repositioning the window when the minimize size is reached.
134 gfx::Size size = GetSizeForDrag(&delta_x, &delta_y);
135 gfx::Point origin = GetOriginForDrag(delta_x, delta_y);
136 gfx::Rect new_bounds(origin, size);
138 // Sizing has to keep the result on the screen. Note that this correction
139 // has to come first since it might have an impact on the origin as well as
140 // on the size.
141 if (details().bounds_change & kBoundsChange_Resizes) {
142 gfx::Rect work_area =
143 Shell::GetScreen()->GetDisplayNearestWindow(GetTarget()).work_area();
144 aura::Window* dock_container = Shell::GetContainer(
145 GetTarget()->GetRootWindow(),
146 internal::kShellWindowId_DockedContainer);
147 internal::DockedWindowLayoutManager* dock_layout =
148 static_cast<internal::DockedWindowLayoutManager*>(
149 dock_container->layout_manager());
151 work_area.Union(dock_layout->docked_bounds());
152 work_area = ScreenUtil::ConvertRectFromScreen(GetTarget()->parent(),
153 work_area);
154 if (details().size_change_direction & kBoundsChangeDirection_Horizontal) {
155 if (IsRightEdge(details().window_component) &&
156 new_bounds.right() < work_area.x() + kMinimumOnScreenArea) {
157 int delta = work_area.x() + kMinimumOnScreenArea - new_bounds.right();
158 new_bounds.set_width(new_bounds.width() + delta);
159 } else if (new_bounds.x() > work_area.right() - kMinimumOnScreenArea) {
160 int width = new_bounds.right() - work_area.right() +
161 kMinimumOnScreenArea;
162 new_bounds.set_x(work_area.right() - kMinimumOnScreenArea);
163 new_bounds.set_width(width);
166 if (details().size_change_direction & kBoundsChangeDirection_Vertical) {
167 if (!IsBottomEdge(details().window_component) &&
168 new_bounds.y() > work_area.bottom() - kMinimumOnScreenArea) {
169 int height = new_bounds.bottom() - work_area.bottom() +
170 kMinimumOnScreenArea;
171 new_bounds.set_y(work_area.bottom() - kMinimumOnScreenArea);
172 new_bounds.set_height(height);
173 } else if (details().window_component == HTBOTTOM ||
174 details().window_component == HTBOTTOMRIGHT ||
175 details().window_component == HTBOTTOMLEFT) {
176 // Update bottom edge to stay in the work area when we are resizing
177 // by dragging the bottom edge or corners.
178 if (new_bounds.bottom() > work_area.bottom())
179 new_bounds.Inset(0, 0, 0,
180 new_bounds.bottom() - work_area.bottom());
183 if (details().bounds_change & kBoundsChange_Repositions &&
184 new_bounds.y() < 0) {
185 int delta = new_bounds.y();
186 new_bounds.set_y(0);
187 new_bounds.set_height(new_bounds.height() + delta);
191 if (details().bounds_change & kBoundsChange_Repositions) {
192 // When we might want to reposition a window which is also restored to its
193 // previous size, to keep the cursor within the dragged window.
194 if (!details().restore_bounds.IsEmpty()) {
195 // However - it is not desirable to change the origin if the window would
196 // be still hit by the cursor.
197 if (details().initial_location_in_parent.x() >
198 details().initial_bounds_in_parent.x() +
199 details().restore_bounds.width())
200 new_bounds.set_x(location.x() - details().restore_bounds.width() / 2);
203 // Make sure that |new_bounds| doesn't leave any of the displays. Note that
204 // the |work_area| above isn't good for this check since it is the work area
205 // for the current display but the window can move to a different one.
206 aura::Window* parent = GetTarget()->parent();
207 gfx::Point passed_location_in_screen(passed_location);
208 wm::ConvertPointToScreen(parent, &passed_location_in_screen);
209 gfx::Rect near_passed_location(passed_location_in_screen, gfx::Size());
210 // Use a pointer location (matching the logic in DragWindowResizer) to
211 // calculate the target display after the drag.
212 const gfx::Display& display =
213 Shell::GetScreen()->GetDisplayMatching(near_passed_location);
214 aura::Window* dock_container = Shell::GetContainer(
215 wm::GetRootWindowMatching(near_passed_location),
216 internal::kShellWindowId_DockedContainer);
217 internal::DockedWindowLayoutManager* dock_layout =
218 static_cast<internal::DockedWindowLayoutManager*>(
219 dock_container->layout_manager());
221 gfx::Rect screen_work_area = display.work_area();
222 screen_work_area.Union(dock_layout->docked_bounds());
223 screen_work_area.Inset(kMinimumOnScreenArea, 0);
224 gfx::Rect new_bounds_in_screen =
225 ScreenUtil::ConvertRectToScreen(parent, new_bounds);
226 if (!screen_work_area.Intersects(new_bounds_in_screen)) {
227 // Make sure that the x origin does not leave the current display.
228 new_bounds_in_screen.set_x(
229 std::max(screen_work_area.x() - new_bounds.width(),
230 std::min(screen_work_area.right(),
231 new_bounds_in_screen.x())));
232 new_bounds =
233 ScreenUtil::ConvertRectFromScreen(parent, new_bounds_in_screen);
237 return new_bounds;
240 // static
241 bool WindowResizer::IsBottomEdge(int window_component) {
242 return window_component == HTBOTTOMLEFT ||
243 window_component == HTBOTTOM ||
244 window_component == HTBOTTOMRIGHT ||
245 window_component == HTGROWBOX;
248 void WindowResizer::AdjustDeltaForTouchResize(int* delta_x, int* delta_y) {
249 if (details().source != aura::client::WINDOW_MOVE_SOURCE_TOUCH ||
250 !(details().bounds_change & kBoundsChange_Resizes))
251 return;
253 if (details().size_change_direction & kBoundsChangeDirection_Horizontal) {
254 if (IsRightEdge(details().window_component)) {
255 *delta_x += details().initial_location_in_parent.x() -
256 details().initial_bounds_in_parent.right();
257 } else {
258 *delta_x += details().initial_location_in_parent.x() -
259 details().initial_bounds_in_parent.x();
262 if (details().size_change_direction & kBoundsChangeDirection_Vertical) {
263 if (IsBottomEdge(details().window_component)) {
264 *delta_y += details().initial_location_in_parent.y() -
265 details().initial_bounds_in_parent.bottom();
266 } else {
267 *delta_y += details().initial_location_in_parent.y() -
268 details().initial_bounds_in_parent.y();
273 gfx::Point WindowResizer::GetOriginForDrag(int delta_x, int delta_y) {
274 gfx::Point origin = details().initial_bounds_in_parent.origin();
275 if (details().bounds_change & kBoundsChange_Repositions) {
276 int pos_change_direction = GetPositionChangeDirectionForWindowComponent(
277 details().window_component);
278 if (pos_change_direction & kBoundsChangeDirection_Horizontal)
279 origin.Offset(delta_x, 0);
280 if (pos_change_direction & kBoundsChangeDirection_Vertical)
281 origin.Offset(0, delta_y);
283 return origin;
286 gfx::Size WindowResizer::GetSizeForDrag(int* delta_x, int* delta_y) {
287 gfx::Size size = details().initial_bounds_in_parent.size();
288 if (details().bounds_change & kBoundsChange_Resizes) {
289 gfx::Size min_size = GetTarget()->delegate()->GetMinimumSize();
290 size.SetSize(GetWidthForDrag(min_size.width(), delta_x),
291 GetHeightForDrag(min_size.height(), delta_y));
292 } else if (!details().restore_bounds.IsEmpty()) {
293 size = details().restore_bounds.size();
295 return size;
298 int WindowResizer::GetWidthForDrag(int min_width, int* delta_x) {
299 int width = details().initial_bounds_in_parent.width();
300 if (details().size_change_direction & kBoundsChangeDirection_Horizontal) {
301 // Along the right edge, positive delta_x increases the window size.
302 int x_multiplier = IsRightEdge(details().window_component) ? 1 : -1;
303 width += x_multiplier * (*delta_x);
305 // Ensure we don't shrink past the minimum width and clamp delta_x
306 // for the window origin computation.
307 if (width < min_width) {
308 width = min_width;
309 *delta_x = -x_multiplier * (details().initial_bounds_in_parent.width() -
310 min_width);
313 // And don't let the window go bigger than the display.
314 int max_width = Shell::GetScreen()->GetDisplayNearestWindow(
315 GetTarget()).bounds().width();
316 gfx::Size max_size = GetTarget()->delegate()->GetMaximumSize();
317 if (max_size.width() != 0)
318 max_width = std::min(max_width, max_size.width());
319 if (width > max_width) {
320 width = max_width;
321 *delta_x = -x_multiplier * (details().initial_bounds_in_parent.width() -
322 max_width);
325 return width;
328 int WindowResizer::GetHeightForDrag(int min_height, int* delta_y) {
329 int height = details().initial_bounds_in_parent.height();
330 if (details().size_change_direction & kBoundsChangeDirection_Vertical) {
331 // Along the bottom edge, positive delta_y increases the window size.
332 int y_multiplier = IsBottomEdge(details().window_component) ? 1 : -1;
333 height += y_multiplier * (*delta_y);
335 // Ensure we don't shrink past the minimum height and clamp delta_y
336 // for the window origin computation.
337 if (height < min_height) {
338 height = min_height;
339 *delta_y = -y_multiplier * (details().initial_bounds_in_parent.height() -
340 min_height);
343 // And don't let the window go bigger than the display.
344 int max_height = Shell::GetScreen()->GetDisplayNearestWindow(
345 GetTarget()).bounds().height();
346 gfx::Size max_size = GetTarget()->delegate()->GetMaximumSize();
347 if (max_size.height() != 0)
348 max_height = std::min(max_height, max_size.height());
349 if (height > max_height) {
350 height = max_height;
351 *delta_y = -y_multiplier * (details().initial_bounds_in_parent.height() -
352 max_height);
355 return height;
358 } // namespace ash