OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/wm/window_resizer.h" | 5 #include "ash/wm/default_window_resizer.h" |
6 | 6 |
7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
8 #include "ash/wm/root_window_event_filter.h" | 8 #include "ash/wm/root_window_event_filter.h" |
9 #include "ui/aura/client/aura_constants.h" | 9 #include "ui/aura/client/aura_constants.h" |
10 #include "ui/aura/root_window.h" | 10 #include "ui/aura/root_window.h" |
11 #include "ui/aura/window.h" | 11 #include "ui/aura/window.h" |
12 #include "ui/aura/window_delegate.h" | 12 #include "ui/aura/window_delegate.h" |
13 #include "ui/base/hit_test.h" | 13 #include "ui/base/hit_test.h" |
14 #include "ui/base/ui_base_types.h" | 14 #include "ui/base/ui_base_types.h" |
15 #include "ui/gfx/compositor/scoped_layer_animation_settings.h" | 15 #include "ui/gfx/compositor/scoped_layer_animation_settings.h" |
16 #include "ui/gfx/screen.h" | 16 #include "ui/gfx/screen.h" |
17 | 17 |
18 namespace ash { | 18 namespace ash { |
19 | 19 |
20 namespace { | 20 DefaultWindowResizer::~DefaultWindowResizer() { |
21 | |
22 int GetPositionChangeDirectionForWindowComponent(int window_component) { | |
23 int pos_change_direction = WindowResizer::kBoundsChangeDirection_None; | |
24 switch (window_component) { | |
25 case HTTOPLEFT: | |
26 case HTBOTTOMRIGHT: | |
27 case HTGROWBOX: | |
28 case HTCAPTION: | |
29 pos_change_direction |= | |
30 WindowResizer::kBoundsChangeDirection_Horizontal | | |
31 WindowResizer::kBoundsChangeDirection_Vertical; | |
32 break; | |
33 case HTTOP: | |
34 case HTTOPRIGHT: | |
35 case HTBOTTOM: | |
36 pos_change_direction |= WindowResizer::kBoundsChangeDirection_Vertical; | |
37 break; | |
38 case HTBOTTOMLEFT: | |
39 case HTRIGHT: | |
40 case HTLEFT: | |
41 pos_change_direction |= WindowResizer::kBoundsChangeDirection_Horizontal; | |
42 break; | |
43 default: | |
44 break; | |
45 } | |
46 return pos_change_direction; | |
47 } | |
48 | |
49 int GetSizeChangeDirectionForWindowComponent(int window_component) { | |
50 int size_change_direction = WindowResizer::kBoundsChangeDirection_None; | |
51 switch (window_component) { | |
52 case HTTOPLEFT: | |
53 case HTTOPRIGHT: | |
54 case HTBOTTOMLEFT: | |
55 case HTBOTTOMRIGHT: | |
56 case HTGROWBOX: | |
57 case HTCAPTION: | |
58 size_change_direction |= | |
59 WindowResizer::kBoundsChangeDirection_Horizontal | | |
60 WindowResizer::kBoundsChangeDirection_Vertical; | |
61 break; | |
62 case HTTOP: | |
63 case HTBOTTOM: | |
64 size_change_direction |= WindowResizer::kBoundsChangeDirection_Vertical; | |
65 break; | |
66 case HTRIGHT: | |
67 case HTLEFT: | |
68 size_change_direction |= WindowResizer::kBoundsChangeDirection_Horizontal; | |
69 break; | |
70 default: | |
71 break; | |
72 } | |
73 return size_change_direction; | |
74 } | |
75 | |
76 // Returns true for resize components along the right edge, where a drag in | |
77 // positive x will make the window larger. | |
78 bool IsRightEdge(int window_component) { | |
79 return window_component == HTTOPRIGHT || | |
80 window_component == HTRIGHT || | |
81 window_component == HTBOTTOMRIGHT || | |
82 window_component == HTGROWBOX; | |
83 } | |
84 | |
85 // Returns true for resize components in along the bottom edge, where a drag | |
86 // in positive y will make the window larger. | |
87 bool IsBottomEdge(int window_component) { | |
88 return window_component == HTBOTTOMLEFT || | |
89 window_component == HTBOTTOM || | |
90 window_component == HTBOTTOMRIGHT || | |
91 window_component == HTGROWBOX; | |
92 } | |
93 | |
94 // Returns the closest location to |location| that is aligned to fall on | |
95 // increments of |grid_size|. | |
96 int AlignToGridRoundUp(int location, int grid_size) { | |
97 if (grid_size <= 1 || location % grid_size == 0) | |
98 return location; | |
99 return (location / grid_size + 1) * grid_size; | |
100 } | |
101 | |
102 gfx::Point ConvertPointToParent(aura::Window* window, | |
103 const gfx::Point& point) { | |
104 gfx::Point result(point); | |
105 aura::Window::ConvertPointToWindow(window, window->parent(), &result); | |
106 return result; | |
107 } | |
108 | |
109 } // namespace | |
110 | |
111 // static | |
112 const int WindowResizer::kBoundsChange_None = 0; | |
113 // static | |
114 const int WindowResizer::kBoundsChange_Repositions = 1; | |
115 // static | |
116 const int WindowResizer::kBoundsChange_Resizes = 2; | |
117 | |
118 // static | |
119 const int WindowResizer::kBoundsChangeDirection_None = 0; | |
120 // static | |
121 const int WindowResizer::kBoundsChangeDirection_Horizontal = 1; | |
122 // static | |
123 const int WindowResizer::kBoundsChangeDirection_Vertical = 2; | |
124 | |
125 WindowResizer::WindowResizer(aura::Window* window, | |
126 const gfx::Point& location, | |
127 int window_component, | |
128 int grid_size) | |
129 : window_(window), | |
130 initial_bounds_(window->bounds()), | |
131 initial_location_in_parent_(ConvertPointToParent(window, location)), | |
132 window_component_(window_component), | |
133 bounds_change_(GetBoundsChangeForWindowComponent(window_component_)), | |
134 position_change_direction_( | |
135 GetPositionChangeDirectionForWindowComponent(window_component_)), | |
136 size_change_direction_( | |
137 GetSizeChangeDirectionForWindowComponent(window_component_)), | |
138 is_resizable_(bounds_change_ != kBoundsChangeDirection_None), | |
139 grid_size_(grid_size), | |
140 did_move_or_resize_(false), | |
141 root_filter_(NULL) { | |
142 if (is_resizable_) { | |
143 root_filter_ = Shell::GetInstance()->root_filter(); | |
144 if (root_filter_) | |
145 root_filter_->LockCursor(); | |
146 } | |
147 } | |
148 | |
149 WindowResizer::~WindowResizer() { | |
150 if (root_filter_) | 21 if (root_filter_) |
151 root_filter_->UnlockCursor(); | 22 root_filter_->UnlockCursor(); |
152 } | 23 } |
153 | 24 |
154 // static | 25 // static |
155 int WindowResizer::GetBoundsChangeForWindowComponent(int component) { | 26 DefaultWindowResizer* |
156 int bounds_change = WindowResizer::kBoundsChange_None; | 27 DefaultWindowResizer::Create(aura::Window* window, |
157 switch (component) { | 28 const gfx::Point& location, |
158 case HTTOPLEFT: | 29 int window_component, |
159 case HTTOP: | 30 int grid_size) { |
160 case HTTOPRIGHT: | 31 Details details(window, location, window_component, grid_size); |
161 case HTLEFT: | 32 return details.is_resizable ? new DefaultWindowResizer(details) : NULL; |
162 case HTBOTTOMLEFT: | |
163 bounds_change |= WindowResizer::kBoundsChange_Repositions | | |
164 WindowResizer::kBoundsChange_Resizes; | |
165 break; | |
166 case HTCAPTION: | |
167 bounds_change |= WindowResizer::kBoundsChange_Repositions; | |
168 break; | |
169 case HTRIGHT: | |
170 case HTBOTTOMRIGHT: | |
171 case HTBOTTOM: | |
172 case HTGROWBOX: | |
173 bounds_change |= WindowResizer::kBoundsChange_Resizes; | |
174 break; | |
175 default: | |
176 break; | |
177 } | |
178 return bounds_change; | |
179 } | 33 } |
180 | 34 |
181 // static | 35 void DefaultWindowResizer::Drag(const gfx::Point& location) { |
182 int WindowResizer::AlignToGrid(int location, int grid_size) { | 36 gfx::Rect bounds(CalculateBoundsForDrag(details_, location)); |
183 if (grid_size <= 1 || location % grid_size == 0) | 37 if (bounds != details_.window->bounds()) { |
184 return location; | |
185 return floor(static_cast<float>(location) / static_cast<float>(grid_size) + | |
186 .5f) * grid_size; | |
187 } | |
188 | |
189 void WindowResizer::Drag(const gfx::Point& location) { | |
190 gfx::Rect bounds = GetBoundsForDrag(location); | |
191 if (bounds != window_->bounds()) { | |
192 did_move_or_resize_ = true; | 38 did_move_or_resize_ = true; |
193 window_->SetBounds(bounds); | 39 details_.window->SetBounds(bounds); |
194 } | 40 } |
195 } | 41 } |
196 | 42 |
197 void WindowResizer::CompleteDrag() { | 43 void DefaultWindowResizer::CompleteDrag() { |
198 if (grid_size_ <= 1 || !did_move_or_resize_) | 44 if (details_.grid_size <= 1 || !did_move_or_resize_) |
199 return; | 45 return; |
200 gfx::Rect new_bounds(GetFinalBounds()); | 46 gfx::Rect new_bounds(AdjustBoundsToGrid(details_)); |
201 if (new_bounds == window_->bounds()) | 47 if (new_bounds == details_.window->bounds()) |
202 return; | 48 return; |
203 | 49 |
204 if (new_bounds.size() != window_->bounds().size()) { | 50 if (new_bounds.size() != details_.window->bounds().size()) { |
205 // Don't attempt to animate a size change. | 51 // Don't attempt to animate a size change. |
206 window_->SetBounds(new_bounds); | 52 details_.window->SetBounds(new_bounds); |
207 return; | 53 return; |
208 } | 54 } |
209 | 55 |
210 ui::ScopedLayerAnimationSettings scoped_setter( | 56 ui::ScopedLayerAnimationSettings scoped_setter( |
211 window_->layer()->GetAnimator()); | 57 details_.window->layer()->GetAnimator()); |
212 // Use a small duration since the grid is small. | 58 // Use a small duration since the grid is small. |
213 scoped_setter.SetTransitionDuration(base::TimeDelta::FromMilliseconds(100)); | 59 scoped_setter.SetTransitionDuration(base::TimeDelta::FromMilliseconds(100)); |
214 window_->SetBounds(new_bounds); | 60 details_.window->SetBounds(new_bounds); |
215 } | 61 } |
216 | 62 |
217 void WindowResizer::RevertDrag() { | 63 void DefaultWindowResizer::RevertDrag() { |
218 if (!did_move_or_resize_) | 64 if (!did_move_or_resize_) |
219 return; | 65 return; |
220 | 66 |
221 window_->SetBounds(initial_bounds_); | 67 details_.window->SetBounds(details_.initial_bounds); |
222 } | 68 } |
223 | 69 |
224 gfx::Rect WindowResizer::GetBoundsForDrag(const gfx::Point& location) { | 70 DefaultWindowResizer::DefaultWindowResizer(const Details& details) |
225 if (!is_resizable()) | 71 : details_(details), |
226 return window_->bounds(); | 72 did_move_or_resize_(false), |
227 | 73 root_filter_(NULL) { |
228 // Dragging a window moves the local coordinate frame, so do arithmetic | 74 DCHECK(details_.is_resizable); |
229 // in the parent coordinate frame. | 75 root_filter_ = Shell::GetInstance()->root_filter(); |
230 gfx::Point event_location_in_parent(location); | 76 if (root_filter_) |
231 aura::Window::ConvertPointToWindow(window_, window_->parent(), | 77 root_filter_->LockCursor(); |
232 &event_location_in_parent); | |
233 int delta_x = event_location_in_parent.x() - initial_location_in_parent_.x(); | |
234 int delta_y = event_location_in_parent.y() - initial_location_in_parent_.y(); | |
235 | |
236 // The minimize size constraint may limit how much we change the window | |
237 // position. For example, dragging the left edge to the right should stop | |
238 // repositioning the window when the minimize size is reached. | |
239 gfx::Size size = GetSizeForDrag(&delta_x, &delta_y); | |
240 gfx::Point origin = GetOriginForDrag(delta_x, delta_y); | |
241 | |
242 gfx::Rect new_bounds(origin, size); | |
243 // Update bottom edge to stay in the work area when we are resizing | |
244 // by dragging the bottome edge or corners. | |
245 if (bounds_change_ & kBoundsChange_Resizes && | |
246 origin.y() == window_->bounds().y()) { | |
247 gfx::Rect work_area = gfx::Screen::GetMonitorWorkAreaNearestWindow(window_); | |
248 if (new_bounds.bottom() > work_area.bottom()) | |
249 new_bounds.Inset(0, 0, 0, | |
250 new_bounds.bottom() - work_area.bottom()); | |
251 } | |
252 if (bounds_change_ & kBoundsChange_Resizes && | |
253 bounds_change_ & kBoundsChange_Repositions && new_bounds.y() < 0) { | |
254 int delta = new_bounds.y(); | |
255 new_bounds.set_y(0); | |
256 new_bounds.set_height(new_bounds.height() + delta); | |
257 } | |
258 return new_bounds; | |
259 } | |
260 | |
261 gfx::Rect WindowResizer::GetFinalBounds() { | |
262 const gfx::Rect& bounds(window_->bounds()); | |
263 int x = AlignToGrid(bounds.x(), grid_size_); | |
264 int y = AlignToGrid(bounds.y(), grid_size_); | |
265 return gfx::Rect(x, y, bounds.width(), bounds.height()); | |
266 } | |
267 | |
268 gfx::Point WindowResizer::GetOriginForDrag( | |
269 int delta_x, | |
270 int delta_y) const { | |
271 gfx::Point origin = initial_bounds_.origin(); | |
272 if (bounds_change_ & kBoundsChange_Repositions) { | |
273 int pos_change_direction = | |
274 GetPositionChangeDirectionForWindowComponent(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); | |
279 } | |
280 return origin; | |
281 } | |
282 | |
283 gfx::Size WindowResizer::GetSizeForDrag( | |
284 int* delta_x, | |
285 int* delta_y) const { | |
286 gfx::Size size = initial_bounds_.size(); | |
287 if (bounds_change_ & kBoundsChange_Resizes) { | |
288 gfx::Size min_size = window_->delegate()->GetMinimumSize(); | |
289 min_size.set_width(AlignToGridRoundUp(min_size.width(), grid_size_)); | |
290 min_size.set_height(AlignToGridRoundUp(min_size.height(), grid_size_)); | |
291 size.SetSize(GetWidthForDrag(min_size.width(), delta_x), | |
292 GetHeightForDrag(min_size.height(), delta_y)); | |
293 } | |
294 return size; | |
295 } | |
296 | |
297 int WindowResizer::GetWidthForDrag(int min_width, int* delta_x) const { | |
298 int width = initial_bounds_.width(); | |
299 if (size_change_direction_ & kBoundsChangeDirection_Horizontal) { | |
300 // Along the right edge, positive delta_x increases the window size. | |
301 int x_multiplier = IsRightEdge(window_component_) ? 1 : -1; | |
302 width += x_multiplier * (*delta_x); | |
303 int adjusted_width = AlignToGrid(width, grid_size_); | |
304 if (adjusted_width != width) { | |
305 *delta_x += -x_multiplier * (width - adjusted_width); | |
306 width = adjusted_width; | |
307 } | |
308 | |
309 // Ensure we don't shrink past the minimum width and clamp delta_x | |
310 // for the window origin computation. | |
311 if (width < min_width) { | |
312 width = min_width; | |
313 *delta_x = -x_multiplier * (initial_bounds_.width() - min_width); | |
314 } | |
315 | |
316 // And don't let the window go bigger than the monitor. | |
317 int max_width = | |
318 gfx::Screen::GetMonitorAreaNearestWindow(window_).width(); | |
319 if (width > max_width) { | |
320 width = max_width; | |
321 *delta_x = -x_multiplier * (initial_bounds_.width() - max_width); | |
322 } | |
323 } | |
324 return width; | |
325 } | |
326 | |
327 int WindowResizer::GetHeightForDrag(int min_height, int* delta_y) const { | |
328 int height = initial_bounds_.height(); | |
329 if (size_change_direction_ & kBoundsChangeDirection_Vertical) { | |
330 // Along the bottom edge, positive delta_y increases the window size. | |
331 int y_multiplier = IsBottomEdge(window_component_) ? 1 : -1; | |
332 height += y_multiplier * (*delta_y); | |
333 int adjusted_height = AlignToGrid(height, grid_size_); | |
334 if (height != adjusted_height) { | |
335 *delta_y += -y_multiplier * (height - adjusted_height); | |
336 height = adjusted_height; | |
337 } | |
338 | |
339 // Ensure we don't shrink past the minimum height and clamp delta_y | |
340 // for the window origin computation. | |
341 if (height < min_height) { | |
342 height = min_height; | |
343 *delta_y = -y_multiplier * (initial_bounds_.height() - min_height); | |
344 } | |
345 | |
346 // And don't let the window go bigger than the monitor. | |
347 int max_height = gfx::Screen::GetMonitorAreaNearestWindow(window_).height(); | |
348 if (height > max_height) { | |
349 height = max_height; | |
350 *delta_y = -y_multiplier * (initial_bounds_.height() - max_height); | |
351 } | |
352 } | |
353 return height; | |
354 } | 78 } |
355 | 79 |
356 } // namespace aura | 80 } // namespace aura |
OLD | NEW |