- add sources.
[platform/framework/web/crosswalk.git] / src / ash / wm / window_resizer.cc
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.
4
5 #include "ash/wm/window_resizer.h"
6
7 #include "ash/screen_ash.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"
23
24 namespace ash {
25
26 namespace {
27
28 int GetPositionChangeDirectionForWindowComponent(int window_component) {
29   int pos_change_direction = WindowResizer::kBoundsChangeDirection_None;
30   switch (window_component) {
31     case HTTOPLEFT:
32     case HTBOTTOMRIGHT:
33     case HTGROWBOX:
34     case HTCAPTION:
35       pos_change_direction |=
36           WindowResizer::kBoundsChangeDirection_Horizontal |
37           WindowResizer::kBoundsChangeDirection_Vertical;
38       break;
39     case HTTOP:
40     case HTTOPRIGHT:
41     case HTBOTTOM:
42       pos_change_direction |= WindowResizer::kBoundsChangeDirection_Vertical;
43       break;
44     case HTBOTTOMLEFT:
45     case HTRIGHT:
46     case HTLEFT:
47       pos_change_direction |= WindowResizer::kBoundsChangeDirection_Horizontal;
48       break;
49     default:
50       break;
51   }
52   return pos_change_direction;
53 }
54
55 int GetSizeChangeDirectionForWindowComponent(int window_component) {
56   int size_change_direction = WindowResizer::kBoundsChangeDirection_None;
57   switch (window_component) {
58     case HTTOPLEFT:
59     case HTTOPRIGHT:
60     case HTBOTTOMLEFT:
61     case HTBOTTOMRIGHT:
62     case HTGROWBOX:
63     case HTCAPTION:
64       size_change_direction |=
65           WindowResizer::kBoundsChangeDirection_Horizontal |
66           WindowResizer::kBoundsChangeDirection_Vertical;
67       break;
68     case HTTOP:
69     case HTBOTTOM:
70       size_change_direction |= WindowResizer::kBoundsChangeDirection_Vertical;
71       break;
72     case HTRIGHT:
73     case HTLEFT:
74       size_change_direction |= WindowResizer::kBoundsChangeDirection_Horizontal;
75       break;
76     default:
77       break;
78   }
79   return size_change_direction;
80 }
81
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;
89 }
90
91 }  // namespace
92
93 // static
94 const int WindowResizer::kBoundsChange_None = 0;
95 // static
96 const int WindowResizer::kBoundsChange_Repositions = 1;
97 // static
98 const int WindowResizer::kBoundsChange_Resizes = 2;
99
100 // static
101 const int WindowResizer::kBoundsChangeDirection_None = 0;
102 // static
103 const int WindowResizer::kBoundsChangeDirection_Horizontal = 1;
104 // static
105 const int WindowResizer::kBoundsChangeDirection_Vertical = 2;
106
107 WindowResizer::Details::Details()
108     : window(NULL),
109       window_state(NULL),
110       window_component(HTNOWHERE),
111       bounds_change(0),
112       position_change_direction(0),
113       size_change_direction(0),
114       is_resizable(false),
115       source(aura::client::WINDOW_MOVE_SOURCE_MOUSE) {
116 }
117
118 WindowResizer::Details::Details(aura::Window* window,
119                                 const gfx::Point& location,
120                                 int window_component,
121                                 aura::client::WindowMoveSource source)
122     : window(window),
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),
135       source(source) {
136   if (window_state->IsNormalShowState() &&
137       window_state->HasRestoreBounds() &&
138       window_component == HTCAPTION)
139     restore_bounds = window_state->GetRestoreBoundsInScreen();
140 }
141
142 WindowResizer::Details::~Details() {
143 }
144
145 WindowResizer::WindowResizer() {
146 }
147
148 WindowResizer::~WindowResizer() {
149 }
150
151 // static
152 int WindowResizer::GetBoundsChangeForWindowComponent(int component) {
153   int bounds_change = WindowResizer::kBoundsChange_None;
154   switch (component) {
155     case HTTOPLEFT:
156     case HTTOP:
157     case HTTOPRIGHT:
158     case HTLEFT:
159     case HTBOTTOMLEFT:
160       bounds_change |= WindowResizer::kBoundsChange_Repositions |
161                       WindowResizer::kBoundsChange_Resizes;
162       break;
163     case HTCAPTION:
164       bounds_change |= WindowResizer::kBoundsChange_Repositions;
165       break;
166     case HTRIGHT:
167     case HTBOTTOMRIGHT:
168     case HTBOTTOM:
169     case HTGROWBOX:
170       bounds_change |= WindowResizer::kBoundsChange_Resizes;
171       break;
172     default:
173       break;
174   }
175   return bounds_change;
176 }
177
178 // static
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;
184
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();
188
189   AdjustDeltaForTouchResize(details, &delta_x, &delta_y);
190
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);
197
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
200   // on the size.
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());
210
211     work_area.Union(dock_layout->docked_bounds());
212     work_area = ScreenAsh::ConvertRectFromScreen(details.window->parent(),
213                                                  work_area);
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);
224       }
225     }
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());
241       }
242     }
243     if (details.bounds_change & kBoundsChange_Repositions &&
244         new_bounds.y() < 0) {
245       int delta = new_bounds.y();
246       new_bounds.set_y(0);
247       new_bounds.set_height(new_bounds.height() + delta);
248     }
249   }
250
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);
260     }
261
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());
276
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())));
286       new_bounds =
287           ScreenAsh::ConvertRectFromScreen(parent, new_bounds_in_screen);
288     }
289   }
290
291   return new_bounds;
292 }
293
294 // static
295 bool WindowResizer::IsBottomEdge(int window_component) {
296   return window_component == HTBOTTOMLEFT ||
297       window_component == HTBOTTOM ||
298       window_component == HTBOTTOMRIGHT ||
299       window_component == HTGROWBOX;
300 }
301
302 // static
303 void WindowResizer::AdjustDeltaForTouchResize(const Details& details,
304                                               int* delta_x,
305                                               int* delta_y) {
306   if (details.source != aura::client::WINDOW_MOVE_SOURCE_TOUCH ||
307       !(details.bounds_change & kBoundsChange_Resizes))
308     return;
309
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();
314     } else {
315       *delta_x += details.initial_location_in_parent.x() -
316           details.initial_bounds_in_parent.x();
317     }
318   }
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();
323     } else {
324       *delta_y += details.initial_location_in_parent.y() -
325           details.initial_bounds_in_parent.y();
326     }
327   }
328 }
329
330 // static
331 gfx::Point WindowResizer::GetOriginForDrag(const Details& details,
332                                            int delta_x,
333                                            int delta_y) {
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);
342   }
343   return origin;
344 }
345
346 // static
347 gfx::Size WindowResizer::GetSizeForDrag(const Details& details,
348                                         int* delta_x,
349                                         int* delta_y) {
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();
357   }
358   return size;
359 }
360
361 // static
362 int WindowResizer::GetWidthForDrag(const Details& details,
363                                    int min_width,
364                                    int* delta_x) {
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);
370
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) {
374       width = min_width;
375       *delta_x = -x_multiplier * (details.initial_bounds_in_parent.width() -
376                                   min_width);
377     }
378
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) {
386       width = max_width;
387       *delta_x = -x_multiplier * (details.initial_bounds_in_parent.width() -
388                                   max_width);
389     }
390   }
391   return width;
392 }
393
394 // static
395 int WindowResizer::GetHeightForDrag(const Details& details,
396                                     int min_height,
397                                     int* delta_y) {
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);
403
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) {
407       height = min_height;
408       *delta_y = -y_multiplier * (details.initial_bounds_in_parent.height() -
409                                   min_height);
410     }
411
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) {
419       height = max_height;
420       *delta_y = -y_multiplier * (details.initial_bounds_in_parent.height() -
421                                   max_height);
422     }
423   }
424   return height;
425 }
426
427 }  // namespace aura