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_ash.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"
28 int GetPositionChangeDirectionForWindowComponent(int window_component) {
29 int pos_change_direction = WindowResizer::kBoundsChangeDirection_None;
30 switch (window_component) {
35 pos_change_direction |=
36 WindowResizer::kBoundsChangeDirection_Horizontal |
37 WindowResizer::kBoundsChangeDirection_Vertical;
42 pos_change_direction |= WindowResizer::kBoundsChangeDirection_Vertical;
47 pos_change_direction |= WindowResizer::kBoundsChangeDirection_Horizontal;
52 return pos_change_direction;
55 int GetSizeChangeDirectionForWindowComponent(int window_component) {
56 int size_change_direction = WindowResizer::kBoundsChangeDirection_None;
57 switch (window_component) {
64 size_change_direction |=
65 WindowResizer::kBoundsChangeDirection_Horizontal |
66 WindowResizer::kBoundsChangeDirection_Vertical;
70 size_change_direction |= WindowResizer::kBoundsChangeDirection_Vertical;
74 size_change_direction |= WindowResizer::kBoundsChangeDirection_Horizontal;
79 return size_change_direction;
82 // Returns true for resize components along the right edge, where a drag in
83 // positive x will make the window larger.
84 bool IsRightEdge(int window_component) {
85 return window_component == HTTOPRIGHT ||
86 window_component == HTRIGHT ||
87 window_component == HTBOTTOMRIGHT ||
88 window_component == HTGROWBOX;
94 const int WindowResizer::kBoundsChange_None = 0;
96 const int WindowResizer::kBoundsChange_Repositions = 1;
98 const int WindowResizer::kBoundsChange_Resizes = 2;
101 const int WindowResizer::kBoundsChangeDirection_None = 0;
103 const int WindowResizer::kBoundsChangeDirection_Horizontal = 1;
105 const int WindowResizer::kBoundsChangeDirection_Vertical = 2;
107 WindowResizer::Details::Details()
110 window_component(HTNOWHERE),
112 position_change_direction(0),
113 size_change_direction(0),
115 source(aura::client::WINDOW_MOVE_SOURCE_MOUSE) {
118 WindowResizer::Details::Details(aura::Window* window,
119 const gfx::Point& location,
120 int window_component,
121 aura::client::WindowMoveSource source)
123 window_state(wm::GetWindowState(window)),
124 initial_bounds_in_parent(window->bounds()),
125 restore_bounds(gfx::Rect()),
126 initial_location_in_parent(location),
127 initial_opacity(window->layer()->opacity()),
128 window_component(window_component),
129 bounds_change(GetBoundsChangeForWindowComponent(window_component)),
130 position_change_direction(
131 GetPositionChangeDirectionForWindowComponent(window_component)),
132 size_change_direction(
133 GetSizeChangeDirectionForWindowComponent(window_component)),
134 is_resizable(bounds_change != kBoundsChangeDirection_None),
136 if (window_state->IsNormalShowState() &&
137 window_state->HasRestoreBounds() &&
138 window_component == HTCAPTION)
139 restore_bounds = window_state->GetRestoreBoundsInScreen();
142 WindowResizer::Details::~Details() {
145 WindowResizer::WindowResizer() {
148 WindowResizer::~WindowResizer() {
152 int WindowResizer::GetBoundsChangeForWindowComponent(int component) {
153 int bounds_change = WindowResizer::kBoundsChange_None;
160 bounds_change |= WindowResizer::kBoundsChange_Repositions |
161 WindowResizer::kBoundsChange_Resizes;
164 bounds_change |= WindowResizer::kBoundsChange_Repositions;
170 bounds_change |= WindowResizer::kBoundsChange_Resizes;
175 return bounds_change;
179 gfx::Rect WindowResizer::CalculateBoundsForDrag(
180 const Details& details,
181 const gfx::Point& passed_location) {
182 if (!details.is_resizable)
183 return details.initial_bounds_in_parent;
185 gfx::Point location = passed_location;
186 int delta_x = location.x() - details.initial_location_in_parent.x();
187 int delta_y = location.y() - details.initial_location_in_parent.y();
189 AdjustDeltaForTouchResize(details, &delta_x, &delta_y);
191 // The minimize size constraint may limit how much we change the window
192 // position. For example, dragging the left edge to the right should stop
193 // repositioning the window when the minimize size is reached.
194 gfx::Size size = GetSizeForDrag(details, &delta_x, &delta_y);
195 gfx::Point origin = GetOriginForDrag(details, delta_x, delta_y);
196 gfx::Rect new_bounds(origin, size);
198 // Sizing has to keep the result on the screen. Note that this correction
199 // has to come first since it might have an impact on the origin as well as
201 if (details.bounds_change & kBoundsChange_Resizes) {
202 gfx::Rect work_area =
203 Shell::GetScreen()->GetDisplayNearestWindow(details.window).work_area();
204 aura::Window* dock_container = Shell::GetContainer(
205 details.window->GetRootWindow(),
206 internal::kShellWindowId_DockedContainer);
207 internal::DockedWindowLayoutManager* dock_layout =
208 static_cast<internal::DockedWindowLayoutManager*>(
209 dock_container->layout_manager());
211 work_area.Union(dock_layout->docked_bounds());
212 work_area = ScreenAsh::ConvertRectFromScreen(details.window->parent(),
214 if (details.size_change_direction & kBoundsChangeDirection_Horizontal) {
215 if (IsRightEdge(details.window_component) &&
216 new_bounds.right() < work_area.x() + kMinimumOnScreenArea) {
217 int delta = work_area.x() + kMinimumOnScreenArea - new_bounds.right();
218 new_bounds.set_width(new_bounds.width() + delta);
219 } else if (new_bounds.x() > work_area.right() - kMinimumOnScreenArea) {
220 int width = new_bounds.right() - work_area.right() +
221 kMinimumOnScreenArea;
222 new_bounds.set_x(work_area.right() - kMinimumOnScreenArea);
223 new_bounds.set_width(width);
226 if (details.size_change_direction & kBoundsChangeDirection_Vertical) {
227 if (!IsBottomEdge(details.window_component) &&
228 new_bounds.y() > work_area.bottom() - kMinimumOnScreenArea) {
229 int height = new_bounds.bottom() - work_area.bottom() +
230 kMinimumOnScreenArea;
231 new_bounds.set_y(work_area.bottom() - kMinimumOnScreenArea);
232 new_bounds.set_height(height);
233 } else if (details.window_component == HTBOTTOM ||
234 details.window_component == HTBOTTOMRIGHT ||
235 details.window_component == HTBOTTOMLEFT) {
236 // Update bottom edge to stay in the work area when we are resizing
237 // by dragging the bottom edge or corners.
238 if (new_bounds.bottom() > work_area.bottom())
239 new_bounds.Inset(0, 0, 0,
240 new_bounds.bottom() - work_area.bottom());
243 if (details.bounds_change & kBoundsChange_Repositions &&
244 new_bounds.y() < 0) {
245 int delta = new_bounds.y();
247 new_bounds.set_height(new_bounds.height() + delta);
251 if (details.bounds_change & kBoundsChange_Repositions) {
252 // When we might want to reposition a window which is also restored to its
253 // previous size, to keep the cursor within the dragged window.
254 if (!details.restore_bounds.IsEmpty()) {
255 // However - it is not desirable to change the origin if the window would
256 // be still hit by the cursor.
257 if (details.initial_location_in_parent.x() >
258 details.initial_bounds_in_parent.x() + details.restore_bounds.width())
259 new_bounds.set_x(location.x() - details.restore_bounds.width() / 2);
262 // Make sure that |new_bounds| doesn't leave any of the displays. Note that
263 // the |work_area| above isn't good for this check since it is the work area
264 // for the current display but the window can move to a different one.
265 aura::Window* parent = details.window->parent();
266 gfx::Rect new_bounds_in_screen =
267 ScreenAsh::ConvertRectToScreen(parent, new_bounds);
268 const gfx::Display& display =
269 Shell::GetScreen()->GetDisplayMatching(new_bounds_in_screen);
270 aura::Window* dock_container = Shell::GetContainer(
271 wm::GetRootWindowMatching(new_bounds_in_screen),
272 internal::kShellWindowId_DockedContainer);
273 internal::DockedWindowLayoutManager* dock_layout =
274 static_cast<internal::DockedWindowLayoutManager*>(
275 dock_container->layout_manager());
277 gfx::Rect screen_work_area = display.work_area();
278 screen_work_area.Union(dock_layout->docked_bounds());
279 screen_work_area.Inset(kMinimumOnScreenArea, 0);
280 if (!screen_work_area.Intersects(new_bounds_in_screen)) {
281 // Make sure that the x origin does not leave the current display.
282 new_bounds_in_screen.set_x(
283 std::max(screen_work_area.x() - new_bounds.width(),
284 std::min(screen_work_area.right(),
285 new_bounds_in_screen.x())));
287 ScreenAsh::ConvertRectFromScreen(parent, new_bounds_in_screen);
295 bool WindowResizer::IsBottomEdge(int window_component) {
296 return window_component == HTBOTTOMLEFT ||
297 window_component == HTBOTTOM ||
298 window_component == HTBOTTOMRIGHT ||
299 window_component == HTGROWBOX;
303 void WindowResizer::AdjustDeltaForTouchResize(const Details& details,
306 if (details.source != aura::client::WINDOW_MOVE_SOURCE_TOUCH ||
307 !(details.bounds_change & kBoundsChange_Resizes))
310 if (details.size_change_direction & kBoundsChangeDirection_Horizontal) {
311 if (IsRightEdge(details.window_component)) {
312 *delta_x += details.initial_location_in_parent.x() -
313 details.initial_bounds_in_parent.right();
315 *delta_x += details.initial_location_in_parent.x() -
316 details.initial_bounds_in_parent.x();
319 if (details.size_change_direction & kBoundsChangeDirection_Vertical) {
320 if (IsBottomEdge(details.window_component)) {
321 *delta_y += details.initial_location_in_parent.y() -
322 details.initial_bounds_in_parent.bottom();
324 *delta_y += details.initial_location_in_parent.y() -
325 details.initial_bounds_in_parent.y();
331 gfx::Point WindowResizer::GetOriginForDrag(const Details& details,
334 gfx::Point origin = details.initial_bounds_in_parent.origin();
335 if (details.bounds_change & kBoundsChange_Repositions) {
336 int pos_change_direction =
337 GetPositionChangeDirectionForWindowComponent(details.window_component);
338 if (pos_change_direction & kBoundsChangeDirection_Horizontal)
339 origin.Offset(delta_x, 0);
340 if (pos_change_direction & kBoundsChangeDirection_Vertical)
341 origin.Offset(0, delta_y);
347 gfx::Size WindowResizer::GetSizeForDrag(const Details& details,
350 gfx::Size size = details.initial_bounds_in_parent.size();
351 if (details.bounds_change & kBoundsChange_Resizes) {
352 gfx::Size min_size = details.window->delegate()->GetMinimumSize();
353 size.SetSize(GetWidthForDrag(details, min_size.width(), delta_x),
354 GetHeightForDrag(details, min_size.height(), delta_y));
355 } else if (!details.restore_bounds.IsEmpty()) {
356 size = details.restore_bounds.size();
362 int WindowResizer::GetWidthForDrag(const Details& details,
365 int width = details.initial_bounds_in_parent.width();
366 if (details.size_change_direction & kBoundsChangeDirection_Horizontal) {
367 // Along the right edge, positive delta_x increases the window size.
368 int x_multiplier = IsRightEdge(details.window_component) ? 1 : -1;
369 width += x_multiplier * (*delta_x);
371 // Ensure we don't shrink past the minimum width and clamp delta_x
372 // for the window origin computation.
373 if (width < min_width) {
375 *delta_x = -x_multiplier * (details.initial_bounds_in_parent.width() -
379 // And don't let the window go bigger than the display.
380 int max_width = Shell::GetScreen()->GetDisplayNearestWindow(
381 details.window).bounds().width();
382 gfx::Size max_size = details.window->delegate()->GetMaximumSize();
383 if (max_size.width() != 0)
384 max_width = std::min(max_width, max_size.width());
385 if (width > max_width) {
387 *delta_x = -x_multiplier * (details.initial_bounds_in_parent.width() -
395 int WindowResizer::GetHeightForDrag(const Details& details,
398 int height = details.initial_bounds_in_parent.height();
399 if (details.size_change_direction & kBoundsChangeDirection_Vertical) {
400 // Along the bottom edge, positive delta_y increases the window size.
401 int y_multiplier = IsBottomEdge(details.window_component) ? 1 : -1;
402 height += y_multiplier * (*delta_y);
404 // Ensure we don't shrink past the minimum height and clamp delta_y
405 // for the window origin computation.
406 if (height < min_height) {
408 *delta_y = -y_multiplier * (details.initial_bounds_in_parent.height() -
412 // And don't let the window go bigger than the display.
413 int max_height = Shell::GetScreen()->GetDisplayNearestWindow(
414 details.window).bounds().height();
415 gfx::Size max_size = details.window->delegate()->GetMaximumSize();
416 if (max_size.height() != 0)
417 max_height = std::min(max_height, max_size.height());
418 if (height > max_height) {
420 *delta_y = -y_multiplier * (details.initial_bounds_in_parent.height() -