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()->window_tree_host_manager()->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()->window_tree_host_manager()->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(
517 aura::client::ActivationChangeObserver::ActivationReason reason
,
518 aura::Window
* gained_active
,
519 aura::Window
* lost_active
) {
520 // Ignore if the panel that is not managed by this was activated.
521 if (gained_active
&& gained_active
->type() == ui::wm::WINDOW_TYPE_PANEL
&&
522 gained_active
->parent() == panel_container_
) {
523 UpdateStacking(gained_active
);
528 ////////////////////////////////////////////////////////////////////////////////
529 // PanelLayoutManager, WindowTreeHostManager::Observer implementation:
531 void PanelLayoutManager::OnDisplayConfigurationChanged() {
535 ////////////////////////////////////////////////////////////////////////////////
536 // PanelLayoutManager, ShelfLayoutManagerObserver implementation:
538 void PanelLayoutManager::WillChangeVisibilityState(
539 ShelfVisibilityState new_state
) {
540 // On entering / leaving full screen mode the shelf visibility state is
541 // changed to / from SHELF_HIDDEN. In this state, panel windows should hide
542 // to allow the full-screen application to use the full screen.
543 bool shelf_hidden
= new_state
== ash::SHELF_HIDDEN
;
545 if (restore_windows_on_shelf_visible_
) {
546 scoped_ptr
<aura::WindowTracker
> restore_windows(
547 restore_windows_on_shelf_visible_
.Pass());
548 for (aura::WindowTracker::Windows::const_iterator iter
=
549 restore_windows
->windows().begin(); iter
!=
550 restore_windows
->windows().end(); ++iter
) {
557 if (restore_windows_on_shelf_visible_
)
559 scoped_ptr
<aura::WindowTracker
> minimized_windows(new aura::WindowTracker
);
560 for (PanelList::iterator iter
= panel_windows_
.begin();
561 iter
!= panel_windows_
.end();) {
562 aura::Window
* window
= iter
->window
;
563 // Minimizing a panel window may remove it from the panel_windows_ list.
564 // Advance the iterator before minimizing it: http://crbug.com/393047.
566 if (window
!= dragged_panel_
&& window
->IsVisible()) {
567 minimized_windows
->Add(window
);
568 wm::GetWindowState(window
)->Minimize();
571 restore_windows_on_shelf_visible_
= minimized_windows
.Pass();
574 ////////////////////////////////////////////////////////////////////////////////
575 // PanelLayoutManager private implementation:
577 void PanelLayoutManager::MinimizePanel(aura::Window
* panel
) {
578 ::wm::SetWindowVisibilityAnimationType(
579 panel
, WINDOW_VISIBILITY_ANIMATION_TYPE_MINIMIZE
);
580 ui::Layer
* layer
= panel
->layer();
581 ui::ScopedLayerAnimationSettings
panel_slide_settings(layer
->GetAnimator());
582 panel_slide_settings
.SetPreemptionStrategy(
583 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
584 panel_slide_settings
.SetTransitionDuration(
585 base::TimeDelta::FromMilliseconds(kPanelSlideDurationMilliseconds
));
586 gfx::Rect
bounds(panel
->bounds());
587 bounds
.Offset(GetSlideInAnimationOffset(
588 shelf_
->shelf_widget()->GetAlignment()));
589 SetChildBoundsDirect(panel
, bounds
);
591 layer
->SetOpacity(0);
592 if (wm::IsActiveWindow(panel
))
593 wm::DeactivateWindow(panel
);
597 void PanelLayoutManager::RestorePanel(aura::Window
* panel
) {
598 PanelList::iterator found
=
599 std::find(panel_windows_
.begin(), panel_windows_
.end(), panel
);
600 DCHECK(found
!= panel_windows_
.end());
601 found
->slide_in
= true;
605 void PanelLayoutManager::Relayout() {
606 if (!shelf_
|| !shelf_
->shelf_widget())
609 // Suppress layouts during overview mode because changing window bounds
610 // interfered with overview mode animations. However, layouts need to be done
611 // when the WindowSelectorController is restoring minimized windows so that
612 // they actually become visible.
613 WindowSelectorController
* window_selector_controller
=
614 Shell::GetInstance()->window_selector_controller();
615 if (in_layout_
|| !window_selector_controller
||
616 (window_selector_controller
->IsSelecting() &&
617 !window_selector_controller
->IsRestoringMinimizedWindows()))
619 base::AutoReset
<bool> auto_reset_in_layout(&in_layout_
, true);
621 ShelfAlignment alignment
= shelf_
->shelf_widget()->GetAlignment();
622 bool horizontal
= alignment
== SHELF_ALIGNMENT_TOP
||
623 alignment
== SHELF_ALIGNMENT_BOTTOM
;
624 gfx::Rect shelf_bounds
= ash::ScreenUtil::ConvertRectFromScreen(
625 panel_container_
, shelf_
->shelf_widget()->GetWindowBoundsInScreen());
626 int panel_start_bounds
= kPanelIdealSpacing
;
627 int panel_end_bounds
= horizontal
?
628 panel_container_
->bounds().width() - kPanelIdealSpacing
:
629 panel_container_
->bounds().height() - kPanelIdealSpacing
;
630 aura::Window
* active_panel
= NULL
;
631 std::vector
<VisiblePanelPositionInfo
> visible_panels
;
632 for (PanelList::iterator iter
= panel_windows_
.begin();
633 iter
!= panel_windows_
.end(); ++iter
) {
634 aura::Window
* panel
= iter
->window
;
635 iter
->callout_widget
->SetAlignment(alignment
);
637 // Consider the dragged panel as part of the layout as long as it is
638 // touching the shelf.
639 if ((!panel
->IsVisible() && !iter
->slide_in
) ||
640 (panel
== dragged_panel_
&&
641 !BoundsAdjacent(panel
->bounds(), shelf_bounds
))) {
645 // If the shelf is currently hidden (full-screen mode), minimize panel until
646 // full-screen mode is exited. When a panel is dragged from another display
647 // the shelf state does not update before the panel is added so we exclude
648 // the dragged panel.
649 if (panel
!= dragged_panel_
&& restore_windows_on_shelf_visible_
) {
650 wm::GetWindowState(panel
)->Minimize();
651 restore_windows_on_shelf_visible_
->Add(panel
);
655 gfx::Rect icon_bounds
= shelf_
->GetScreenBoundsOfItemIconForWindow(panel
);
657 // If both the icon width and height are 0 then there is no icon in the
658 // shelf. If the shelf is hidden, one of the height or width will be
659 // 0 but the position in the shelf and major dimension is still reported
660 // correctly and the panel can be aligned above where the hidden icon is.
661 if (icon_bounds
.width() == 0 && icon_bounds
.height() == 0)
664 if (panel
->HasFocus() ||
666 aura::client::GetFocusClient(panel
)->GetFocusedWindow())) {
667 DCHECK(!active_panel
);
668 active_panel
= panel
;
670 icon_bounds
= ScreenUtil::ConvertRectFromScreen(panel_container_
,
672 gfx::Point icon_origin
= icon_bounds
.origin();
673 VisiblePanelPositionInfo position_info
;
674 int icon_start
= horizontal
? icon_origin
.x() : icon_origin
.y();
675 int icon_end
= icon_start
+ (horizontal
? icon_bounds
.width() :
676 icon_bounds
.height());
677 position_info
.major_length
= horizontal
?
678 panel
->bounds().width() : panel
->bounds().height();
679 position_info
.min_major
= std::max(
680 panel_start_bounds
+ position_info
.major_length
/ 2,
681 icon_end
- position_info
.major_length
/ 2);
682 position_info
.max_major
= std::min(
683 icon_start
+ position_info
.major_length
/ 2,
684 panel_end_bounds
- position_info
.major_length
/ 2);
685 position_info
.major_pos
= (icon_start
+ icon_end
) / 2;
686 position_info
.window
= panel
;
687 position_info
.slide_in
= iter
->slide_in
;
688 iter
->slide_in
= false;
689 visible_panels
.push_back(position_info
);
692 // Sort panels by their X positions and fan out groups of overlapping panels.
693 // The fan out method may result in new overlapping panels however given that
694 // the panels start at least a full panel width apart this overlap will
695 // never completely obscure a panel.
696 // TODO(flackr): Rearrange panels if new overlaps are introduced.
697 std::sort(visible_panels
.begin(), visible_panels
.end(), CompareWindowMajor
);
698 size_t first_overlapping_panel
= 0;
699 for (size_t i
= 1; i
< visible_panels
.size(); ++i
) {
700 if (visible_panels
[i
- 1].major_pos
+
701 visible_panels
[i
- 1].major_length
/ 2 < visible_panels
[i
].major_pos
-
702 visible_panels
[i
].major_length
/ 2) {
703 FanOutPanels(visible_panels
.begin() + first_overlapping_panel
,
704 visible_panels
.begin() + i
);
705 first_overlapping_panel
= i
;
708 FanOutPanels(visible_panels
.begin() + first_overlapping_panel
,
709 visible_panels
.end());
711 for (size_t i
= 0; i
< visible_panels
.size(); ++i
) {
712 if (visible_panels
[i
].window
== dragged_panel_
)
714 bool slide_in
= visible_panels
[i
].slide_in
;
715 gfx::Rect bounds
= visible_panels
[i
].window
->GetTargetBounds();
717 case SHELF_ALIGNMENT_BOTTOM
:
718 bounds
.set_y(shelf_bounds
.y() - bounds
.height());
720 case SHELF_ALIGNMENT_LEFT
:
721 bounds
.set_x(shelf_bounds
.right());
723 case SHELF_ALIGNMENT_RIGHT
:
724 bounds
.set_x(shelf_bounds
.x() - bounds
.width());
726 case SHELF_ALIGNMENT_TOP
:
727 bounds
.set_y(shelf_bounds
.bottom());
730 bool on_shelf
= visible_panels
[i
].window
->GetTargetBounds() == bounds
;
733 bounds
.set_x(visible_panels
[i
].major_pos
-
734 visible_panels
[i
].major_length
/ 2);
736 bounds
.set_y(visible_panels
[i
].major_pos
-
737 visible_panels
[i
].major_length
/ 2);
740 ui::Layer
* layer
= visible_panels
[i
].window
->layer();
742 // New windows shift up from the shelf into position and fade in.
743 layer
->SetOpacity(0);
744 gfx::Rect
initial_bounds(bounds
);
745 initial_bounds
.Offset(GetSlideInAnimationOffset(alignment
));
746 SetChildBoundsDirect(visible_panels
[i
].window
, initial_bounds
);
747 // Set on shelf so that the panel animates into its target position.
752 ui::ScopedLayerAnimationSettings
panel_slide_settings(
753 layer
->GetAnimator());
754 panel_slide_settings
.SetPreemptionStrategy(
755 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
756 panel_slide_settings
.SetTransitionDuration(
757 base::TimeDelta::FromMilliseconds(kPanelSlideDurationMilliseconds
));
758 SetChildBoundsDirect(visible_panels
[i
].window
, bounds
);
760 layer
->SetOpacity(1);
761 visible_panels
[i
].window
->Show();
764 // If the shelf moved don't animate, move immediately to the new
766 SetChildBoundsDirect(visible_panels
[i
].window
, bounds
);
770 UpdateStacking(active_panel
);
774 void PanelLayoutManager::UpdateStacking(aura::Window
* active_panel
) {
776 if (!last_active_panel_
)
778 active_panel
= last_active_panel_
;
781 ShelfAlignment alignment
= shelf_
->alignment();
782 bool horizontal
= alignment
== SHELF_ALIGNMENT_TOP
||
783 alignment
== SHELF_ALIGNMENT_BOTTOM
;
785 // We want to to stack the panels like a deck of cards:
786 // ,--,--,--,-------.--.--.
790 // We use the middle of each panel to figure out how to stack the panels. This
791 // allows us to update the stacking when a panel is being dragged around by
792 // the titlebar--even though it doesn't update the shelf icon positions, we
793 // still want the visual effect.
794 std::map
<int, aura::Window
*> window_ordering
;
795 for (PanelList::const_iterator it
= panel_windows_
.begin();
796 it
!= panel_windows_
.end(); ++it
) {
797 gfx::Rect bounds
= it
->window
->bounds();
798 window_ordering
.insert(std::make_pair(horizontal
?
799 bounds
.x() + bounds
.width() / 2 :
800 bounds
.y() + bounds
.height() / 2,
804 aura::Window
* previous_panel
= NULL
;
805 for (std::map
<int, aura::Window
*>::const_iterator it
=
806 window_ordering
.begin();
807 it
!= window_ordering
.end() && it
->second
!= active_panel
; ++it
) {
809 panel_container_
->StackChildAbove(it
->second
, previous_panel
);
810 previous_panel
= it
->second
;
813 previous_panel
= NULL
;
814 for (std::map
<int, aura::Window
*>::const_reverse_iterator it
=
815 window_ordering
.rbegin();
816 it
!= window_ordering
.rend() && it
->second
!= active_panel
; ++it
) {
818 panel_container_
->StackChildAbove(it
->second
, previous_panel
);
819 previous_panel
= it
->second
;
822 panel_container_
->StackChildAtTop(active_panel
);
823 if (dragged_panel_
&& dragged_panel_
->parent() == panel_container_
)
824 panel_container_
->StackChildAtTop(dragged_panel_
);
825 last_active_panel_
= active_panel
;
828 void PanelLayoutManager::UpdateCallouts() {
829 ShelfAlignment alignment
= shelf_
->alignment();
830 bool horizontal
= alignment
== SHELF_ALIGNMENT_TOP
||
831 alignment
== SHELF_ALIGNMENT_BOTTOM
;
833 for (PanelList::iterator iter
= panel_windows_
.begin();
834 iter
!= panel_windows_
.end(); ++iter
) {
835 aura::Window
* panel
= iter
->window
;
836 views::Widget
* callout_widget
= iter
->callout_widget
;
838 gfx::Rect current_bounds
= panel
->GetBoundsInScreen();
839 gfx::Rect bounds
= ScreenUtil::ConvertRectToScreen(
841 panel
->GetTargetBounds());
842 gfx::Rect icon_bounds
= shelf_
->GetScreenBoundsOfItemIconForWindow(panel
);
843 if (icon_bounds
.IsEmpty() || !panel
->layer()->GetTargetVisibility() ||
844 panel
== dragged_panel_
|| !show_callout_widgets_
) {
845 callout_widget
->Hide();
846 callout_widget
->GetNativeWindow()->layer()->SetOpacity(0);
850 gfx::Rect callout_bounds
= callout_widget
->GetWindowBoundsInScreen();
851 gfx::Vector2d slide_vector
= bounds
.origin() - current_bounds
.origin();
852 int slide_distance
= horizontal
? slide_vector
.x() : slide_vector
.y();
853 int distance_until_over_panel
= 0;
855 callout_bounds
.set_x(
856 icon_bounds
.x() + (icon_bounds
.width() - callout_bounds
.width()) / 2);
857 distance_until_over_panel
= std::max(
858 current_bounds
.x() - callout_bounds
.x(),
859 callout_bounds
.right() - current_bounds
.right());
861 callout_bounds
.set_y(
862 icon_bounds
.y() + (icon_bounds
.height() -
863 callout_bounds
.height()) / 2);
864 distance_until_over_panel
= std::max(
865 current_bounds
.y() - callout_bounds
.y(),
866 callout_bounds
.bottom() - current_bounds
.bottom());
869 case SHELF_ALIGNMENT_BOTTOM
:
870 callout_bounds
.set_y(bounds
.bottom());
872 case SHELF_ALIGNMENT_LEFT
:
873 callout_bounds
.set_x(bounds
.x() - callout_bounds
.width());
875 case SHELF_ALIGNMENT_RIGHT
:
876 callout_bounds
.set_x(bounds
.right());
878 case SHELF_ALIGNMENT_TOP
:
879 callout_bounds
.set_y(bounds
.y() - callout_bounds
.height());
882 callout_bounds
= ScreenUtil::ConvertRectFromScreen(
883 callout_widget
->GetNativeWindow()->parent(),
886 SetChildBoundsDirect(callout_widget
->GetNativeWindow(), callout_bounds
);
887 panel_container_
->StackChildAbove(callout_widget
->GetNativeWindow(),
890 ui::Layer
* layer
= callout_widget
->GetNativeWindow()->layer();
891 // If the panel is not over the callout position or has just become visible
892 // then fade in the callout.
893 if ((distance_until_over_panel
> 0 || layer
->GetTargetOpacity() < 1)) {
894 if (distance_until_over_panel
> 0 &&
895 slide_distance
>= distance_until_over_panel
) {
896 // If the panel is not yet over the callout, then delay fading in
897 // the callout until after the panel should be over it.
898 int delay
= kPanelSlideDurationMilliseconds
*
899 distance_until_over_panel
/ slide_distance
;
900 layer
->SetOpacity(0);
901 layer
->GetAnimator()->StopAnimating();
902 layer
->GetAnimator()->SchedulePauseForProperties(
903 base::TimeDelta::FromMilliseconds(delay
),
904 ui::LayerAnimationElement::OPACITY
);
906 ui::ScopedLayerAnimationSettings
callout_settings(layer
->GetAnimator());
907 callout_settings
.SetPreemptionStrategy(
908 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS
);
909 callout_settings
.SetTransitionDuration(
910 base::TimeDelta::FromMilliseconds(
911 kCalloutFadeDurationMilliseconds
));
912 layer
->SetOpacity(1);
915 // Show after changing the opacity animation. This way we don't have a
916 // state where the widget is visible but the opacity is 0.
917 callout_widget
->Show();
921 ////////////////////////////////////////////////////////////////////////////////
922 // keyboard::KeyboardControllerObserver implementation:
924 void PanelLayoutManager::OnKeyboardBoundsChanging(
925 const gfx::Rect
& keyboard_bounds
) {
926 gfx::Rect parent_bounds
= panel_container_
->bounds();
927 int available_space
= parent_bounds
.height() - keyboard_bounds
.height();
928 for (PanelList::iterator iter
= panel_windows_
.begin();
929 iter
!= panel_windows_
.end();
931 aura::Window
* panel
= iter
->window
;
932 wm::WindowState
* panel_state
= wm::GetWindowState(panel
);
933 if (keyboard_bounds
.height() > 0) {
934 // Save existing bounds, so that we can restore them when the keyboard
936 panel_state
->SaveCurrentBoundsForRestore();
938 gfx::Rect panel_bounds
= ScreenUtil::ConvertRectToScreen(
939 panel
->parent(), panel
->GetTargetBounds());
940 int delta
= panel_bounds
.height() - available_space
;
941 // Ensure panels are not pushed above the parent boundaries, shrink any
942 // panels that violate this constraint.
944 SetChildBounds(panel
,
945 gfx::Rect(panel_bounds
.x(),
946 panel_bounds
.y() + delta
,
947 panel_bounds
.width(),
948 panel_bounds
.height() - delta
));
950 } else if (panel_state
->HasRestoreBounds()) {
951 // Keyboard hidden, restore original bounds if they exist.
952 SetChildBounds(panel
, panel_state
->GetRestoreBoundsInScreen());
955 // This bounds change will have caused a change to the Shelf which does not
956 // propogate automatically to this class, so manually recalculate bounds.