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/window_tree_host_manager.h"
13 #include "ash/metrics/user_metrics_recorder.h"
14 #include "ash/root_window_controller.h"
15 #include "ash/screen_util.h"
16 #include "ash/shell.h"
17 #include "ash/shell_window_ids.h"
18 #include "ash/wm/default_window_resizer.h"
19 #include "ash/wm/dock/docked_window_layout_manager.h"
20 #include "ash/wm/dock/docked_window_resizer.h"
21 #include "ash/wm/drag_window_resizer.h"
22 #include "ash/wm/panels/panel_window_resizer.h"
23 #include "ash/wm/window_state.h"
24 #include "ash/wm/window_util.h"
25 #include "ash/wm/wm_event.h"
26 #include "ash/wm/workspace/phantom_window_controller.h"
27 #include "ash/wm/workspace/two_step_edge_cycler.h"
28 #include "base/command_line.h"
29 #include "base/memory/weak_ptr.h"
30 #include "ui/aura/client/aura_constants.h"
31 #include "ui/aura/client/screen_position_client.h"
32 #include "ui/aura/window.h"
33 #include "ui/aura/window_delegate.h"
34 #include "ui/aura/window_event_dispatcher.h"
35 #include "ui/base/hit_test.h"
36 #include "ui/compositor/layer.h"
37 #include "ui/gfx/screen.h"
38 #include "ui/gfx/transform.h"
39 #include "ui/wm/core/coordinate_conversion.h"
40 #include "ui/wm/core/window_util.h"
41 #include "ui/wm/public/window_types.h"
45 scoped_ptr
<WindowResizer
> CreateWindowResizer(
47 const gfx::Point
& point_in_parent
,
49 aura::client::WindowMoveSource source
) {
51 wm::WindowState
* window_state
= wm::GetWindowState(window
);
52 // No need to return a resizer when the window cannot get resized or when a
53 // resizer already exists for this window.
54 if ((!window_state
->CanResize() && window_component
!= HTCAPTION
) ||
55 window_state
->drag_details()) {
56 return scoped_ptr
<WindowResizer
>();
59 if (window_component
== HTCAPTION
&& !window_state
->can_be_dragged())
60 return scoped_ptr
<WindowResizer
>();
62 // TODO(varkha): The chaining of window resizers causes some of the logic
63 // to be repeated and the logic flow difficult to control. With some windows
64 // classes using reparenting during drag operations it becomes challenging to
65 // implement proper transition from one resizer to another during or at the
66 // end of the drag. This also causes http://crbug.com/247085.
67 // It seems the only thing the panel or dock resizer needs to do is notify the
68 // layout manager when a docked window is being dragged. We should have a
69 // better way of doing this, perhaps by having a way of observing drags or
70 // having a generic drag window wrapper which informs a layout manager that a
71 // drag has started or stopped.
72 // It may be possible to refactor and eliminate chaining.
73 WindowResizer
* window_resizer
= NULL
;
75 if (!window_state
->IsNormalOrSnapped() && !window_state
->IsDocked())
76 return scoped_ptr
<WindowResizer
>();
78 int bounds_change
= WindowResizer::GetBoundsChangeForWindowComponent(
80 if (bounds_change
== WindowResizer::kBoundsChangeDirection_None
)
81 return scoped_ptr
<WindowResizer
>();
83 window_state
->CreateDragDetails(window
, point_in_parent
, window_component
,
85 if (window
->parent() &&
86 (window
->parent()->id() == kShellWindowId_DefaultContainer
||
87 window
->parent()->id() == kShellWindowId_DockedContainer
||
88 window
->parent()->id() == kShellWindowId_PanelContainer
)) {
89 window_resizer
= WorkspaceWindowResizer::Create(
90 window_state
, std::vector
<aura::Window
*>());
92 window_resizer
= DefaultWindowResizer::Create(window_state
);
94 window_resizer
= DragWindowResizer::Create(window_resizer
, window_state
);
95 if (window
->type() == ui::wm::WINDOW_TYPE_PANEL
)
96 window_resizer
= PanelWindowResizer::Create(window_resizer
, window_state
);
97 if (window_resizer
&& window
->parent() &&
98 !::wm::GetTransientParent(window
) &&
99 (window
->parent()->id() == kShellWindowId_DefaultContainer
||
100 window
->parent()->id() == kShellWindowId_DockedContainer
||
101 window
->parent()->id() == kShellWindowId_PanelContainer
)) {
102 window_resizer
= DockedWindowResizer::Create(window_resizer
, window_state
);
104 return make_scoped_ptr
<WindowResizer
>(window_resizer
);
109 // Snapping distance used instead of WorkspaceWindowResizer::kScreenEdgeInset
110 // when resizing a window using touchscreen.
111 const int kScreenEdgeInsetForTouchDrag
= 32;
113 // Current instance for use by the WorkspaceWindowResizerTest.
114 WorkspaceWindowResizer
* instance
= NULL
;
116 // Returns true if the window should stick to the edge.
117 bool ShouldStickToEdge(int distance_from_edge
, int sticky_size
) {
118 return distance_from_edge
< sticky_size
&&
119 distance_from_edge
> -sticky_size
* 2;
122 // Returns the coordinate along the secondary axis to snap to.
123 int CoordinateAlongSecondaryAxis(SecondaryMagnetismEdge edge
,
128 case SECONDARY_MAGNETISM_EDGE_LEADING
:
130 case SECONDARY_MAGNETISM_EDGE_TRAILING
:
132 case SECONDARY_MAGNETISM_EDGE_NONE
:
139 // Returns the origin for |src| when magnetically attaching to |attach_to| along
140 // the edges |edges|. |edges| is a bitmask of the MagnetismEdges.
141 gfx::Point
OriginForMagneticAttach(const gfx::Rect
& src
,
142 const gfx::Rect
& attach_to
,
143 const MatchedEdge
& edge
) {
145 switch (edge
.primary_edge
) {
146 case MAGNETISM_EDGE_TOP
:
147 y
= attach_to
.bottom();
149 case MAGNETISM_EDGE_LEFT
:
150 x
= attach_to
.right();
152 case MAGNETISM_EDGE_BOTTOM
:
153 y
= attach_to
.y() - src
.height();
155 case MAGNETISM_EDGE_RIGHT
:
156 x
= attach_to
.x() - src
.width();
159 switch (edge
.primary_edge
) {
160 case MAGNETISM_EDGE_TOP
:
161 case MAGNETISM_EDGE_BOTTOM
:
162 x
= CoordinateAlongSecondaryAxis(
163 edge
.secondary_edge
, attach_to
.x(), attach_to
.right() - src
.width(),
166 case MAGNETISM_EDGE_LEFT
:
167 case MAGNETISM_EDGE_RIGHT
:
168 y
= CoordinateAlongSecondaryAxis(
169 edge
.secondary_edge
, attach_to
.y(), attach_to
.bottom() - src
.height(),
173 return gfx::Point(x
, y
);
176 // Returns the bounds for a magnetic attach when resizing. |src| is the bounds
177 // of window being resized, |attach_to| the bounds of the window to attach to
178 // and |edge| identifies the edge to attach to.
179 gfx::Rect
BoundsForMagneticResizeAttach(const gfx::Rect
& src
,
180 const gfx::Rect
& attach_to
,
181 const MatchedEdge
& edge
) {
185 int h
= src
.height();
186 gfx::Point
attach_origin(OriginForMagneticAttach(src
, attach_to
, edge
));
187 switch (edge
.primary_edge
) {
188 case MAGNETISM_EDGE_LEFT
:
189 x
= attach_origin
.x();
192 case MAGNETISM_EDGE_RIGHT
:
193 w
+= attach_origin
.x() - src
.x();
195 case MAGNETISM_EDGE_TOP
:
196 y
= attach_origin
.y();
197 h
= src
.bottom() - y
;
199 case MAGNETISM_EDGE_BOTTOM
:
200 h
+= attach_origin
.y() - src
.y();
203 switch (edge
.primary_edge
) {
204 case MAGNETISM_EDGE_LEFT
:
205 case MAGNETISM_EDGE_RIGHT
:
206 if (edge
.secondary_edge
== SECONDARY_MAGNETISM_EDGE_LEADING
) {
207 y
= attach_origin
.y();
208 h
= src
.bottom() - y
;
209 } else if (edge
.secondary_edge
== SECONDARY_MAGNETISM_EDGE_TRAILING
) {
210 h
+= attach_origin
.y() - src
.y();
213 case MAGNETISM_EDGE_TOP
:
214 case MAGNETISM_EDGE_BOTTOM
:
215 if (edge
.secondary_edge
== SECONDARY_MAGNETISM_EDGE_LEADING
) {
216 x
= attach_origin
.x();
218 } else if (edge
.secondary_edge
== SECONDARY_MAGNETISM_EDGE_TRAILING
) {
219 w
+= attach_origin
.x() - src
.x();
223 return gfx::Rect(x
, y
, w
, h
);
226 // Converts a window component edge to the magnetic edge to snap to.
227 uint32
WindowComponentToMagneticEdge(int window_component
) {
228 switch (window_component
) {
230 return MAGNETISM_EDGE_LEFT
| MAGNETISM_EDGE_TOP
;
232 return MAGNETISM_EDGE_TOP
| MAGNETISM_EDGE_RIGHT
;
234 return MAGNETISM_EDGE_LEFT
| MAGNETISM_EDGE_BOTTOM
;
236 return MAGNETISM_EDGE_RIGHT
| MAGNETISM_EDGE_BOTTOM
;
238 return MAGNETISM_EDGE_TOP
;
240 return MAGNETISM_EDGE_BOTTOM
;
242 return MAGNETISM_EDGE_RIGHT
;
244 return MAGNETISM_EDGE_LEFT
;
254 const int WorkspaceWindowResizer::kMinOnscreenSize
= 20;
257 const int WorkspaceWindowResizer::kMinOnscreenHeight
= 32;
260 const int WorkspaceWindowResizer::kScreenEdgeInset
= 8;
262 WorkspaceWindowResizer
* WorkspaceWindowResizer::GetInstanceForTest() {
266 // Represents the width or height of a window with constraints on its minimum
267 // and maximum size. 0 represents a lack of a constraint.
270 WindowSize(int size
, int min
, int max
)
274 // Grow the min/max bounds to include the starting size.
275 if (is_underflowing())
277 if (is_overflowing())
281 bool is_at_capacity(bool shrinking
) {
282 return size_
== (shrinking
? min_
: max_
);
289 bool has_min() const {
293 bool has_max() const {
297 bool is_valid() const {
298 return !is_overflowing() && !is_underflowing();
301 bool is_overflowing() const {
302 return has_max() && size_
> max_
;
305 bool is_underflowing() const {
306 return has_min() && size_
< min_
;
309 // Add |amount| to this WindowSize not exceeding min or max size constraints.
310 // Returns by how much |size_| + |amount| exceeds the min/max constraints.
311 int Add(int amount
) {
313 int new_value
= size_
+ amount
;
315 if (has_min() && new_value
< min_
) {
317 return new_value
- min_
;
320 if (has_max() && new_value
> max_
) {
322 return new_value
- max_
;
335 WorkspaceWindowResizer::~WorkspaceWindowResizer() {
336 if (did_lock_cursor_
) {
337 Shell
* shell
= Shell::GetInstance();
338 shell
->cursor_manager()->UnlockCursor();
340 if (instance
== this)
345 WorkspaceWindowResizer
* WorkspaceWindowResizer::Create(
346 wm::WindowState
* window_state
,
347 const std::vector
<aura::Window
*>& attached_windows
) {
348 return new WorkspaceWindowResizer(window_state
, attached_windows
);
351 void WorkspaceWindowResizer::Drag(const gfx::Point
& location_in_parent
,
353 last_mouse_location_
= location_in_parent
;
356 if (event_flags
& ui::EF_CONTROL_DOWN
) {
358 } else if ((details().bounds_change
& kBoundsChange_Resizes
) &&
359 details().source
== aura::client::WINDOW_MOVE_SOURCE_TOUCH
) {
360 sticky_size
= kScreenEdgeInsetForTouchDrag
;
362 sticky_size
= kScreenEdgeInset
;
364 // |bounds| is in |GetTarget()->parent()|'s coordinates.
365 gfx::Rect bounds
= CalculateBoundsForDrag(location_in_parent
);
366 AdjustBoundsForMainWindow(sticky_size
, &bounds
);
368 if (bounds
!= GetTarget()->bounds()) {
369 if (!did_move_or_resize_
) {
370 if (!details().restore_bounds
.IsEmpty())
371 window_state()->ClearRestoreBounds();
374 did_move_or_resize_
= true;
377 gfx::Point location_in_screen
= location_in_parent
;
378 ::wm::ConvertPointToScreen(GetTarget()->parent(), &location_in_screen
);
380 aura::Window
* root
= NULL
;
381 gfx::Display display
=
382 ScreenUtil::FindDisplayContainingPoint(location_in_screen
);
383 // Track the last screen that the pointer was on to keep the snap phantom
385 if (display
.is_valid()) {
386 root
= Shell::GetInstance()
387 ->window_tree_host_manager()
388 ->GetRootWindowForDisplayId(display
.id());
390 if (!attached_windows_
.empty())
391 LayoutAttachedWindows(&bounds
);
392 if (bounds
!= GetTarget()->bounds()) {
393 // SetBounds needs to be called to update the layout which affects where the
394 // phantom window is drawn. Keep track if the window was destroyed during
395 // the drag and quit early if so.
396 base::WeakPtr
<WorkspaceWindowResizer
> resizer(
397 weak_ptr_factory_
.GetWeakPtr());
398 GetTarget()->SetBounds(bounds
);
402 const bool in_original_root
= !root
|| root
== GetTarget()->GetRootWindow();
403 // Hide a phantom window for snapping if the cursor is in another root window.
404 if (in_original_root
) {
405 UpdateSnapPhantomWindow(location_in_parent
, bounds
);
407 snap_type_
= SNAP_NONE
;
408 snap_phantom_window_controller_
.reset();
409 edge_cycler_
.reset();
410 SetDraggedWindowDocked(false);
414 void WorkspaceWindowResizer::CompleteDrag() {
415 if (!did_move_or_resize_
)
418 window_state()->set_bounds_changed_by_user(true);
419 snap_phantom_window_controller_
.reset();
421 // If the window's state type changed over the course of the drag do not snap
422 // the window. This happens when the user minimizes or maximizes the window
423 // using a keyboard shortcut while dragging it.
424 if (window_state()->GetStateType() != details().initial_state_type
)
427 bool snapped
= false;
428 if (snap_type_
== SNAP_LEFT
|| snap_type_
== SNAP_RIGHT
) {
429 if (!window_state()->HasRestoreBounds()) {
430 gfx::Rect initial_bounds
= ScreenUtil::ConvertRectToScreen(
431 GetTarget()->parent(), details().initial_bounds_in_parent
);
432 window_state()->SetRestoreBoundsInScreen(
433 details().restore_bounds
.IsEmpty() ?
435 details().restore_bounds
);
437 if (!dock_layout_
->is_dragged_window_docked()) {
438 UserMetricsRecorder
* metrics
= Shell::GetInstance()->metrics();
439 // TODO(oshima): Add event source type to WMEvent and move
440 // metrics recording inside WindowState::OnWMEvent.
441 const wm::WMEvent
event(snap_type_
== SNAP_LEFT
?
442 wm::WM_EVENT_SNAP_LEFT
: wm::WM_EVENT_SNAP_RIGHT
);
443 window_state()->OnWMEvent(&event
);
444 metrics
->RecordUserMetricsAction(
445 snap_type_
== SNAP_LEFT
?
446 UMA_DRAG_MAXIMIZE_LEFT
: UMA_DRAG_MAXIMIZE_RIGHT
);
452 if (window_state()->IsSnapped()) {
453 // Keep the window snapped if the user resizes the window such that the
454 // window has valid bounds for a snapped window. Always unsnap the window
455 // if the user dragged the window via the caption area because doing this
456 // is slightly less confusing.
457 if (details().window_component
== HTCAPTION
||
458 !AreBoundsValidSnappedBounds(window_state()->GetStateType(),
459 GetTarget()->bounds())) {
460 // Set the window to WINDOW_STATE_TYPE_NORMAL but keep the
461 // window at the bounds that the user has moved/resized the
462 // window to. ClearRestoreBounds() is used instead of
463 // SaveCurrentBoundsForRestore() because most of the restore
464 // logic is skipped because we are still in the middle of a
465 // drag. TODO(pkotwicz): Fix this and use
466 // SaveCurrentBoundsForRestore().
467 window_state()->ClearRestoreBounds();
468 window_state()->Restore();
470 } else if (!dock_layout_
->is_dragged_window_docked()) {
471 // The window was not snapped and is not snapped. This is a user
472 // resize/drag and so the current bounds should be maintained, clearing
473 // any prior restore bounds. When the window is docked the restore bound
474 // must be kept so the docked state can be reverted properly.
475 window_state()->ClearRestoreBounds();
480 void WorkspaceWindowResizer::RevertDrag() {
481 window_state()->set_bounds_changed_by_user(initial_bounds_changed_by_user_
);
482 snap_phantom_window_controller_
.reset();
484 if (!did_move_or_resize_
)
487 GetTarget()->SetBounds(details().initial_bounds_in_parent
);
488 if (!details().restore_bounds
.IsEmpty()) {
489 window_state()->SetRestoreBoundsInScreen(details().restore_bounds
);
492 if (details().window_component
== HTRIGHT
) {
493 int last_x
= details().initial_bounds_in_parent
.right();
494 for (size_t i
= 0; i
< attached_windows_
.size(); ++i
) {
495 gfx::Rect
bounds(attached_windows_
[i
]->bounds());
496 bounds
.set_x(last_x
);
497 bounds
.set_width(initial_size_
[i
]);
498 attached_windows_
[i
]->SetBounds(bounds
);
499 last_x
= attached_windows_
[i
]->bounds().right();
502 int last_y
= details().initial_bounds_in_parent
.bottom();
503 for (size_t i
= 0; i
< attached_windows_
.size(); ++i
) {
504 gfx::Rect
bounds(attached_windows_
[i
]->bounds());
505 bounds
.set_y(last_y
);
506 bounds
.set_height(initial_size_
[i
]);
507 attached_windows_
[i
]->SetBounds(bounds
);
508 last_y
= attached_windows_
[i
]->bounds().bottom();
513 WorkspaceWindowResizer::WorkspaceWindowResizer(
514 wm::WindowState
* window_state
,
515 const std::vector
<aura::Window
*>& attached_windows
)
516 : WindowResizer(window_state
),
517 attached_windows_(attached_windows
),
518 did_lock_cursor_(false),
519 did_move_or_resize_(false),
520 initial_bounds_changed_by_user_(window_state_
->bounds_changed_by_user()),
522 total_initial_size_(0),
523 snap_type_(SNAP_NONE
),
524 num_mouse_moves_since_bounds_change_(0),
525 magnetism_window_(NULL
),
526 weak_ptr_factory_(this) {
527 DCHECK(details().is_resizable
);
529 // A mousemove should still show the cursor even if the window is
530 // being moved or resized with touch, so do not lock the cursor.
531 if (details().source
!= aura::client::WINDOW_MOVE_SOURCE_TOUCH
) {
532 Shell
* shell
= Shell::GetInstance();
533 shell
->cursor_manager()->LockCursor();
534 did_lock_cursor_
= true;
537 aura::Window
* dock_container
= Shell::GetContainer(
538 GetTarget()->GetRootWindow(), kShellWindowId_DockedContainer
);
539 dock_layout_
= static_cast<DockedWindowLayoutManager
*>(
540 dock_container
->layout_manager());
542 // Only support attaching to the right/bottom.
543 DCHECK(attached_windows_
.empty() ||
544 (details().window_component
== HTRIGHT
||
545 details().window_component
== HTBOTTOM
));
547 // TODO: figure out how to deal with window going off the edge.
549 // Calculate sizes so that we can maintain the ratios if we need to resize.
550 int total_available
= 0;
551 for (size_t i
= 0; i
< attached_windows_
.size(); ++i
) {
552 gfx::Size
min(attached_windows_
[i
]->delegate()->GetMinimumSize());
553 int initial_size
= PrimaryAxisSize(attached_windows_
[i
]->bounds().size());
554 initial_size_
.push_back(initial_size
);
555 // If current size is smaller than the min, use the current size as the min.
556 // This way we don't snap on resize.
557 int min_size
= std::min(initial_size
,
558 std::max(PrimaryAxisSize(min
), kMinOnscreenSize
));
559 total_min_
+= min_size
;
560 total_initial_size_
+= initial_size
;
561 total_available
+= std::max(min_size
, initial_size
) - min_size
;
566 void WorkspaceWindowResizer::LayoutAttachedWindows(
568 gfx::Rect
work_area(ScreenUtil::GetDisplayWorkAreaBoundsInParent(
570 int initial_size
= PrimaryAxisSize(details().initial_bounds_in_parent
.size());
571 int current_size
= PrimaryAxisSize(bounds
->size());
572 int start
= PrimaryAxisCoordinate(bounds
->right(), bounds
->bottom());
573 int end
= PrimaryAxisCoordinate(work_area
.right(), work_area
.bottom());
575 int delta
= current_size
- initial_size
;
576 int available_size
= end
- start
;
577 std::vector
<int> sizes
;
578 int leftovers
= CalculateAttachedSizes(delta
, available_size
, &sizes
);
580 // leftovers > 0 means that the attached windows can't grow to compensate for
581 // the shrinkage of the main window. This line causes the attached windows to
582 // be moved so they are still flush against the main window, rather than the
583 // main window being prevented from shrinking.
584 leftovers
= std::min(0, leftovers
);
585 // Reallocate any leftover pixels back into the main window. This is
586 // necessary when, for example, the main window shrinks, but none of the
587 // attached windows can grow without exceeding their max size constraints.
588 // Adding the pixels back to the main window effectively prevents the main
589 // window from resizing too far.
590 if (details().window_component
== HTRIGHT
)
591 bounds
->set_width(bounds
->width() + leftovers
);
593 bounds
->set_height(bounds
->height() + leftovers
);
595 DCHECK_EQ(attached_windows_
.size(), sizes
.size());
596 int last
= PrimaryAxisCoordinate(bounds
->right(), bounds
->bottom());
597 for (size_t i
= 0; i
< attached_windows_
.size(); ++i
) {
598 gfx::Rect
attached_bounds(attached_windows_
[i
]->bounds());
599 if (details().window_component
== HTRIGHT
) {
600 attached_bounds
.set_x(last
);
601 attached_bounds
.set_width(sizes
[i
]);
603 attached_bounds
.set_y(last
);
604 attached_bounds
.set_height(sizes
[i
]);
606 attached_windows_
[i
]->SetBounds(attached_bounds
);
611 int WorkspaceWindowResizer::CalculateAttachedSizes(
614 std::vector
<int>* sizes
) const {
615 std::vector
<WindowSize
> window_sizes
;
616 CreateBucketsForAttached(&window_sizes
);
618 // How much we need to grow the attached by (collectively).
619 int grow_attached_by
= 0;
621 // If the attached windows don't fit when at their initial size, we will
622 // have to shrink them by how much they overflow.
623 if (total_initial_size_
>= available_size
)
624 grow_attached_by
= available_size
- total_initial_size_
;
626 // If we're shrinking, we grow the attached so the total size remains
628 grow_attached_by
= -delta
;
631 int leftover_pixels
= 0;
632 while (grow_attached_by
!= 0) {
633 int leftovers
= GrowFairly(grow_attached_by
, window_sizes
);
634 if (leftovers
== grow_attached_by
) {
635 leftover_pixels
= leftovers
;
638 grow_attached_by
= leftovers
;
641 for (size_t i
= 0; i
< window_sizes
.size(); ++i
)
642 sizes
->push_back(window_sizes
[i
].size());
644 return leftover_pixels
;
647 int WorkspaceWindowResizer::GrowFairly(
649 std::vector
<WindowSize
>& sizes
) const {
650 bool shrinking
= pixels
< 0;
651 std::vector
<WindowSize
*> nonfull_windows
;
652 for (size_t i
= 0; i
< sizes
.size(); ++i
) {
653 if (!sizes
[i
].is_at_capacity(shrinking
))
654 nonfull_windows
.push_back(&sizes
[i
]);
656 std::vector
<float> ratios
;
657 CalculateGrowthRatios(nonfull_windows
, &ratios
);
659 int remaining_pixels
= pixels
;
660 bool add_leftover_pixels_to_last
= true;
661 for (size_t i
= 0; i
< nonfull_windows
.size(); ++i
) {
662 int grow_by
= pixels
* ratios
[i
];
663 // Put any leftover pixels into the last window.
664 if (i
== nonfull_windows
.size() - 1 && add_leftover_pixels_to_last
)
665 grow_by
= remaining_pixels
;
666 int remainder
= nonfull_windows
[i
]->Add(grow_by
);
667 int consumed
= grow_by
- remainder
;
668 remaining_pixels
-= consumed
;
669 if (nonfull_windows
[i
]->is_at_capacity(shrinking
) && remainder
> 0) {
670 // Because this window overflowed, some of the pixels in
671 // |remaining_pixels| aren't there due to rounding errors. Rather than
672 // unfairly giving all those pixels to the last window, we refrain from
673 // allocating them so that this function can be called again to distribute
674 // the pixels fairly.
675 add_leftover_pixels_to_last
= false;
678 return remaining_pixels
;
681 void WorkspaceWindowResizer::CalculateGrowthRatios(
682 const std::vector
<WindowSize
*>& sizes
,
683 std::vector
<float>* out_ratios
) const {
684 DCHECK(out_ratios
->empty());
686 for (size_t i
= 0; i
< sizes
.size(); ++i
)
687 total_value
+= sizes
[i
]->size();
689 for (size_t i
= 0; i
< sizes
.size(); ++i
)
690 out_ratios
->push_back(
691 (static_cast<float>(sizes
[i
]->size())) / total_value
);
694 void WorkspaceWindowResizer::CreateBucketsForAttached(
695 std::vector
<WindowSize
>* sizes
) const {
696 for (size_t i
= 0; i
< attached_windows_
.size(); i
++) {
697 int initial_size
= initial_size_
[i
];
698 aura::WindowDelegate
* delegate
= attached_windows_
[i
]->delegate();
699 int min
= PrimaryAxisSize(delegate
->GetMinimumSize());
700 int max
= PrimaryAxisSize(delegate
->GetMaximumSize());
702 sizes
->push_back(WindowSize(initial_size
, min
, max
));
706 void WorkspaceWindowResizer::MagneticallySnapToOtherWindows(gfx::Rect
* bounds
) {
707 if (UpdateMagnetismWindow(*bounds
, kAllMagnetismEdges
)) {
708 gfx::Point point
= OriginForMagneticAttach(
709 ScreenUtil::ConvertRectToScreen(GetTarget()->parent(), *bounds
),
710 magnetism_window_
->GetBoundsInScreen(),
712 aura::client::GetScreenPositionClient(GetTarget()->GetRootWindow())->
713 ConvertPointFromScreen(GetTarget()->parent(), &point
);
714 bounds
->set_origin(point
);
718 void WorkspaceWindowResizer::MagneticallySnapResizeToOtherWindows(
720 const uint32 edges
= WindowComponentToMagneticEdge(
721 details().window_component
);
722 if (UpdateMagnetismWindow(*bounds
, edges
)) {
723 *bounds
= ScreenUtil::ConvertRectFromScreen(
724 GetTarget()->parent(),
725 BoundsForMagneticResizeAttach(
726 ScreenUtil::ConvertRectToScreen(GetTarget()->parent(), *bounds
),
727 magnetism_window_
->GetBoundsInScreen(),
732 bool WorkspaceWindowResizer::UpdateMagnetismWindow(const gfx::Rect
& bounds
,
734 // |bounds| are in coordinates of original window's parent.
735 gfx::Rect bounds_in_screen
=
736 ScreenUtil::ConvertRectToScreen(GetTarget()->parent(), bounds
);
737 MagnetismMatcher
matcher(bounds_in_screen
, edges
);
739 // If we snapped to a window then check it first. That way we don't bounce
740 // around when close to multiple edges.
741 if (magnetism_window_
) {
742 if (window_tracker_
.Contains(magnetism_window_
) &&
743 matcher
.ShouldAttach(magnetism_window_
->GetBoundsInScreen(),
747 window_tracker_
.Remove(magnetism_window_
);
748 magnetism_window_
= NULL
;
751 // Avoid magnetically snapping windows that are not resizable.
752 // TODO(oshima): change this to window.type() == TYPE_NORMAL.
753 if (!window_state()->CanResize())
756 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
757 for (aura::Window::Windows::iterator iter
= root_windows
.begin();
758 iter
!= root_windows
.end(); ++iter
) {
759 const aura::Window
* root_window
= *iter
;
760 // Test all children from the desktop in each root window.
761 const aura::Window::Windows
& children
= Shell::GetContainer(
762 root_window
, kShellWindowId_DefaultContainer
)->children();
763 for (aura::Window::Windows::const_reverse_iterator i
= children
.rbegin();
764 i
!= children
.rend() && !matcher
.AreEdgesObscured(); ++i
) {
765 wm::WindowState
* other_state
= wm::GetWindowState(*i
);
766 if (other_state
->window() == GetTarget() ||
767 !other_state
->window()->IsVisible() ||
768 !other_state
->IsNormalOrSnapped() ||
769 !other_state
->CanResize()) {
772 if (matcher
.ShouldAttach(
773 other_state
->window()->GetBoundsInScreen(), &magnetism_edge_
)) {
774 magnetism_window_
= other_state
->window();
775 window_tracker_
.Add(magnetism_window_
);
783 void WorkspaceWindowResizer::AdjustBoundsForMainWindow(
786 gfx::Point last_mouse_location_in_screen
= last_mouse_location_
;
787 ::wm::ConvertPointToScreen(GetTarget()->parent(),
788 &last_mouse_location_in_screen
);
789 gfx::Display display
= Shell::GetScreen()->GetDisplayNearestPoint(
790 last_mouse_location_in_screen
);
791 gfx::Rect work_area
=
792 ScreenUtil::ConvertRectFromScreen(GetTarget()->parent(),
793 display
.work_area());
794 if (details().window_component
== HTCAPTION
) {
795 // Adjust the bounds to the work area where the mouse cursor is located.
796 // Always keep kMinOnscreenHeight or the window height (whichever is less)
798 int max_y
= work_area
.bottom() - std::min(kMinOnscreenHeight
,
800 if (bounds
->y() > max_y
) {
801 bounds
->set_y(max_y
);
802 } else if (bounds
->y() <= work_area
.y()) {
803 // Don't allow dragging above the top of the display until the mouse
804 // cursor reaches the work area above if any.
805 bounds
->set_y(work_area
.y());
808 if (sticky_size
> 0) {
809 // Possibly stick to edge except when a mouse pointer is outside the
811 if (display
.work_area().Contains(last_mouse_location_in_screen
))
812 StickToWorkAreaOnMove(work_area
, sticky_size
, bounds
);
813 MagneticallySnapToOtherWindows(bounds
);
815 } else if (sticky_size
> 0) {
816 MagneticallySnapResizeToOtherWindows(bounds
);
817 if (!magnetism_window_
&& sticky_size
> 0)
818 StickToWorkAreaOnResize(work_area
, sticky_size
, bounds
);
821 if (attached_windows_
.empty())
824 if (details().window_component
== HTRIGHT
) {
825 bounds
->set_width(std::min(bounds
->width(),
826 work_area
.right() - total_min_
- bounds
->x()));
828 DCHECK_EQ(HTBOTTOM
, details().window_component
);
829 bounds
->set_height(std::min(bounds
->height(),
830 work_area
.bottom() - total_min_
- bounds
->y()));
834 bool WorkspaceWindowResizer::StickToWorkAreaOnMove(
835 const gfx::Rect
& work_area
,
837 gfx::Rect
* bounds
) const {
838 const int left_edge
= work_area
.x();
839 const int right_edge
= work_area
.right();
840 const int top_edge
= work_area
.y();
841 const int bottom_edge
= work_area
.bottom();
842 bool updated
= false;
843 if (ShouldStickToEdge(bounds
->x() - left_edge
, sticky_size
)) {
844 bounds
->set_x(left_edge
);
846 } else if (ShouldStickToEdge(right_edge
- bounds
->right(), sticky_size
)) {
847 bounds
->set_x(right_edge
- bounds
->width());
850 if (ShouldStickToEdge(bounds
->y() - top_edge
, sticky_size
)) {
851 bounds
->set_y(top_edge
);
853 } else if (ShouldStickToEdge(bottom_edge
- bounds
->bottom(), sticky_size
) &&
854 bounds
->height() < (bottom_edge
- top_edge
)) {
855 // Only snap to the bottom if the window is smaller than the work area.
856 // Doing otherwise can lead to window snapping in weird ways as it bounces
857 // between snapping to top then bottom.
858 bounds
->set_y(bottom_edge
- bounds
->height());
864 void WorkspaceWindowResizer::StickToWorkAreaOnResize(
865 const gfx::Rect
& work_area
,
867 gfx::Rect
* bounds
) const {
868 const uint32 edges
= WindowComponentToMagneticEdge(
869 details().window_component
);
870 const int left_edge
= work_area
.x();
871 const int right_edge
= work_area
.right();
872 const int top_edge
= work_area
.y();
873 const int bottom_edge
= work_area
.bottom();
874 if (edges
& MAGNETISM_EDGE_TOP
&&
875 ShouldStickToEdge(bounds
->y() - top_edge
, sticky_size
)) {
876 bounds
->set_height(bounds
->bottom() - top_edge
);
877 bounds
->set_y(top_edge
);
879 if (edges
& MAGNETISM_EDGE_LEFT
&&
880 ShouldStickToEdge(bounds
->x() - left_edge
, sticky_size
)) {
881 bounds
->set_width(bounds
->right() - left_edge
);
882 bounds
->set_x(left_edge
);
884 if (edges
& MAGNETISM_EDGE_BOTTOM
&&
885 ShouldStickToEdge(bottom_edge
- bounds
->bottom(), sticky_size
)) {
886 bounds
->set_height(bottom_edge
- bounds
->y());
888 if (edges
& MAGNETISM_EDGE_RIGHT
&&
889 ShouldStickToEdge(right_edge
- bounds
->right(), sticky_size
)) {
890 bounds
->set_width(right_edge
- bounds
->x());
894 int WorkspaceWindowResizer::PrimaryAxisSize(const gfx::Size
& size
) const {
895 return PrimaryAxisCoordinate(size
.width(), size
.height());
898 int WorkspaceWindowResizer::PrimaryAxisCoordinate(int x
, int y
) const {
899 switch (details().window_component
) {
910 void WorkspaceWindowResizer::UpdateSnapPhantomWindow(const gfx::Point
& location
,
911 const gfx::Rect
& bounds
) {
912 if (!did_move_or_resize_
|| details().window_component
!= HTCAPTION
)
915 SnapType last_type
= snap_type_
;
916 snap_type_
= GetSnapType(location
);
917 if (snap_type_
== SNAP_NONE
|| snap_type_
!= last_type
) {
918 snap_phantom_window_controller_
.reset();
919 edge_cycler_
.reset();
920 if (snap_type_
== SNAP_NONE
) {
921 SetDraggedWindowDocked(false);
926 DCHECK(snap_type_
== SNAP_LEFT
|| snap_type_
== SNAP_RIGHT
);
927 DockedAlignment desired_alignment
= (snap_type_
== SNAP_LEFT
) ?
928 DOCKED_ALIGNMENT_LEFT
: DOCKED_ALIGNMENT_RIGHT
;
929 const bool can_dock
=
930 dock_layout_
->CanDockWindow(GetTarget(), desired_alignment
) &&
931 dock_layout_
->GetAlignmentOfWindow(GetTarget()) != DOCKED_ALIGNMENT_NONE
;
933 // If the window cannot be docked, undock the window. This may change the
934 // workspace bounds and hence |snap_type_|.
935 SetDraggedWindowDocked(false);
936 snap_type_
= GetSnapType(location
);
938 const bool can_snap
= snap_type_
!= SNAP_NONE
&& window_state()->CanSnap();
939 if (!can_snap
&& !can_dock
) {
940 snap_type_
= SNAP_NONE
;
941 snap_phantom_window_controller_
.reset();
942 edge_cycler_
.reset();
946 edge_cycler_
.reset(new TwoStepEdgeCycler(
947 location
, snap_type_
== SNAP_LEFT
948 ? TwoStepEdgeCycler::DIRECTION_LEFT
949 : TwoStepEdgeCycler::DIRECTION_RIGHT
));
951 edge_cycler_
->OnMove(location
);
954 // Update phantom window with snapped or docked guide bounds.
955 // Windows that cannot be snapped or are less wide than kMaxDockWidth can get
956 // docked without going through a snapping sequence.
957 gfx::Rect phantom_bounds
;
958 const bool should_dock
= can_dock
&&
960 GetTarget()->bounds().width() <=
961 DockedWindowLayoutManager::kMaxDockWidth
||
962 edge_cycler_
->use_second_mode() ||
963 dock_layout_
->is_dragged_window_docked());
965 SetDraggedWindowDocked(true);
966 phantom_bounds
= ScreenUtil::ConvertRectFromScreen(
967 GetTarget()->parent(), dock_layout_
->dragged_bounds());
969 phantom_bounds
= (snap_type_
== SNAP_LEFT
) ?
970 wm::GetDefaultLeftSnappedWindowBoundsInParent(GetTarget()) :
971 wm::GetDefaultRightSnappedWindowBoundsInParent(GetTarget());
974 if (!snap_phantom_window_controller_
) {
975 snap_phantom_window_controller_
.reset(
976 new PhantomWindowController(GetTarget()));
978 snap_phantom_window_controller_
->Show(ScreenUtil::ConvertRectToScreen(
979 GetTarget()->parent(), phantom_bounds
));
982 void WorkspaceWindowResizer::RestackWindows() {
983 if (attached_windows_
.empty())
985 // Build a map from index in children to window, returning if there is a
986 // window with a different parent.
987 typedef std::map
<size_t, aura::Window
*> IndexToWindowMap
;
988 IndexToWindowMap map
;
989 aura::Window
* parent
= GetTarget()->parent();
990 const aura::Window::Windows
& windows(parent
->children());
991 map
[std::find(windows
.begin(), windows
.end(), GetTarget()) -
992 windows
.begin()] = GetTarget();
993 for (std::vector
<aura::Window
*>::const_iterator i
=
994 attached_windows_
.begin(); i
!= attached_windows_
.end(); ++i
) {
995 if ((*i
)->parent() != parent
)
998 std::find(windows
.begin(), windows
.end(), *i
) - windows
.begin();
1002 // Reorder the windows starting at the topmost.
1003 parent
->StackChildAtTop(map
.rbegin()->second
);
1004 for (IndexToWindowMap::const_reverse_iterator i
= map
.rbegin();
1005 i
!= map
.rend(); ) {
1006 aura::Window
* window
= i
->second
;
1008 if (i
!= map
.rend())
1009 parent
->StackChildBelow(i
->second
, window
);
1013 WorkspaceWindowResizer::SnapType
WorkspaceWindowResizer::GetSnapType(
1014 const gfx::Point
& location
) const {
1015 // TODO: this likely only wants total display area, not the area of a single
1017 gfx::Rect
area(ScreenUtil::GetDisplayWorkAreaBoundsInParent(GetTarget()));
1018 if (details().source
== aura::client::WINDOW_MOVE_SOURCE_TOUCH
) {
1019 // Increase tolerance for touch-snapping near the screen edges. This is only
1020 // necessary when the work area left or right edge is same as screen edge.
1021 gfx::Rect
display_bounds(ScreenUtil::GetDisplayBoundsInParent(GetTarget()));
1023 if (area
.x() == display_bounds
.x())
1024 inset_left
= kScreenEdgeInsetForTouchDrag
;
1025 int inset_right
= 0;
1026 if (area
.right() == display_bounds
.right())
1027 inset_right
= kScreenEdgeInsetForTouchDrag
;
1028 area
.Inset(inset_left
, 0, inset_right
, 0);
1030 if (location
.x() <= area
.x())
1032 if (location
.x() >= area
.right() - 1)
1037 void WorkspaceWindowResizer::SetDraggedWindowDocked(bool should_dock
) {
1039 if (!dock_layout_
->is_dragged_window_docked()) {
1040 window_state()->set_bounds_changed_by_user(false);
1041 dock_layout_
->DockDraggedWindow(GetTarget());
1044 if (dock_layout_
->is_dragged_window_docked()) {
1045 dock_layout_
->UndockDraggedWindow();
1046 window_state()->set_bounds_changed_by_user(true);
1051 bool WorkspaceWindowResizer::AreBoundsValidSnappedBounds(
1052 wm::WindowStateType snapped_type
,
1053 const gfx::Rect
& bounds_in_parent
) const {
1054 DCHECK(snapped_type
== wm::WINDOW_STATE_TYPE_LEFT_SNAPPED
||
1055 snapped_type
== wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED
);
1056 gfx::Rect snapped_bounds
= ScreenUtil::GetDisplayWorkAreaBoundsInParent(
1058 if (snapped_type
== wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED
)
1059 snapped_bounds
.set_x(snapped_bounds
.right() - bounds_in_parent
.width());
1060 snapped_bounds
.set_width(bounds_in_parent
.width());
1061 return bounds_in_parent
== snapped_bounds
;