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"
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"
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
;
40 const int WindowResizer::kBoundsChange_None
= 0;
42 const int WindowResizer::kBoundsChange_Repositions
= 1;
44 const int WindowResizer::kBoundsChange_Resizes
= 2;
47 const int WindowResizer::kBoundsChangeDirection_None
= 0;
49 const int WindowResizer::kBoundsChangeDirection_Horizontal
= 1;
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() {
65 int WindowResizer::GetBoundsChangeForWindowComponent(int component
) {
66 int bounds_change
= WindowResizer::kBoundsChange_None
;
73 bounds_change
|= WindowResizer::kBoundsChange_Repositions
|
74 WindowResizer::kBoundsChange_Resizes
;
77 bounds_change
|= WindowResizer::kBoundsChange_Repositions
;
83 bounds_change
|= WindowResizer::kBoundsChange_Resizes
;
92 int WindowResizer::GetPositionChangeDirectionForWindowComponent(
93 int window_component
) {
94 int pos_change_direction
= WindowResizer::kBoundsChangeDirection_None
;
95 switch (window_component
) {
100 pos_change_direction
|=
101 WindowResizer::kBoundsChangeDirection_Horizontal
|
102 WindowResizer::kBoundsChangeDirection_Vertical
;
107 pos_change_direction
|= WindowResizer::kBoundsChangeDirection_Vertical
;
112 pos_change_direction
|= WindowResizer::kBoundsChangeDirection_Horizontal
;
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
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(), kShellWindowId_DockedContainer
);
146 DockedWindowLayoutManager
* dock_layout
=
147 static_cast<DockedWindowLayoutManager
*>(
148 dock_container
->layout_manager());
150 work_area
.Union(dock_layout
->docked_bounds());
151 work_area
= ScreenUtil::ConvertRectFromScreen(GetTarget()->parent(),
153 if (details().size_change_direction
& kBoundsChangeDirection_Horizontal
) {
154 if (IsRightEdge(details().window_component
) &&
155 new_bounds
.right() < work_area
.x() + kMinimumOnScreenArea
) {
156 int delta
= work_area
.x() + kMinimumOnScreenArea
- new_bounds
.right();
157 new_bounds
.set_width(new_bounds
.width() + delta
);
158 } else if (new_bounds
.x() > work_area
.right() - kMinimumOnScreenArea
) {
159 int width
= new_bounds
.right() - work_area
.right() +
160 kMinimumOnScreenArea
;
161 new_bounds
.set_x(work_area
.right() - kMinimumOnScreenArea
);
162 new_bounds
.set_width(width
);
165 if (details().size_change_direction
& kBoundsChangeDirection_Vertical
) {
166 if (!IsBottomEdge(details().window_component
) &&
167 new_bounds
.y() > work_area
.bottom() - kMinimumOnScreenArea
) {
168 int height
= new_bounds
.bottom() - work_area
.bottom() +
169 kMinimumOnScreenArea
;
170 new_bounds
.set_y(work_area
.bottom() - kMinimumOnScreenArea
);
171 new_bounds
.set_height(height
);
172 } else if (details().window_component
== HTBOTTOM
||
173 details().window_component
== HTBOTTOMRIGHT
||
174 details().window_component
== HTBOTTOMLEFT
) {
175 // Update bottom edge to stay in the work area when we are resizing
176 // by dragging the bottom edge or corners.
177 if (new_bounds
.bottom() > work_area
.bottom())
178 new_bounds
.Inset(0, 0, 0,
179 new_bounds
.bottom() - work_area
.bottom());
182 if (details().bounds_change
& kBoundsChange_Repositions
&&
183 new_bounds
.y() < 0) {
184 int delta
= new_bounds
.y();
186 new_bounds
.set_height(new_bounds
.height() + delta
);
190 if (details().bounds_change
& kBoundsChange_Repositions
) {
191 // When we might want to reposition a window which is also restored to its
192 // previous size, to keep the cursor within the dragged window.
193 if (!details().restore_bounds
.IsEmpty()) {
194 // However - it is not desirable to change the origin if the window would
195 // be still hit by the cursor.
196 if (details().initial_location_in_parent
.x() >
197 details().initial_bounds_in_parent
.x() +
198 details().restore_bounds
.width())
199 new_bounds
.set_x(location
.x() - details().restore_bounds
.width() / 2);
202 // Make sure that |new_bounds| doesn't leave any of the displays. Note that
203 // the |work_area| above isn't good for this check since it is the work area
204 // for the current display but the window can move to a different one.
205 aura::Window
* parent
= GetTarget()->parent();
206 gfx::Point
passed_location_in_screen(passed_location
);
207 wm::ConvertPointToScreen(parent
, &passed_location_in_screen
);
208 gfx::Rect
near_passed_location(passed_location_in_screen
, gfx::Size());
209 // Use a pointer location (matching the logic in DragWindowResizer) to
210 // calculate the target display after the drag.
211 const gfx::Display
& display
=
212 Shell::GetScreen()->GetDisplayMatching(near_passed_location
);
213 aura::Window
* dock_container
=
214 Shell::GetContainer(wm::GetRootWindowMatching(near_passed_location
),
215 kShellWindowId_DockedContainer
);
216 DockedWindowLayoutManager
* dock_layout
=
217 static_cast<DockedWindowLayoutManager
*>(
218 dock_container
->layout_manager());
220 gfx::Rect screen_work_area
= display
.work_area();
221 screen_work_area
.Union(dock_layout
->docked_bounds());
222 screen_work_area
.Inset(kMinimumOnScreenArea
, 0);
223 gfx::Rect new_bounds_in_screen
=
224 ScreenUtil::ConvertRectToScreen(parent
, new_bounds
);
225 if (!screen_work_area
.Intersects(new_bounds_in_screen
)) {
226 // Make sure that the x origin does not leave the current display.
227 new_bounds_in_screen
.set_x(
228 std::max(screen_work_area
.x() - new_bounds
.width(),
229 std::min(screen_work_area
.right(),
230 new_bounds_in_screen
.x())));
232 ScreenUtil::ConvertRectFromScreen(parent
, new_bounds_in_screen
);
240 bool WindowResizer::IsBottomEdge(int window_component
) {
241 return window_component
== HTBOTTOMLEFT
||
242 window_component
== HTBOTTOM
||
243 window_component
== HTBOTTOMRIGHT
||
244 window_component
== HTGROWBOX
;
247 void WindowResizer::AdjustDeltaForTouchResize(int* delta_x
, int* delta_y
) {
248 if (details().source
!= aura::client::WINDOW_MOVE_SOURCE_TOUCH
||
249 !(details().bounds_change
& kBoundsChange_Resizes
))
252 if (details().size_change_direction
& kBoundsChangeDirection_Horizontal
) {
253 if (IsRightEdge(details().window_component
)) {
254 *delta_x
+= details().initial_location_in_parent
.x() -
255 details().initial_bounds_in_parent
.right();
257 *delta_x
+= details().initial_location_in_parent
.x() -
258 details().initial_bounds_in_parent
.x();
261 if (details().size_change_direction
& kBoundsChangeDirection_Vertical
) {
262 if (IsBottomEdge(details().window_component
)) {
263 *delta_y
+= details().initial_location_in_parent
.y() -
264 details().initial_bounds_in_parent
.bottom();
266 *delta_y
+= details().initial_location_in_parent
.y() -
267 details().initial_bounds_in_parent
.y();
272 gfx::Point
WindowResizer::GetOriginForDrag(int delta_x
, int delta_y
) {
273 gfx::Point origin
= details().initial_bounds_in_parent
.origin();
274 if (details().bounds_change
& kBoundsChange_Repositions
) {
275 int pos_change_direction
= GetPositionChangeDirectionForWindowComponent(
276 details().window_component
);
277 if (pos_change_direction
& kBoundsChangeDirection_Horizontal
)
278 origin
.Offset(delta_x
, 0);
279 if (pos_change_direction
& kBoundsChangeDirection_Vertical
)
280 origin
.Offset(0, delta_y
);
285 gfx::Size
WindowResizer::GetSizeForDrag(int* delta_x
, int* delta_y
) {
286 gfx::Size size
= details().initial_bounds_in_parent
.size();
287 if (details().bounds_change
& kBoundsChange_Resizes
) {
288 gfx::Size min_size
= GetTarget()->delegate()->GetMinimumSize();
289 size
.SetSize(GetWidthForDrag(min_size
.width(), delta_x
),
290 GetHeightForDrag(min_size
.height(), delta_y
));
291 } else if (!details().restore_bounds
.IsEmpty()) {
292 size
= details().restore_bounds
.size();
297 int WindowResizer::GetWidthForDrag(int min_width
, int* delta_x
) {
298 int width
= details().initial_bounds_in_parent
.width();
299 if (details().size_change_direction
& kBoundsChangeDirection_Horizontal
) {
300 // Along the right edge, positive delta_x increases the window size.
301 int x_multiplier
= IsRightEdge(details().window_component
) ? 1 : -1;
302 width
+= x_multiplier
* (*delta_x
);
304 // Ensure we don't shrink past the minimum width and clamp delta_x
305 // for the window origin computation.
306 if (width
< min_width
) {
308 *delta_x
= -x_multiplier
* (details().initial_bounds_in_parent
.width() -
312 // And don't let the window go bigger than the display.
313 int max_width
= Shell::GetScreen()->GetDisplayNearestWindow(
314 GetTarget()).bounds().width();
315 gfx::Size max_size
= GetTarget()->delegate()->GetMaximumSize();
316 if (max_size
.width() != 0)
317 max_width
= std::min(max_width
, max_size
.width());
318 if (width
> max_width
) {
320 *delta_x
= -x_multiplier
* (details().initial_bounds_in_parent
.width() -
327 int WindowResizer::GetHeightForDrag(int min_height
, int* delta_y
) {
328 int height
= details().initial_bounds_in_parent
.height();
329 if (details().size_change_direction
& kBoundsChangeDirection_Vertical
) {
330 // Along the bottom edge, positive delta_y increases the window size.
331 int y_multiplier
= IsBottomEdge(details().window_component
) ? 1 : -1;
332 height
+= y_multiplier
* (*delta_y
);
334 // Ensure we don't shrink past the minimum height and clamp delta_y
335 // for the window origin computation.
336 if (height
< min_height
) {
338 *delta_y
= -y_multiplier
* (details().initial_bounds_in_parent
.height() -
342 // And don't let the window go bigger than the display.
343 int max_height
= Shell::GetScreen()->GetDisplayNearestWindow(
344 GetTarget()).bounds().height();
345 gfx::Size max_size
= GetTarget()->delegate()->GetMaximumSize();
346 if (max_size
.height() != 0)
347 max_height
= std::min(max_height
, max_size
.height());
348 if (height
> max_height
) {
350 *delta_y
= -y_multiplier
* (details().initial_bounds_in_parent
.height() -