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/window_animations.h"
19 #include "ash/wm/window_state.h"
20 #include "ash/wm/window_util.h"
21 #include "base/auto_reset.h"
22 #include "base/bind.h"
23 #include "base/bind_helpers.h"
24 #include "third_party/skia/include/core/SkColor.h"
25 #include "third_party/skia/include/core/SkPaint.h"
26 #include "third_party/skia/include/core/SkPath.h"
27 #include "ui/aura/client/focus_client.h"
28 #include "ui/aura/client/window_tree_client.h"
29 #include "ui/aura/window.h"
30 #include "ui/aura/window_delegate.h"
31 #include "ui/aura/window_event_dispatcher.h"
32 #include "ui/aura/window_tracker.h"
33 #include "ui/compositor/scoped_layer_animation_settings.h"
34 #include "ui/gfx/canvas.h"
35 #include "ui/gfx/rect.h"
36 #include "ui/gfx/vector2d.h"
37 #include "ui/views/background.h"
38 #include "ui/views/widget/widget.h"
39 #include "ui/wm/public/activation_client.h"
44 const int kPanelIdealSpacing
= 4;
46 const float kMaxHeightFactor
= .80f
;
47 const float kMaxWidthFactor
= .50f
;
49 // Duration for panel animations.
50 const int kPanelSlideDurationMilliseconds
= 50;
51 const int kCalloutFadeDurationMilliseconds
= 50;
53 // Offset used when sliding panel in/out of the shelf. Used for minimizing,
54 // restoring and the initial showing of a panel.
55 const int kPanelSlideInOffset
= 20;
57 // Callout arrow dimensions.
58 const int kArrowWidth
= 18;
59 const int kArrowHeight
= 9;
61 class CalloutWidgetBackground
: public views::Background
{
63 CalloutWidgetBackground() : alignment_(SHELF_ALIGNMENT_BOTTOM
) {
66 void Paint(gfx::Canvas
* canvas
, views::View
* view
) const override
{
69 case SHELF_ALIGNMENT_BOTTOM
:
70 path
.moveTo(SkIntToScalar(0), SkIntToScalar(0));
71 path
.lineTo(SkIntToScalar(kArrowWidth
/ 2),
72 SkIntToScalar(kArrowHeight
));
73 path
.lineTo(SkIntToScalar(kArrowWidth
), SkIntToScalar(0));
75 case SHELF_ALIGNMENT_LEFT
:
76 path
.moveTo(SkIntToScalar(kArrowHeight
), SkIntToScalar(kArrowWidth
));
77 path
.lineTo(SkIntToScalar(0), SkIntToScalar(kArrowWidth
/ 2));
78 path
.lineTo(SkIntToScalar(kArrowHeight
), SkIntToScalar(0));
80 case SHELF_ALIGNMENT_TOP
:
81 path
.moveTo(SkIntToScalar(0), SkIntToScalar(kArrowHeight
));
82 path
.lineTo(SkIntToScalar(kArrowWidth
/ 2), SkIntToScalar(0));
83 path
.lineTo(SkIntToScalar(kArrowWidth
), SkIntToScalar(kArrowHeight
));
85 case SHELF_ALIGNMENT_RIGHT
:
86 path
.moveTo(SkIntToScalar(0), SkIntToScalar(0));
87 path
.lineTo(SkIntToScalar(kArrowHeight
),
88 SkIntToScalar(kArrowWidth
/ 2));
89 path
.lineTo(SkIntToScalar(0), SkIntToScalar(kArrowWidth
));
92 // Hard code the arrow color for now.
94 paint
.setStyle(SkPaint::kFill_Style
);
95 paint
.setColor(SkColorSetARGB(0xff, 0xe5, 0xe5, 0xe5));
96 canvas
->DrawPath(path
, paint
);
99 ShelfAlignment
alignment() {
103 void set_alignment(ShelfAlignment alignment
) {
104 alignment_
= alignment
;
108 ShelfAlignment alignment_
;
110 DISALLOW_COPY_AND_ASSIGN(CalloutWidgetBackground
);
113 struct VisiblePanelPositionInfo
{
114 VisiblePanelPositionInfo()
126 aura::Window
* window
;
130 bool CompareWindowMajor(const VisiblePanelPositionInfo
& win1
,
131 const VisiblePanelPositionInfo
& win2
) {
132 return win1
.major_pos
< win2
.major_pos
;
135 void FanOutPanels(std::vector
<VisiblePanelPositionInfo
>::iterator first
,
136 std::vector
<VisiblePanelPositionInfo
>::iterator last
) {
137 int num_panels
= last
- first
;
138 if (num_panels
== 1) {
139 (*first
).major_pos
= std::max((*first
).min_major
, std::min(
140 (*first
).max_major
, (*first
).major_pos
));
145 if (num_panels
== 2) {
146 // If there are two adjacent overlapping windows, separate them by the
147 // minimum major_length necessary.
148 std::vector
<VisiblePanelPositionInfo
>::iterator second
= first
+ 1;
149 int separation
= (*first
).major_length
/ 2 + (*second
).major_length
/ 2 +
151 int overlap
= (*first
).major_pos
+ separation
- (*second
).major_pos
;
152 (*first
).major_pos
= std::max((*first
).min_major
,
153 (*first
).major_pos
- overlap
/ 2);
154 (*second
).major_pos
= std::min((*second
).max_major
,
155 (*first
).major_pos
+ separation
);
156 // Recalculate the first panel position in case the second one was
157 // constrained on the right.
158 (*first
).major_pos
= std::max((*first
).min_major
,
159 (*second
).major_pos
- separation
);
163 // If there are more than two overlapping windows, fan them out from minimum
164 // position to maximum position equally spaced.
165 int delta
= ((*(last
- 1)).max_major
- (*first
).min_major
) / (num_panels
- 1);
166 int major_pos
= (*first
).min_major
;
167 for (std::vector
<VisiblePanelPositionInfo
>::iterator iter
= first
;
168 iter
!= last
; ++iter
) {
169 (*iter
).major_pos
= std::max((*iter
).min_major
,
170 std::min((*iter
).max_major
, major_pos
));
175 bool BoundsAdjacent(const gfx::Rect
& bounds1
, const gfx::Rect
& bounds2
) {
176 return bounds1
.x() == bounds2
.right() ||
177 bounds1
.y() == bounds2
.bottom() ||
178 bounds1
.right() == bounds2
.x() ||
179 bounds1
.bottom() == bounds2
.y();
182 gfx::Vector2d
GetSlideInAnimationOffset(ShelfAlignment alignment
) {
183 gfx::Vector2d offset
;
185 case SHELF_ALIGNMENT_BOTTOM
:
186 offset
.set_y(kPanelSlideInOffset
);
188 case SHELF_ALIGNMENT_LEFT
:
189 offset
.set_x(-kPanelSlideInOffset
);
191 case SHELF_ALIGNMENT_RIGHT
:
192 offset
.set_x(kPanelSlideInOffset
);
194 case SHELF_ALIGNMENT_TOP
:
195 offset
.set_y(-kPanelSlideInOffset
);
203 class PanelCalloutWidget
: public views::Widget
{
205 explicit PanelCalloutWidget(aura::Window
* container
)
206 : background_(NULL
) {
207 InitWidget(container
);
210 void SetAlignment(ShelfAlignment alignment
) {
211 gfx::Rect callout_bounds
= GetWindowBoundsInScreen();
212 if (alignment
== SHELF_ALIGNMENT_BOTTOM
||
213 alignment
== SHELF_ALIGNMENT_TOP
) {
214 callout_bounds
.set_width(kArrowWidth
);
215 callout_bounds
.set_height(kArrowHeight
);
217 callout_bounds
.set_width(kArrowHeight
);
218 callout_bounds
.set_height(kArrowWidth
);
220 GetNativeWindow()->SetBounds(callout_bounds
);
221 if (background_
->alignment() != alignment
) {
222 background_
->set_alignment(alignment
);
223 SchedulePaintInRect(gfx::Rect(gfx::Point(), callout_bounds
.size()));
228 void InitWidget(aura::Window
* parent
) {
229 views::Widget::InitParams params
;
230 params
.type
= views::Widget::InitParams::TYPE_POPUP
;
231 params
.opacity
= views::Widget::InitParams::TRANSLUCENT_WINDOW
;
232 params
.keep_on_top
= true;
233 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
234 params
.parent
= parent
;
235 params
.bounds
= ScreenUtil::ConvertRectToScreen(parent
, gfx::Rect());
236 params
.bounds
.set_width(kArrowWidth
);
237 params
.bounds
.set_height(kArrowHeight
);
238 params
.accept_events
= false;
239 set_focus_on_creation(false);
241 DCHECK_EQ(GetNativeView()->GetRootWindow(), parent
->GetRootWindow());
242 views::View
* content_view
= new views::View
;
243 background_
= new CalloutWidgetBackground
;
244 content_view
->set_background(background_
);
245 SetContentsView(content_view
);
246 GetNativeWindow()->layer()->SetOpacity(0);
249 // Weak pointer owned by this widget's content view.
250 CalloutWidgetBackground
* background_
;
252 DISALLOW_COPY_AND_ASSIGN(PanelCalloutWidget
);
255 ////////////////////////////////////////////////////////////////////////////////
256 // PanelLayoutManager public implementation:
257 PanelLayoutManager::PanelLayoutManager(aura::Window
* panel_container
)
258 : panel_container_(panel_container
),
259 in_add_window_(false),
261 show_callout_widgets_(true),
262 dragged_panel_(NULL
),
264 shelf_layout_manager_(NULL
),
265 last_active_panel_(NULL
),
266 weak_factory_(this) {
267 DCHECK(panel_container
);
268 aura::client::GetActivationClient(Shell::GetPrimaryRootWindow())->
270 Shell::GetInstance()->display_controller()->AddObserver(this);
271 Shell::GetInstance()->AddShellObserver(this);
274 PanelLayoutManager::~PanelLayoutManager() {
278 void PanelLayoutManager::Shutdown() {
279 if (shelf_layout_manager_
)
280 shelf_layout_manager_
->RemoveObserver(this);
281 shelf_layout_manager_
= NULL
;
282 for (PanelList::iterator iter
= panel_windows_
.begin();
283 iter
!= panel_windows_
.end(); ++iter
) {
284 delete iter
->callout_widget
;
286 panel_windows_
.clear();
288 shelf_
->RemoveIconObserver(this);
290 aura::client::GetActivationClient(Shell::GetPrimaryRootWindow())->
291 RemoveObserver(this);
292 Shell::GetInstance()->display_controller()->RemoveObserver(this);
293 Shell::GetInstance()->RemoveShellObserver(this);
296 void PanelLayoutManager::StartDragging(aura::Window
* panel
) {
297 DCHECK(!dragged_panel_
);
298 dragged_panel_
= panel
;
302 void PanelLayoutManager::FinishDragging() {
303 dragged_panel_
= NULL
;
307 void PanelLayoutManager::SetShelf(Shelf
* shelf
) {
309 DCHECK(!shelf_layout_manager_
);
311 shelf_
->AddIconObserver(this);
312 if (shelf_
->shelf_widget()) {
313 shelf_layout_manager_
= ash::ShelfLayoutManager::ForShelf(
314 shelf_
->shelf_widget()->GetNativeWindow());
315 WillChangeVisibilityState(shelf_layout_manager_
->visibility_state());
316 shelf_layout_manager_
->AddObserver(this);
320 void PanelLayoutManager::ToggleMinimize(aura::Window
* panel
) {
321 DCHECK(panel
->parent() == panel_container_
);
322 wm::WindowState
* window_state
= wm::GetWindowState(panel
);
323 if (window_state
->IsMinimized())
324 window_state
->Restore();
326 window_state
->Minimize();
329 void PanelLayoutManager::SetShowCalloutWidgets(bool show
) {
330 if (show_callout_widgets_
== show
)
332 show_callout_widgets_
= show
;
336 views::Widget
* PanelLayoutManager::GetCalloutWidgetForPanel(
337 aura::Window
* panel
) {
338 DCHECK(panel
->parent() == panel_container_
);
339 PanelList::iterator found
=
340 std::find(panel_windows_
.begin(), panel_windows_
.end(), panel
);
341 DCHECK(found
!= panel_windows_
.end());
342 return found
->callout_widget
;
345 ////////////////////////////////////////////////////////////////////////////////
346 // PanelLayoutManager, aura::LayoutManager implementation:
347 void PanelLayoutManager::OnWindowResized() {
351 void PanelLayoutManager::OnWindowAddedToLayout(aura::Window
* child
) {
352 if (child
->type() == ui::wm::WINDOW_TYPE_POPUP
)
356 base::AutoReset
<bool> auto_reset_in_add_window(&in_add_window_
, true);
357 if (!wm::GetWindowState(child
)->panel_attached()) {
358 // This should only happen when a window is added to panel container as a
359 // result of bounds change from within the application during a drag.
360 // If so we have already stopped the drag and should reparent the panel
361 // back to appropriate container and ignore it.
362 // TODO(varkha): Updating bounds during a drag can cause problems and a more
363 // general solution is needed. See http://crbug.com/251813 .
364 aura::Window
* old_parent
= child
->parent();
365 aura::client::ParentWindowWithContext(
366 child
, child
, child
->GetRootWindow()->GetBoundsInScreen());
367 wm::ReparentTransientChildrenOfChild(child
, old_parent
, child
->parent());
368 DCHECK(child
->parent()->id() != kShellWindowId_PanelContainer
);
371 PanelInfo panel_info
;
372 panel_info
.window
= child
;
373 panel_info
.callout_widget
= new PanelCalloutWidget(panel_container_
);
374 panel_info
.slide_in
= child
!= dragged_panel_
;
375 panel_windows_
.push_back(panel_info
);
376 child
->AddObserver(this);
377 wm::GetWindowState(child
)->AddObserver(this);
381 void PanelLayoutManager::OnWillRemoveWindowFromLayout(aura::Window
* child
) {
384 void PanelLayoutManager::OnWindowRemovedFromLayout(aura::Window
* child
) {
385 if (child
->type() == ui::wm::WINDOW_TYPE_POPUP
)
387 PanelList::iterator found
=
388 std::find(panel_windows_
.begin(), panel_windows_
.end(), child
);
389 if (found
!= panel_windows_
.end()) {
390 delete found
->callout_widget
;
391 panel_windows_
.erase(found
);
393 if (restore_windows_on_shelf_visible_
)
394 restore_windows_on_shelf_visible_
->Remove(child
);
395 child
->RemoveObserver(this);
396 wm::GetWindowState(child
)->RemoveObserver(this);
398 if (dragged_panel_
== child
)
399 dragged_panel_
= NULL
;
401 if (last_active_panel_
== child
)
402 last_active_panel_
= NULL
;
407 void PanelLayoutManager::OnChildWindowVisibilityChanged(aura::Window
* child
,
410 wm::GetWindowState(child
)->Restore();
414 void PanelLayoutManager::SetChildBounds(aura::Window
* child
,
415 const gfx::Rect
& requested_bounds
) {
416 gfx::Rect
bounds(requested_bounds
);
417 const gfx::Rect
& max_bounds
= panel_container_
->GetRootWindow()->bounds();
418 const int max_width
= max_bounds
.width() * kMaxWidthFactor
;
419 const int max_height
= max_bounds
.height() * kMaxHeightFactor
;
420 if (bounds
.width() > max_width
)
421 bounds
.set_width(max_width
);
422 if (bounds
.height() > max_height
)
423 bounds
.set_height(max_height
);
425 // Reposition dragged panel in the panel order.
426 if (dragged_panel_
== child
) {
427 PanelList::iterator dragged_panel_iter
=
428 std::find(panel_windows_
.begin(), panel_windows_
.end(), dragged_panel_
);
429 DCHECK(dragged_panel_iter
!= panel_windows_
.end());
430 PanelList::iterator new_position
;
431 for (new_position
= panel_windows_
.begin();
432 new_position
!= panel_windows_
.end();
434 const gfx::Rect
& bounds
= (*new_position
).window
->bounds();
435 if (bounds
.x() + bounds
.width()/2 <= requested_bounds
.x()) break;
437 if (new_position
!= dragged_panel_iter
) {
438 PanelInfo dragged_panel_info
= *dragged_panel_iter
;
439 panel_windows_
.erase(dragged_panel_iter
);
440 panel_windows_
.insert(new_position
, dragged_panel_info
);
443 // Respect the minimum size of the window.
444 if (child
->delegate()) {
445 const gfx::Size
& min_size
= child
->delegate()->GetMinimumSize();
446 bounds
.set_width(std::max(min_size
.width(), bounds
.width()));
447 bounds
.set_height(std::max(min_size
.height(), bounds
.height()));
450 SetChildBoundsDirect(child
, bounds
);
454 ////////////////////////////////////////////////////////////////////////////////
455 // PanelLayoutManager, ShelfIconObserver implementation:
457 void PanelLayoutManager::OnShelfIconPositionsChanged() {
458 // TODO: As this is called for every animation step now. Relayout needs to be
459 // updated to use current icon position instead of use the ideal bounds so
460 // that the panels slide with their icons instead of jumping.
464 ////////////////////////////////////////////////////////////////////////////////
465 // PanelLayoutManager, ash::ShellObserver implementation:
467 void PanelLayoutManager::OnShelfAlignmentChanged(aura::Window
* root_window
) {
468 if (panel_container_
->GetRootWindow() == root_window
)
472 /////////////////////////////////////////////////////////////////////////////
473 // PanelLayoutManager, WindowObserver implementation:
475 void PanelLayoutManager::OnWindowPropertyChanged(aura::Window
* window
,
478 // Trigger a relayout to position the panels whenever the panel icon is set
484 /////////////////////////////////////////////////////////////////////////////
485 // PanelLayoutManager, WindowStateObserver implementation:
487 void PanelLayoutManager::OnPostWindowStateTypeChange(
488 wm::WindowState
* window_state
,
489 wm::WindowStateType old_type
) {
490 // If the shelf is currently hidden then windows will not actually be shown
491 // but the set to restore when the shelf becomes visible is updated.
492 if (restore_windows_on_shelf_visible_
) {
493 if (window_state
->IsMinimized()) {
494 MinimizePanel(window_state
->window());
495 restore_windows_on_shelf_visible_
->Remove(window_state
->window());
497 restore_windows_on_shelf_visible_
->Add(window_state
->window());
502 if (window_state
->IsMinimized())
503 MinimizePanel(window_state
->window());
505 RestorePanel(window_state
->window());
508 ////////////////////////////////////////////////////////////////////////////////
509 // PanelLayoutManager, aura::client::ActivationChangeObserver implementation:
511 void PanelLayoutManager::OnWindowActivated(aura::Window
* gained_active
,
512 aura::Window
* lost_active
) {
513 // Ignore if the panel that is not managed by this was activated.
514 if (gained_active
&& gained_active
->type() == ui::wm::WINDOW_TYPE_PANEL
&&
515 gained_active
->parent() == panel_container_
) {
516 UpdateStacking(gained_active
);
521 ////////////////////////////////////////////////////////////////////////////////
522 // PanelLayoutManager, DisplayController::Observer implementation:
524 void PanelLayoutManager::OnDisplayConfigurationChanged() {
528 ////////////////////////////////////////////////////////////////////////////////
529 // PanelLayoutManager, ShelfLayoutManagerObserver implementation:
531 void PanelLayoutManager::WillChangeVisibilityState(
532 ShelfVisibilityState new_state
) {
533 // On entering / leaving full screen mode the shelf visibility state is
534 // changed to / from SHELF_HIDDEN. In this state, panel windows should hide
535 // to allow the full-screen application to use the full screen.
536 bool shelf_hidden
= new_state
== ash::SHELF_HIDDEN
;
538 if (restore_windows_on_shelf_visible_
) {
539 scoped_ptr
<aura::WindowTracker
> restore_windows(
540 restore_windows_on_shelf_visible_
.Pass());
541 for (aura::WindowTracker::Windows::const_iterator iter
=
542 restore_windows
->windows().begin(); iter
!=
543 restore_windows
->windows().end(); ++iter
) {
550 if (restore_windows_on_shelf_visible_
)
552 scoped_ptr
<aura::WindowTracker
> minimized_windows(new aura::WindowTracker
);
553 for (PanelList::iterator iter
= panel_windows_
.begin();
554 iter
!= panel_windows_
.end();) {
555 aura::Window
* window
= iter
->window
;
556 // Minimizing a panel window may remove it from the panel_windows_ list.
557 // Advance the iterator before minimizing it: http://crbug.com/393047.
559 if (window
!= dragged_panel_
&& window
->IsVisible()) {
560 minimized_windows
->Add(window
);
561 wm::GetWindowState(window
)->Minimize();
564 restore_windows_on_shelf_visible_
= minimized_windows
.Pass();
567 ////////////////////////////////////////////////////////////////////////////////
568 // PanelLayoutManager private implementation:
570 void PanelLayoutManager::MinimizePanel(aura::Window
* panel
) {
571 ::wm::SetWindowVisibilityAnimationType(
572 panel
, WINDOW_VISIBILITY_ANIMATION_TYPE_MINIMIZE
);
573 ui::Layer
* layer
= panel
->layer();
574 ui::ScopedLayerAnimationSettings
panel_slide_settings(layer
->GetAnimator());
575 panel_slide_settings
.SetPreemptionStrategy(
576 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
577 panel_slide_settings
.SetTransitionDuration(
578 base::TimeDelta::FromMilliseconds(kPanelSlideDurationMilliseconds
));
579 gfx::Rect
bounds(panel
->bounds());
580 bounds
.Offset(GetSlideInAnimationOffset(
581 shelf_
->shelf_widget()->GetAlignment()));
582 SetChildBoundsDirect(panel
, bounds
);
584 layer
->SetOpacity(0);
585 if (wm::IsActiveWindow(panel
))
586 wm::DeactivateWindow(panel
);
590 void PanelLayoutManager::RestorePanel(aura::Window
* panel
) {
591 PanelList::iterator found
=
592 std::find(panel_windows_
.begin(), panel_windows_
.end(), panel
);
593 DCHECK(found
!= panel_windows_
.end());
594 found
->slide_in
= true;
598 void PanelLayoutManager::Relayout() {
599 if (!shelf_
|| !shelf_
->shelf_widget())
604 base::AutoReset
<bool> auto_reset_in_layout(&in_layout_
, true);
606 ShelfAlignment alignment
= shelf_
->shelf_widget()->GetAlignment();
607 bool horizontal
= alignment
== SHELF_ALIGNMENT_TOP
||
608 alignment
== SHELF_ALIGNMENT_BOTTOM
;
609 gfx::Rect shelf_bounds
= ash::ScreenUtil::ConvertRectFromScreen(
610 panel_container_
, shelf_
->shelf_widget()->GetWindowBoundsInScreen());
611 int panel_start_bounds
= kPanelIdealSpacing
;
612 int panel_end_bounds
= horizontal
?
613 panel_container_
->bounds().width() - kPanelIdealSpacing
:
614 panel_container_
->bounds().height() - kPanelIdealSpacing
;
615 aura::Window
* active_panel
= NULL
;
616 std::vector
<VisiblePanelPositionInfo
> visible_panels
;
617 for (PanelList::iterator iter
= panel_windows_
.begin();
618 iter
!= panel_windows_
.end(); ++iter
) {
619 aura::Window
* panel
= iter
->window
;
620 iter
->callout_widget
->SetAlignment(alignment
);
622 // Consider the dragged panel as part of the layout as long as it is
623 // touching the shelf.
624 if ((!panel
->IsVisible() && !iter
->slide_in
) ||
625 (panel
== dragged_panel_
&&
626 !BoundsAdjacent(panel
->bounds(), shelf_bounds
))) {
630 // If the shelf is currently hidden (full-screen mode), minimize panel until
631 // full-screen mode is exited. When a panel is dragged from another display
632 // the shelf state does not update before the panel is added so we exclude
633 // the dragged panel.
634 if (panel
!= dragged_panel_
&& restore_windows_on_shelf_visible_
) {
635 wm::GetWindowState(panel
)->Minimize();
636 restore_windows_on_shelf_visible_
->Add(panel
);
640 gfx::Rect icon_bounds
= shelf_
->GetScreenBoundsOfItemIconForWindow(panel
);
642 // If both the icon width and height are 0 then there is no icon in the
643 // shelf. If the shelf is hidden, one of the height or width will be
644 // 0 but the position in the shelf and major dimension is still reported
645 // correctly and the panel can be aligned above where the hidden icon is.
646 if (icon_bounds
.width() == 0 && icon_bounds
.height() == 0)
649 if (panel
->HasFocus() ||
651 aura::client::GetFocusClient(panel
)->GetFocusedWindow())) {
652 DCHECK(!active_panel
);
653 active_panel
= panel
;
655 icon_bounds
= ScreenUtil::ConvertRectFromScreen(panel_container_
,
657 gfx::Point icon_origin
= icon_bounds
.origin();
658 VisiblePanelPositionInfo position_info
;
659 int icon_start
= horizontal
? icon_origin
.x() : icon_origin
.y();
660 int icon_end
= icon_start
+ (horizontal
? icon_bounds
.width() :
661 icon_bounds
.height());
662 position_info
.major_length
= horizontal
?
663 panel
->bounds().width() : panel
->bounds().height();
664 position_info
.min_major
= std::max(
665 panel_start_bounds
+ position_info
.major_length
/ 2,
666 icon_end
- position_info
.major_length
/ 2);
667 position_info
.max_major
= std::min(
668 icon_start
+ position_info
.major_length
/ 2,
669 panel_end_bounds
- position_info
.major_length
/ 2);
670 position_info
.major_pos
= (icon_start
+ icon_end
) / 2;
671 position_info
.window
= panel
;
672 position_info
.slide_in
= iter
->slide_in
;
673 iter
->slide_in
= false;
674 visible_panels
.push_back(position_info
);
677 // Sort panels by their X positions and fan out groups of overlapping panels.
678 // The fan out method may result in new overlapping panels however given that
679 // the panels start at least a full panel width apart this overlap will
680 // never completely obscure a panel.
681 // TODO(flackr): Rearrange panels if new overlaps are introduced.
682 std::sort(visible_panels
.begin(), visible_panels
.end(), CompareWindowMajor
);
683 size_t first_overlapping_panel
= 0;
684 for (size_t i
= 1; i
< visible_panels
.size(); ++i
) {
685 if (visible_panels
[i
- 1].major_pos
+
686 visible_panels
[i
- 1].major_length
/ 2 < visible_panels
[i
].major_pos
-
687 visible_panels
[i
].major_length
/ 2) {
688 FanOutPanels(visible_panels
.begin() + first_overlapping_panel
,
689 visible_panels
.begin() + i
);
690 first_overlapping_panel
= i
;
693 FanOutPanels(visible_panels
.begin() + first_overlapping_panel
,
694 visible_panels
.end());
696 for (size_t i
= 0; i
< visible_panels
.size(); ++i
) {
697 if (visible_panels
[i
].window
== dragged_panel_
)
699 bool slide_in
= visible_panels
[i
].slide_in
;
700 gfx::Rect bounds
= visible_panels
[i
].window
->GetTargetBounds();
702 case SHELF_ALIGNMENT_BOTTOM
:
703 bounds
.set_y(shelf_bounds
.y() - bounds
.height());
705 case SHELF_ALIGNMENT_LEFT
:
706 bounds
.set_x(shelf_bounds
.right());
708 case SHELF_ALIGNMENT_RIGHT
:
709 bounds
.set_x(shelf_bounds
.x() - bounds
.width());
711 case SHELF_ALIGNMENT_TOP
:
712 bounds
.set_y(shelf_bounds
.bottom());
715 bool on_shelf
= visible_panels
[i
].window
->GetTargetBounds() == bounds
;
718 bounds
.set_x(visible_panels
[i
].major_pos
-
719 visible_panels
[i
].major_length
/ 2);
721 bounds
.set_y(visible_panels
[i
].major_pos
-
722 visible_panels
[i
].major_length
/ 2);
725 ui::Layer
* layer
= visible_panels
[i
].window
->layer();
727 // New windows shift up from the shelf into position and fade in.
728 layer
->SetOpacity(0);
729 gfx::Rect
initial_bounds(bounds
);
730 initial_bounds
.Offset(GetSlideInAnimationOffset(alignment
));
731 SetChildBoundsDirect(visible_panels
[i
].window
, initial_bounds
);
732 // Set on shelf so that the panel animates into its target position.
737 ui::ScopedLayerAnimationSettings
panel_slide_settings(
738 layer
->GetAnimator());
739 panel_slide_settings
.SetPreemptionStrategy(
740 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
741 panel_slide_settings
.SetTransitionDuration(
742 base::TimeDelta::FromMilliseconds(kPanelSlideDurationMilliseconds
));
743 SetChildBoundsDirect(visible_panels
[i
].window
, bounds
);
745 layer
->SetOpacity(1);
746 visible_panels
[i
].window
->Show();
749 // If the shelf moved don't animate, move immediately to the new
751 SetChildBoundsDirect(visible_panels
[i
].window
, bounds
);
755 UpdateStacking(active_panel
);
759 void PanelLayoutManager::UpdateStacking(aura::Window
* active_panel
) {
761 if (!last_active_panel_
)
763 active_panel
= last_active_panel_
;
766 ShelfAlignment alignment
= shelf_
->alignment();
767 bool horizontal
= alignment
== SHELF_ALIGNMENT_TOP
||
768 alignment
== SHELF_ALIGNMENT_BOTTOM
;
770 // We want to to stack the panels like a deck of cards:
771 // ,--,--,--,-------.--.--.
775 // We use the middle of each panel to figure out how to stack the panels. This
776 // allows us to update the stacking when a panel is being dragged around by
777 // the titlebar--even though it doesn't update the shelf icon positions, we
778 // still want the visual effect.
779 std::map
<int, aura::Window
*> window_ordering
;
780 for (PanelList::const_iterator it
= panel_windows_
.begin();
781 it
!= panel_windows_
.end(); ++it
) {
782 gfx::Rect bounds
= it
->window
->bounds();
783 window_ordering
.insert(std::make_pair(horizontal
?
784 bounds
.x() + bounds
.width() / 2 :
785 bounds
.y() + bounds
.height() / 2,
789 aura::Window
* previous_panel
= NULL
;
790 for (std::map
<int, aura::Window
*>::const_iterator it
=
791 window_ordering
.begin();
792 it
!= window_ordering
.end() && it
->second
!= active_panel
; ++it
) {
794 panel_container_
->StackChildAbove(it
->second
, previous_panel
);
795 previous_panel
= it
->second
;
798 previous_panel
= NULL
;
799 for (std::map
<int, aura::Window
*>::const_reverse_iterator it
=
800 window_ordering
.rbegin();
801 it
!= window_ordering
.rend() && it
->second
!= active_panel
; ++it
) {
803 panel_container_
->StackChildAbove(it
->second
, previous_panel
);
804 previous_panel
= it
->second
;
807 panel_container_
->StackChildAtTop(active_panel
);
808 if (dragged_panel_
&& dragged_panel_
->parent() == panel_container_
)
809 panel_container_
->StackChildAtTop(dragged_panel_
);
810 last_active_panel_
= active_panel
;
813 void PanelLayoutManager::UpdateCallouts() {
814 ShelfAlignment alignment
= shelf_
->alignment();
815 bool horizontal
= alignment
== SHELF_ALIGNMENT_TOP
||
816 alignment
== SHELF_ALIGNMENT_BOTTOM
;
818 for (PanelList::iterator iter
= panel_windows_
.begin();
819 iter
!= panel_windows_
.end(); ++iter
) {
820 aura::Window
* panel
= iter
->window
;
821 views::Widget
* callout_widget
= iter
->callout_widget
;
823 gfx::Rect current_bounds
= panel
->GetBoundsInScreen();
824 gfx::Rect bounds
= ScreenUtil::ConvertRectToScreen(
826 panel
->GetTargetBounds());
827 gfx::Rect icon_bounds
= shelf_
->GetScreenBoundsOfItemIconForWindow(panel
);
828 if (icon_bounds
.IsEmpty() || !panel
->layer()->GetTargetVisibility() ||
829 panel
== dragged_panel_
|| !show_callout_widgets_
) {
830 callout_widget
->Hide();
831 callout_widget
->GetNativeWindow()->layer()->SetOpacity(0);
835 gfx::Rect callout_bounds
= callout_widget
->GetWindowBoundsInScreen();
836 gfx::Vector2d slide_vector
= bounds
.origin() - current_bounds
.origin();
837 int slide_distance
= horizontal
? slide_vector
.x() : slide_vector
.y();
838 int distance_until_over_panel
= 0;
840 callout_bounds
.set_x(
841 icon_bounds
.x() + (icon_bounds
.width() - callout_bounds
.width()) / 2);
842 distance_until_over_panel
= std::max(
843 current_bounds
.x() - callout_bounds
.x(),
844 callout_bounds
.right() - current_bounds
.right());
846 callout_bounds
.set_y(
847 icon_bounds
.y() + (icon_bounds
.height() -
848 callout_bounds
.height()) / 2);
849 distance_until_over_panel
= std::max(
850 current_bounds
.y() - callout_bounds
.y(),
851 callout_bounds
.bottom() - current_bounds
.bottom());
854 case SHELF_ALIGNMENT_BOTTOM
:
855 callout_bounds
.set_y(bounds
.bottom());
857 case SHELF_ALIGNMENT_LEFT
:
858 callout_bounds
.set_x(bounds
.x() - callout_bounds
.width());
860 case SHELF_ALIGNMENT_RIGHT
:
861 callout_bounds
.set_x(bounds
.right());
863 case SHELF_ALIGNMENT_TOP
:
864 callout_bounds
.set_y(bounds
.y() - callout_bounds
.height());
867 callout_bounds
= ScreenUtil::ConvertRectFromScreen(
868 callout_widget
->GetNativeWindow()->parent(),
871 SetChildBoundsDirect(callout_widget
->GetNativeWindow(), callout_bounds
);
872 panel_container_
->StackChildAbove(callout_widget
->GetNativeWindow(),
875 ui::Layer
* layer
= callout_widget
->GetNativeWindow()->layer();
876 // If the panel is not over the callout position or has just become visible
877 // then fade in the callout.
878 if ((distance_until_over_panel
> 0 || layer
->GetTargetOpacity() < 1)) {
879 if (distance_until_over_panel
> 0 &&
880 slide_distance
>= distance_until_over_panel
) {
881 // If the panel is not yet over the callout, then delay fading in
882 // the callout until after the panel should be over it.
883 int delay
= kPanelSlideDurationMilliseconds
*
884 distance_until_over_panel
/ slide_distance
;
885 layer
->SetOpacity(0);
886 layer
->GetAnimator()->StopAnimating();
887 layer
->GetAnimator()->SchedulePauseForProperties(
888 base::TimeDelta::FromMilliseconds(delay
),
889 ui::LayerAnimationElement::OPACITY
);
891 ui::ScopedLayerAnimationSettings
callout_settings(layer
->GetAnimator());
892 callout_settings
.SetPreemptionStrategy(
893 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS
);
894 callout_settings
.SetTransitionDuration(
895 base::TimeDelta::FromMilliseconds(
896 kCalloutFadeDurationMilliseconds
));
897 layer
->SetOpacity(1);
900 // Show after changing the opacity animation. This way we don't have a
901 // state where the widget is visible but the opacity is 0.
902 callout_widget
->Show();
906 ////////////////////////////////////////////////////////////////////////////////
907 // keyboard::KeyboardControllerObserver implementation:
909 void PanelLayoutManager::OnKeyboardBoundsChanging(
910 const gfx::Rect
& keyboard_bounds
) {
911 gfx::Rect parent_bounds
= panel_container_
->bounds();
912 int available_space
= parent_bounds
.height() - keyboard_bounds
.height();
913 for (PanelList::iterator iter
= panel_windows_
.begin();
914 iter
!= panel_windows_
.end();
916 aura::Window
* panel
= iter
->window
;
917 wm::WindowState
* panel_state
= wm::GetWindowState(panel
);
918 if (keyboard_bounds
.height() > 0) {
919 // Save existing bounds, so that we can restore them when the keyboard
921 panel_state
->SaveCurrentBoundsForRestore();
923 gfx::Rect panel_bounds
= ScreenUtil::ConvertRectToScreen(
924 panel
->parent(), panel
->GetTargetBounds());
925 int delta
= panel_bounds
.height() - available_space
;
926 // Ensure panels are not pushed above the parent boundaries, shrink any
927 // panels that violate this constraint.
929 SetChildBounds(panel
,
930 gfx::Rect(panel_bounds
.x(),
931 panel_bounds
.y() + delta
,
932 panel_bounds
.width(),
933 panel_bounds
.height() - delta
));
935 } else if (panel_state
->HasRestoreBounds()) {
936 // Keyboard hidden, restore original bounds if they exist.
937 SetChildBounds(panel
, panel_state
->GetRestoreBoundsInScreen());
940 // This bounds change will have caused a change to the Shelf which does not
941 // propogate automatically to this class, so manually recalculate bounds.