Fix mouse warp with 2x displays
[chromium-blink-merge.git] / ash / wm / window_resizer.cc
blob245d85ef061d8a8919db431ef387a2d4434a30f6
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/window.h"
16 #include "ui/aura/window_delegate.h"
17 #include "ui/aura/window_event_dispatcher.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"
23 #include "ui/wm/core/coordinate_conversion.h"
25 namespace ash {
27 namespace {
29 // Returns true for resize components along the right edge, where a drag in
30 // positive x will make the window larger.
31 bool IsRightEdge(int window_component) {
32 return window_component == HTTOPRIGHT ||
33 window_component == HTRIGHT ||
34 window_component == HTBOTTOMRIGHT ||
35 window_component == HTGROWBOX;
38 } // namespace
40 // static
41 const int WindowResizer::kBoundsChange_None = 0;
42 // static
43 const int WindowResizer::kBoundsChange_Repositions = 1;
44 // static
45 const int WindowResizer::kBoundsChange_Resizes = 2;
47 // static
48 const int WindowResizer::kBoundsChangeDirection_None = 0;
49 // static
50 const int WindowResizer::kBoundsChangeDirection_Horizontal = 1;
51 // static
52 const int WindowResizer::kBoundsChangeDirection_Vertical = 2;
54 WindowResizer::WindowResizer(wm::WindowState* window_state)
55 : window_state_(window_state) {
56 DCHECK(window_state_->drag_details());
59 WindowResizer::~WindowResizer() {
62 // static
63 int WindowResizer::GetBoundsChangeForWindowComponent(int component) {
64 int bounds_change = WindowResizer::kBoundsChange_None;
65 switch (component) {
66 case HTTOPLEFT:
67 case HTTOP:
68 case HTTOPRIGHT:
69 case HTLEFT:
70 case HTBOTTOMLEFT:
71 bounds_change |= WindowResizer::kBoundsChange_Repositions |
72 WindowResizer::kBoundsChange_Resizes;
73 break;
74 case HTCAPTION:
75 bounds_change |= WindowResizer::kBoundsChange_Repositions;
76 break;
77 case HTRIGHT:
78 case HTBOTTOMRIGHT:
79 case HTBOTTOM:
80 case HTGROWBOX:
81 bounds_change |= WindowResizer::kBoundsChange_Resizes;
82 break;
83 default:
84 break;
86 return bounds_change;
89 // static
90 int WindowResizer::GetPositionChangeDirectionForWindowComponent(
91 int window_component) {
92 int pos_change_direction = WindowResizer::kBoundsChangeDirection_None;
93 switch (window_component) {
94 case HTTOPLEFT:
95 case HTBOTTOMRIGHT:
96 case HTGROWBOX:
97 case HTCAPTION:
98 pos_change_direction |=
99 WindowResizer::kBoundsChangeDirection_Horizontal |
100 WindowResizer::kBoundsChangeDirection_Vertical;
101 break;
102 case HTTOP:
103 case HTTOPRIGHT:
104 case HTBOTTOM:
105 pos_change_direction |= WindowResizer::kBoundsChangeDirection_Vertical;
106 break;
107 case HTBOTTOMLEFT:
108 case HTRIGHT:
109 case HTLEFT:
110 pos_change_direction |= WindowResizer::kBoundsChangeDirection_Horizontal;
111 break;
112 default:
113 break;
115 return pos_change_direction;
118 gfx::Rect WindowResizer::CalculateBoundsForDrag(
119 const gfx::Point& passed_location) {
120 if (!details().is_resizable)
121 return details().initial_bounds_in_parent;
123 gfx::Point location = passed_location;
124 int delta_x = location.x() - details().initial_location_in_parent.x();
125 int delta_y = location.y() - details().initial_location_in_parent.y();
127 AdjustDeltaForTouchResize(&delta_x, &delta_y);
129 // The minimize size constraint may limit how much we change the window
130 // position. For example, dragging the left edge to the right should stop
131 // repositioning the window when the minimize size is reached.
132 gfx::Size size = GetSizeForDrag(&delta_x, &delta_y);
133 gfx::Point origin = GetOriginForDrag(delta_x, delta_y);
134 gfx::Rect new_bounds(origin, size);
136 // Sizing has to keep the result on the screen. Note that this correction
137 // has to come first since it might have an impact on the origin as well as
138 // on the size.
139 if (details().bounds_change & kBoundsChange_Resizes) {
140 gfx::Rect work_area =
141 Shell::GetScreen()->GetDisplayNearestWindow(GetTarget()).work_area();
142 aura::Window* dock_container = Shell::GetContainer(
143 GetTarget()->GetRootWindow(), kShellWindowId_DockedContainer);
144 DockedWindowLayoutManager* dock_layout =
145 static_cast<DockedWindowLayoutManager*>(
146 dock_container->layout_manager());
148 work_area.Union(dock_layout->docked_bounds());
149 work_area = ScreenUtil::ConvertRectFromScreen(GetTarget()->parent(),
150 work_area);
151 if (details().size_change_direction & kBoundsChangeDirection_Horizontal) {
152 if (IsRightEdge(details().window_component) &&
153 new_bounds.right() < work_area.x() + kMinimumOnScreenArea) {
154 int delta = work_area.x() + kMinimumOnScreenArea - new_bounds.right();
155 new_bounds.set_width(new_bounds.width() + delta);
156 } else if (new_bounds.x() > work_area.right() - kMinimumOnScreenArea) {
157 int width = new_bounds.right() - work_area.right() +
158 kMinimumOnScreenArea;
159 new_bounds.set_x(work_area.right() - kMinimumOnScreenArea);
160 new_bounds.set_width(width);
163 if (details().size_change_direction & kBoundsChangeDirection_Vertical) {
164 if (!IsBottomEdge(details().window_component) &&
165 new_bounds.y() > work_area.bottom() - kMinimumOnScreenArea) {
166 int height = new_bounds.bottom() - work_area.bottom() +
167 kMinimumOnScreenArea;
168 new_bounds.set_y(work_area.bottom() - kMinimumOnScreenArea);
169 new_bounds.set_height(height);
170 } else if (details().window_component == HTBOTTOM ||
171 details().window_component == HTBOTTOMRIGHT ||
172 details().window_component == HTBOTTOMLEFT) {
173 // Update bottom edge to stay in the work area when we are resizing
174 // by dragging the bottom edge or corners.
175 if (new_bounds.bottom() > work_area.bottom())
176 new_bounds.Inset(0, 0, 0,
177 new_bounds.bottom() - work_area.bottom());
180 if (details().bounds_change & kBoundsChange_Repositions &&
181 new_bounds.y() < 0) {
182 int delta = new_bounds.y();
183 new_bounds.set_y(0);
184 new_bounds.set_height(new_bounds.height() + delta);
188 if (details().bounds_change & kBoundsChange_Repositions) {
189 // When we might want to reposition a window which is also restored to its
190 // previous size, to keep the cursor within the dragged window.
191 if (!details().restore_bounds.IsEmpty()) {
192 // However - it is not desirable to change the origin if the window would
193 // be still hit by the cursor.
194 if (details().initial_location_in_parent.x() >
195 details().initial_bounds_in_parent.x() +
196 details().restore_bounds.width())
197 new_bounds.set_x(location.x() - details().restore_bounds.width() / 2);
200 // Make sure that |new_bounds| doesn't leave any of the displays. Note that
201 // the |work_area| above isn't good for this check since it is the work area
202 // for the current display but the window can move to a different one.
203 aura::Window* parent = GetTarget()->parent();
204 gfx::Point passed_location_in_screen(passed_location);
205 ::wm::ConvertPointToScreen(parent, &passed_location_in_screen);
206 gfx::Rect near_passed_location(passed_location_in_screen, gfx::Size());
207 // Use a pointer location (matching the logic in DragWindowResizer) to
208 // calculate the target display after the drag.
209 const gfx::Display& display =
210 Shell::GetScreen()->GetDisplayMatching(near_passed_location);
211 aura::Window* dock_container =
212 Shell::GetContainer(wm::GetRootWindowMatching(near_passed_location),
213 kShellWindowId_DockedContainer);
214 DockedWindowLayoutManager* dock_layout =
215 static_cast<DockedWindowLayoutManager*>(
216 dock_container->layout_manager());
218 gfx::Rect screen_work_area = display.work_area();
219 screen_work_area.Union(dock_layout->docked_bounds());
220 screen_work_area.Inset(kMinimumOnScreenArea, 0);
221 gfx::Rect new_bounds_in_screen =
222 ScreenUtil::ConvertRectToScreen(parent, new_bounds);
223 if (!screen_work_area.Intersects(new_bounds_in_screen)) {
224 // Make sure that the x origin does not leave the current display.
225 new_bounds_in_screen.set_x(
226 std::max(screen_work_area.x() - new_bounds.width(),
227 std::min(screen_work_area.right(),
228 new_bounds_in_screen.x())));
229 new_bounds =
230 ScreenUtil::ConvertRectFromScreen(parent, new_bounds_in_screen);
234 return new_bounds;
237 // static
238 bool WindowResizer::IsBottomEdge(int window_component) {
239 return window_component == HTBOTTOMLEFT ||
240 window_component == HTBOTTOM ||
241 window_component == HTBOTTOMRIGHT ||
242 window_component == HTGROWBOX;
245 void WindowResizer::AdjustDeltaForTouchResize(int* delta_x, int* delta_y) {
246 if (details().source != aura::client::WINDOW_MOVE_SOURCE_TOUCH ||
247 !(details().bounds_change & kBoundsChange_Resizes))
248 return;
250 if (details().size_change_direction & kBoundsChangeDirection_Horizontal) {
251 if (IsRightEdge(details().window_component)) {
252 *delta_x += details().initial_location_in_parent.x() -
253 details().initial_bounds_in_parent.right();
254 } else {
255 *delta_x += details().initial_location_in_parent.x() -
256 details().initial_bounds_in_parent.x();
259 if (details().size_change_direction & kBoundsChangeDirection_Vertical) {
260 if (IsBottomEdge(details().window_component)) {
261 *delta_y += details().initial_location_in_parent.y() -
262 details().initial_bounds_in_parent.bottom();
263 } else {
264 *delta_y += details().initial_location_in_parent.y() -
265 details().initial_bounds_in_parent.y();
270 gfx::Point WindowResizer::GetOriginForDrag(int delta_x, int delta_y) {
271 gfx::Point origin = details().initial_bounds_in_parent.origin();
272 if (details().bounds_change & kBoundsChange_Repositions) {
273 int pos_change_direction = GetPositionChangeDirectionForWindowComponent(
274 details().window_component);
275 if (pos_change_direction & kBoundsChangeDirection_Horizontal)
276 origin.Offset(delta_x, 0);
277 if (pos_change_direction & kBoundsChangeDirection_Vertical)
278 origin.Offset(0, delta_y);
280 return origin;
283 gfx::Size WindowResizer::GetSizeForDrag(int* delta_x, int* delta_y) {
284 gfx::Size size = details().initial_bounds_in_parent.size();
285 if (details().bounds_change & kBoundsChange_Resizes) {
286 gfx::Size min_size = GetTarget()->delegate()->GetMinimumSize();
287 size.SetSize(GetWidthForDrag(min_size.width(), delta_x),
288 GetHeightForDrag(min_size.height(), delta_y));
289 } else if (!details().restore_bounds.IsEmpty()) {
290 size = details().restore_bounds.size();
292 return size;
295 int WindowResizer::GetWidthForDrag(int min_width, int* delta_x) {
296 int width = details().initial_bounds_in_parent.width();
297 if (details().size_change_direction & kBoundsChangeDirection_Horizontal) {
298 // Along the right edge, positive delta_x increases the window size.
299 int x_multiplier = IsRightEdge(details().window_component) ? 1 : -1;
300 width += x_multiplier * (*delta_x);
302 // Ensure we don't shrink past the minimum width and clamp delta_x
303 // for the window origin computation.
304 if (width < min_width) {
305 width = min_width;
306 *delta_x = -x_multiplier * (details().initial_bounds_in_parent.width() -
307 min_width);
310 // And don't let the window go bigger than the display.
311 int max_width = Shell::GetScreen()->GetDisplayNearestWindow(
312 GetTarget()).bounds().width();
313 gfx::Size max_size = GetTarget()->delegate()->GetMaximumSize();
314 if (max_size.width() != 0)
315 max_width = std::min(max_width, max_size.width());
316 if (width > max_width) {
317 width = max_width;
318 *delta_x = -x_multiplier * (details().initial_bounds_in_parent.width() -
319 max_width);
322 return width;
325 int WindowResizer::GetHeightForDrag(int min_height, int* delta_y) {
326 int height = details().initial_bounds_in_parent.height();
327 if (details().size_change_direction & kBoundsChangeDirection_Vertical) {
328 // Along the bottom edge, positive delta_y increases the window size.
329 int y_multiplier = IsBottomEdge(details().window_component) ? 1 : -1;
330 height += y_multiplier * (*delta_y);
332 // Ensure we don't shrink past the minimum height and clamp delta_y
333 // for the window origin computation.
334 if (height < min_height) {
335 height = min_height;
336 *delta_y = -y_multiplier * (details().initial_bounds_in_parent.height() -
337 min_height);
340 // And don't let the window go bigger than the display.
341 int max_height = Shell::GetScreen()->GetDisplayNearestWindow(
342 GetTarget()).bounds().height();
343 gfx::Size max_size = GetTarget()->delegate()->GetMaximumSize();
344 if (max_size.height() != 0)
345 max_height = std::min(max_height, max_size.height());
346 if (height > max_height) {
347 height = max_height;
348 *delta_y = -y_multiplier * (details().initial_bounds_in_parent.height() -
349 max_height);
352 return height;
355 } // namespace ash