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/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" |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 | 75 |
76 // Returns true for resize components along the right edge, where a drag in | 76 // Returns true for resize components along the right edge, where a drag in |
77 // positive x will make the window larger. | 77 // positive x will make the window larger. |
78 bool IsRightEdge(int window_component) { | 78 bool IsRightEdge(int window_component) { |
79 return window_component == HTTOPRIGHT || | 79 return window_component == HTTOPRIGHT || |
80 window_component == HTRIGHT || | 80 window_component == HTRIGHT || |
81 window_component == HTBOTTOMRIGHT || | 81 window_component == HTBOTTOMRIGHT || |
82 window_component == HTGROWBOX; | 82 window_component == HTGROWBOX; |
83 } | 83 } |
84 | 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 | 85 // Returns the closest location to |location| that is aligned to fall on |
95 // increments of |grid_size|. | 86 // increments of |grid_size|. |
96 int AlignToGridRoundUp(int location, int grid_size) { | 87 int AlignToGridRoundUp(int location, int grid_size) { |
97 if (grid_size <= 1 || location % grid_size == 0) | 88 if (grid_size <= 1 || location % grid_size == 0) |
98 return location; | 89 return location; |
99 return (location / grid_size + 1) * grid_size; | 90 return (location / grid_size + 1) * grid_size; |
100 } | 91 } |
101 | 92 |
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 | 93 } // namespace |
110 | 94 |
111 // static | 95 // static |
112 const int WindowResizer::kBoundsChange_None = 0; | 96 const int WindowResizer::kBoundsChange_None = 0; |
113 // static | 97 // static |
114 const int WindowResizer::kBoundsChange_Repositions = 1; | 98 const int WindowResizer::kBoundsChange_Repositions = 1; |
115 // static | 99 // static |
116 const int WindowResizer::kBoundsChange_Resizes = 2; | 100 const int WindowResizer::kBoundsChange_Resizes = 2; |
117 | 101 |
118 // static | 102 // static |
119 const int WindowResizer::kBoundsChangeDirection_None = 0; | 103 const int WindowResizer::kBoundsChangeDirection_None = 0; |
120 // static | 104 // static |
121 const int WindowResizer::kBoundsChangeDirection_Horizontal = 1; | 105 const int WindowResizer::kBoundsChangeDirection_Horizontal = 1; |
122 // static | 106 // static |
123 const int WindowResizer::kBoundsChangeDirection_Vertical = 2; | 107 const int WindowResizer::kBoundsChangeDirection_Vertical = 2; |
124 | 108 |
125 WindowResizer::WindowResizer(aura::Window* window, | 109 WindowResizer::Details::Details() |
126 const gfx::Point& location, | 110 : window(NULL), |
127 int window_component, | 111 window_component(HTNOWHERE), |
128 int grid_size) | 112 bounds_change(0), |
129 : window_(window), | 113 position_change_direction(0), |
130 initial_bounds_(window->bounds()), | 114 size_change_direction(0), |
131 initial_location_in_parent_(ConvertPointToParent(window, location)), | 115 is_resizable(false), |
132 window_component_(window_component), | 116 grid_size(0) { |
133 bounds_change_(GetBoundsChangeForWindowComponent(window_component_)), | 117 } |
134 position_change_direction_( | 118 |
135 GetPositionChangeDirectionForWindowComponent(window_component_)), | 119 WindowResizer::Details::Details(aura::Window* window, |
136 size_change_direction_( | 120 const gfx::Point& location, |
137 GetSizeChangeDirectionForWindowComponent(window_component_)), | 121 int window_component, |
138 is_resizable_(bounds_change_ != kBoundsChangeDirection_None), | 122 int grid_size) |
139 grid_size_(grid_size), | 123 : window(window), |
140 did_move_or_resize_(false), | 124 initial_bounds(window->bounds()), |
141 root_filter_(NULL) { | 125 initial_location_in_parent(location), |
142 if (is_resizable_) { | 126 window_component(window_component), |
143 root_filter_ = Shell::GetInstance()->root_filter(); | 127 bounds_change(GetBoundsChangeForWindowComponent(window_component)), |
144 if (root_filter_) | 128 position_change_direction( |
145 root_filter_->LockCursor(); | 129 GetPositionChangeDirectionForWindowComponent(window_component)), |
146 } | 130 size_change_direction( |
| 131 GetSizeChangeDirectionForWindowComponent(window_component)), |
| 132 is_resizable(bounds_change != kBoundsChangeDirection_None), |
| 133 grid_size(grid_size) { |
| 134 } |
| 135 |
| 136 WindowResizer::Details::~Details() { |
| 137 } |
| 138 |
| 139 WindowResizer::WindowResizer() { |
147 } | 140 } |
148 | 141 |
149 WindowResizer::~WindowResizer() { | 142 WindowResizer::~WindowResizer() { |
150 if (root_filter_) | |
151 root_filter_->UnlockCursor(); | |
152 } | 143 } |
153 | 144 |
154 // static | 145 // static |
155 int WindowResizer::GetBoundsChangeForWindowComponent(int component) { | 146 int WindowResizer::GetBoundsChangeForWindowComponent(int component) { |
156 int bounds_change = WindowResizer::kBoundsChange_None; | 147 int bounds_change = WindowResizer::kBoundsChange_None; |
157 switch (component) { | 148 switch (component) { |
158 case HTTOPLEFT: | 149 case HTTOPLEFT: |
159 case HTTOP: | 150 case HTTOP: |
160 case HTTOPRIGHT: | 151 case HTTOPRIGHT: |
161 case HTLEFT: | 152 case HTLEFT: |
(...skipping 17 matching lines...) Expand all Loading... |
179 } | 170 } |
180 | 171 |
181 // static | 172 // static |
182 int WindowResizer::AlignToGrid(int location, int grid_size) { | 173 int WindowResizer::AlignToGrid(int location, int grid_size) { |
183 if (grid_size <= 1 || location % grid_size == 0) | 174 if (grid_size <= 1 || location % grid_size == 0) |
184 return location; | 175 return location; |
185 return floor(static_cast<float>(location) / static_cast<float>(grid_size) + | 176 return floor(static_cast<float>(location) / static_cast<float>(grid_size) + |
186 .5f) * grid_size; | 177 .5f) * grid_size; |
187 } | 178 } |
188 | 179 |
189 void WindowResizer::Drag(const gfx::Point& location) { | 180 // static |
190 gfx::Rect bounds = GetBoundsForDrag(location); | 181 gfx::Rect WindowResizer::CalculateBoundsForDrag( |
191 if (bounds != window_->bounds()) { | 182 const Details& details, |
192 did_move_or_resize_ = true; | 183 const gfx::Point& location) { |
193 window_->SetBounds(bounds); | 184 if (!details.is_resizable) |
194 } | 185 return details.initial_bounds; |
195 } | |
196 | 186 |
197 void WindowResizer::CompleteDrag() { | 187 int delta_x = location.x() - details.initial_location_in_parent.x(); |
198 if (grid_size_ <= 1 || !did_move_or_resize_) | 188 int delta_y = location.y() - details.initial_location_in_parent.y(); |
199 return; | |
200 gfx::Rect new_bounds(GetFinalBounds()); | |
201 if (new_bounds == window_->bounds()) | |
202 return; | |
203 | |
204 if (new_bounds.size() != window_->bounds().size()) { | |
205 // Don't attempt to animate a size change. | |
206 window_->SetBounds(new_bounds); | |
207 return; | |
208 } | |
209 | |
210 ui::ScopedLayerAnimationSettings scoped_setter( | |
211 window_->layer()->GetAnimator()); | |
212 // Use a small duration since the grid is small. | |
213 scoped_setter.SetTransitionDuration(base::TimeDelta::FromMilliseconds(100)); | |
214 window_->SetBounds(new_bounds); | |
215 } | |
216 | |
217 void WindowResizer::RevertDrag() { | |
218 if (!did_move_or_resize_) | |
219 return; | |
220 | |
221 window_->SetBounds(initial_bounds_); | |
222 } | |
223 | |
224 gfx::Rect WindowResizer::GetBoundsForDrag(const gfx::Point& location) { | |
225 if (!is_resizable()) | |
226 return window_->bounds(); | |
227 | |
228 // Dragging a window moves the local coordinate frame, so do arithmetic | |
229 // in the parent coordinate frame. | |
230 gfx::Point event_location_in_parent(location); | |
231 aura::Window::ConvertPointToWindow(window_, window_->parent(), | |
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 | 189 |
236 // The minimize size constraint may limit how much we change the window | 190 // 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 | 191 // position. For example, dragging the left edge to the right should stop |
238 // repositioning the window when the minimize size is reached. | 192 // repositioning the window when the minimize size is reached. |
239 gfx::Size size = GetSizeForDrag(&delta_x, &delta_y); | 193 gfx::Size size = GetSizeForDrag(details, &delta_x, &delta_y); |
240 gfx::Point origin = GetOriginForDrag(delta_x, delta_y); | 194 gfx::Point origin = GetOriginForDrag(details, delta_x, delta_y); |
241 | 195 |
242 gfx::Rect new_bounds(origin, size); | 196 gfx::Rect new_bounds(origin, size); |
243 // Update bottom edge to stay in the work area when we are resizing | 197 // Update bottom edge to stay in the work area when we are resizing |
244 // by dragging the bottome edge or corners. | 198 // by dragging the bottome edge or corners. |
245 if (bounds_change_ & kBoundsChange_Resizes && | 199 if (details.bounds_change & kBoundsChange_Resizes && |
246 origin.y() == window_->bounds().y()) { | 200 origin.y() == details.window->bounds().y()) { |
247 gfx::Rect work_area = gfx::Screen::GetMonitorWorkAreaNearestWindow(window_); | 201 gfx::Rect work_area = gfx::Screen::GetMonitorWorkAreaNearestWindow( |
| 202 details.window); |
248 if (new_bounds.bottom() > work_area.bottom()) | 203 if (new_bounds.bottom() > work_area.bottom()) |
249 new_bounds.Inset(0, 0, 0, | 204 new_bounds.Inset(0, 0, 0, |
250 new_bounds.bottom() - work_area.bottom()); | 205 new_bounds.bottom() - work_area.bottom()); |
251 } | 206 } |
252 if (bounds_change_ & kBoundsChange_Resizes && | 207 if (details.bounds_change & kBoundsChange_Resizes && |
253 bounds_change_ & kBoundsChange_Repositions && new_bounds.y() < 0) { | 208 details.bounds_change & kBoundsChange_Repositions && new_bounds.y() < 0) { |
254 int delta = new_bounds.y(); | 209 int delta = new_bounds.y(); |
255 new_bounds.set_y(0); | 210 new_bounds.set_y(0); |
256 new_bounds.set_height(new_bounds.height() + delta); | 211 new_bounds.set_height(new_bounds.height() + delta); |
257 } | 212 } |
258 return new_bounds; | 213 return new_bounds; |
259 } | 214 } |
260 | 215 |
261 gfx::Rect WindowResizer::GetFinalBounds() { | 216 // static |
262 const gfx::Rect& bounds(window_->bounds()); | 217 gfx::Rect WindowResizer::AdjustBoundsToGrid(const Details& details) { |
263 int x = AlignToGrid(bounds.x(), grid_size_); | 218 const gfx::Rect& bounds(details.window->bounds()); |
264 int y = AlignToGrid(bounds.y(), grid_size_); | 219 if (details.grid_size <= 1) |
| 220 return bounds; |
| 221 int x = AlignToGrid(bounds.x(), details.grid_size); |
| 222 int y = AlignToGrid(bounds.y(), details.grid_size); |
265 return gfx::Rect(x, y, bounds.width(), bounds.height()); | 223 return gfx::Rect(x, y, bounds.width(), bounds.height()); |
266 } | 224 } |
267 | 225 |
268 gfx::Point WindowResizer::GetOriginForDrag( | 226 // static |
269 int delta_x, | 227 bool WindowResizer::IsBottomEdge(int window_component) { |
270 int delta_y) const { | 228 return window_component == HTBOTTOMLEFT || |
271 gfx::Point origin = initial_bounds_.origin(); | 229 window_component == HTBOTTOM || |
272 if (bounds_change_ & kBoundsChange_Repositions) { | 230 window_component == HTBOTTOMRIGHT || |
| 231 window_component == HTGROWBOX; |
| 232 } |
| 233 |
| 234 // static |
| 235 gfx::Point WindowResizer::GetOriginForDrag(const Details& details, |
| 236 int delta_x, |
| 237 int delta_y) { |
| 238 gfx::Point origin = details.initial_bounds.origin(); |
| 239 if (details.bounds_change & kBoundsChange_Repositions) { |
273 int pos_change_direction = | 240 int pos_change_direction = |
274 GetPositionChangeDirectionForWindowComponent(window_component_); | 241 GetPositionChangeDirectionForWindowComponent(details.window_component); |
275 if (pos_change_direction & kBoundsChangeDirection_Horizontal) | 242 if (pos_change_direction & kBoundsChangeDirection_Horizontal) |
276 origin.Offset(delta_x, 0); | 243 origin.Offset(delta_x, 0); |
277 if (pos_change_direction & kBoundsChangeDirection_Vertical) | 244 if (pos_change_direction & kBoundsChangeDirection_Vertical) |
278 origin.Offset(0, delta_y); | 245 origin.Offset(0, delta_y); |
279 } | 246 } |
280 return origin; | 247 return origin; |
281 } | 248 } |
282 | 249 |
283 gfx::Size WindowResizer::GetSizeForDrag( | 250 // static |
284 int* delta_x, | 251 gfx::Size WindowResizer::GetSizeForDrag(const Details& details, |
285 int* delta_y) const { | 252 int* delta_x, |
286 gfx::Size size = initial_bounds_.size(); | 253 int* delta_y) { |
287 if (bounds_change_ & kBoundsChange_Resizes) { | 254 gfx::Size size = details.initial_bounds.size(); |
288 gfx::Size min_size = window_->delegate()->GetMinimumSize(); | 255 if (details.bounds_change & kBoundsChange_Resizes) { |
289 min_size.set_width(AlignToGridRoundUp(min_size.width(), grid_size_)); | 256 gfx::Size min_size = details.window->delegate()->GetMinimumSize(); |
290 min_size.set_height(AlignToGridRoundUp(min_size.height(), grid_size_)); | 257 min_size.set_width(AlignToGridRoundUp(min_size.width(), details.grid_size)); |
291 size.SetSize(GetWidthForDrag(min_size.width(), delta_x), | 258 min_size.set_height(AlignToGridRoundUp(min_size.height(), |
292 GetHeightForDrag(min_size.height(), delta_y)); | 259 details.grid_size)); |
| 260 size.SetSize(GetWidthForDrag(details, min_size.width(), delta_x), |
| 261 GetHeightForDrag(details, min_size.height(), delta_y)); |
293 } | 262 } |
294 return size; | 263 return size; |
295 } | 264 } |
296 | 265 |
297 int WindowResizer::GetWidthForDrag(int min_width, int* delta_x) const { | 266 // static |
298 int width = initial_bounds_.width(); | 267 int WindowResizer::GetWidthForDrag(const Details& details, |
299 if (size_change_direction_ & kBoundsChangeDirection_Horizontal) { | 268 int min_width, |
| 269 int* delta_x) { |
| 270 int width = details.initial_bounds.width(); |
| 271 if (details.size_change_direction & kBoundsChangeDirection_Horizontal) { |
300 // Along the right edge, positive delta_x increases the window size. | 272 // Along the right edge, positive delta_x increases the window size. |
301 int x_multiplier = IsRightEdge(window_component_) ? 1 : -1; | 273 int x_multiplier = IsRightEdge(details.window_component) ? 1 : -1; |
302 width += x_multiplier * (*delta_x); | 274 width += x_multiplier * (*delta_x); |
303 int adjusted_width = AlignToGrid(width, grid_size_); | 275 int adjusted_width = AlignToGrid(width, details.grid_size); |
304 if (adjusted_width != width) { | 276 if (adjusted_width != width) { |
305 *delta_x += -x_multiplier * (width - adjusted_width); | 277 *delta_x += -x_multiplier * (width - adjusted_width); |
306 width = adjusted_width; | 278 width = adjusted_width; |
307 } | 279 } |
308 | 280 |
309 // Ensure we don't shrink past the minimum width and clamp delta_x | 281 // Ensure we don't shrink past the minimum width and clamp delta_x |
310 // for the window origin computation. | 282 // for the window origin computation. |
311 if (width < min_width) { | 283 if (width < min_width) { |
312 width = min_width; | 284 width = min_width; |
313 *delta_x = -x_multiplier * (initial_bounds_.width() - min_width); | 285 *delta_x = -x_multiplier * (details.initial_bounds.width() - min_width); |
314 } | 286 } |
315 | 287 |
316 // And don't let the window go bigger than the monitor. | 288 // And don't let the window go bigger than the monitor. |
317 int max_width = | 289 int max_width = |
318 gfx::Screen::GetMonitorAreaNearestWindow(window_).width(); | 290 gfx::Screen::GetMonitorAreaNearestWindow(details.window).width(); |
319 if (width > max_width) { | 291 if (width > max_width) { |
320 width = max_width; | 292 width = max_width; |
321 *delta_x = -x_multiplier * (initial_bounds_.width() - max_width); | 293 *delta_x = -x_multiplier * (details.initial_bounds.width() - max_width); |
322 } | 294 } |
323 } | 295 } |
324 return width; | 296 return width; |
325 } | 297 } |
326 | 298 |
327 int WindowResizer::GetHeightForDrag(int min_height, int* delta_y) const { | 299 // static |
328 int height = initial_bounds_.height(); | 300 int WindowResizer::GetHeightForDrag(const Details& details, |
329 if (size_change_direction_ & kBoundsChangeDirection_Vertical) { | 301 int min_height, |
| 302 int* delta_y) { |
| 303 int height = details.initial_bounds.height(); |
| 304 if (details.size_change_direction & kBoundsChangeDirection_Vertical) { |
330 // Along the bottom edge, positive delta_y increases the window size. | 305 // Along the bottom edge, positive delta_y increases the window size. |
331 int y_multiplier = IsBottomEdge(window_component_) ? 1 : -1; | 306 int y_multiplier = IsBottomEdge(details.window_component) ? 1 : -1; |
332 height += y_multiplier * (*delta_y); | 307 height += y_multiplier * (*delta_y); |
333 int adjusted_height = AlignToGrid(height, grid_size_); | 308 int adjusted_height = AlignToGrid(height, details.grid_size); |
334 if (height != adjusted_height) { | 309 if (height != adjusted_height) { |
335 *delta_y += -y_multiplier * (height - adjusted_height); | 310 *delta_y += -y_multiplier * (height - adjusted_height); |
336 height = adjusted_height; | 311 height = adjusted_height; |
337 } | 312 } |
338 | 313 |
339 // Ensure we don't shrink past the minimum height and clamp delta_y | 314 // Ensure we don't shrink past the minimum height and clamp delta_y |
340 // for the window origin computation. | 315 // for the window origin computation. |
341 if (height < min_height) { | 316 if (height < min_height) { |
342 height = min_height; | 317 height = min_height; |
343 *delta_y = -y_multiplier * (initial_bounds_.height() - min_height); | 318 *delta_y = -y_multiplier * (details.initial_bounds.height() - min_height); |
344 } | 319 } |
345 | 320 |
346 // And don't let the window go bigger than the monitor. | 321 // And don't let the window go bigger than the monitor. |
347 int max_height = gfx::Screen::GetMonitorAreaNearestWindow(window_).height(); | 322 int max_height = |
| 323 gfx::Screen::GetMonitorAreaNearestWindow(details.window).height(); |
348 if (height > max_height) { | 324 if (height > max_height) { |
349 height = max_height; | 325 height = max_height; |
350 *delta_y = -y_multiplier * (initial_bounds_.height() - max_height); | 326 *delta_y = -y_multiplier * (details.initial_bounds.height() - max_height); |
351 } | 327 } |
352 } | 328 } |
353 return height; | 329 return height; |
354 } | 330 } |
355 | 331 |
356 } // namespace aura | 332 } // namespace aura |
OLD | NEW |