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/panels/panel_layout_manager.h"
10 #include "ash/screen_util.h"
11 #include "ash/shelf/shelf.h"
12 #include "ash/shelf/shelf_layout_manager.h"
13 #include "ash/shelf/shelf_types.h"
14 #include "ash/shelf/shelf_util.h"
15 #include "ash/shelf/shelf_widget.h"
16 #include "ash/shell.h"
17 #include "ash/shell_window_ids.h"
18 #include "ash/wm/overview/window_selector_controller.h"
19 #include "ash/wm/window_animations.h"
20 #include "ash/wm/window_state.h"
21 #include "ash/wm/window_util.h"
22 #include "base/auto_reset.h"
23 #include "base/bind.h"
24 #include "base/bind_helpers.h"
25 #include "third_party/skia/include/core/SkColor.h"
26 #include "third_party/skia/include/core/SkPaint.h"
27 #include "third_party/skia/include/core/SkPath.h"
28 #include "ui/aura/client/focus_client.h"
29 #include "ui/aura/client/window_tree_client.h"
30 #include "ui/aura/window.h"
31 #include "ui/aura/window_delegate.h"
32 #include "ui/aura/window_event_dispatcher.h"
33 #include "ui/aura/window_tracker.h"
34 #include "ui/compositor/scoped_layer_animation_settings.h"
35 #include "ui/gfx/canvas.h"
36 #include "ui/gfx/geometry/rect.h"
37 #include "ui/gfx/geometry/vector2d.h"
38 #include "ui/views/background.h"
39 #include "ui/views/widget/widget.h"
40 #include "ui/wm/public/activation_client.h"
45 const int kPanelIdealSpacing
= 4;
47 const float kMaxHeightFactor
= .80f
;
48 const float kMaxWidthFactor
= .50f
;
50 // Duration for panel animations.
51 const int kPanelSlideDurationMilliseconds
= 50;
52 const int kCalloutFadeDurationMilliseconds
= 50;
54 // Offset used when sliding panel in/out of the shelf. Used for minimizing,
55 // restoring and the initial showing of a panel.
56 const int kPanelSlideInOffset
= 20;
58 // Callout arrow dimensions.
59 const int kArrowWidth
= 18;
60 const int kArrowHeight
= 9;
62 class CalloutWidgetBackground
: public views::Background
{
64 CalloutWidgetBackground() : alignment_(SHELF_ALIGNMENT_BOTTOM
) {
67 void Paint(gfx::Canvas
* canvas
, views::View
* view
) const override
{
70 case SHELF_ALIGNMENT_BOTTOM
:
71 path
.moveTo(SkIntToScalar(0), SkIntToScalar(0));
72 path
.lineTo(SkIntToScalar(kArrowWidth
/ 2),
73 SkIntToScalar(kArrowHeight
));
74 path
.lineTo(SkIntToScalar(kArrowWidth
), SkIntToScalar(0));
76 case SHELF_ALIGNMENT_LEFT
:
77 path
.moveTo(SkIntToScalar(kArrowHeight
), SkIntToScalar(kArrowWidth
));
78 path
.lineTo(SkIntToScalar(0), SkIntToScalar(kArrowWidth
/ 2));
79 path
.lineTo(SkIntToScalar(kArrowHeight
), SkIntToScalar(0));
81 case SHELF_ALIGNMENT_TOP
:
82 path
.moveTo(SkIntToScalar(0), SkIntToScalar(kArrowHeight
));
83 path
.lineTo(SkIntToScalar(kArrowWidth
/ 2), SkIntToScalar(0));
84 path
.lineTo(SkIntToScalar(kArrowWidth
), SkIntToScalar(kArrowHeight
));
86 case SHELF_ALIGNMENT_RIGHT
:
87 path
.moveTo(SkIntToScalar(0), SkIntToScalar(0));
88 path
.lineTo(SkIntToScalar(kArrowHeight
),
89 SkIntToScalar(kArrowWidth
/ 2));
90 path
.lineTo(SkIntToScalar(0), SkIntToScalar(kArrowWidth
));
93 // Hard code the arrow color for now.
95 paint
.setStyle(SkPaint::kFill_Style
);
96 paint
.setColor(SkColorSetARGB(0xff, 0xe5, 0xe5, 0xe5));
97 canvas
->DrawPath(path
, paint
);
100 ShelfAlignment
alignment() {
104 void set_alignment(ShelfAlignment alignment
) {
105 alignment_
= alignment
;
109 ShelfAlignment alignment_
;
111 DISALLOW_COPY_AND_ASSIGN(CalloutWidgetBackground
);
114 struct VisiblePanelPositionInfo
{
115 VisiblePanelPositionInfo()
127 aura::Window
* window
;
131 bool CompareWindowMajor(const VisiblePanelPositionInfo
& win1
,
132 const VisiblePanelPositionInfo
& win2
) {
133 return win1
.major_pos
< win2
.major_pos
;
136 void FanOutPanels(std::vector
<VisiblePanelPositionInfo
>::iterator first
,
137 std::vector
<VisiblePanelPositionInfo
>::iterator last
) {
138 int num_panels
= last
- first
;
139 if (num_panels
== 1) {
140 (*first
).major_pos
= std::max((*first
).min_major
, std::min(
141 (*first
).max_major
, (*first
).major_pos
));
146 if (num_panels
== 2) {
147 // If there are two adjacent overlapping windows, separate them by the
148 // minimum major_length necessary.
149 std::vector
<VisiblePanelPositionInfo
>::iterator second
= first
+ 1;
150 int separation
= (*first
).major_length
/ 2 + (*second
).major_length
/ 2 +
152 int overlap
= (*first
).major_pos
+ separation
- (*second
).major_pos
;
153 (*first
).major_pos
= std::max((*first
).min_major
,
154 (*first
).major_pos
- overlap
/ 2);
155 (*second
).major_pos
= std::min((*second
).max_major
,
156 (*first
).major_pos
+ separation
);
157 // Recalculate the first panel position in case the second one was
158 // constrained on the right.
159 (*first
).major_pos
= std::max((*first
).min_major
,
160 (*second
).major_pos
- separation
);
164 // If there are more than two overlapping windows, fan them out from minimum
165 // position to maximum position equally spaced.
166 int delta
= ((*(last
- 1)).max_major
- (*first
).min_major
) / (num_panels
- 1);
167 int major_pos
= (*first
).min_major
;
168 for (std::vector
<VisiblePanelPositionInfo
>::iterator iter
= first
;
169 iter
!= last
; ++iter
) {
170 (*iter
).major_pos
= std::max((*iter
).min_major
,
171 std::min((*iter
).max_major
, major_pos
));
176 bool BoundsAdjacent(const gfx::Rect
& bounds1
, const gfx::Rect
& bounds2
) {
177 return bounds1
.x() == bounds2
.right() ||
178 bounds1
.y() == bounds2
.bottom() ||
179 bounds1
.right() == bounds2
.x() ||
180 bounds1
.bottom() == bounds2
.y();
183 gfx::Vector2d
GetSlideInAnimationOffset(ShelfAlignment alignment
) {
184 gfx::Vector2d offset
;
186 case SHELF_ALIGNMENT_BOTTOM
:
187 offset
.set_y(kPanelSlideInOffset
);
189 case SHELF_ALIGNMENT_LEFT
:
190 offset
.set_x(-kPanelSlideInOffset
);
192 case SHELF_ALIGNMENT_RIGHT
:
193 offset
.set_x(kPanelSlideInOffset
);
195 case SHELF_ALIGNMENT_TOP
:
196 offset
.set_y(-kPanelSlideInOffset
);
204 class PanelCalloutWidget
: public views::Widget
{
206 explicit PanelCalloutWidget(aura::Window
* container
)
207 : background_(NULL
) {
208 InitWidget(container
);
211 void SetAlignment(ShelfAlignment alignment
) {
212 gfx::Rect callout_bounds
= GetWindowBoundsInScreen();
213 if (alignment
== SHELF_ALIGNMENT_BOTTOM
||
214 alignment
== SHELF_ALIGNMENT_TOP
) {
215 callout_bounds
.set_width(kArrowWidth
);
216 callout_bounds
.set_height(kArrowHeight
);
218 callout_bounds
.set_width(kArrowHeight
);
219 callout_bounds
.set_height(kArrowWidth
);
221 GetNativeWindow()->SetBounds(callout_bounds
);
222 if (background_
->alignment() != alignment
) {
223 background_
->set_alignment(alignment
);
224 SchedulePaintInRect(gfx::Rect(gfx::Point(), callout_bounds
.size()));
229 void InitWidget(aura::Window
* parent
) {
230 views::Widget::InitParams params
;
231 params
.type
= views::Widget::InitParams::TYPE_POPUP
;
232 params
.opacity
= views::Widget::InitParams::TRANSLUCENT_WINDOW
;
233 params
.keep_on_top
= true;
234 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
235 params
.parent
= parent
;
236 params
.bounds
= ScreenUtil::ConvertRectToScreen(parent
, gfx::Rect());
237 params
.bounds
.set_width(kArrowWidth
);
238 params
.bounds
.set_height(kArrowHeight
);
239 params
.accept_events
= false;
240 set_focus_on_creation(false);
242 DCHECK_EQ(GetNativeView()->GetRootWindow(), parent
->GetRootWindow());
243 views::View
* content_view
= new views::View
;
244 background_
= new CalloutWidgetBackground
;
245 content_view
->set_background(background_
);
246 SetContentsView(content_view
);
247 GetNativeWindow()->layer()->SetOpacity(0);
250 // Weak pointer owned by this widget's content view.
251 CalloutWidgetBackground
* background_
;
253 DISALLOW_COPY_AND_ASSIGN(PanelCalloutWidget
);
256 ////////////////////////////////////////////////////////////////////////////////
257 // PanelLayoutManager public implementation:
258 PanelLayoutManager::PanelLayoutManager(aura::Window
* panel_container
)
259 : panel_container_(panel_container
),
260 in_add_window_(false),
262 show_callout_widgets_(true),
263 dragged_panel_(NULL
),
265 shelf_layout_manager_(NULL
),
266 last_active_panel_(NULL
),
267 weak_factory_(this) {
268 DCHECK(panel_container
);
269 aura::client::GetActivationClient(Shell::GetPrimaryRootWindow())->
271 Shell::GetInstance()->display_controller()->AddObserver(this);
272 Shell::GetInstance()->AddShellObserver(this);
275 PanelLayoutManager::~PanelLayoutManager() {
279 void PanelLayoutManager::Shutdown() {
280 if (shelf_layout_manager_
)
281 shelf_layout_manager_
->RemoveObserver(this);
282 shelf_layout_manager_
= NULL
;
283 for (PanelList::iterator iter
= panel_windows_
.begin();
284 iter
!= panel_windows_
.end(); ++iter
) {
285 delete iter
->callout_widget
;
287 panel_windows_
.clear();
289 shelf_
->RemoveIconObserver(this);
291 aura::client::GetActivationClient(Shell::GetPrimaryRootWindow())->
292 RemoveObserver(this);
293 Shell::GetInstance()->display_controller()->RemoveObserver(this);
294 Shell::GetInstance()->RemoveShellObserver(this);
297 void PanelLayoutManager::StartDragging(aura::Window
* panel
) {
298 DCHECK(!dragged_panel_
);
299 dragged_panel_
= panel
;
303 void PanelLayoutManager::FinishDragging() {
304 dragged_panel_
= NULL
;
308 void PanelLayoutManager::SetShelf(Shelf
* shelf
) {
310 DCHECK(!shelf_layout_manager_
);
312 shelf_
->AddIconObserver(this);
313 if (shelf_
->shelf_widget()) {
314 shelf_layout_manager_
= ash::ShelfLayoutManager::ForShelf(
315 shelf_
->shelf_widget()->GetNativeWindow());
316 WillChangeVisibilityState(shelf_layout_manager_
->visibility_state());
317 shelf_layout_manager_
->AddObserver(this);
321 void PanelLayoutManager::ToggleMinimize(aura::Window
* panel
) {
322 DCHECK(panel
->parent() == panel_container_
);
323 wm::WindowState
* window_state
= wm::GetWindowState(panel
);
324 if (window_state
->IsMinimized())
325 window_state
->Restore();
327 window_state
->Minimize();
330 void PanelLayoutManager::SetShowCalloutWidgets(bool show
) {
331 if (show_callout_widgets_
== show
)
333 show_callout_widgets_
= show
;
337 views::Widget
* PanelLayoutManager::GetCalloutWidgetForPanel(
338 aura::Window
* panel
) {
339 DCHECK(panel
->parent() == panel_container_
);
340 PanelList::iterator found
=
341 std::find(panel_windows_
.begin(), panel_windows_
.end(), panel
);
342 DCHECK(found
!= panel_windows_
.end());
343 return found
->callout_widget
;
346 ////////////////////////////////////////////////////////////////////////////////
347 // PanelLayoutManager, aura::LayoutManager implementation:
348 void PanelLayoutManager::OnWindowResized() {
352 void PanelLayoutManager::OnWindowAddedToLayout(aura::Window
* child
) {
353 if (child
->type() == ui::wm::WINDOW_TYPE_POPUP
)
357 base::AutoReset
<bool> auto_reset_in_add_window(&in_add_window_
, true);
358 if (!wm::GetWindowState(child
)->panel_attached()) {
359 // This should only happen when a window is added to panel container as a
360 // result of bounds change from within the application during a drag.
361 // If so we have already stopped the drag and should reparent the panel
362 // back to appropriate container and ignore it.
363 // TODO(varkha): Updating bounds during a drag can cause problems and a more
364 // general solution is needed. See http://crbug.com/251813 .
365 aura::Window
* old_parent
= child
->parent();
366 aura::client::ParentWindowWithContext(
367 child
, child
, child
->GetRootWindow()->GetBoundsInScreen());
368 wm::ReparentTransientChildrenOfChild(child
, old_parent
, child
->parent());
369 DCHECK(child
->parent()->id() != kShellWindowId_PanelContainer
);
372 PanelInfo panel_info
;
373 panel_info
.window
= child
;
374 panel_info
.callout_widget
= new PanelCalloutWidget(panel_container_
);
375 panel_info
.slide_in
= child
!= dragged_panel_
;
376 panel_windows_
.push_back(panel_info
);
377 child
->AddObserver(this);
378 wm::GetWindowState(child
)->AddObserver(this);
382 void PanelLayoutManager::OnWillRemoveWindowFromLayout(aura::Window
* child
) {
385 void PanelLayoutManager::OnWindowRemovedFromLayout(aura::Window
* child
) {
386 if (child
->type() == ui::wm::WINDOW_TYPE_POPUP
)
388 PanelList::iterator found
=
389 std::find(panel_windows_
.begin(), panel_windows_
.end(), child
);
390 if (found
!= panel_windows_
.end()) {
391 delete found
->callout_widget
;
392 panel_windows_
.erase(found
);
394 if (restore_windows_on_shelf_visible_
)
395 restore_windows_on_shelf_visible_
->Remove(child
);
396 child
->RemoveObserver(this);
397 wm::GetWindowState(child
)->RemoveObserver(this);
399 if (dragged_panel_
== child
)
400 dragged_panel_
= NULL
;
402 if (last_active_panel_
== child
)
403 last_active_panel_
= NULL
;
408 void PanelLayoutManager::OnChildWindowVisibilityChanged(aura::Window
* child
,
411 wm::GetWindowState(child
)->Restore();
415 void PanelLayoutManager::SetChildBounds(aura::Window
* child
,
416 const gfx::Rect
& requested_bounds
) {
417 gfx::Rect
bounds(requested_bounds
);
418 const gfx::Rect
& max_bounds
= panel_container_
->GetRootWindow()->bounds();
419 const int max_width
= max_bounds
.width() * kMaxWidthFactor
;
420 const int max_height
= max_bounds
.height() * kMaxHeightFactor
;
421 if (bounds
.width() > max_width
)
422 bounds
.set_width(max_width
);
423 if (bounds
.height() > max_height
)
424 bounds
.set_height(max_height
);
426 // Reposition dragged panel in the panel order.
427 if (dragged_panel_
== child
) {
428 PanelList::iterator dragged_panel_iter
=
429 std::find(panel_windows_
.begin(), panel_windows_
.end(), dragged_panel_
);
430 DCHECK(dragged_panel_iter
!= panel_windows_
.end());
431 PanelList::iterator new_position
;
432 for (new_position
= panel_windows_
.begin();
433 new_position
!= panel_windows_
.end();
435 const gfx::Rect
& bounds
= (*new_position
).window
->bounds();
436 if (bounds
.x() + bounds
.width()/2 <= requested_bounds
.x()) break;
438 if (new_position
!= dragged_panel_iter
) {
439 PanelInfo dragged_panel_info
= *dragged_panel_iter
;
440 panel_windows_
.erase(dragged_panel_iter
);
441 panel_windows_
.insert(new_position
, dragged_panel_info
);
444 // Respect the minimum size of the window.
445 if (child
->delegate()) {
446 const gfx::Size
& min_size
= child
->delegate()->GetMinimumSize();
447 bounds
.set_width(std::max(min_size
.width(), bounds
.width()));
448 bounds
.set_height(std::max(min_size
.height(), bounds
.height()));
451 SetChildBoundsDirect(child
, bounds
);
455 ////////////////////////////////////////////////////////////////////////////////
456 // PanelLayoutManager, ShelfIconObserver implementation:
458 void PanelLayoutManager::OnShelfIconPositionsChanged() {
459 // TODO: As this is called for every animation step now. Relayout needs to be
460 // updated to use current icon position instead of use the ideal bounds so
461 // that the panels slide with their icons instead of jumping.
465 ////////////////////////////////////////////////////////////////////////////////
466 // PanelLayoutManager, ash::ShellObserver implementation:
468 void PanelLayoutManager::OnOverviewModeEnded() {
472 void PanelLayoutManager::OnShelfAlignmentChanged(aura::Window
* root_window
) {
473 if (panel_container_
->GetRootWindow() == root_window
)
477 /////////////////////////////////////////////////////////////////////////////
478 // PanelLayoutManager, WindowObserver implementation:
480 void PanelLayoutManager::OnWindowPropertyChanged(aura::Window
* window
,
483 // Trigger a relayout to position the panels whenever the panel icon is set
489 /////////////////////////////////////////////////////////////////////////////
490 // PanelLayoutManager, WindowStateObserver implementation:
492 void PanelLayoutManager::OnPostWindowStateTypeChange(
493 wm::WindowState
* window_state
,
494 wm::WindowStateType old_type
) {
495 // If the shelf is currently hidden then windows will not actually be shown
496 // but the set to restore when the shelf becomes visible is updated.
497 if (restore_windows_on_shelf_visible_
) {
498 if (window_state
->IsMinimized()) {
499 MinimizePanel(window_state
->window());
500 restore_windows_on_shelf_visible_
->Remove(window_state
->window());
502 restore_windows_on_shelf_visible_
->Add(window_state
->window());
507 if (window_state
->IsMinimized())
508 MinimizePanel(window_state
->window());
510 RestorePanel(window_state
->window());
513 ////////////////////////////////////////////////////////////////////////////////
514 // PanelLayoutManager, aura::client::ActivationChangeObserver implementation:
516 void PanelLayoutManager::OnWindowActivated(aura::Window
* gained_active
,
517 aura::Window
* lost_active
) {
518 // Ignore if the panel that is not managed by this was activated.
519 if (gained_active
&& gained_active
->type() == ui::wm::WINDOW_TYPE_PANEL
&&
520 gained_active
->parent() == panel_container_
) {
521 UpdateStacking(gained_active
);
526 ////////////////////////////////////////////////////////////////////////////////
527 // PanelLayoutManager, DisplayController::Observer implementation:
529 void PanelLayoutManager::OnDisplayConfigurationChanged() {
533 ////////////////////////////////////////////////////////////////////////////////
534 // PanelLayoutManager, ShelfLayoutManagerObserver implementation:
536 void PanelLayoutManager::WillChangeVisibilityState(
537 ShelfVisibilityState new_state
) {
538 // On entering / leaving full screen mode the shelf visibility state is
539 // changed to / from SHELF_HIDDEN. In this state, panel windows should hide
540 // to allow the full-screen application to use the full screen.
541 bool shelf_hidden
= new_state
== ash::SHELF_HIDDEN
;
543 if (restore_windows_on_shelf_visible_
) {
544 scoped_ptr
<aura::WindowTracker
> restore_windows(
545 restore_windows_on_shelf_visible_
.Pass());
546 for (aura::WindowTracker::Windows::const_iterator iter
=
547 restore_windows
->windows().begin(); iter
!=
548 restore_windows
->windows().end(); ++iter
) {
555 if (restore_windows_on_shelf_visible_
)
557 scoped_ptr
<aura::WindowTracker
> minimized_windows(new aura::WindowTracker
);
558 for (PanelList::iterator iter
= panel_windows_
.begin();
559 iter
!= panel_windows_
.end();) {
560 aura::Window
* window
= iter
->window
;
561 // Minimizing a panel window may remove it from the panel_windows_ list.
562 // Advance the iterator before minimizing it: http://crbug.com/393047.
564 if (window
!= dragged_panel_
&& window
->IsVisible()) {
565 minimized_windows
->Add(window
);
566 wm::GetWindowState(window
)->Minimize();
569 restore_windows_on_shelf_visible_
= minimized_windows
.Pass();
572 ////////////////////////////////////////////////////////////////////////////////
573 // PanelLayoutManager private implementation:
575 void PanelLayoutManager::MinimizePanel(aura::Window
* panel
) {
576 ::wm::SetWindowVisibilityAnimationType(
577 panel
, WINDOW_VISIBILITY_ANIMATION_TYPE_MINIMIZE
);
578 ui::Layer
* layer
= panel
->layer();
579 ui::ScopedLayerAnimationSettings
panel_slide_settings(layer
->GetAnimator());
580 panel_slide_settings
.SetPreemptionStrategy(
581 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
582 panel_slide_settings
.SetTransitionDuration(
583 base::TimeDelta::FromMilliseconds(kPanelSlideDurationMilliseconds
));
584 gfx::Rect
bounds(panel
->bounds());
585 bounds
.Offset(GetSlideInAnimationOffset(
586 shelf_
->shelf_widget()->GetAlignment()));
587 SetChildBoundsDirect(panel
, bounds
);
589 layer
->SetOpacity(0);
590 if (wm::IsActiveWindow(panel
))
591 wm::DeactivateWindow(panel
);
595 void PanelLayoutManager::RestorePanel(aura::Window
* panel
) {
596 PanelList::iterator found
=
597 std::find(panel_windows_
.begin(), panel_windows_
.end(), panel
);
598 DCHECK(found
!= panel_windows_
.end());
599 found
->slide_in
= true;
603 void PanelLayoutManager::Relayout() {
604 if (!shelf_
|| !shelf_
->shelf_widget())
607 // Suppress layouts during overview mode because changing window bounds
608 // interfered with overview mode animations. However, layouts need to be done
609 // when the WindowSelectorController is restoring minimized windows so that
610 // they actually become visible.
611 WindowSelectorController
* window_selector_controller
=
612 Shell::GetInstance()->window_selector_controller();
613 if (in_layout_
|| !window_selector_controller
||
614 (window_selector_controller
->IsSelecting() &&
615 !window_selector_controller
->IsRestoringMinimizedWindows()))
617 base::AutoReset
<bool> auto_reset_in_layout(&in_layout_
, true);
619 ShelfAlignment alignment
= shelf_
->shelf_widget()->GetAlignment();
620 bool horizontal
= alignment
== SHELF_ALIGNMENT_TOP
||
621 alignment
== SHELF_ALIGNMENT_BOTTOM
;
622 gfx::Rect shelf_bounds
= ash::ScreenUtil::ConvertRectFromScreen(
623 panel_container_
, shelf_
->shelf_widget()->GetWindowBoundsInScreen());
624 int panel_start_bounds
= kPanelIdealSpacing
;
625 int panel_end_bounds
= horizontal
?
626 panel_container_
->bounds().width() - kPanelIdealSpacing
:
627 panel_container_
->bounds().height() - kPanelIdealSpacing
;
628 aura::Window
* active_panel
= NULL
;
629 std::vector
<VisiblePanelPositionInfo
> visible_panels
;
630 for (PanelList::iterator iter
= panel_windows_
.begin();
631 iter
!= panel_windows_
.end(); ++iter
) {
632 aura::Window
* panel
= iter
->window
;
633 iter
->callout_widget
->SetAlignment(alignment
);
635 // Consider the dragged panel as part of the layout as long as it is
636 // touching the shelf.
637 if ((!panel
->IsVisible() && !iter
->slide_in
) ||
638 (panel
== dragged_panel_
&&
639 !BoundsAdjacent(panel
->bounds(), shelf_bounds
))) {
643 // If the shelf is currently hidden (full-screen mode), minimize panel until
644 // full-screen mode is exited. When a panel is dragged from another display
645 // the shelf state does not update before the panel is added so we exclude
646 // the dragged panel.
647 if (panel
!= dragged_panel_
&& restore_windows_on_shelf_visible_
) {
648 wm::GetWindowState(panel
)->Minimize();
649 restore_windows_on_shelf_visible_
->Add(panel
);
653 gfx::Rect icon_bounds
= shelf_
->GetScreenBoundsOfItemIconForWindow(panel
);
655 // If both the icon width and height are 0 then there is no icon in the
656 // shelf. If the shelf is hidden, one of the height or width will be
657 // 0 but the position in the shelf and major dimension is still reported
658 // correctly and the panel can be aligned above where the hidden icon is.
659 if (icon_bounds
.width() == 0 && icon_bounds
.height() == 0)
662 if (panel
->HasFocus() ||
664 aura::client::GetFocusClient(panel
)->GetFocusedWindow())) {
665 DCHECK(!active_panel
);
666 active_panel
= panel
;
668 icon_bounds
= ScreenUtil::ConvertRectFromScreen(panel_container_
,
670 gfx::Point icon_origin
= icon_bounds
.origin();
671 VisiblePanelPositionInfo position_info
;
672 int icon_start
= horizontal
? icon_origin
.x() : icon_origin
.y();
673 int icon_end
= icon_start
+ (horizontal
? icon_bounds
.width() :
674 icon_bounds
.height());
675 position_info
.major_length
= horizontal
?
676 panel
->bounds().width() : panel
->bounds().height();
677 position_info
.min_major
= std::max(
678 panel_start_bounds
+ position_info
.major_length
/ 2,
679 icon_end
- position_info
.major_length
/ 2);
680 position_info
.max_major
= std::min(
681 icon_start
+ position_info
.major_length
/ 2,
682 panel_end_bounds
- position_info
.major_length
/ 2);
683 position_info
.major_pos
= (icon_start
+ icon_end
) / 2;
684 position_info
.window
= panel
;
685 position_info
.slide_in
= iter
->slide_in
;
686 iter
->slide_in
= false;
687 visible_panels
.push_back(position_info
);
690 // Sort panels by their X positions and fan out groups of overlapping panels.
691 // The fan out method may result in new overlapping panels however given that
692 // the panels start at least a full panel width apart this overlap will
693 // never completely obscure a panel.
694 // TODO(flackr): Rearrange panels if new overlaps are introduced.
695 std::sort(visible_panels
.begin(), visible_panels
.end(), CompareWindowMajor
);
696 size_t first_overlapping_panel
= 0;
697 for (size_t i
= 1; i
< visible_panels
.size(); ++i
) {
698 if (visible_panels
[i
- 1].major_pos
+
699 visible_panels
[i
- 1].major_length
/ 2 < visible_panels
[i
].major_pos
-
700 visible_panels
[i
].major_length
/ 2) {
701 FanOutPanels(visible_panels
.begin() + first_overlapping_panel
,
702 visible_panels
.begin() + i
);
703 first_overlapping_panel
= i
;
706 FanOutPanels(visible_panels
.begin() + first_overlapping_panel
,
707 visible_panels
.end());
709 for (size_t i
= 0; i
< visible_panels
.size(); ++i
) {
710 if (visible_panels
[i
].window
== dragged_panel_
)
712 bool slide_in
= visible_panels
[i
].slide_in
;
713 gfx::Rect bounds
= visible_panels
[i
].window
->GetTargetBounds();
715 case SHELF_ALIGNMENT_BOTTOM
:
716 bounds
.set_y(shelf_bounds
.y() - bounds
.height());
718 case SHELF_ALIGNMENT_LEFT
:
719 bounds
.set_x(shelf_bounds
.right());
721 case SHELF_ALIGNMENT_RIGHT
:
722 bounds
.set_x(shelf_bounds
.x() - bounds
.width());
724 case SHELF_ALIGNMENT_TOP
:
725 bounds
.set_y(shelf_bounds
.bottom());
728 bool on_shelf
= visible_panels
[i
].window
->GetTargetBounds() == bounds
;
731 bounds
.set_x(visible_panels
[i
].major_pos
-
732 visible_panels
[i
].major_length
/ 2);
734 bounds
.set_y(visible_panels
[i
].major_pos
-
735 visible_panels
[i
].major_length
/ 2);
738 ui::Layer
* layer
= visible_panels
[i
].window
->layer();
740 // New windows shift up from the shelf into position and fade in.
741 layer
->SetOpacity(0);
742 gfx::Rect
initial_bounds(bounds
);
743 initial_bounds
.Offset(GetSlideInAnimationOffset(alignment
));
744 SetChildBoundsDirect(visible_panels
[i
].window
, initial_bounds
);
745 // Set on shelf so that the panel animates into its target position.
750 ui::ScopedLayerAnimationSettings
panel_slide_settings(
751 layer
->GetAnimator());
752 panel_slide_settings
.SetPreemptionStrategy(
753 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
754 panel_slide_settings
.SetTransitionDuration(
755 base::TimeDelta::FromMilliseconds(kPanelSlideDurationMilliseconds
));
756 SetChildBoundsDirect(visible_panels
[i
].window
, bounds
);
758 layer
->SetOpacity(1);
759 visible_panels
[i
].window
->Show();
762 // If the shelf moved don't animate, move immediately to the new
764 SetChildBoundsDirect(visible_panels
[i
].window
, bounds
);
768 UpdateStacking(active_panel
);
772 void PanelLayoutManager::UpdateStacking(aura::Window
* active_panel
) {
774 if (!last_active_panel_
)
776 active_panel
= last_active_panel_
;
779 ShelfAlignment alignment
= shelf_
->alignment();
780 bool horizontal
= alignment
== SHELF_ALIGNMENT_TOP
||
781 alignment
== SHELF_ALIGNMENT_BOTTOM
;
783 // We want to to stack the panels like a deck of cards:
784 // ,--,--,--,-------.--.--.
788 // We use the middle of each panel to figure out how to stack the panels. This
789 // allows us to update the stacking when a panel is being dragged around by
790 // the titlebar--even though it doesn't update the shelf icon positions, we
791 // still want the visual effect.
792 std::map
<int, aura::Window
*> window_ordering
;
793 for (PanelList::const_iterator it
= panel_windows_
.begin();
794 it
!= panel_windows_
.end(); ++it
) {
795 gfx::Rect bounds
= it
->window
->bounds();
796 window_ordering
.insert(std::make_pair(horizontal
?
797 bounds
.x() + bounds
.width() / 2 :
798 bounds
.y() + bounds
.height() / 2,
802 aura::Window
* previous_panel
= NULL
;
803 for (std::map
<int, aura::Window
*>::const_iterator it
=
804 window_ordering
.begin();
805 it
!= window_ordering
.end() && it
->second
!= active_panel
; ++it
) {
807 panel_container_
->StackChildAbove(it
->second
, previous_panel
);
808 previous_panel
= it
->second
;
811 previous_panel
= NULL
;
812 for (std::map
<int, aura::Window
*>::const_reverse_iterator it
=
813 window_ordering
.rbegin();
814 it
!= window_ordering
.rend() && it
->second
!= active_panel
; ++it
) {
816 panel_container_
->StackChildAbove(it
->second
, previous_panel
);
817 previous_panel
= it
->second
;
820 panel_container_
->StackChildAtTop(active_panel
);
821 if (dragged_panel_
&& dragged_panel_
->parent() == panel_container_
)
822 panel_container_
->StackChildAtTop(dragged_panel_
);
823 last_active_panel_
= active_panel
;
826 void PanelLayoutManager::UpdateCallouts() {
827 ShelfAlignment alignment
= shelf_
->alignment();
828 bool horizontal
= alignment
== SHELF_ALIGNMENT_TOP
||
829 alignment
== SHELF_ALIGNMENT_BOTTOM
;
831 for (PanelList::iterator iter
= panel_windows_
.begin();
832 iter
!= panel_windows_
.end(); ++iter
) {
833 aura::Window
* panel
= iter
->window
;
834 views::Widget
* callout_widget
= iter
->callout_widget
;
836 gfx::Rect current_bounds
= panel
->GetBoundsInScreen();
837 gfx::Rect bounds
= ScreenUtil::ConvertRectToScreen(
839 panel
->GetTargetBounds());
840 gfx::Rect icon_bounds
= shelf_
->GetScreenBoundsOfItemIconForWindow(panel
);
841 if (icon_bounds
.IsEmpty() || !panel
->layer()->GetTargetVisibility() ||
842 panel
== dragged_panel_
|| !show_callout_widgets_
) {
843 callout_widget
->Hide();
844 callout_widget
->GetNativeWindow()->layer()->SetOpacity(0);
848 gfx::Rect callout_bounds
= callout_widget
->GetWindowBoundsInScreen();
849 gfx::Vector2d slide_vector
= bounds
.origin() - current_bounds
.origin();
850 int slide_distance
= horizontal
? slide_vector
.x() : slide_vector
.y();
851 int distance_until_over_panel
= 0;
853 callout_bounds
.set_x(
854 icon_bounds
.x() + (icon_bounds
.width() - callout_bounds
.width()) / 2);
855 distance_until_over_panel
= std::max(
856 current_bounds
.x() - callout_bounds
.x(),
857 callout_bounds
.right() - current_bounds
.right());
859 callout_bounds
.set_y(
860 icon_bounds
.y() + (icon_bounds
.height() -
861 callout_bounds
.height()) / 2);
862 distance_until_over_panel
= std::max(
863 current_bounds
.y() - callout_bounds
.y(),
864 callout_bounds
.bottom() - current_bounds
.bottom());
867 case SHELF_ALIGNMENT_BOTTOM
:
868 callout_bounds
.set_y(bounds
.bottom());
870 case SHELF_ALIGNMENT_LEFT
:
871 callout_bounds
.set_x(bounds
.x() - callout_bounds
.width());
873 case SHELF_ALIGNMENT_RIGHT
:
874 callout_bounds
.set_x(bounds
.right());
876 case SHELF_ALIGNMENT_TOP
:
877 callout_bounds
.set_y(bounds
.y() - callout_bounds
.height());
880 callout_bounds
= ScreenUtil::ConvertRectFromScreen(
881 callout_widget
->GetNativeWindow()->parent(),
884 SetChildBoundsDirect(callout_widget
->GetNativeWindow(), callout_bounds
);
885 panel_container_
->StackChildAbove(callout_widget
->GetNativeWindow(),
888 ui::Layer
* layer
= callout_widget
->GetNativeWindow()->layer();
889 // If the panel is not over the callout position or has just become visible
890 // then fade in the callout.
891 if ((distance_until_over_panel
> 0 || layer
->GetTargetOpacity() < 1)) {
892 if (distance_until_over_panel
> 0 &&
893 slide_distance
>= distance_until_over_panel
) {
894 // If the panel is not yet over the callout, then delay fading in
895 // the callout until after the panel should be over it.
896 int delay
= kPanelSlideDurationMilliseconds
*
897 distance_until_over_panel
/ slide_distance
;
898 layer
->SetOpacity(0);
899 layer
->GetAnimator()->StopAnimating();
900 layer
->GetAnimator()->SchedulePauseForProperties(
901 base::TimeDelta::FromMilliseconds(delay
),
902 ui::LayerAnimationElement::OPACITY
);
904 ui::ScopedLayerAnimationSettings
callout_settings(layer
->GetAnimator());
905 callout_settings
.SetPreemptionStrategy(
906 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS
);
907 callout_settings
.SetTransitionDuration(
908 base::TimeDelta::FromMilliseconds(
909 kCalloutFadeDurationMilliseconds
));
910 layer
->SetOpacity(1);
913 // Show after changing the opacity animation. This way we don't have a
914 // state where the widget is visible but the opacity is 0.
915 callout_widget
->Show();
919 ////////////////////////////////////////////////////////////////////////////////
920 // keyboard::KeyboardControllerObserver implementation:
922 void PanelLayoutManager::OnKeyboardBoundsChanging(
923 const gfx::Rect
& keyboard_bounds
) {
924 gfx::Rect parent_bounds
= panel_container_
->bounds();
925 int available_space
= parent_bounds
.height() - keyboard_bounds
.height();
926 for (PanelList::iterator iter
= panel_windows_
.begin();
927 iter
!= panel_windows_
.end();
929 aura::Window
* panel
= iter
->window
;
930 wm::WindowState
* panel_state
= wm::GetWindowState(panel
);
931 if (keyboard_bounds
.height() > 0) {
932 // Save existing bounds, so that we can restore them when the keyboard
934 panel_state
->SaveCurrentBoundsForRestore();
936 gfx::Rect panel_bounds
= ScreenUtil::ConvertRectToScreen(
937 panel
->parent(), panel
->GetTargetBounds());
938 int delta
= panel_bounds
.height() - available_space
;
939 // Ensure panels are not pushed above the parent boundaries, shrink any
940 // panels that violate this constraint.
942 SetChildBounds(panel
,
943 gfx::Rect(panel_bounds
.x(),
944 panel_bounds
.y() + delta
,
945 panel_bounds
.width(),
946 panel_bounds
.height() - delta
));
948 } else if (panel_state
->HasRestoreBounds()) {
949 // Keyboard hidden, restore original bounds if they exist.
950 SetChildBounds(panel
, panel_state
->GetRestoreBoundsInScreen());
953 // This bounds change will have caused a change to the Shelf which does not
954 // propogate automatically to this class, so manually recalculate bounds.