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/ash_switches.h"
13 #include "ash/display/display_controller.h"
14 #include "ash/screen_ash.h"
15 #include "ash/shell.h"
16 #include "ash/shell_window_ids.h"
17 #include "ash/wm/coordinate_conversion.h"
18 #include "ash/wm/default_window_resizer.h"
19 #include "ash/wm/drag_window_resizer.h"
20 #include "ash/wm/panels/panel_window_resizer.h"
21 #include "ash/wm/property_util.h"
22 #include "ash/wm/window_properties.h"
23 #include "ash/wm/window_util.h"
24 #include "ash/wm/workspace/phantom_window_controller.h"
25 #include "ash/wm/workspace/snap_sizer.h"
26 #include "base/command_line.h"
27 #include "ui/aura/client/aura_constants.h"
28 #include "ui/aura/client/window_types.h"
29 #include "ui/aura/root_window.h"
30 #include "ui/aura/window.h"
31 #include "ui/aura/window_delegate.h"
32 #include "ui/base/hit_test.h"
33 #include "ui/compositor/layer.h"
34 #include "ui/gfx/screen.h"
35 #include "ui/gfx/transform.h"
39 scoped_ptr
<WindowResizer
> CreateWindowResizer(aura::Window
* window
,
40 const gfx::Point
& point_in_parent
,
41 int window_component
) {
43 // No need to return a resizer when the window cannot get resized.
44 if (!wm::CanResizeWindow(window
) && window_component
!= HTCAPTION
)
45 return scoped_ptr
<WindowResizer
>();
47 WindowResizer
* window_resizer
= NULL
;
48 if (window
->parent() &&
49 window
->parent()->id() == internal::kShellWindowId_WorkspaceContainer
) {
50 // Allow dragging maximized windows if it's not tracked by workspace. This
51 // is set by tab dragging code.
52 if (!wm::IsWindowNormal(window
) &&
53 (window_component
!= HTCAPTION
|| GetTrackedByWorkspace(window
)))
54 return scoped_ptr
<WindowResizer
>();
55 window_resizer
= internal::WorkspaceWindowResizer::Create(
59 std::vector
<aura::Window
*>());
60 } else if (wm::IsWindowNormal(window
)) {
61 window_resizer
= DefaultWindowResizer::Create(
62 window
, point_in_parent
, window_component
);
65 window_resizer
= internal::DragWindowResizer::Create(
66 window_resizer
, window
, point_in_parent
, window_component
);
68 if (window_resizer
&& window
->type() == aura::client::WINDOW_TYPE_PANEL
) {
69 window_resizer
= PanelWindowResizer::Create(
70 window_resizer
, window
, point_in_parent
, window_component
);
72 return make_scoped_ptr
<WindowResizer
>(window_resizer
);
79 // Distance in pixels that the cursor must move past an edge for a window
80 // to move or resize beyond that edge.
81 const int kStickyDistancePixels
= 64;
83 // Returns true if the window should stick to the edge.
84 bool ShouldStickToEdge(int distance_from_edge
, int sticky_size
) {
85 if (CommandLine::ForCurrentProcess()->HasSwitch(
86 switches::kAshEnableStickyEdges
)) {
87 return distance_from_edge
< 0 &&
88 distance_from_edge
> -sticky_size
;
90 return distance_from_edge
< sticky_size
&&
91 distance_from_edge
> -sticky_size
* 2;
94 // Returns the coordinate along the secondary axis to snap to.
95 int CoordinateAlongSecondaryAxis(SecondaryMagnetismEdge edge
,
100 case SECONDARY_MAGNETISM_EDGE_LEADING
:
102 case SECONDARY_MAGNETISM_EDGE_TRAILING
:
104 case SECONDARY_MAGNETISM_EDGE_NONE
:
111 // Returns the origin for |src| when magnetically attaching to |attach_to| along
112 // the edges |edges|. |edges| is a bitmask of the MagnetismEdges.
113 gfx::Point
OriginForMagneticAttach(const gfx::Rect
& src
,
114 const gfx::Rect
& attach_to
,
115 const MatchedEdge
& edge
) {
117 switch (edge
.primary_edge
) {
118 case MAGNETISM_EDGE_TOP
:
119 y
= attach_to
.bottom();
121 case MAGNETISM_EDGE_LEFT
:
122 x
= attach_to
.right();
124 case MAGNETISM_EDGE_BOTTOM
:
125 y
= attach_to
.y() - src
.height();
127 case MAGNETISM_EDGE_RIGHT
:
128 x
= attach_to
.x() - src
.width();
131 switch (edge
.primary_edge
) {
132 case MAGNETISM_EDGE_TOP
:
133 case MAGNETISM_EDGE_BOTTOM
:
134 x
= CoordinateAlongSecondaryAxis(
135 edge
.secondary_edge
, attach_to
.x(), attach_to
.right() - src
.width(),
138 case MAGNETISM_EDGE_LEFT
:
139 case MAGNETISM_EDGE_RIGHT
:
140 y
= CoordinateAlongSecondaryAxis(
141 edge
.secondary_edge
, attach_to
.y(), attach_to
.bottom() - src
.height(),
145 return gfx::Point(x
, y
);
148 // Returns the bounds for a magnetic attach when resizing. |src| is the bounds
149 // of window being resized, |attach_to| the bounds of the window to attach to
150 // and |edge| identifies the edge to attach to.
151 gfx::Rect
BoundsForMagneticResizeAttach(const gfx::Rect
& src
,
152 const gfx::Rect
& attach_to
,
153 const MatchedEdge
& edge
) {
157 int h
= src
.height();
158 gfx::Point
attach_origin(OriginForMagneticAttach(src
, attach_to
, edge
));
159 switch (edge
.primary_edge
) {
160 case MAGNETISM_EDGE_LEFT
:
161 x
= attach_origin
.x();
164 case MAGNETISM_EDGE_RIGHT
:
165 w
+= attach_origin
.x() - src
.x();
167 case MAGNETISM_EDGE_TOP
:
168 y
= attach_origin
.y();
169 h
= src
.bottom() - y
;
171 case MAGNETISM_EDGE_BOTTOM
:
172 h
+= attach_origin
.y() - src
.y();
175 switch (edge
.primary_edge
) {
176 case MAGNETISM_EDGE_LEFT
:
177 case MAGNETISM_EDGE_RIGHT
:
178 if (edge
.secondary_edge
== SECONDARY_MAGNETISM_EDGE_LEADING
) {
179 y
= attach_origin
.y();
180 h
= src
.bottom() - y
;
181 } else if (edge
.secondary_edge
== SECONDARY_MAGNETISM_EDGE_TRAILING
) {
182 h
+= attach_origin
.y() - src
.y();
185 case MAGNETISM_EDGE_TOP
:
186 case MAGNETISM_EDGE_BOTTOM
:
187 if (edge
.secondary_edge
== SECONDARY_MAGNETISM_EDGE_LEADING
) {
188 x
= attach_origin
.x();
190 } else if (edge
.secondary_edge
== SECONDARY_MAGNETISM_EDGE_TRAILING
) {
191 w
+= attach_origin
.x() - src
.x();
195 return gfx::Rect(x
, y
, w
, h
);
198 // Converts a window component edge to the magnetic edge to snap to.
199 uint32
WindowComponentToMagneticEdge(int window_component
) {
200 switch (window_component
) {
202 return MAGNETISM_EDGE_LEFT
| MAGNETISM_EDGE_TOP
;
204 return MAGNETISM_EDGE_TOP
| MAGNETISM_EDGE_RIGHT
;
206 return MAGNETISM_EDGE_LEFT
| MAGNETISM_EDGE_BOTTOM
;
208 return MAGNETISM_EDGE_RIGHT
| MAGNETISM_EDGE_BOTTOM
;
210 return MAGNETISM_EDGE_TOP
;
212 return MAGNETISM_EDGE_BOTTOM
;
214 return MAGNETISM_EDGE_RIGHT
;
216 return MAGNETISM_EDGE_LEFT
;
226 const int WorkspaceWindowResizer::kMinOnscreenSize
= 20;
229 const int WorkspaceWindowResizer::kMinOnscreenHeight
= 32;
232 const int WorkspaceWindowResizer::kScreenEdgeInset
= 8;
234 // Represents the width or height of a window with constraints on its minimum
235 // and maximum size. 0 represents a lack of a constraint.
238 WindowSize(int size
, int min
, int max
)
242 // Grow the min/max bounds to include the starting size.
243 if (is_underflowing())
245 if (is_overflowing())
249 bool is_at_capacity(bool shrinking
) {
250 return size_
== (shrinking
? min_
: max_
);
257 bool has_min() const {
261 bool has_max() const {
265 bool is_valid() const {
266 return !is_overflowing() && !is_underflowing();
269 bool is_overflowing() const {
270 return has_max() && size_
> max_
;
273 bool is_underflowing() const {
274 return has_min() && size_
< min_
;
277 // Add |amount| to this WindowSize not exceeding min or max size constraints.
278 // Returns by how much |size_| + |amount| exceeds the min/max constraints.
279 int Add(int amount
) {
281 int new_value
= size_
+ amount
;
283 if (has_min() && new_value
< min_
) {
285 return new_value
- min_
;
288 if (has_max() && new_value
> max_
) {
290 return new_value
- max_
;
303 WorkspaceWindowResizer::~WorkspaceWindowResizer() {
304 Shell
* shell
= Shell::GetInstance();
305 shell
->cursor_manager()->UnlockCursor();
309 WorkspaceWindowResizer
* WorkspaceWindowResizer::Create(
310 aura::Window
* window
,
311 const gfx::Point
& location_in_parent
,
312 int window_component
,
313 const std::vector
<aura::Window
*>& attached_windows
) {
314 Details
details(window
, location_in_parent
, window_component
);
315 return details
.is_resizable
?
316 new WorkspaceWindowResizer(details
, attached_windows
) : NULL
;
319 void WorkspaceWindowResizer::Drag(const gfx::Point
& location_in_parent
,
321 last_mouse_location_
= location_in_parent
;
324 if (event_flags
& ui::EF_CONTROL_DOWN
) {
326 } else if (CommandLine::ForCurrentProcess()->HasSwitch(
327 switches::kAshEnableStickyEdges
)) {
328 sticky_size
= kStickyDistancePixels
;
330 sticky_size
= kScreenEdgeInset
;
332 // |bounds| is in |window()->parent()|'s coordinates.
333 gfx::Rect bounds
= CalculateBoundsForDrag(details_
, location_in_parent
);
335 if (wm::IsWindowNormal(window()))
336 AdjustBoundsForMainWindow(sticky_size
, &bounds
);
338 if (bounds
!= window()->bounds()) {
339 if (!did_move_or_resize_
) {
340 if (!details_
.restore_bounds
.IsEmpty())
341 ClearRestoreBounds(window());
344 did_move_or_resize_
= true;
347 gfx::Point location_in_screen
= location_in_parent
;
348 wm::ConvertPointToScreen(window()->parent(), &location_in_screen
);
349 const bool in_original_root
=
350 wm::GetRootWindowAt(location_in_screen
) == window()->GetRootWindow();
351 // Hide a phantom window for snapping if the cursor is in another root window.
352 if (in_original_root
&& wm::CanResizeWindow(window())) {
353 UpdateSnapPhantomWindow(location_in_parent
, bounds
);
355 snap_type_
= SNAP_NONE
;
356 snap_phantom_window_controller_
.reset();
359 if (!attached_windows_
.empty())
360 LayoutAttachedWindows(&bounds
);
361 if (bounds
!= window()->bounds())
362 window()->SetBounds(bounds
);
365 void WorkspaceWindowResizer::CompleteDrag(int event_flags
) {
366 wm::SetUserHasChangedWindowPositionOrSize(details_
.window
, true);
367 snap_phantom_window_controller_
.reset();
368 if (!did_move_or_resize_
|| details_
.window_component
!= HTCAPTION
)
371 // When the window is not in the normal show state, we do not snap thw window.
372 // This happens when the user minimizes or maximizes the window by keyboard
373 // shortcut while dragging it. If the window is the result of dragging a tab
374 // out of a maximized window, it's already in the normal show state when this
375 // is called, so it does not matter.
376 if (wm::IsWindowNormal(window()) &&
377 (window()->type() != aura::client::WINDOW_TYPE_PANEL
||
378 !window()->GetProperty(kPanelAttachedKey
)) &&
379 (snap_type_
== SNAP_LEFT_EDGE
|| snap_type_
== SNAP_RIGHT_EDGE
)) {
380 if (!GetRestoreBoundsInScreen(window())) {
381 gfx::Rect initial_bounds
= ScreenAsh::ConvertRectToScreen(
382 window()->parent(), details_
.initial_bounds_in_parent
);
383 SetRestoreBoundsInScreen(window(), details_
.restore_bounds
.IsEmpty() ?
385 details_
.restore_bounds
);
387 window()->SetBounds(snap_sizer_
->target_bounds());
392 void WorkspaceWindowResizer::RevertDrag() {
393 snap_phantom_window_controller_
.reset();
395 if (!did_move_or_resize_
)
398 window()->SetBounds(details_
.initial_bounds_in_parent
);
399 if (!details_
.restore_bounds
.IsEmpty())
400 SetRestoreBoundsInScreen(details_
.window
, details_
.restore_bounds
);
402 if (details_
.window_component
== HTRIGHT
) {
403 int last_x
= details_
.initial_bounds_in_parent
.right();
404 for (size_t i
= 0; i
< attached_windows_
.size(); ++i
) {
405 gfx::Rect
bounds(attached_windows_
[i
]->bounds());
406 bounds
.set_x(last_x
);
407 bounds
.set_width(initial_size_
[i
]);
408 attached_windows_
[i
]->SetBounds(bounds
);
409 last_x
= attached_windows_
[i
]->bounds().right();
412 int last_y
= details_
.initial_bounds_in_parent
.bottom();
413 for (size_t i
= 0; i
< attached_windows_
.size(); ++i
) {
414 gfx::Rect
bounds(attached_windows_
[i
]->bounds());
415 bounds
.set_y(last_y
);
416 bounds
.set_height(initial_size_
[i
]);
417 attached_windows_
[i
]->SetBounds(bounds
);
418 last_y
= attached_windows_
[i
]->bounds().bottom();
423 aura::Window
* WorkspaceWindowResizer::GetTarget() {
424 return details_
.window
;
427 WorkspaceWindowResizer::WorkspaceWindowResizer(
428 const Details
& details
,
429 const std::vector
<aura::Window
*>& attached_windows
)
431 attached_windows_(attached_windows
),
432 did_move_or_resize_(false),
434 total_initial_size_(0),
435 snap_type_(SNAP_NONE
),
436 num_mouse_moves_since_bounds_change_(0),
437 magnetism_window_(NULL
) {
438 DCHECK(details_
.is_resizable
);
440 Shell
* shell
= Shell::GetInstance();
441 shell
->cursor_manager()->LockCursor();
443 // Only support attaching to the right/bottom.
444 DCHECK(attached_windows_
.empty() ||
445 (details
.window_component
== HTRIGHT
||
446 details
.window_component
== HTBOTTOM
));
448 // TODO: figure out how to deal with window going off the edge.
450 // Calculate sizes so that we can maintain the ratios if we need to resize.
451 int total_available
= 0;
452 for (size_t i
= 0; i
< attached_windows_
.size(); ++i
) {
453 gfx::Size
min(attached_windows_
[i
]->delegate()->GetMinimumSize());
454 int initial_size
= PrimaryAxisSize(attached_windows_
[i
]->bounds().size());
455 initial_size_
.push_back(initial_size
);
456 // If current size is smaller than the min, use the current size as the min.
457 // This way we don't snap on resize.
458 int min_size
= std::min(initial_size
,
459 std::max(PrimaryAxisSize(min
), kMinOnscreenSize
));
460 total_min_
+= min_size
;
461 total_initial_size_
+= initial_size
;
462 total_available
+= std::max(min_size
, initial_size
) - min_size
;
466 gfx::Rect
WorkspaceWindowResizer::GetFinalBounds(
467 const gfx::Rect
& bounds
) const {
468 if (snap_phantom_window_controller_
.get() &&
469 snap_phantom_window_controller_
->IsShowing()) {
470 return snap_phantom_window_controller_
->bounds();
475 void WorkspaceWindowResizer::LayoutAttachedWindows(
477 gfx::Rect
work_area(ScreenAsh::GetDisplayWorkAreaBoundsInParent(window()));
478 int initial_size
= PrimaryAxisSize(details_
.initial_bounds_in_parent
.size());
479 int current_size
= PrimaryAxisSize(bounds
->size());
480 int start
= PrimaryAxisCoordinate(bounds
->right(), bounds
->bottom());
481 int end
= PrimaryAxisCoordinate(work_area
.right(), work_area
.bottom());
483 int delta
= current_size
- initial_size
;
484 int available_size
= end
- start
;
485 std::vector
<int> sizes
;
486 int leftovers
= CalculateAttachedSizes(delta
, available_size
, &sizes
);
488 // leftovers > 0 means that the attached windows can't grow to compensate for
489 // the shrinkage of the main window. This line causes the attached windows to
490 // be moved so they are still flush against the main window, rather than the
491 // main window being prevented from shrinking.
492 leftovers
= std::min(0, leftovers
);
493 // Reallocate any leftover pixels back into the main window. This is
494 // necessary when, for example, the main window shrinks, but none of the
495 // attached windows can grow without exceeding their max size constraints.
496 // Adding the pixels back to the main window effectively prevents the main
497 // window from resizing too far.
498 if (details_
.window_component
== HTRIGHT
)
499 bounds
->set_width(bounds
->width() + leftovers
);
501 bounds
->set_height(bounds
->height() + leftovers
);
503 DCHECK_EQ(attached_windows_
.size(), sizes
.size());
504 int last
= PrimaryAxisCoordinate(bounds
->right(), bounds
->bottom());
505 for (size_t i
= 0; i
< attached_windows_
.size(); ++i
) {
506 gfx::Rect
attached_bounds(attached_windows_
[i
]->bounds());
507 if (details_
.window_component
== HTRIGHT
) {
508 attached_bounds
.set_x(last
);
509 attached_bounds
.set_width(sizes
[i
]);
511 attached_bounds
.set_y(last
);
512 attached_bounds
.set_height(sizes
[i
]);
514 attached_windows_
[i
]->SetBounds(attached_bounds
);
519 int WorkspaceWindowResizer::CalculateAttachedSizes(
522 std::vector
<int>* sizes
) const {
523 std::vector
<WindowSize
> window_sizes
;
524 CreateBucketsForAttached(&window_sizes
);
526 // How much we need to grow the attached by (collectively).
527 int grow_attached_by
= 0;
529 // If the attached windows don't fit when at their initial size, we will
530 // have to shrink them by how much they overflow.
531 if (total_initial_size_
>= available_size
)
532 grow_attached_by
= available_size
- total_initial_size_
;
534 // If we're shrinking, we grow the attached so the total size remains
536 grow_attached_by
= -delta
;
539 int leftover_pixels
= 0;
540 while (grow_attached_by
!= 0) {
541 int leftovers
= GrowFairly(grow_attached_by
, window_sizes
);
542 if (leftovers
== grow_attached_by
) {
543 leftover_pixels
= leftovers
;
546 grow_attached_by
= leftovers
;
549 for (size_t i
= 0; i
< window_sizes
.size(); ++i
)
550 sizes
->push_back(window_sizes
[i
].size());
552 return leftover_pixels
;
555 int WorkspaceWindowResizer::GrowFairly(
557 std::vector
<WindowSize
>& sizes
) const {
558 bool shrinking
= pixels
< 0;
559 std::vector
<WindowSize
*> nonfull_windows
;
560 for (size_t i
= 0; i
< sizes
.size(); ++i
) {
561 if (!sizes
[i
].is_at_capacity(shrinking
))
562 nonfull_windows
.push_back(&sizes
[i
]);
564 std::vector
<float> ratios
;
565 CalculateGrowthRatios(nonfull_windows
, &ratios
);
567 int remaining_pixels
= pixels
;
568 bool add_leftover_pixels_to_last
= true;
569 for (size_t i
= 0; i
< nonfull_windows
.size(); ++i
) {
570 int grow_by
= pixels
* ratios
[i
];
571 // Put any leftover pixels into the last window.
572 if (i
== nonfull_windows
.size() - 1 && add_leftover_pixels_to_last
)
573 grow_by
= remaining_pixels
;
574 int remainder
= nonfull_windows
[i
]->Add(grow_by
);
575 int consumed
= grow_by
- remainder
;
576 remaining_pixels
-= consumed
;
577 if (nonfull_windows
[i
]->is_at_capacity(shrinking
) && remainder
> 0) {
578 // Because this window overflowed, some of the pixels in
579 // |remaining_pixels| aren't there due to rounding errors. Rather than
580 // unfairly giving all those pixels to the last window, we refrain from
581 // allocating them so that this function can be called again to distribute
582 // the pixels fairly.
583 add_leftover_pixels_to_last
= false;
586 return remaining_pixels
;
589 void WorkspaceWindowResizer::CalculateGrowthRatios(
590 const std::vector
<WindowSize
*>& sizes
,
591 std::vector
<float>* out_ratios
) const {
592 DCHECK(out_ratios
->empty());
594 for (size_t i
= 0; i
< sizes
.size(); ++i
)
595 total_value
+= sizes
[i
]->size();
597 for (size_t i
= 0; i
< sizes
.size(); ++i
)
598 out_ratios
->push_back(
599 (static_cast<float>(sizes
[i
]->size())) / total_value
);
602 void WorkspaceWindowResizer::CreateBucketsForAttached(
603 std::vector
<WindowSize
>* sizes
) const {
604 for (size_t i
= 0; i
< attached_windows_
.size(); i
++) {
605 int initial_size
= initial_size_
[i
];
606 aura::WindowDelegate
* delegate
= attached_windows_
[i
]->delegate();
607 int min
= PrimaryAxisSize(delegate
->GetMinimumSize());
608 int max
= PrimaryAxisSize(delegate
->GetMaximumSize());
610 sizes
->push_back(WindowSize(initial_size
, min
, max
));
614 void WorkspaceWindowResizer::MagneticallySnapToOtherWindows(gfx::Rect
* bounds
) {
615 if (UpdateMagnetismWindow(*bounds
, kAllMagnetismEdges
)) {
617 OriginForMagneticAttach(*bounds
, magnetism_window_
->bounds(),
622 void WorkspaceWindowResizer::MagneticallySnapResizeToOtherWindows(
624 const uint32 edges
= WindowComponentToMagneticEdge(details_
.window_component
);
625 if (UpdateMagnetismWindow(*bounds
, edges
)) {
626 *bounds
= BoundsForMagneticResizeAttach(
627 *bounds
, magnetism_window_
->bounds(), magnetism_edge_
);
631 bool WorkspaceWindowResizer::UpdateMagnetismWindow(const gfx::Rect
& bounds
,
633 MagnetismMatcher
matcher(bounds
, edges
);
635 // If we snapped to a window then check it first. That way we don't bounce
636 // around when close to multiple edges.
637 if (magnetism_window_
) {
638 if (window_tracker_
.Contains(magnetism_window_
) &&
639 matcher
.ShouldAttach(magnetism_window_
->bounds(), &magnetism_edge_
)) {
642 window_tracker_
.Remove(magnetism_window_
);
643 magnetism_window_
= NULL
;
646 aura::Window
* parent
= window()->parent();
647 const aura::Window::Windows
& windows(parent
->children());
648 for (aura::Window::Windows::const_reverse_iterator i
= windows
.rbegin();
649 i
!= windows
.rend() && !matcher
.AreEdgesObscured(); ++i
) {
650 aura::Window
* other
= *i
;
651 if (other
== window() || !other
->IsVisible())
653 if (matcher
.ShouldAttach(other
->bounds(), &magnetism_edge_
)) {
654 magnetism_window_
= other
;
655 window_tracker_
.Add(magnetism_window_
);
662 void WorkspaceWindowResizer::AdjustBoundsForMainWindow(
665 gfx::Point last_mouse_location_in_screen
= last_mouse_location_
;
666 wm::ConvertPointToScreen(window()->parent(), &last_mouse_location_in_screen
);
667 gfx::Display display
= Shell::GetScreen()->GetDisplayNearestPoint(
668 last_mouse_location_in_screen
);
669 gfx::Rect work_area
=
670 ScreenAsh::ConvertRectFromScreen(window()->parent(), display
.work_area());
671 if (details_
.window_component
== HTCAPTION
) {
672 // Adjust the bounds to the work area where the mouse cursor is located.
673 // Always keep kMinOnscreenHeight on the bottom.
674 int max_y
= work_area
.bottom() - kMinOnscreenHeight
;
675 if (bounds
->y() > max_y
) {
676 bounds
->set_y(max_y
);
677 } else if (bounds
->y() <= work_area
.y()) {
678 // Don't allow dragging above the top of the display until the mouse
679 // cursor reaches the work area above if any.
680 bounds
->set_y(work_area
.y());
683 if (sticky_size
> 0) {
684 StickToWorkAreaOnMove(work_area
, sticky_size
, bounds
);
685 MagneticallySnapToOtherWindows(bounds
);
687 } else if (sticky_size
> 0) {
688 MagneticallySnapResizeToOtherWindows(bounds
);
689 if (!magnetism_window_
&& sticky_size
> 0)
690 StickToWorkAreaOnResize(work_area
, sticky_size
, bounds
);
693 if (attached_windows_
.empty())
696 if (details_
.window_component
== HTRIGHT
) {
697 bounds
->set_width(std::min(bounds
->width(),
698 work_area
.right() - total_min_
- bounds
->x()));
700 DCHECK_EQ(HTBOTTOM
, details_
.window_component
);
701 bounds
->set_height(std::min(bounds
->height(),
702 work_area
.bottom() - total_min_
- bounds
->y()));
706 void WorkspaceWindowResizer::StickToWorkAreaOnMove(
707 const gfx::Rect
& work_area
,
709 gfx::Rect
* bounds
) const {
710 const int left_edge
= work_area
.x();
711 const int right_edge
= work_area
.right();
712 const int top_edge
= work_area
.y();
713 const int bottom_edge
= work_area
.bottom();
714 if (ShouldStickToEdge(bounds
->x() - left_edge
, sticky_size
)) {
715 bounds
->set_x(left_edge
);
716 } else if (ShouldStickToEdge(right_edge
- bounds
->right(), sticky_size
)) {
717 bounds
->set_x(right_edge
- bounds
->width());
719 if (ShouldStickToEdge(bounds
->y() - top_edge
, sticky_size
)) {
720 bounds
->set_y(top_edge
);
721 } else if (ShouldStickToEdge(bottom_edge
- bounds
->bottom(), sticky_size
) &&
722 bounds
->height() < (bottom_edge
- top_edge
)) {
723 // Only snap to the bottom if the window is smaller than the work area.
724 // Doing otherwise can lead to window snapping in weird ways as it bounces
725 // between snapping to top then bottom.
726 bounds
->set_y(bottom_edge
- bounds
->height());
730 void WorkspaceWindowResizer::StickToWorkAreaOnResize(
731 const gfx::Rect
& work_area
,
733 gfx::Rect
* bounds
) const {
734 const uint32 edges
= WindowComponentToMagneticEdge(details_
.window_component
);
735 const int left_edge
= work_area
.x();
736 const int right_edge
= work_area
.right();
737 const int top_edge
= work_area
.y();
738 const int bottom_edge
= work_area
.bottom();
739 if (edges
& MAGNETISM_EDGE_TOP
&&
740 ShouldStickToEdge(bounds
->y() - top_edge
, sticky_size
)) {
741 bounds
->set_height(bounds
->bottom() - top_edge
);
742 bounds
->set_y(top_edge
);
744 if (edges
& MAGNETISM_EDGE_LEFT
&&
745 ShouldStickToEdge(bounds
->x() - left_edge
, sticky_size
)) {
746 bounds
->set_width(bounds
->right() - left_edge
);
747 bounds
->set_x(left_edge
);
749 if (edges
& MAGNETISM_EDGE_BOTTOM
&&
750 ShouldStickToEdge(bottom_edge
- bounds
->bottom(), sticky_size
)) {
751 bounds
->set_height(bottom_edge
- bounds
->y());
753 if (edges
& MAGNETISM_EDGE_RIGHT
&&
754 ShouldStickToEdge(right_edge
- bounds
->right(), sticky_size
)) {
755 bounds
->set_width(right_edge
- bounds
->x());
759 int WorkspaceWindowResizer::PrimaryAxisSize(const gfx::Size
& size
) const {
760 return PrimaryAxisCoordinate(size
.width(), size
.height());
763 int WorkspaceWindowResizer::PrimaryAxisCoordinate(int x
, int y
) const {
764 switch (details_
.window_component
) {
775 void WorkspaceWindowResizer::UpdateSnapPhantomWindow(const gfx::Point
& location
,
776 const gfx::Rect
& bounds
) {
777 if (!did_move_or_resize_
|| details_
.window_component
!= HTCAPTION
)
780 if (!wm::CanSnapWindow(window()))
783 SnapType last_type
= snap_type_
;
784 snap_type_
= GetSnapType(location
);
785 if (snap_type_
== SNAP_NONE
|| snap_type_
!= last_type
) {
786 snap_phantom_window_controller_
.reset();
788 if (snap_type_
== SNAP_NONE
)
792 SnapSizer::Edge edge
= (snap_type_
== SNAP_LEFT_EDGE
) ?
793 SnapSizer::LEFT_EDGE
: SnapSizer::RIGHT_EDGE
;
794 snap_sizer_
.reset(new SnapSizer(window(),
797 internal::SnapSizer::OTHER_INPUT
));
799 snap_sizer_
->Update(location
);
801 if (!snap_phantom_window_controller_
) {
802 snap_phantom_window_controller_
.reset(
803 new PhantomWindowController(window()));
805 snap_phantom_window_controller_
->Show(ScreenAsh::ConvertRectToScreen(
806 window()->parent(), snap_sizer_
->target_bounds()));
809 void WorkspaceWindowResizer::RestackWindows() {
810 if (attached_windows_
.empty())
812 // Build a map from index in children to window, returning if there is a
813 // window with a different parent.
814 typedef std::map
<size_t, aura::Window
*> IndexToWindowMap
;
815 IndexToWindowMap map
;
816 aura::Window
* parent
= window()->parent();
817 const aura::Window::Windows
& windows(parent
->children());
818 map
[std::find(windows
.begin(), windows
.end(), window()) -
819 windows
.begin()] = window();
820 for (std::vector
<aura::Window
*>::const_iterator i
=
821 attached_windows_
.begin(); i
!= attached_windows_
.end(); ++i
) {
822 if ((*i
)->parent() != parent
)
825 std::find(windows
.begin(), windows
.end(), *i
) - windows
.begin();
829 // Reorder the windows starting at the topmost.
830 parent
->StackChildAtTop(map
.rbegin()->second
);
831 for (IndexToWindowMap::const_reverse_iterator i
= map
.rbegin();
833 aura::Window
* window
= i
->second
;
836 parent
->StackChildBelow(i
->second
, window
);
840 WorkspaceWindowResizer::SnapType
WorkspaceWindowResizer::GetSnapType(
841 const gfx::Point
& location
) const {
842 // TODO: this likely only wants total display area, not the area of a single
844 gfx::Rect
area(ScreenAsh::GetDisplayBoundsInParent(window()));
845 if (location
.x() <= area
.x())
846 return SNAP_LEFT_EDGE
;
847 if (location
.x() >= area
.right() - 1)
848 return SNAP_RIGHT_EDGE
;
852 } // namespace internal