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/workspace/workspace_window_resizer.h"
12 #include "ash/display/display_controller.h"
13 #include "ash/screen_ash.h"
14 #include "ash/shell.h"
15 #include "ash/shell_window_ids.h"
16 #include "ash/wm/coordinate_conversion.h"
17 #include "ash/wm/default_window_resizer.h"
18 #include "ash/wm/drag_window_resizer.h"
19 #include "ash/wm/panels/panel_window_resizer.h"
20 #include "ash/wm/property_util.h"
21 #include "ash/wm/window_properties.h"
22 #include "ash/wm/window_util.h"
23 #include "ash/wm/workspace/phantom_window_controller.h"
24 #include "ash/wm/workspace/snap_sizer.h"
25 #include "ui/aura/client/aura_constants.h"
26 #include "ui/aura/client/window_types.h"
27 #include "ui/aura/root_window.h"
28 #include "ui/aura/window.h"
29 #include "ui/aura/window_delegate.h"
30 #include "ui/base/hit_test.h"
31 #include "ui/compositor/layer.h"
32 #include "ui/gfx/screen.h"
33 #include "ui/gfx/transform.h"
37 scoped_ptr
<WindowResizer
> CreateWindowResizer(aura::Window
* window
,
38 const gfx::Point
& point_in_parent
,
39 int window_component
) {
41 // No need to return a resizer when the window cannot get resized.
42 if (!wm::CanResizeWindow(window
) && window_component
!= HTCAPTION
)
43 return scoped_ptr
<WindowResizer
>();
45 WindowResizer
* window_resizer
= NULL
;
46 if (window
->parent() &&
47 window
->parent()->id() == internal::kShellWindowId_WorkspaceContainer
) {
48 // Allow dragging maximized windows if it's not tracked by workspace. This
49 // is set by tab dragging code.
50 if (!wm::IsWindowNormal(window
) &&
51 (window_component
!= HTCAPTION
|| GetTrackedByWorkspace(window
)))
52 return scoped_ptr
<WindowResizer
>();
53 window_resizer
= internal::WorkspaceWindowResizer::Create(
57 std::vector
<aura::Window
*>());
58 } else if (wm::IsWindowNormal(window
)) {
59 window_resizer
= DefaultWindowResizer::Create(
60 window
, point_in_parent
, window_component
);
63 window_resizer
= internal::DragWindowResizer::Create(
64 window_resizer
, window
, point_in_parent
, window_component
);
66 if (window_resizer
&& window
->type() == aura::client::WINDOW_TYPE_PANEL
) {
67 window_resizer
= PanelWindowResizer::Create(
68 window_resizer
, window
, point_in_parent
, window_component
);
70 return make_scoped_ptr
<WindowResizer
>(window_resizer
);
77 // Duration of the animation when snapping the window into place.
78 const int kSnapDurationMS
= 100;
80 // Returns true if should snap to the edge.
81 bool ShouldSnapToEdge(int distance_from_edge
, int grid_size
) {
82 return distance_from_edge
< grid_size
&&
83 distance_from_edge
> -grid_size
* 2;
86 // Returns the coordinate along the secondary axis to snap to.
87 int CoordinateAlongSecondaryAxis(SecondaryMagnetismEdge edge
,
92 case SECONDARY_MAGNETISM_EDGE_LEADING
:
94 case SECONDARY_MAGNETISM_EDGE_TRAILING
:
96 case SECONDARY_MAGNETISM_EDGE_NONE
:
103 // Returns the origin for |src| when magnetically attaching to |attach_to| along
104 // the edges |edges|. |edges| is a bitmask of the MagnetismEdges.
105 gfx::Point
OriginForMagneticAttach(const gfx::Rect
& src
,
106 const gfx::Rect
& attach_to
,
107 const MatchedEdge
& edge
) {
109 switch (edge
.primary_edge
) {
110 case MAGNETISM_EDGE_TOP
:
111 y
= attach_to
.bottom();
113 case MAGNETISM_EDGE_LEFT
:
114 x
= attach_to
.right();
116 case MAGNETISM_EDGE_BOTTOM
:
117 y
= attach_to
.y() - src
.height();
119 case MAGNETISM_EDGE_RIGHT
:
120 x
= attach_to
.x() - src
.width();
123 switch (edge
.primary_edge
) {
124 case MAGNETISM_EDGE_TOP
:
125 case MAGNETISM_EDGE_BOTTOM
:
126 x
= CoordinateAlongSecondaryAxis(
127 edge
.secondary_edge
, attach_to
.x(), attach_to
.right() - src
.width(),
130 case MAGNETISM_EDGE_LEFT
:
131 case MAGNETISM_EDGE_RIGHT
:
132 y
= CoordinateAlongSecondaryAxis(
133 edge
.secondary_edge
, attach_to
.y(), attach_to
.bottom() - src
.height(),
137 return gfx::Point(x
, y
);
140 // Returns the bounds for a magnetic attach when resizing. |src| is the bounds
141 // of window being resized, |attach_to| the bounds of the window to attach to
142 // and |edge| identifies the edge to attach to.
143 gfx::Rect
BoundsForMagneticResizeAttach(const gfx::Rect
& src
,
144 const gfx::Rect
& attach_to
,
145 const MatchedEdge
& edge
) {
149 int h
= src
.height();
150 gfx::Point
attach_origin(OriginForMagneticAttach(src
, attach_to
, edge
));
151 switch (edge
.primary_edge
) {
152 case MAGNETISM_EDGE_LEFT
:
153 x
= attach_origin
.x();
156 case MAGNETISM_EDGE_RIGHT
:
157 w
+= attach_origin
.x() - src
.x();
159 case MAGNETISM_EDGE_TOP
:
160 y
= attach_origin
.y();
161 h
= src
.bottom() - y
;
163 case MAGNETISM_EDGE_BOTTOM
:
164 h
+= attach_origin
.y() - src
.y();
167 switch (edge
.primary_edge
) {
168 case MAGNETISM_EDGE_LEFT
:
169 case MAGNETISM_EDGE_RIGHT
:
170 if (edge
.secondary_edge
== SECONDARY_MAGNETISM_EDGE_LEADING
) {
171 y
= attach_origin
.y();
172 h
= src
.bottom() - y
;
173 } else if (edge
.secondary_edge
== SECONDARY_MAGNETISM_EDGE_TRAILING
) {
174 h
+= attach_origin
.y() - src
.y();
177 case MAGNETISM_EDGE_TOP
:
178 case MAGNETISM_EDGE_BOTTOM
:
179 if (edge
.secondary_edge
== SECONDARY_MAGNETISM_EDGE_LEADING
) {
180 x
= attach_origin
.x();
182 } else if (edge
.secondary_edge
== SECONDARY_MAGNETISM_EDGE_TRAILING
) {
183 w
+= attach_origin
.x() - src
.x();
187 return gfx::Rect(x
, y
, w
, h
);
190 // Converts a window comopnent edge to the magnetic edge to snap to.
191 uint32
WindowComponentToMagneticEdge(int window_component
) {
192 switch (window_component
) {
194 return MAGNETISM_EDGE_LEFT
| MAGNETISM_EDGE_TOP
;
196 return MAGNETISM_EDGE_TOP
| MAGNETISM_EDGE_RIGHT
;
198 return MAGNETISM_EDGE_LEFT
| MAGNETISM_EDGE_BOTTOM
;
200 return MAGNETISM_EDGE_RIGHT
| MAGNETISM_EDGE_BOTTOM
;
202 return MAGNETISM_EDGE_TOP
;
204 return MAGNETISM_EDGE_BOTTOM
;
206 return MAGNETISM_EDGE_RIGHT
;
208 return MAGNETISM_EDGE_LEFT
;
218 const int WorkspaceWindowResizer::kMinOnscreenSize
= 20;
221 const int WorkspaceWindowResizer::kMinOnscreenHeight
= 32;
224 const int WorkspaceWindowResizer::kScreenEdgeInset
= 8;
226 // Represents the width or height of a window with constraints on its minimum
227 // and maximum size. 0 represents a lack of a constraint.
230 WindowSize(int size
, int min
, int max
)
234 // Grow the min/max bounds to include the starting size.
235 if (is_underflowing())
237 if (is_overflowing())
241 bool is_at_capacity(bool shrinking
) {
242 return size_
== (shrinking
? min_
: max_
);
249 bool has_min() const {
253 bool has_max() const {
257 bool is_valid() const {
258 return !is_overflowing() && !is_underflowing();
261 bool is_overflowing() const {
262 return has_max() && size_
> max_
;
265 bool is_underflowing() const {
266 return has_min() && size_
< min_
;
269 // Add |amount| to this WindowSize not exceeding min or max size constraints.
270 // Returns by how much |size_| + |amount| exceeds the min/max constraints.
271 int Add(int amount
) {
273 int new_value
= size_
+ amount
;
275 if (has_min() && new_value
< min_
) {
277 return new_value
- min_
;
280 if (has_max() && new_value
> max_
) {
282 return new_value
- max_
;
295 WorkspaceWindowResizer::~WorkspaceWindowResizer() {
296 Shell
* shell
= Shell::GetInstance();
297 shell
->cursor_manager()->UnlockCursor();
301 WorkspaceWindowResizer
* WorkspaceWindowResizer::Create(
302 aura::Window
* window
,
303 const gfx::Point
& location_in_parent
,
304 int window_component
,
305 const std::vector
<aura::Window
*>& attached_windows
) {
306 Details
details(window
, location_in_parent
, window_component
);
307 return details
.is_resizable
?
308 new WorkspaceWindowResizer(details
, attached_windows
) : NULL
;
311 void WorkspaceWindowResizer::Drag(const gfx::Point
& location_in_parent
,
313 last_mouse_location_
= location_in_parent
;
315 const int snap_size
=
316 event_flags
& ui::EF_CONTROL_DOWN
? 0 : kScreenEdgeInset
;
317 // |bounds| is in |window()->parent()|'s coordinates.
318 gfx::Rect bounds
= CalculateBoundsForDrag(details_
, location_in_parent
);
320 if (wm::IsWindowNormal(window()))
321 AdjustBoundsForMainWindow(snap_size
, &bounds
);
323 if (bounds
!= window()->bounds()) {
324 if (!did_move_or_resize_
) {
325 if (!details_
.restore_bounds
.IsEmpty())
326 ClearRestoreBounds(window());
329 did_move_or_resize_
= true;
332 gfx::Point location_in_screen
= location_in_parent
;
333 wm::ConvertPointToScreen(window()->parent(), &location_in_screen
);
334 const bool in_original_root
=
335 wm::GetRootWindowAt(location_in_screen
) == window()->GetRootWindow();
336 // Hide a phantom window for snapping if the cursor is in another root window.
337 if (in_original_root
&& wm::CanResizeWindow(window())) {
338 UpdateSnapPhantomWindow(location_in_parent
, bounds
);
340 snap_type_
= SNAP_NONE
;
341 snap_phantom_window_controller_
.reset();
344 if (!attached_windows_
.empty())
345 LayoutAttachedWindows(&bounds
);
346 if (bounds
!= window()->bounds())
347 window()->SetBounds(bounds
);
350 void WorkspaceWindowResizer::CompleteDrag(int event_flags
) {
351 wm::SetUserHasChangedWindowPositionOrSize(details_
.window
, true);
352 snap_phantom_window_controller_
.reset();
353 if (!did_move_or_resize_
|| details_
.window_component
!= HTCAPTION
)
356 // When the window is not in the normal show state, we do not snap thw window.
357 // This happens when the user minimizes or maximizes the window by keyboard
358 // shortcut while dragging it. If the window is the result of dragging a tab
359 // out of a maximized window, it's already in the normal show state when this
360 // is called, so it does not matter.
361 if (wm::IsWindowNormal(window()) &&
362 (window()->type() != aura::client::WINDOW_TYPE_PANEL
||
363 !window()->GetProperty(kPanelAttachedKey
)) &&
364 (snap_type_
== SNAP_LEFT_EDGE
|| snap_type_
== SNAP_RIGHT_EDGE
)) {
365 if (!GetRestoreBoundsInScreen(window())) {
366 gfx::Rect initial_bounds
= ScreenAsh::ConvertRectToScreen(
367 window()->parent(), details_
.initial_bounds_in_parent
);
368 SetRestoreBoundsInScreen(window(), details_
.restore_bounds
.IsEmpty() ?
370 details_
.restore_bounds
);
372 window()->SetBounds(snap_sizer_
->target_bounds());
377 void WorkspaceWindowResizer::RevertDrag() {
378 snap_phantom_window_controller_
.reset();
380 if (!did_move_or_resize_
)
383 window()->SetBounds(details_
.initial_bounds_in_parent
);
384 if (!details_
.restore_bounds
.IsEmpty())
385 SetRestoreBoundsInScreen(details_
.window
, details_
.restore_bounds
);
387 if (details_
.window_component
== HTRIGHT
) {
388 int last_x
= details_
.initial_bounds_in_parent
.right();
389 for (size_t i
= 0; i
< attached_windows_
.size(); ++i
) {
390 gfx::Rect
bounds(attached_windows_
[i
]->bounds());
391 bounds
.set_x(last_x
);
392 bounds
.set_width(initial_size_
[i
]);
393 attached_windows_
[i
]->SetBounds(bounds
);
394 last_x
= attached_windows_
[i
]->bounds().right();
397 int last_y
= details_
.initial_bounds_in_parent
.bottom();
398 for (size_t i
= 0; i
< attached_windows_
.size(); ++i
) {
399 gfx::Rect
bounds(attached_windows_
[i
]->bounds());
400 bounds
.set_y(last_y
);
401 bounds
.set_height(initial_size_
[i
]);
402 attached_windows_
[i
]->SetBounds(bounds
);
403 last_y
= attached_windows_
[i
]->bounds().bottom();
408 aura::Window
* WorkspaceWindowResizer::GetTarget() {
409 return details_
.window
;
412 WorkspaceWindowResizer::WorkspaceWindowResizer(
413 const Details
& details
,
414 const std::vector
<aura::Window
*>& attached_windows
)
416 attached_windows_(attached_windows
),
417 did_move_or_resize_(false),
419 total_initial_size_(0),
420 snap_type_(SNAP_NONE
),
421 num_mouse_moves_since_bounds_change_(0),
422 magnetism_window_(NULL
) {
423 DCHECK(details_
.is_resizable
);
425 Shell
* shell
= Shell::GetInstance();
426 shell
->cursor_manager()->LockCursor();
428 // Only support attaching to the right/bottom.
429 DCHECK(attached_windows_
.empty() ||
430 (details
.window_component
== HTRIGHT
||
431 details
.window_component
== HTBOTTOM
));
433 // TODO: figure out how to deal with window going off the edge.
435 // Calculate sizes so that we can maintain the ratios if we need to resize.
436 int total_available
= 0;
437 for (size_t i
= 0; i
< attached_windows_
.size(); ++i
) {
438 gfx::Size
min(attached_windows_
[i
]->delegate()->GetMinimumSize());
439 int initial_size
= PrimaryAxisSize(attached_windows_
[i
]->bounds().size());
440 initial_size_
.push_back(initial_size
);
441 // If current size is smaller than the min, use the current size as the min.
442 // This way we don't snap on resize.
443 int min_size
= std::min(initial_size
,
444 std::max(PrimaryAxisSize(min
), kMinOnscreenSize
));
445 total_min_
+= min_size
;
446 total_initial_size_
+= initial_size
;
447 total_available
+= std::max(min_size
, initial_size
) - min_size
;
451 gfx::Rect
WorkspaceWindowResizer::GetFinalBounds(
452 const gfx::Rect
& bounds
) const {
453 if (snap_phantom_window_controller_
.get() &&
454 snap_phantom_window_controller_
->IsShowing()) {
455 return snap_phantom_window_controller_
->bounds();
460 void WorkspaceWindowResizer::LayoutAttachedWindows(
462 gfx::Rect
work_area(ScreenAsh::GetDisplayWorkAreaBoundsInParent(window()));
463 int initial_size
= PrimaryAxisSize(details_
.initial_bounds_in_parent
.size());
464 int current_size
= PrimaryAxisSize(bounds
->size());
465 int start
= PrimaryAxisCoordinate(bounds
->right(), bounds
->bottom());
466 int end
= PrimaryAxisCoordinate(work_area
.right(), work_area
.bottom());
468 int delta
= current_size
- initial_size
;
469 int available_size
= end
- start
;
470 std::vector
<int> sizes
;
471 int leftovers
= CalculateAttachedSizes(delta
, available_size
, &sizes
);
473 // leftovers > 0 means that the attached windows can't grow to compensate for
474 // the shrinkage of the main window. This line causes the attached windows to
475 // be moved so they are still flush against the main window, rather than the
476 // main window being prevented from shrinking.
477 leftovers
= std::min(0, leftovers
);
478 // Reallocate any leftover pixels back into the main window. This is
479 // necessary when, for example, the main window shrinks, but none of the
480 // attached windows can grow without exceeding their max size constraints.
481 // Adding the pixels back to the main window effectively prevents the main
482 // window from resizing too far.
483 if (details_
.window_component
== HTRIGHT
)
484 bounds
->set_width(bounds
->width() + leftovers
);
486 bounds
->set_height(bounds
->height() + leftovers
);
488 DCHECK_EQ(attached_windows_
.size(), sizes
.size());
489 int last
= PrimaryAxisCoordinate(bounds
->right(), bounds
->bottom());
490 for (size_t i
= 0; i
< attached_windows_
.size(); ++i
) {
491 gfx::Rect
attached_bounds(attached_windows_
[i
]->bounds());
492 if (details_
.window_component
== HTRIGHT
) {
493 attached_bounds
.set_x(last
);
494 attached_bounds
.set_width(sizes
[i
]);
496 attached_bounds
.set_y(last
);
497 attached_bounds
.set_height(sizes
[i
]);
499 attached_windows_
[i
]->SetBounds(attached_bounds
);
504 int WorkspaceWindowResizer::CalculateAttachedSizes(
507 std::vector
<int>* sizes
) const {
508 std::vector
<WindowSize
> window_sizes
;
509 CreateBucketsForAttached(&window_sizes
);
511 // How much we need to grow the attached by (collectively).
512 int grow_attached_by
= 0;
514 // If the attached windows don't fit when at their initial size, we will
515 // have to shrink them by how much they overflow.
516 if (total_initial_size_
>= available_size
)
517 grow_attached_by
= available_size
- total_initial_size_
;
519 // If we're shrinking, we grow the attached so the total size remains
521 grow_attached_by
= -delta
;
524 int leftover_pixels
= 0;
525 while (grow_attached_by
!= 0) {
526 int leftovers
= GrowFairly(grow_attached_by
, window_sizes
);
527 if (leftovers
== grow_attached_by
) {
528 leftover_pixels
= leftovers
;
531 grow_attached_by
= leftovers
;
534 for (size_t i
= 0; i
< window_sizes
.size(); ++i
)
535 sizes
->push_back(window_sizes
[i
].size());
537 return leftover_pixels
;
540 int WorkspaceWindowResizer::GrowFairly(
542 std::vector
<WindowSize
>& sizes
) const {
543 bool shrinking
= pixels
< 0;
544 std::vector
<WindowSize
*> nonfull_windows
;
545 for (size_t i
= 0; i
< sizes
.size(); ++i
) {
546 if (!sizes
[i
].is_at_capacity(shrinking
))
547 nonfull_windows
.push_back(&sizes
[i
]);
549 std::vector
<float> ratios
;
550 CalculateGrowthRatios(nonfull_windows
, &ratios
);
552 int remaining_pixels
= pixels
;
553 bool add_leftover_pixels_to_last
= true;
554 for (size_t i
= 0; i
< nonfull_windows
.size(); ++i
) {
555 int grow_by
= pixels
* ratios
[i
];
556 // Put any leftover pixels into the last window.
557 if (i
== nonfull_windows
.size() - 1 && add_leftover_pixels_to_last
)
558 grow_by
= remaining_pixels
;
559 int remainder
= nonfull_windows
[i
]->Add(grow_by
);
560 int consumed
= grow_by
- remainder
;
561 remaining_pixels
-= consumed
;
562 if (nonfull_windows
[i
]->is_at_capacity(shrinking
) && remainder
> 0) {
563 // Because this window overflowed, some of the pixels in
564 // |remaining_pixels| aren't there due to rounding errors. Rather than
565 // unfairly giving all those pixels to the last window, we refrain from
566 // allocating them so that this function can be called again to distribute
567 // the pixels fairly.
568 add_leftover_pixels_to_last
= false;
571 return remaining_pixels
;
574 void WorkspaceWindowResizer::CalculateGrowthRatios(
575 const std::vector
<WindowSize
*>& sizes
,
576 std::vector
<float>* out_ratios
) const {
577 DCHECK(out_ratios
->empty());
579 for (size_t i
= 0; i
< sizes
.size(); ++i
)
580 total_value
+= sizes
[i
]->size();
582 for (size_t i
= 0; i
< sizes
.size(); ++i
)
583 out_ratios
->push_back(
584 (static_cast<float>(sizes
[i
]->size())) / total_value
);
587 void WorkspaceWindowResizer::CreateBucketsForAttached(
588 std::vector
<WindowSize
>* sizes
) const {
589 for (size_t i
= 0; i
< attached_windows_
.size(); i
++) {
590 int initial_size
= initial_size_
[i
];
591 aura::WindowDelegate
* delegate
= attached_windows_
[i
]->delegate();
592 int min
= PrimaryAxisSize(delegate
->GetMinimumSize());
593 int max
= PrimaryAxisSize(delegate
->GetMaximumSize());
595 sizes
->push_back(WindowSize(initial_size
, min
, max
));
599 void WorkspaceWindowResizer::MagneticallySnapToOtherWindows(gfx::Rect
* bounds
) {
600 if (UpdateMagnetismWindow(*bounds
, kAllMagnetismEdges
)) {
602 OriginForMagneticAttach(*bounds
, magnetism_window_
->bounds(),
607 void WorkspaceWindowResizer::MagneticallySnapResizeToOtherWindows(
609 const uint32 edges
= WindowComponentToMagneticEdge(details_
.window_component
);
610 if (UpdateMagnetismWindow(*bounds
, edges
)) {
611 *bounds
= BoundsForMagneticResizeAttach(
612 *bounds
, magnetism_window_
->bounds(), magnetism_edge_
);
616 bool WorkspaceWindowResizer::UpdateMagnetismWindow(const gfx::Rect
& bounds
,
618 MagnetismMatcher
matcher(bounds
, edges
);
620 // If we snapped to a window then check it first. That way we don't bounce
621 // around when close to multiple edges.
622 if (magnetism_window_
) {
623 if (window_tracker_
.Contains(magnetism_window_
) &&
624 matcher
.ShouldAttach(magnetism_window_
->bounds(), &magnetism_edge_
)) {
627 window_tracker_
.Remove(magnetism_window_
);
628 magnetism_window_
= NULL
;
631 aura::Window
* parent
= window()->parent();
632 const aura::Window::Windows
& windows(parent
->children());
633 for (aura::Window::Windows::const_reverse_iterator i
= windows
.rbegin();
634 i
!= windows
.rend() && !matcher
.AreEdgesObscured(); ++i
) {
635 aura::Window
* other
= *i
;
636 if (other
== window() || !other
->IsVisible())
638 if (matcher
.ShouldAttach(other
->bounds(), &magnetism_edge_
)) {
639 magnetism_window_
= other
;
640 window_tracker_
.Add(magnetism_window_
);
647 void WorkspaceWindowResizer::AdjustBoundsForMainWindow(
650 gfx::Point last_mouse_location_in_screen
= last_mouse_location_
;
651 wm::ConvertPointToScreen(window()->parent(), &last_mouse_location_in_screen
);
652 gfx::Display display
= Shell::GetScreen()->GetDisplayNearestPoint(
653 last_mouse_location_in_screen
);
654 gfx::Rect work_area
=
655 ScreenAsh::ConvertRectFromScreen(window()->parent(), display
.work_area());
656 if (details_
.window_component
== HTCAPTION
) {
657 // Adjust the bounds to the work area where the mouse cursor is located.
658 // Always keep kMinOnscreenHeight on the bottom.
659 int max_y
= work_area
.bottom() - kMinOnscreenHeight
;
660 if (bounds
->y() > max_y
) {
661 bounds
->set_y(max_y
);
662 } else if (bounds
->y() <= work_area
.y()) {
663 // Don't allow dragging above the top of the display until the mouse
664 // cursor reaches the work area above if any.
665 bounds
->set_y(work_area
.y());
669 SnapToWorkAreaEdges(work_area
, snap_size
, bounds
);
670 MagneticallySnapToOtherWindows(bounds
);
672 } else if (snap_size
> 0) {
673 MagneticallySnapResizeToOtherWindows(bounds
);
674 if (!magnetism_window_
&& snap_size
> 0)
675 SnapResizeToWorkAreaBounds(work_area
, snap_size
, bounds
);
678 if (attached_windows_
.empty())
681 if (details_
.window_component
== HTRIGHT
) {
682 bounds
->set_width(std::min(bounds
->width(),
683 work_area
.right() - total_min_
- bounds
->x()));
685 DCHECK_EQ(HTBOTTOM
, details_
.window_component
);
686 bounds
->set_height(std::min(bounds
->height(),
687 work_area
.bottom() - total_min_
- bounds
->y()));
691 void WorkspaceWindowResizer::SnapToWorkAreaEdges(
692 const gfx::Rect
& work_area
,
694 gfx::Rect
* bounds
) const {
695 const int left_edge
= work_area
.x();
696 const int right_edge
= work_area
.right();
697 const int top_edge
= work_area
.y();
698 const int bottom_edge
= work_area
.bottom();
699 if (ShouldSnapToEdge(bounds
->x() - left_edge
, snap_size
)) {
700 bounds
->set_x(left_edge
);
701 } else if (ShouldSnapToEdge(right_edge
- bounds
->right(),
703 bounds
->set_x(right_edge
- bounds
->width());
705 if (ShouldSnapToEdge(bounds
->y() - top_edge
, snap_size
)) {
706 bounds
->set_y(top_edge
);
707 } else if (ShouldSnapToEdge(bottom_edge
- bounds
->bottom(), snap_size
) &&
708 bounds
->height() < (bottom_edge
- top_edge
)) {
709 // Only snap to the bottom if the window is smaller than the work area.
710 // Doing otherwise can lead to window snapping in weird ways as it bounces
711 // between snapping to top then bottom.
712 bounds
->set_y(bottom_edge
- bounds
->height());
716 void WorkspaceWindowResizer::SnapResizeToWorkAreaBounds(
717 const gfx::Rect
& work_area
,
719 gfx::Rect
* bounds
) const {
720 const uint32 edges
= WindowComponentToMagneticEdge(details_
.window_component
);
721 const int left_edge
= work_area
.x();
722 const int right_edge
= work_area
.right();
723 const int top_edge
= work_area
.y();
724 const int bottom_edge
= work_area
.bottom();
725 if (edges
& MAGNETISM_EDGE_TOP
&&
726 ShouldSnapToEdge(bounds
->y() - top_edge
, snap_size
)) {
727 bounds
->set_height(bounds
->bottom() - top_edge
);
728 bounds
->set_y(top_edge
);
730 if (edges
& MAGNETISM_EDGE_LEFT
&&
731 ShouldSnapToEdge(bounds
->x() - left_edge
, snap_size
)) {
732 bounds
->set_width(bounds
->right() - left_edge
);
733 bounds
->set_x(left_edge
);
735 if (edges
& MAGNETISM_EDGE_BOTTOM
&&
736 ShouldSnapToEdge(bottom_edge
- bounds
->bottom(), snap_size
)) {
737 bounds
->set_height(bottom_edge
- bounds
->y());
739 if (edges
& MAGNETISM_EDGE_RIGHT
&&
740 ShouldSnapToEdge(right_edge
- bounds
->right(), snap_size
)) {
741 bounds
->set_width(right_edge
- bounds
->x());
745 int WorkspaceWindowResizer::PrimaryAxisSize(const gfx::Size
& size
) const {
746 return PrimaryAxisCoordinate(size
.width(), size
.height());
749 int WorkspaceWindowResizer::PrimaryAxisCoordinate(int x
, int y
) const {
750 switch (details_
.window_component
) {
761 void WorkspaceWindowResizer::UpdateSnapPhantomWindow(const gfx::Point
& location
,
762 const gfx::Rect
& bounds
) {
763 if (!did_move_or_resize_
|| details_
.window_component
!= HTCAPTION
)
766 if (!wm::CanSnapWindow(window()))
769 SnapType last_type
= snap_type_
;
770 snap_type_
= GetSnapType(location
);
771 if (snap_type_
== SNAP_NONE
|| snap_type_
!= last_type
) {
772 snap_phantom_window_controller_
.reset();
774 if (snap_type_
== SNAP_NONE
)
777 if (!snap_sizer_
.get()) {
778 SnapSizer::Edge edge
= (snap_type_
== SNAP_LEFT_EDGE
) ?
779 SnapSizer::LEFT_EDGE
: SnapSizer::RIGHT_EDGE
;
780 snap_sizer_
.reset(new SnapSizer(window(),
783 internal::SnapSizer::OTHER_INPUT
));
785 snap_sizer_
->Update(location
);
787 if (!snap_phantom_window_controller_
.get()) {
788 snap_phantom_window_controller_
.reset(
789 new PhantomWindowController(window()));
791 snap_phantom_window_controller_
->Show(ScreenAsh::ConvertRectToScreen(
792 window()->parent(), snap_sizer_
->target_bounds()));
795 void WorkspaceWindowResizer::RestackWindows() {
796 if (attached_windows_
.empty())
798 // Build a map from index in children to window, returning if there is a
799 // window with a different parent.
800 typedef std::map
<size_t, aura::Window
*> IndexToWindowMap
;
801 IndexToWindowMap map
;
802 aura::Window
* parent
= window()->parent();
803 const aura::Window::Windows
& windows(parent
->children());
804 map
[std::find(windows
.begin(), windows
.end(), window()) -
805 windows
.begin()] = window();
806 for (std::vector
<aura::Window
*>::const_iterator i
=
807 attached_windows_
.begin(); i
!= attached_windows_
.end(); ++i
) {
808 if ((*i
)->parent() != parent
)
811 std::find(windows
.begin(), windows
.end(), *i
) - windows
.begin();
815 // Reorder the windows starting at the topmost.
816 parent
->StackChildAtTop(map
.rbegin()->second
);
817 for (IndexToWindowMap::const_reverse_iterator i
= map
.rbegin();
819 aura::Window
* window
= i
->second
;
822 parent
->StackChildBelow(i
->second
, window
);
826 WorkspaceWindowResizer::SnapType
WorkspaceWindowResizer::GetSnapType(
827 const gfx::Point
& location
) const {
828 // TODO: this likely only wants total display area, not the area of a single
830 gfx::Rect
area(ScreenAsh::GetDisplayBoundsInParent(window()));
831 if (location
.x() <= area
.x())
832 return SNAP_LEFT_EDGE
;
833 if (location
.x() >= area
.right() - 1)
834 return SNAP_RIGHT_EDGE
;
838 } // namespace internal