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/coordinate_conversion.h"
19 #include "ash/wm/default_window_resizer.h"
20 #include "ash/wm/dock/docked_window_layout_manager.h"
21 #include "ash/wm/dock/docked_window_resizer.h"
22 #include "ash/wm/drag_window_resizer.h"
23 #include "ash/wm/panels/panel_window_resizer.h"
24 #include "ash/wm/window_state.h"
25 #include "ash/wm/window_util.h"
26 #include "ash/wm/wm_event.h"
27 #include "ash/wm/workspace/phantom_window_controller.h"
28 #include "ash/wm/workspace/two_step_edge_cycler.h"
29 #include "base/command_line.h"
30 #include "base/memory/weak_ptr.h"
31 #include "ui/aura/client/aura_constants.h"
32 #include "ui/aura/client/screen_position_client.h"
33 #include "ui/aura/window.h"
34 #include "ui/aura/window_delegate.h"
35 #include "ui/aura/window_event_dispatcher.h"
36 #include "ui/base/hit_test.h"
37 #include "ui/compositor/layer.h"
38 #include "ui/gfx/screen.h"
39 #include "ui/gfx/transform.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
);
446 if (!snapped
&& window_state()->IsSnapped()) {
447 // Keep the window snapped if the user resizes the window such that the
448 // window has valid bounds for a snapped window. Always unsnap the window
449 // if the user dragged the window via the caption area because doing this is
450 // slightly less confusing.
451 if (details().window_component
== HTCAPTION
||
452 !AreBoundsValidSnappedBounds(window_state()->GetStateType(),
453 GetTarget()->bounds())) {
454 // Set the window to WINDOW_STATE_TYPE_NORMAL but keep the
455 // window at the bounds that the user has moved/resized the
456 // window to. ClearRestoreBounds() is used instead of
457 // SaveCurrentBoundsForRestore() because most of the restore
458 // logic is skipped because we are still in the middle of a
459 // drag. TODO(pkotwicz): Fix this and use
460 // SaveCurrentBoundsForRestore().
461 window_state()->ClearRestoreBounds();
462 window_state()->Restore();
467 void WorkspaceWindowResizer::RevertDrag() {
468 window_state()->set_bounds_changed_by_user(initial_bounds_changed_by_user_
);
469 snap_phantom_window_controller_
.reset();
471 if (!did_move_or_resize_
)
474 GetTarget()->SetBounds(details().initial_bounds_in_parent
);
475 if (!details().restore_bounds
.IsEmpty()) {
476 window_state()->SetRestoreBoundsInScreen(details().restore_bounds
);
479 if (details().window_component
== HTRIGHT
) {
480 int last_x
= details().initial_bounds_in_parent
.right();
481 for (size_t i
= 0; i
< attached_windows_
.size(); ++i
) {
482 gfx::Rect
bounds(attached_windows_
[i
]->bounds());
483 bounds
.set_x(last_x
);
484 bounds
.set_width(initial_size_
[i
]);
485 attached_windows_
[i
]->SetBounds(bounds
);
486 last_x
= attached_windows_
[i
]->bounds().right();
489 int last_y
= details().initial_bounds_in_parent
.bottom();
490 for (size_t i
= 0; i
< attached_windows_
.size(); ++i
) {
491 gfx::Rect
bounds(attached_windows_
[i
]->bounds());
492 bounds
.set_y(last_y
);
493 bounds
.set_height(initial_size_
[i
]);
494 attached_windows_
[i
]->SetBounds(bounds
);
495 last_y
= attached_windows_
[i
]->bounds().bottom();
500 WorkspaceWindowResizer::WorkspaceWindowResizer(
501 wm::WindowState
* window_state
,
502 const std::vector
<aura::Window
*>& attached_windows
)
503 : WindowResizer(window_state
),
504 attached_windows_(attached_windows
),
505 did_lock_cursor_(false),
506 did_move_or_resize_(false),
507 initial_bounds_changed_by_user_(window_state_
->bounds_changed_by_user()),
509 total_initial_size_(0),
510 snap_type_(SNAP_NONE
),
511 num_mouse_moves_since_bounds_change_(0),
512 magnetism_window_(NULL
),
513 weak_ptr_factory_(this) {
514 DCHECK(details().is_resizable
);
516 // A mousemove should still show the cursor even if the window is
517 // being moved or resized with touch, so do not lock the cursor.
518 if (details().source
!= aura::client::WINDOW_MOVE_SOURCE_TOUCH
) {
519 Shell
* shell
= Shell::GetInstance();
520 shell
->cursor_manager()->LockCursor();
521 did_lock_cursor_
= true;
524 aura::Window
* dock_container
= Shell::GetContainer(
525 GetTarget()->GetRootWindow(), kShellWindowId_DockedContainer
);
526 dock_layout_
= static_cast<DockedWindowLayoutManager
*>(
527 dock_container
->layout_manager());
529 // Only support attaching to the right/bottom.
530 DCHECK(attached_windows_
.empty() ||
531 (details().window_component
== HTRIGHT
||
532 details().window_component
== HTBOTTOM
));
534 // TODO: figure out how to deal with window going off the edge.
536 // Calculate sizes so that we can maintain the ratios if we need to resize.
537 int total_available
= 0;
538 for (size_t i
= 0; i
< attached_windows_
.size(); ++i
) {
539 gfx::Size
min(attached_windows_
[i
]->delegate()->GetMinimumSize());
540 int initial_size
= PrimaryAxisSize(attached_windows_
[i
]->bounds().size());
541 initial_size_
.push_back(initial_size
);
542 // If current size is smaller than the min, use the current size as the min.
543 // This way we don't snap on resize.
544 int min_size
= std::min(initial_size
,
545 std::max(PrimaryAxisSize(min
), kMinOnscreenSize
));
546 total_min_
+= min_size
;
547 total_initial_size_
+= initial_size
;
548 total_available
+= std::max(min_size
, initial_size
) - min_size
;
553 void WorkspaceWindowResizer::LayoutAttachedWindows(
555 gfx::Rect
work_area(ScreenUtil::GetDisplayWorkAreaBoundsInParent(
557 int initial_size
= PrimaryAxisSize(details().initial_bounds_in_parent
.size());
558 int current_size
= PrimaryAxisSize(bounds
->size());
559 int start
= PrimaryAxisCoordinate(bounds
->right(), bounds
->bottom());
560 int end
= PrimaryAxisCoordinate(work_area
.right(), work_area
.bottom());
562 int delta
= current_size
- initial_size
;
563 int available_size
= end
- start
;
564 std::vector
<int> sizes
;
565 int leftovers
= CalculateAttachedSizes(delta
, available_size
, &sizes
);
567 // leftovers > 0 means that the attached windows can't grow to compensate for
568 // the shrinkage of the main window. This line causes the attached windows to
569 // be moved so they are still flush against the main window, rather than the
570 // main window being prevented from shrinking.
571 leftovers
= std::min(0, leftovers
);
572 // Reallocate any leftover pixels back into the main window. This is
573 // necessary when, for example, the main window shrinks, but none of the
574 // attached windows can grow without exceeding their max size constraints.
575 // Adding the pixels back to the main window effectively prevents the main
576 // window from resizing too far.
577 if (details().window_component
== HTRIGHT
)
578 bounds
->set_width(bounds
->width() + leftovers
);
580 bounds
->set_height(bounds
->height() + leftovers
);
582 DCHECK_EQ(attached_windows_
.size(), sizes
.size());
583 int last
= PrimaryAxisCoordinate(bounds
->right(), bounds
->bottom());
584 for (size_t i
= 0; i
< attached_windows_
.size(); ++i
) {
585 gfx::Rect
attached_bounds(attached_windows_
[i
]->bounds());
586 if (details().window_component
== HTRIGHT
) {
587 attached_bounds
.set_x(last
);
588 attached_bounds
.set_width(sizes
[i
]);
590 attached_bounds
.set_y(last
);
591 attached_bounds
.set_height(sizes
[i
]);
593 attached_windows_
[i
]->SetBounds(attached_bounds
);
598 int WorkspaceWindowResizer::CalculateAttachedSizes(
601 std::vector
<int>* sizes
) const {
602 std::vector
<WindowSize
> window_sizes
;
603 CreateBucketsForAttached(&window_sizes
);
605 // How much we need to grow the attached by (collectively).
606 int grow_attached_by
= 0;
608 // If the attached windows don't fit when at their initial size, we will
609 // have to shrink them by how much they overflow.
610 if (total_initial_size_
>= available_size
)
611 grow_attached_by
= available_size
- total_initial_size_
;
613 // If we're shrinking, we grow the attached so the total size remains
615 grow_attached_by
= -delta
;
618 int leftover_pixels
= 0;
619 while (grow_attached_by
!= 0) {
620 int leftovers
= GrowFairly(grow_attached_by
, window_sizes
);
621 if (leftovers
== grow_attached_by
) {
622 leftover_pixels
= leftovers
;
625 grow_attached_by
= leftovers
;
628 for (size_t i
= 0; i
< window_sizes
.size(); ++i
)
629 sizes
->push_back(window_sizes
[i
].size());
631 return leftover_pixels
;
634 int WorkspaceWindowResizer::GrowFairly(
636 std::vector
<WindowSize
>& sizes
) const {
637 bool shrinking
= pixels
< 0;
638 std::vector
<WindowSize
*> nonfull_windows
;
639 for (size_t i
= 0; i
< sizes
.size(); ++i
) {
640 if (!sizes
[i
].is_at_capacity(shrinking
))
641 nonfull_windows
.push_back(&sizes
[i
]);
643 std::vector
<float> ratios
;
644 CalculateGrowthRatios(nonfull_windows
, &ratios
);
646 int remaining_pixels
= pixels
;
647 bool add_leftover_pixels_to_last
= true;
648 for (size_t i
= 0; i
< nonfull_windows
.size(); ++i
) {
649 int grow_by
= pixels
* ratios
[i
];
650 // Put any leftover pixels into the last window.
651 if (i
== nonfull_windows
.size() - 1 && add_leftover_pixels_to_last
)
652 grow_by
= remaining_pixels
;
653 int remainder
= nonfull_windows
[i
]->Add(grow_by
);
654 int consumed
= grow_by
- remainder
;
655 remaining_pixels
-= consumed
;
656 if (nonfull_windows
[i
]->is_at_capacity(shrinking
) && remainder
> 0) {
657 // Because this window overflowed, some of the pixels in
658 // |remaining_pixels| aren't there due to rounding errors. Rather than
659 // unfairly giving all those pixels to the last window, we refrain from
660 // allocating them so that this function can be called again to distribute
661 // the pixels fairly.
662 add_leftover_pixels_to_last
= false;
665 return remaining_pixels
;
668 void WorkspaceWindowResizer::CalculateGrowthRatios(
669 const std::vector
<WindowSize
*>& sizes
,
670 std::vector
<float>* out_ratios
) const {
671 DCHECK(out_ratios
->empty());
673 for (size_t i
= 0; i
< sizes
.size(); ++i
)
674 total_value
+= sizes
[i
]->size();
676 for (size_t i
= 0; i
< sizes
.size(); ++i
)
677 out_ratios
->push_back(
678 (static_cast<float>(sizes
[i
]->size())) / total_value
);
681 void WorkspaceWindowResizer::CreateBucketsForAttached(
682 std::vector
<WindowSize
>* sizes
) const {
683 for (size_t i
= 0; i
< attached_windows_
.size(); i
++) {
684 int initial_size
= initial_size_
[i
];
685 aura::WindowDelegate
* delegate
= attached_windows_
[i
]->delegate();
686 int min
= PrimaryAxisSize(delegate
->GetMinimumSize());
687 int max
= PrimaryAxisSize(delegate
->GetMaximumSize());
689 sizes
->push_back(WindowSize(initial_size
, min
, max
));
693 void WorkspaceWindowResizer::MagneticallySnapToOtherWindows(gfx::Rect
* bounds
) {
694 if (UpdateMagnetismWindow(*bounds
, kAllMagnetismEdges
)) {
695 gfx::Point point
= OriginForMagneticAttach(
696 ScreenUtil::ConvertRectToScreen(GetTarget()->parent(), *bounds
),
697 magnetism_window_
->GetBoundsInScreen(),
699 aura::client::GetScreenPositionClient(GetTarget()->GetRootWindow())->
700 ConvertPointFromScreen(GetTarget()->parent(), &point
);
701 bounds
->set_origin(point
);
705 void WorkspaceWindowResizer::MagneticallySnapResizeToOtherWindows(
707 const uint32 edges
= WindowComponentToMagneticEdge(
708 details().window_component
);
709 if (UpdateMagnetismWindow(*bounds
, edges
)) {
710 *bounds
= ScreenUtil::ConvertRectFromScreen(
711 GetTarget()->parent(),
712 BoundsForMagneticResizeAttach(
713 ScreenUtil::ConvertRectToScreen(GetTarget()->parent(), *bounds
),
714 magnetism_window_
->GetBoundsInScreen(),
719 bool WorkspaceWindowResizer::UpdateMagnetismWindow(const gfx::Rect
& bounds
,
721 // |bounds| are in coordinates of original window's parent.
722 gfx::Rect bounds_in_screen
=
723 ScreenUtil::ConvertRectToScreen(GetTarget()->parent(), bounds
);
724 MagnetismMatcher
matcher(bounds_in_screen
, edges
);
726 // If we snapped to a window then check it first. That way we don't bounce
727 // around when close to multiple edges.
728 if (magnetism_window_
) {
729 if (window_tracker_
.Contains(magnetism_window_
) &&
730 matcher
.ShouldAttach(magnetism_window_
->GetBoundsInScreen(),
734 window_tracker_
.Remove(magnetism_window_
);
735 magnetism_window_
= NULL
;
738 // Avoid magnetically snapping windows that are not resizable.
739 // TODO(oshima): change this to window.type() == TYPE_NORMAL.
740 if (!window_state()->CanResize())
743 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
744 for (aura::Window::Windows::iterator iter
= root_windows
.begin();
745 iter
!= root_windows
.end(); ++iter
) {
746 const aura::Window
* root_window
= *iter
;
747 // Test all children from the desktop in each root window.
748 const aura::Window::Windows
& children
= Shell::GetContainer(
749 root_window
, kShellWindowId_DefaultContainer
)->children();
750 for (aura::Window::Windows::const_reverse_iterator i
= children
.rbegin();
751 i
!= children
.rend() && !matcher
.AreEdgesObscured(); ++i
) {
752 wm::WindowState
* other_state
= wm::GetWindowState(*i
);
753 if (other_state
->window() == GetTarget() ||
754 !other_state
->window()->IsVisible() ||
755 !other_state
->IsNormalOrSnapped() ||
756 !other_state
->CanResize()) {
759 if (matcher
.ShouldAttach(
760 other_state
->window()->GetBoundsInScreen(), &magnetism_edge_
)) {
761 magnetism_window_
= other_state
->window();
762 window_tracker_
.Add(magnetism_window_
);
770 void WorkspaceWindowResizer::AdjustBoundsForMainWindow(
773 gfx::Point last_mouse_location_in_screen
= last_mouse_location_
;
774 wm::ConvertPointToScreen(GetTarget()->parent(),
775 &last_mouse_location_in_screen
);
776 gfx::Display display
= Shell::GetScreen()->GetDisplayNearestPoint(
777 last_mouse_location_in_screen
);
778 gfx::Rect work_area
=
779 ScreenUtil::ConvertRectFromScreen(GetTarget()->parent(),
780 display
.work_area());
781 if (details().window_component
== HTCAPTION
) {
782 // Adjust the bounds to the work area where the mouse cursor is located.
783 // Always keep kMinOnscreenHeight or the window height (whichever is less)
785 int max_y
= work_area
.bottom() - std::min(kMinOnscreenHeight
,
787 if (bounds
->y() > max_y
) {
788 bounds
->set_y(max_y
);
789 } else if (bounds
->y() <= work_area
.y()) {
790 // Don't allow dragging above the top of the display until the mouse
791 // cursor reaches the work area above if any.
792 bounds
->set_y(work_area
.y());
795 if (sticky_size
> 0) {
796 // Possibly stick to edge except when a mouse pointer is outside the
798 if (display
.work_area().Contains(last_mouse_location_in_screen
))
799 StickToWorkAreaOnMove(work_area
, sticky_size
, bounds
);
800 MagneticallySnapToOtherWindows(bounds
);
802 } else if (sticky_size
> 0) {
803 MagneticallySnapResizeToOtherWindows(bounds
);
804 if (!magnetism_window_
&& sticky_size
> 0)
805 StickToWorkAreaOnResize(work_area
, sticky_size
, bounds
);
808 if (attached_windows_
.empty())
811 if (details().window_component
== HTRIGHT
) {
812 bounds
->set_width(std::min(bounds
->width(),
813 work_area
.right() - total_min_
- bounds
->x()));
815 DCHECK_EQ(HTBOTTOM
, details().window_component
);
816 bounds
->set_height(std::min(bounds
->height(),
817 work_area
.bottom() - total_min_
- bounds
->y()));
821 bool WorkspaceWindowResizer::StickToWorkAreaOnMove(
822 const gfx::Rect
& work_area
,
824 gfx::Rect
* bounds
) const {
825 const int left_edge
= work_area
.x();
826 const int right_edge
= work_area
.right();
827 const int top_edge
= work_area
.y();
828 const int bottom_edge
= work_area
.bottom();
829 bool updated
= false;
830 if (ShouldStickToEdge(bounds
->x() - left_edge
, sticky_size
)) {
831 bounds
->set_x(left_edge
);
833 } else if (ShouldStickToEdge(right_edge
- bounds
->right(), sticky_size
)) {
834 bounds
->set_x(right_edge
- bounds
->width());
837 if (ShouldStickToEdge(bounds
->y() - top_edge
, sticky_size
)) {
838 bounds
->set_y(top_edge
);
840 } else if (ShouldStickToEdge(bottom_edge
- bounds
->bottom(), sticky_size
) &&
841 bounds
->height() < (bottom_edge
- top_edge
)) {
842 // Only snap to the bottom if the window is smaller than the work area.
843 // Doing otherwise can lead to window snapping in weird ways as it bounces
844 // between snapping to top then bottom.
845 bounds
->set_y(bottom_edge
- bounds
->height());
851 void WorkspaceWindowResizer::StickToWorkAreaOnResize(
852 const gfx::Rect
& work_area
,
854 gfx::Rect
* bounds
) const {
855 const uint32 edges
= WindowComponentToMagneticEdge(
856 details().window_component
);
857 const int left_edge
= work_area
.x();
858 const int right_edge
= work_area
.right();
859 const int top_edge
= work_area
.y();
860 const int bottom_edge
= work_area
.bottom();
861 if (edges
& MAGNETISM_EDGE_TOP
&&
862 ShouldStickToEdge(bounds
->y() - top_edge
, sticky_size
)) {
863 bounds
->set_height(bounds
->bottom() - top_edge
);
864 bounds
->set_y(top_edge
);
866 if (edges
& MAGNETISM_EDGE_LEFT
&&
867 ShouldStickToEdge(bounds
->x() - left_edge
, sticky_size
)) {
868 bounds
->set_width(bounds
->right() - left_edge
);
869 bounds
->set_x(left_edge
);
871 if (edges
& MAGNETISM_EDGE_BOTTOM
&&
872 ShouldStickToEdge(bottom_edge
- bounds
->bottom(), sticky_size
)) {
873 bounds
->set_height(bottom_edge
- bounds
->y());
875 if (edges
& MAGNETISM_EDGE_RIGHT
&&
876 ShouldStickToEdge(right_edge
- bounds
->right(), sticky_size
)) {
877 bounds
->set_width(right_edge
- bounds
->x());
881 int WorkspaceWindowResizer::PrimaryAxisSize(const gfx::Size
& size
) const {
882 return PrimaryAxisCoordinate(size
.width(), size
.height());
885 int WorkspaceWindowResizer::PrimaryAxisCoordinate(int x
, int y
) const {
886 switch (details().window_component
) {
897 void WorkspaceWindowResizer::UpdateSnapPhantomWindow(const gfx::Point
& location
,
898 const gfx::Rect
& bounds
) {
899 if (!did_move_or_resize_
|| details().window_component
!= HTCAPTION
)
902 SnapType last_type
= snap_type_
;
903 snap_type_
= GetSnapType(location
);
904 if (snap_type_
== SNAP_NONE
|| snap_type_
!= last_type
) {
905 snap_phantom_window_controller_
.reset();
906 edge_cycler_
.reset();
907 if (snap_type_
== SNAP_NONE
) {
908 SetDraggedWindowDocked(false);
913 DCHECK(snap_type_
== SNAP_LEFT
|| snap_type_
== SNAP_RIGHT
);
914 DockedAlignment desired_alignment
= (snap_type_
== SNAP_LEFT
) ?
915 DOCKED_ALIGNMENT_LEFT
: DOCKED_ALIGNMENT_RIGHT
;
916 const bool can_dock
=
917 dock_layout_
->CanDockWindow(GetTarget(), desired_alignment
) &&
918 dock_layout_
->GetAlignmentOfWindow(GetTarget()) != DOCKED_ALIGNMENT_NONE
;
920 // If the window cannot be docked, undock the window. This may change the
921 // workspace bounds and hence |snap_type_|.
922 SetDraggedWindowDocked(false);
923 snap_type_
= GetSnapType(location
);
925 const bool can_snap
= snap_type_
!= SNAP_NONE
&& window_state()->CanSnap();
926 if (!can_snap
&& !can_dock
) {
927 snap_type_
= SNAP_NONE
;
928 snap_phantom_window_controller_
.reset();
929 edge_cycler_
.reset();
933 edge_cycler_
.reset(new TwoStepEdgeCycler(location
));
935 edge_cycler_
->OnMove(location
);
937 // Update phantom window with snapped or docked guide bounds.
938 // Windows that cannot be snapped or are less wide than kMaxDockWidth can get
939 // docked without going through a snapping sequence.
940 gfx::Rect phantom_bounds
;
941 const bool should_dock
= can_dock
&&
943 GetTarget()->bounds().width() <=
944 DockedWindowLayoutManager::kMaxDockWidth
||
945 edge_cycler_
->use_second_mode() ||
946 dock_layout_
->is_dragged_window_docked());
948 SetDraggedWindowDocked(true);
949 phantom_bounds
= ScreenUtil::ConvertRectFromScreen(
950 GetTarget()->parent(), dock_layout_
->dragged_bounds());
952 phantom_bounds
= (snap_type_
== SNAP_LEFT
) ?
953 wm::GetDefaultLeftSnappedWindowBoundsInParent(GetTarget()) :
954 wm::GetDefaultRightSnappedWindowBoundsInParent(GetTarget());
957 if (!snap_phantom_window_controller_
) {
958 snap_phantom_window_controller_
.reset(
959 new PhantomWindowController(GetTarget()));
961 snap_phantom_window_controller_
->Show(ScreenUtil::ConvertRectToScreen(
962 GetTarget()->parent(), phantom_bounds
));
965 void WorkspaceWindowResizer::RestackWindows() {
966 if (attached_windows_
.empty())
968 // Build a map from index in children to window, returning if there is a
969 // window with a different parent.
970 typedef std::map
<size_t, aura::Window
*> IndexToWindowMap
;
971 IndexToWindowMap map
;
972 aura::Window
* parent
= GetTarget()->parent();
973 const aura::Window::Windows
& windows(parent
->children());
974 map
[std::find(windows
.begin(), windows
.end(), GetTarget()) -
975 windows
.begin()] = GetTarget();
976 for (std::vector
<aura::Window
*>::const_iterator i
=
977 attached_windows_
.begin(); i
!= attached_windows_
.end(); ++i
) {
978 if ((*i
)->parent() != parent
)
981 std::find(windows
.begin(), windows
.end(), *i
) - windows
.begin();
985 // Reorder the windows starting at the topmost.
986 parent
->StackChildAtTop(map
.rbegin()->second
);
987 for (IndexToWindowMap::const_reverse_iterator i
= map
.rbegin();
989 aura::Window
* window
= i
->second
;
992 parent
->StackChildBelow(i
->second
, window
);
996 WorkspaceWindowResizer::SnapType
WorkspaceWindowResizer::GetSnapType(
997 const gfx::Point
& location
) const {
998 // TODO: this likely only wants total display area, not the area of a single
1000 gfx::Rect
area(ScreenUtil::GetDisplayWorkAreaBoundsInParent(GetTarget()));
1001 if (details().source
== aura::client::WINDOW_MOVE_SOURCE_TOUCH
) {
1002 // Increase tolerance for touch-snapping near the screen edges. This is only
1003 // necessary when the work area left or right edge is same as screen edge.
1004 gfx::Rect
display_bounds(ScreenUtil::GetDisplayBoundsInParent(GetTarget()));
1006 if (area
.x() == display_bounds
.x())
1007 inset_left
= kScreenEdgeInsetForTouchDrag
;
1008 int inset_right
= 0;
1009 if (area
.right() == display_bounds
.right())
1010 inset_right
= kScreenEdgeInsetForTouchDrag
;
1011 area
.Inset(inset_left
, 0, inset_right
, 0);
1013 if (location
.x() <= area
.x())
1015 if (location
.x() >= area
.right() - 1)
1020 void WorkspaceWindowResizer::SetDraggedWindowDocked(bool should_dock
) {
1022 if (!dock_layout_
->is_dragged_window_docked()) {
1023 window_state()->set_bounds_changed_by_user(false);
1024 dock_layout_
->DockDraggedWindow(GetTarget());
1027 if (dock_layout_
->is_dragged_window_docked()) {
1028 dock_layout_
->UndockDraggedWindow();
1029 window_state()->set_bounds_changed_by_user(true);
1034 bool WorkspaceWindowResizer::AreBoundsValidSnappedBounds(
1035 wm::WindowStateType snapped_type
,
1036 const gfx::Rect
& bounds_in_parent
) const {
1037 DCHECK(snapped_type
== wm::WINDOW_STATE_TYPE_LEFT_SNAPPED
||
1038 snapped_type
== wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED
);
1039 gfx::Rect snapped_bounds
= ScreenUtil::GetDisplayWorkAreaBoundsInParent(
1041 if (snapped_type
== wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED
)
1042 snapped_bounds
.set_x(snapped_bounds
.right() - bounds_in_parent
.width());
1043 snapped_bounds
.set_width(bounds_in_parent
.width());
1044 return bounds_in_parent
== snapped_bounds
;