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/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())
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 // Returns true if the window should stick to the edge.
114 bool ShouldStickToEdge(int distance_from_edge
, int sticky_size
) {
115 return distance_from_edge
< sticky_size
&&
116 distance_from_edge
> -sticky_size
* 2;
119 // Returns the coordinate along the secondary axis to snap to.
120 int CoordinateAlongSecondaryAxis(SecondaryMagnetismEdge edge
,
125 case SECONDARY_MAGNETISM_EDGE_LEADING
:
127 case SECONDARY_MAGNETISM_EDGE_TRAILING
:
129 case SECONDARY_MAGNETISM_EDGE_NONE
:
136 // Returns the origin for |src| when magnetically attaching to |attach_to| along
137 // the edges |edges|. |edges| is a bitmask of the MagnetismEdges.
138 gfx::Point
OriginForMagneticAttach(const gfx::Rect
& src
,
139 const gfx::Rect
& attach_to
,
140 const MatchedEdge
& edge
) {
142 switch (edge
.primary_edge
) {
143 case MAGNETISM_EDGE_TOP
:
144 y
= attach_to
.bottom();
146 case MAGNETISM_EDGE_LEFT
:
147 x
= attach_to
.right();
149 case MAGNETISM_EDGE_BOTTOM
:
150 y
= attach_to
.y() - src
.height();
152 case MAGNETISM_EDGE_RIGHT
:
153 x
= attach_to
.x() - src
.width();
156 switch (edge
.primary_edge
) {
157 case MAGNETISM_EDGE_TOP
:
158 case MAGNETISM_EDGE_BOTTOM
:
159 x
= CoordinateAlongSecondaryAxis(
160 edge
.secondary_edge
, attach_to
.x(), attach_to
.right() - src
.width(),
163 case MAGNETISM_EDGE_LEFT
:
164 case MAGNETISM_EDGE_RIGHT
:
165 y
= CoordinateAlongSecondaryAxis(
166 edge
.secondary_edge
, attach_to
.y(), attach_to
.bottom() - src
.height(),
170 return gfx::Point(x
, y
);
173 // Returns the bounds for a magnetic attach when resizing. |src| is the bounds
174 // of window being resized, |attach_to| the bounds of the window to attach to
175 // and |edge| identifies the edge to attach to.
176 gfx::Rect
BoundsForMagneticResizeAttach(const gfx::Rect
& src
,
177 const gfx::Rect
& attach_to
,
178 const MatchedEdge
& edge
) {
182 int h
= src
.height();
183 gfx::Point
attach_origin(OriginForMagneticAttach(src
, attach_to
, edge
));
184 switch (edge
.primary_edge
) {
185 case MAGNETISM_EDGE_LEFT
:
186 x
= attach_origin
.x();
189 case MAGNETISM_EDGE_RIGHT
:
190 w
+= attach_origin
.x() - src
.x();
192 case MAGNETISM_EDGE_TOP
:
193 y
= attach_origin
.y();
194 h
= src
.bottom() - y
;
196 case MAGNETISM_EDGE_BOTTOM
:
197 h
+= attach_origin
.y() - src
.y();
200 switch (edge
.primary_edge
) {
201 case MAGNETISM_EDGE_LEFT
:
202 case MAGNETISM_EDGE_RIGHT
:
203 if (edge
.secondary_edge
== SECONDARY_MAGNETISM_EDGE_LEADING
) {
204 y
= attach_origin
.y();
205 h
= src
.bottom() - y
;
206 } else if (edge
.secondary_edge
== SECONDARY_MAGNETISM_EDGE_TRAILING
) {
207 h
+= attach_origin
.y() - src
.y();
210 case MAGNETISM_EDGE_TOP
:
211 case MAGNETISM_EDGE_BOTTOM
:
212 if (edge
.secondary_edge
== SECONDARY_MAGNETISM_EDGE_LEADING
) {
213 x
= attach_origin
.x();
215 } else if (edge
.secondary_edge
== SECONDARY_MAGNETISM_EDGE_TRAILING
) {
216 w
+= attach_origin
.x() - src
.x();
220 return gfx::Rect(x
, y
, w
, h
);
223 // Converts a window component edge to the magnetic edge to snap to.
224 uint32
WindowComponentToMagneticEdge(int window_component
) {
225 switch (window_component
) {
227 return MAGNETISM_EDGE_LEFT
| MAGNETISM_EDGE_TOP
;
229 return MAGNETISM_EDGE_TOP
| MAGNETISM_EDGE_RIGHT
;
231 return MAGNETISM_EDGE_LEFT
| MAGNETISM_EDGE_BOTTOM
;
233 return MAGNETISM_EDGE_RIGHT
| MAGNETISM_EDGE_BOTTOM
;
235 return MAGNETISM_EDGE_TOP
;
237 return MAGNETISM_EDGE_BOTTOM
;
239 return MAGNETISM_EDGE_RIGHT
;
241 return MAGNETISM_EDGE_LEFT
;
251 const int WorkspaceWindowResizer::kMinOnscreenSize
= 20;
254 const int WorkspaceWindowResizer::kMinOnscreenHeight
= 32;
257 const int WorkspaceWindowResizer::kScreenEdgeInset
= 8;
260 WorkspaceWindowResizer
* WorkspaceWindowResizer::instance_
= NULL
;
262 // Represents the width or height of a window with constraints on its minimum
263 // and maximum size. 0 represents a lack of a constraint.
266 WindowSize(int size
, int min
, int max
)
270 // Grow the min/max bounds to include the starting size.
271 if (is_underflowing())
273 if (is_overflowing())
277 bool is_at_capacity(bool shrinking
) {
278 return size_
== (shrinking
? min_
: max_
);
285 bool has_min() const {
289 bool has_max() const {
293 bool is_valid() const {
294 return !is_overflowing() && !is_underflowing();
297 bool is_overflowing() const {
298 return has_max() && size_
> max_
;
301 bool is_underflowing() const {
302 return has_min() && size_
< min_
;
305 // Add |amount| to this WindowSize not exceeding min or max size constraints.
306 // Returns by how much |size_| + |amount| exceeds the min/max constraints.
307 int Add(int amount
) {
309 int new_value
= size_
+ amount
;
311 if (has_min() && new_value
< min_
) {
313 return new_value
- min_
;
316 if (has_max() && new_value
> max_
) {
318 return new_value
- max_
;
331 WorkspaceWindowResizer::~WorkspaceWindowResizer() {
332 if (did_lock_cursor_
) {
333 Shell
* shell
= Shell::GetInstance();
334 shell
->cursor_manager()->UnlockCursor();
336 if (instance_
== this)
341 WorkspaceWindowResizer
* WorkspaceWindowResizer::Create(
342 wm::WindowState
* window_state
,
343 const std::vector
<aura::Window
*>& attached_windows
) {
344 return new WorkspaceWindowResizer(window_state
, attached_windows
);
347 void WorkspaceWindowResizer::Drag(const gfx::Point
& location_in_parent
,
349 last_mouse_location_
= location_in_parent
;
352 if (event_flags
& ui::EF_CONTROL_DOWN
) {
354 } else if ((details().bounds_change
& kBoundsChange_Resizes
) &&
355 details().source
== aura::client::WINDOW_MOVE_SOURCE_TOUCH
) {
356 sticky_size
= kScreenEdgeInsetForTouchDrag
;
358 sticky_size
= kScreenEdgeInset
;
360 // |bounds| is in |GetTarget()->parent()|'s coordinates.
361 gfx::Rect bounds
= CalculateBoundsForDrag(location_in_parent
);
362 AdjustBoundsForMainWindow(sticky_size
, &bounds
);
364 if (bounds
!= GetTarget()->bounds()) {
365 if (!did_move_or_resize_
) {
366 if (!details().restore_bounds
.IsEmpty())
367 window_state()->ClearRestoreBounds();
370 did_move_or_resize_
= true;
373 gfx::Point location_in_screen
= location_in_parent
;
374 ::wm::ConvertPointToScreen(GetTarget()->parent(), &location_in_screen
);
376 aura::Window
* root
= NULL
;
377 gfx::Display display
=
378 ScreenUtil::FindDisplayContainingPoint(location_in_screen
);
379 // Track the last screen that the pointer was on to keep the snap phantom
381 if (display
.is_valid()) {
382 root
= Shell::GetInstance()->display_controller()->
383 GetRootWindowForDisplayId(display
.id());
385 if (!attached_windows_
.empty())
386 LayoutAttachedWindows(&bounds
);
387 if (bounds
!= GetTarget()->bounds()) {
388 // SetBounds needs to be called to update the layout which affects where the
389 // phantom window is drawn. Keep track if the window was destroyed during
390 // the drag and quit early if so.
391 base::WeakPtr
<WorkspaceWindowResizer
> resizer(
392 weak_ptr_factory_
.GetWeakPtr());
393 GetTarget()->SetBounds(bounds
);
397 const bool in_original_root
= !root
|| root
== GetTarget()->GetRootWindow();
398 // Hide a phantom window for snapping if the cursor is in another root window.
399 if (in_original_root
) {
400 UpdateSnapPhantomWindow(location_in_parent
, bounds
);
402 snap_type_
= SNAP_NONE
;
403 snap_phantom_window_controller_
.reset();
404 edge_cycler_
.reset();
405 SetDraggedWindowDocked(false);
409 void WorkspaceWindowResizer::CompleteDrag() {
410 if (!did_move_or_resize_
)
413 window_state()->set_bounds_changed_by_user(true);
414 snap_phantom_window_controller_
.reset();
416 // If the window's state type changed over the course of the drag do not snap
417 // the window. This happens when the user minimizes or maximizes the window
418 // using a keyboard shortcut while dragging it.
419 if (window_state()->GetStateType() != details().initial_state_type
)
422 bool snapped
= false;
423 if (snap_type_
== SNAP_LEFT
|| snap_type_
== SNAP_RIGHT
) {
424 if (!window_state()->HasRestoreBounds()) {
425 gfx::Rect initial_bounds
= ScreenUtil::ConvertRectToScreen(
426 GetTarget()->parent(), details().initial_bounds_in_parent
);
427 window_state()->SetRestoreBoundsInScreen(
428 details().restore_bounds
.IsEmpty() ?
430 details().restore_bounds
);
432 if (!dock_layout_
->is_dragged_window_docked()) {
433 UserMetricsRecorder
* metrics
= Shell::GetInstance()->metrics();
434 // TODO(oshima): Add event source type to WMEvent and move
435 // metrics recording inside WindowState::OnWMEvent.
436 const wm::WMEvent
event(snap_type_
== SNAP_LEFT
?
437 wm::WM_EVENT_SNAP_LEFT
: wm::WM_EVENT_SNAP_RIGHT
);
438 window_state()->OnWMEvent(&event
);
439 metrics
->RecordUserMetricsAction(
440 snap_type_
== SNAP_LEFT
?
441 UMA_DRAG_MAXIMIZE_LEFT
: UMA_DRAG_MAXIMIZE_RIGHT
);
447 if (window_state()->IsSnapped()) {
448 // Keep the window snapped if the user resizes the window such that the
449 // window has valid bounds for a snapped window. Always unsnap the window
450 // if the user dragged the window via the caption area because doing this
451 // is slightly less confusing.
452 if (details().window_component
== HTCAPTION
||
453 !AreBoundsValidSnappedBounds(window_state()->GetStateType(),
454 GetTarget()->bounds())) {
455 // Set the window to WINDOW_STATE_TYPE_NORMAL but keep the
456 // window at the bounds that the user has moved/resized the
457 // window to. ClearRestoreBounds() is used instead of
458 // SaveCurrentBoundsForRestore() because most of the restore
459 // logic is skipped because we are still in the middle of a
460 // drag. TODO(pkotwicz): Fix this and use
461 // SaveCurrentBoundsForRestore().
462 window_state()->ClearRestoreBounds();
463 window_state()->Restore();
465 } else if (!dock_layout_
->is_dragged_window_docked()) {
466 // The window was not snapped and is not snapped. This is a user
467 // resize/drag and so the current bounds should be maintained, clearing
468 // any prior restore bounds. When the window is docked the restore bound
469 // must be kept so the docked state can be reverted properly.
470 window_state()->ClearRestoreBounds();
475 void WorkspaceWindowResizer::RevertDrag() {
476 window_state()->set_bounds_changed_by_user(initial_bounds_changed_by_user_
);
477 snap_phantom_window_controller_
.reset();
479 if (!did_move_or_resize_
)
482 GetTarget()->SetBounds(details().initial_bounds_in_parent
);
483 if (!details().restore_bounds
.IsEmpty()) {
484 window_state()->SetRestoreBoundsInScreen(details().restore_bounds
);
487 if (details().window_component
== HTRIGHT
) {
488 int last_x
= details().initial_bounds_in_parent
.right();
489 for (size_t i
= 0; i
< attached_windows_
.size(); ++i
) {
490 gfx::Rect
bounds(attached_windows_
[i
]->bounds());
491 bounds
.set_x(last_x
);
492 bounds
.set_width(initial_size_
[i
]);
493 attached_windows_
[i
]->SetBounds(bounds
);
494 last_x
= attached_windows_
[i
]->bounds().right();
497 int last_y
= details().initial_bounds_in_parent
.bottom();
498 for (size_t i
= 0; i
< attached_windows_
.size(); ++i
) {
499 gfx::Rect
bounds(attached_windows_
[i
]->bounds());
500 bounds
.set_y(last_y
);
501 bounds
.set_height(initial_size_
[i
]);
502 attached_windows_
[i
]->SetBounds(bounds
);
503 last_y
= attached_windows_
[i
]->bounds().bottom();
508 WorkspaceWindowResizer::WorkspaceWindowResizer(
509 wm::WindowState
* window_state
,
510 const std::vector
<aura::Window
*>& attached_windows
)
511 : WindowResizer(window_state
),
512 attached_windows_(attached_windows
),
513 did_lock_cursor_(false),
514 did_move_or_resize_(false),
515 initial_bounds_changed_by_user_(window_state_
->bounds_changed_by_user()),
517 total_initial_size_(0),
518 snap_type_(SNAP_NONE
),
519 num_mouse_moves_since_bounds_change_(0),
520 magnetism_window_(NULL
),
521 weak_ptr_factory_(this) {
522 DCHECK(details().is_resizable
);
524 // A mousemove should still show the cursor even if the window is
525 // being moved or resized with touch, so do not lock the cursor.
526 if (details().source
!= aura::client::WINDOW_MOVE_SOURCE_TOUCH
) {
527 Shell
* shell
= Shell::GetInstance();
528 shell
->cursor_manager()->LockCursor();
529 did_lock_cursor_
= true;
532 aura::Window
* dock_container
= Shell::GetContainer(
533 GetTarget()->GetRootWindow(), kShellWindowId_DockedContainer
);
534 dock_layout_
= static_cast<DockedWindowLayoutManager
*>(
535 dock_container
->layout_manager());
537 // Only support attaching to the right/bottom.
538 DCHECK(attached_windows_
.empty() ||
539 (details().window_component
== HTRIGHT
||
540 details().window_component
== HTBOTTOM
));
542 // TODO: figure out how to deal with window going off the edge.
544 // Calculate sizes so that we can maintain the ratios if we need to resize.
545 int total_available
= 0;
546 for (size_t i
= 0; i
< attached_windows_
.size(); ++i
) {
547 gfx::Size
min(attached_windows_
[i
]->delegate()->GetMinimumSize());
548 int initial_size
= PrimaryAxisSize(attached_windows_
[i
]->bounds().size());
549 initial_size_
.push_back(initial_size
);
550 // If current size is smaller than the min, use the current size as the min.
551 // This way we don't snap on resize.
552 int min_size
= std::min(initial_size
,
553 std::max(PrimaryAxisSize(min
), kMinOnscreenSize
));
554 total_min_
+= min_size
;
555 total_initial_size_
+= initial_size
;
556 total_available
+= std::max(min_size
, initial_size
) - min_size
;
561 void WorkspaceWindowResizer::LayoutAttachedWindows(
563 gfx::Rect
work_area(ScreenUtil::GetDisplayWorkAreaBoundsInParent(
565 int initial_size
= PrimaryAxisSize(details().initial_bounds_in_parent
.size());
566 int current_size
= PrimaryAxisSize(bounds
->size());
567 int start
= PrimaryAxisCoordinate(bounds
->right(), bounds
->bottom());
568 int end
= PrimaryAxisCoordinate(work_area
.right(), work_area
.bottom());
570 int delta
= current_size
- initial_size
;
571 int available_size
= end
- start
;
572 std::vector
<int> sizes
;
573 int leftovers
= CalculateAttachedSizes(delta
, available_size
, &sizes
);
575 // leftovers > 0 means that the attached windows can't grow to compensate for
576 // the shrinkage of the main window. This line causes the attached windows to
577 // be moved so they are still flush against the main window, rather than the
578 // main window being prevented from shrinking.
579 leftovers
= std::min(0, leftovers
);
580 // Reallocate any leftover pixels back into the main window. This is
581 // necessary when, for example, the main window shrinks, but none of the
582 // attached windows can grow without exceeding their max size constraints.
583 // Adding the pixels back to the main window effectively prevents the main
584 // window from resizing too far.
585 if (details().window_component
== HTRIGHT
)
586 bounds
->set_width(bounds
->width() + leftovers
);
588 bounds
->set_height(bounds
->height() + leftovers
);
590 DCHECK_EQ(attached_windows_
.size(), sizes
.size());
591 int last
= PrimaryAxisCoordinate(bounds
->right(), bounds
->bottom());
592 for (size_t i
= 0; i
< attached_windows_
.size(); ++i
) {
593 gfx::Rect
attached_bounds(attached_windows_
[i
]->bounds());
594 if (details().window_component
== HTRIGHT
) {
595 attached_bounds
.set_x(last
);
596 attached_bounds
.set_width(sizes
[i
]);
598 attached_bounds
.set_y(last
);
599 attached_bounds
.set_height(sizes
[i
]);
601 attached_windows_
[i
]->SetBounds(attached_bounds
);
606 int WorkspaceWindowResizer::CalculateAttachedSizes(
609 std::vector
<int>* sizes
) const {
610 std::vector
<WindowSize
> window_sizes
;
611 CreateBucketsForAttached(&window_sizes
);
613 // How much we need to grow the attached by (collectively).
614 int grow_attached_by
= 0;
616 // If the attached windows don't fit when at their initial size, we will
617 // have to shrink them by how much they overflow.
618 if (total_initial_size_
>= available_size
)
619 grow_attached_by
= available_size
- total_initial_size_
;
621 // If we're shrinking, we grow the attached so the total size remains
623 grow_attached_by
= -delta
;
626 int leftover_pixels
= 0;
627 while (grow_attached_by
!= 0) {
628 int leftovers
= GrowFairly(grow_attached_by
, window_sizes
);
629 if (leftovers
== grow_attached_by
) {
630 leftover_pixels
= leftovers
;
633 grow_attached_by
= leftovers
;
636 for (size_t i
= 0; i
< window_sizes
.size(); ++i
)
637 sizes
->push_back(window_sizes
[i
].size());
639 return leftover_pixels
;
642 int WorkspaceWindowResizer::GrowFairly(
644 std::vector
<WindowSize
>& sizes
) const {
645 bool shrinking
= pixels
< 0;
646 std::vector
<WindowSize
*> nonfull_windows
;
647 for (size_t i
= 0; i
< sizes
.size(); ++i
) {
648 if (!sizes
[i
].is_at_capacity(shrinking
))
649 nonfull_windows
.push_back(&sizes
[i
]);
651 std::vector
<float> ratios
;
652 CalculateGrowthRatios(nonfull_windows
, &ratios
);
654 int remaining_pixels
= pixels
;
655 bool add_leftover_pixels_to_last
= true;
656 for (size_t i
= 0; i
< nonfull_windows
.size(); ++i
) {
657 int grow_by
= pixels
* ratios
[i
];
658 // Put any leftover pixels into the last window.
659 if (i
== nonfull_windows
.size() - 1 && add_leftover_pixels_to_last
)
660 grow_by
= remaining_pixels
;
661 int remainder
= nonfull_windows
[i
]->Add(grow_by
);
662 int consumed
= grow_by
- remainder
;
663 remaining_pixels
-= consumed
;
664 if (nonfull_windows
[i
]->is_at_capacity(shrinking
) && remainder
> 0) {
665 // Because this window overflowed, some of the pixels in
666 // |remaining_pixels| aren't there due to rounding errors. Rather than
667 // unfairly giving all those pixels to the last window, we refrain from
668 // allocating them so that this function can be called again to distribute
669 // the pixels fairly.
670 add_leftover_pixels_to_last
= false;
673 return remaining_pixels
;
676 void WorkspaceWindowResizer::CalculateGrowthRatios(
677 const std::vector
<WindowSize
*>& sizes
,
678 std::vector
<float>* out_ratios
) const {
679 DCHECK(out_ratios
->empty());
681 for (size_t i
= 0; i
< sizes
.size(); ++i
)
682 total_value
+= sizes
[i
]->size();
684 for (size_t i
= 0; i
< sizes
.size(); ++i
)
685 out_ratios
->push_back(
686 (static_cast<float>(sizes
[i
]->size())) / total_value
);
689 void WorkspaceWindowResizer::CreateBucketsForAttached(
690 std::vector
<WindowSize
>* sizes
) const {
691 for (size_t i
= 0; i
< attached_windows_
.size(); i
++) {
692 int initial_size
= initial_size_
[i
];
693 aura::WindowDelegate
* delegate
= attached_windows_
[i
]->delegate();
694 int min
= PrimaryAxisSize(delegate
->GetMinimumSize());
695 int max
= PrimaryAxisSize(delegate
->GetMaximumSize());
697 sizes
->push_back(WindowSize(initial_size
, min
, max
));
701 void WorkspaceWindowResizer::MagneticallySnapToOtherWindows(gfx::Rect
* bounds
) {
702 if (UpdateMagnetismWindow(*bounds
, kAllMagnetismEdges
)) {
703 gfx::Point point
= OriginForMagneticAttach(
704 ScreenUtil::ConvertRectToScreen(GetTarget()->parent(), *bounds
),
705 magnetism_window_
->GetBoundsInScreen(),
707 aura::client::GetScreenPositionClient(GetTarget()->GetRootWindow())->
708 ConvertPointFromScreen(GetTarget()->parent(), &point
);
709 bounds
->set_origin(point
);
713 void WorkspaceWindowResizer::MagneticallySnapResizeToOtherWindows(
715 const uint32 edges
= WindowComponentToMagneticEdge(
716 details().window_component
);
717 if (UpdateMagnetismWindow(*bounds
, edges
)) {
718 *bounds
= ScreenUtil::ConvertRectFromScreen(
719 GetTarget()->parent(),
720 BoundsForMagneticResizeAttach(
721 ScreenUtil::ConvertRectToScreen(GetTarget()->parent(), *bounds
),
722 magnetism_window_
->GetBoundsInScreen(),
727 bool WorkspaceWindowResizer::UpdateMagnetismWindow(const gfx::Rect
& bounds
,
729 // |bounds| are in coordinates of original window's parent.
730 gfx::Rect bounds_in_screen
=
731 ScreenUtil::ConvertRectToScreen(GetTarget()->parent(), bounds
);
732 MagnetismMatcher
matcher(bounds_in_screen
, edges
);
734 // If we snapped to a window then check it first. That way we don't bounce
735 // around when close to multiple edges.
736 if (magnetism_window_
) {
737 if (window_tracker_
.Contains(magnetism_window_
) &&
738 matcher
.ShouldAttach(magnetism_window_
->GetBoundsInScreen(),
742 window_tracker_
.Remove(magnetism_window_
);
743 magnetism_window_
= NULL
;
746 // Avoid magnetically snapping windows that are not resizable.
747 // TODO(oshima): change this to window.type() == TYPE_NORMAL.
748 if (!window_state()->CanResize())
751 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
752 for (aura::Window::Windows::iterator iter
= root_windows
.begin();
753 iter
!= root_windows
.end(); ++iter
) {
754 const aura::Window
* root_window
= *iter
;
755 // Test all children from the desktop in each root window.
756 const aura::Window::Windows
& children
= Shell::GetContainer(
757 root_window
, kShellWindowId_DefaultContainer
)->children();
758 for (aura::Window::Windows::const_reverse_iterator i
= children
.rbegin();
759 i
!= children
.rend() && !matcher
.AreEdgesObscured(); ++i
) {
760 wm::WindowState
* other_state
= wm::GetWindowState(*i
);
761 if (other_state
->window() == GetTarget() ||
762 !other_state
->window()->IsVisible() ||
763 !other_state
->IsNormalOrSnapped() ||
764 !other_state
->CanResize()) {
767 if (matcher
.ShouldAttach(
768 other_state
->window()->GetBoundsInScreen(), &magnetism_edge_
)) {
769 magnetism_window_
= other_state
->window();
770 window_tracker_
.Add(magnetism_window_
);
778 void WorkspaceWindowResizer::AdjustBoundsForMainWindow(
781 gfx::Point last_mouse_location_in_screen
= last_mouse_location_
;
782 ::wm::ConvertPointToScreen(GetTarget()->parent(),
783 &last_mouse_location_in_screen
);
784 gfx::Display display
= Shell::GetScreen()->GetDisplayNearestPoint(
785 last_mouse_location_in_screen
);
786 gfx::Rect work_area
=
787 ScreenUtil::ConvertRectFromScreen(GetTarget()->parent(),
788 display
.work_area());
789 if (details().window_component
== HTCAPTION
) {
790 // Adjust the bounds to the work area where the mouse cursor is located.
791 // Always keep kMinOnscreenHeight or the window height (whichever is less)
793 int max_y
= work_area
.bottom() - std::min(kMinOnscreenHeight
,
795 if (bounds
->y() > max_y
) {
796 bounds
->set_y(max_y
);
797 } else if (bounds
->y() <= work_area
.y()) {
798 // Don't allow dragging above the top of the display until the mouse
799 // cursor reaches the work area above if any.
800 bounds
->set_y(work_area
.y());
803 if (sticky_size
> 0) {
804 // Possibly stick to edge except when a mouse pointer is outside the
806 if (display
.work_area().Contains(last_mouse_location_in_screen
))
807 StickToWorkAreaOnMove(work_area
, sticky_size
, bounds
);
808 MagneticallySnapToOtherWindows(bounds
);
810 } else if (sticky_size
> 0) {
811 MagneticallySnapResizeToOtherWindows(bounds
);
812 if (!magnetism_window_
&& sticky_size
> 0)
813 StickToWorkAreaOnResize(work_area
, sticky_size
, bounds
);
816 if (attached_windows_
.empty())
819 if (details().window_component
== HTRIGHT
) {
820 bounds
->set_width(std::min(bounds
->width(),
821 work_area
.right() - total_min_
- bounds
->x()));
823 DCHECK_EQ(HTBOTTOM
, details().window_component
);
824 bounds
->set_height(std::min(bounds
->height(),
825 work_area
.bottom() - total_min_
- bounds
->y()));
829 bool WorkspaceWindowResizer::StickToWorkAreaOnMove(
830 const gfx::Rect
& work_area
,
832 gfx::Rect
* bounds
) const {
833 const int left_edge
= work_area
.x();
834 const int right_edge
= work_area
.right();
835 const int top_edge
= work_area
.y();
836 const int bottom_edge
= work_area
.bottom();
837 bool updated
= false;
838 if (ShouldStickToEdge(bounds
->x() - left_edge
, sticky_size
)) {
839 bounds
->set_x(left_edge
);
841 } else if (ShouldStickToEdge(right_edge
- bounds
->right(), sticky_size
)) {
842 bounds
->set_x(right_edge
- bounds
->width());
845 if (ShouldStickToEdge(bounds
->y() - top_edge
, sticky_size
)) {
846 bounds
->set_y(top_edge
);
848 } else if (ShouldStickToEdge(bottom_edge
- bounds
->bottom(), sticky_size
) &&
849 bounds
->height() < (bottom_edge
- top_edge
)) {
850 // Only snap to the bottom if the window is smaller than the work area.
851 // Doing otherwise can lead to window snapping in weird ways as it bounces
852 // between snapping to top then bottom.
853 bounds
->set_y(bottom_edge
- bounds
->height());
859 void WorkspaceWindowResizer::StickToWorkAreaOnResize(
860 const gfx::Rect
& work_area
,
862 gfx::Rect
* bounds
) const {
863 const uint32 edges
= WindowComponentToMagneticEdge(
864 details().window_component
);
865 const int left_edge
= work_area
.x();
866 const int right_edge
= work_area
.right();
867 const int top_edge
= work_area
.y();
868 const int bottom_edge
= work_area
.bottom();
869 if (edges
& MAGNETISM_EDGE_TOP
&&
870 ShouldStickToEdge(bounds
->y() - top_edge
, sticky_size
)) {
871 bounds
->set_height(bounds
->bottom() - top_edge
);
872 bounds
->set_y(top_edge
);
874 if (edges
& MAGNETISM_EDGE_LEFT
&&
875 ShouldStickToEdge(bounds
->x() - left_edge
, sticky_size
)) {
876 bounds
->set_width(bounds
->right() - left_edge
);
877 bounds
->set_x(left_edge
);
879 if (edges
& MAGNETISM_EDGE_BOTTOM
&&
880 ShouldStickToEdge(bottom_edge
- bounds
->bottom(), sticky_size
)) {
881 bounds
->set_height(bottom_edge
- bounds
->y());
883 if (edges
& MAGNETISM_EDGE_RIGHT
&&
884 ShouldStickToEdge(right_edge
- bounds
->right(), sticky_size
)) {
885 bounds
->set_width(right_edge
- bounds
->x());
889 int WorkspaceWindowResizer::PrimaryAxisSize(const gfx::Size
& size
) const {
890 return PrimaryAxisCoordinate(size
.width(), size
.height());
893 int WorkspaceWindowResizer::PrimaryAxisCoordinate(int x
, int y
) const {
894 switch (details().window_component
) {
905 void WorkspaceWindowResizer::UpdateSnapPhantomWindow(const gfx::Point
& location
,
906 const gfx::Rect
& bounds
) {
907 if (!did_move_or_resize_
|| details().window_component
!= HTCAPTION
)
910 SnapType last_type
= snap_type_
;
911 snap_type_
= GetSnapType(location
);
912 if (snap_type_
== SNAP_NONE
|| snap_type_
!= last_type
) {
913 snap_phantom_window_controller_
.reset();
914 edge_cycler_
.reset();
915 if (snap_type_
== SNAP_NONE
) {
916 SetDraggedWindowDocked(false);
921 DCHECK(snap_type_
== SNAP_LEFT
|| snap_type_
== SNAP_RIGHT
);
922 DockedAlignment desired_alignment
= (snap_type_
== SNAP_LEFT
) ?
923 DOCKED_ALIGNMENT_LEFT
: DOCKED_ALIGNMENT_RIGHT
;
924 const bool can_dock
=
925 dock_layout_
->CanDockWindow(GetTarget(), desired_alignment
) &&
926 dock_layout_
->GetAlignmentOfWindow(GetTarget()) != DOCKED_ALIGNMENT_NONE
;
928 // If the window cannot be docked, undock the window. This may change the
929 // workspace bounds and hence |snap_type_|.
930 SetDraggedWindowDocked(false);
931 snap_type_
= GetSnapType(location
);
933 const bool can_snap
= snap_type_
!= SNAP_NONE
&& window_state()->CanSnap();
934 if (!can_snap
&& !can_dock
) {
935 snap_type_
= SNAP_NONE
;
936 snap_phantom_window_controller_
.reset();
937 edge_cycler_
.reset();
941 edge_cycler_
.reset(new TwoStepEdgeCycler(location
));
943 edge_cycler_
->OnMove(location
);
945 // Update phantom window with snapped or docked guide bounds.
946 // Windows that cannot be snapped or are less wide than kMaxDockWidth can get
947 // docked without going through a snapping sequence.
948 gfx::Rect phantom_bounds
;
949 const bool should_dock
= can_dock
&&
951 GetTarget()->bounds().width() <=
952 DockedWindowLayoutManager::kMaxDockWidth
||
953 edge_cycler_
->use_second_mode() ||
954 dock_layout_
->is_dragged_window_docked());
956 SetDraggedWindowDocked(true);
957 phantom_bounds
= ScreenUtil::ConvertRectFromScreen(
958 GetTarget()->parent(), dock_layout_
->dragged_bounds());
960 phantom_bounds
= (snap_type_
== SNAP_LEFT
) ?
961 wm::GetDefaultLeftSnappedWindowBoundsInParent(GetTarget()) :
962 wm::GetDefaultRightSnappedWindowBoundsInParent(GetTarget());
965 if (!snap_phantom_window_controller_
) {
966 snap_phantom_window_controller_
.reset(
967 new PhantomWindowController(GetTarget()));
969 snap_phantom_window_controller_
->Show(ScreenUtil::ConvertRectToScreen(
970 GetTarget()->parent(), phantom_bounds
));
973 void WorkspaceWindowResizer::RestackWindows() {
974 if (attached_windows_
.empty())
976 // Build a map from index in children to window, returning if there is a
977 // window with a different parent.
978 typedef std::map
<size_t, aura::Window
*> IndexToWindowMap
;
979 IndexToWindowMap map
;
980 aura::Window
* parent
= GetTarget()->parent();
981 const aura::Window::Windows
& windows(parent
->children());
982 map
[std::find(windows
.begin(), windows
.end(), GetTarget()) -
983 windows
.begin()] = GetTarget();
984 for (std::vector
<aura::Window
*>::const_iterator i
=
985 attached_windows_
.begin(); i
!= attached_windows_
.end(); ++i
) {
986 if ((*i
)->parent() != parent
)
989 std::find(windows
.begin(), windows
.end(), *i
) - windows
.begin();
993 // Reorder the windows starting at the topmost.
994 parent
->StackChildAtTop(map
.rbegin()->second
);
995 for (IndexToWindowMap::const_reverse_iterator i
= map
.rbegin();
997 aura::Window
* window
= i
->second
;
1000 parent
->StackChildBelow(i
->second
, window
);
1004 WorkspaceWindowResizer::SnapType
WorkspaceWindowResizer::GetSnapType(
1005 const gfx::Point
& location
) const {
1006 // TODO: this likely only wants total display area, not the area of a single
1008 gfx::Rect
area(ScreenUtil::GetDisplayWorkAreaBoundsInParent(GetTarget()));
1009 if (details().source
== aura::client::WINDOW_MOVE_SOURCE_TOUCH
) {
1010 // Increase tolerance for touch-snapping near the screen edges. This is only
1011 // necessary when the work area left or right edge is same as screen edge.
1012 gfx::Rect
display_bounds(ScreenUtil::GetDisplayBoundsInParent(GetTarget()));
1014 if (area
.x() == display_bounds
.x())
1015 inset_left
= kScreenEdgeInsetForTouchDrag
;
1016 int inset_right
= 0;
1017 if (area
.right() == display_bounds
.right())
1018 inset_right
= kScreenEdgeInsetForTouchDrag
;
1019 area
.Inset(inset_left
, 0, inset_right
, 0);
1021 if (location
.x() <= area
.x())
1023 if (location
.x() >= area
.right() - 1)
1028 void WorkspaceWindowResizer::SetDraggedWindowDocked(bool should_dock
) {
1030 if (!dock_layout_
->is_dragged_window_docked()) {
1031 window_state()->set_bounds_changed_by_user(false);
1032 dock_layout_
->DockDraggedWindow(GetTarget());
1035 if (dock_layout_
->is_dragged_window_docked()) {
1036 dock_layout_
->UndockDraggedWindow();
1037 window_state()->set_bounds_changed_by_user(true);
1042 bool WorkspaceWindowResizer::AreBoundsValidSnappedBounds(
1043 wm::WindowStateType snapped_type
,
1044 const gfx::Rect
& bounds_in_parent
) const {
1045 DCHECK(snapped_type
== wm::WINDOW_STATE_TYPE_LEFT_SNAPPED
||
1046 snapped_type
== wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED
);
1047 gfx::Rect snapped_bounds
= ScreenUtil::GetDisplayWorkAreaBoundsInParent(
1049 if (snapped_type
== wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED
)
1050 snapped_bounds
.set_x(snapped_bounds
.right() - bounds_in_parent
.width());
1051 snapped_bounds
.set_width(bounds_in_parent
.width());
1052 return bounds_in_parent
== snapped_bounds
;