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 virtual 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(); ++iter
) {
555 if (iter
->window
->IsVisible()) {
556 minimized_windows
->Add(iter
->window
);
557 wm::GetWindowState(iter
->window
)->Minimize();
560 restore_windows_on_shelf_visible_
= minimized_windows
.Pass();
563 ////////////////////////////////////////////////////////////////////////////////
564 // PanelLayoutManager private implementation:
566 void PanelLayoutManager::MinimizePanel(aura::Window
* panel
) {
567 ::wm::SetWindowVisibilityAnimationType(
568 panel
, WINDOW_VISIBILITY_ANIMATION_TYPE_MINIMIZE
);
569 ui::Layer
* layer
= panel
->layer();
570 ui::ScopedLayerAnimationSettings
panel_slide_settings(layer
->GetAnimator());
571 panel_slide_settings
.SetPreemptionStrategy(
572 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
573 panel_slide_settings
.SetTransitionDuration(
574 base::TimeDelta::FromMilliseconds(kPanelSlideDurationMilliseconds
));
575 gfx::Rect
bounds(panel
->bounds());
576 bounds
.Offset(GetSlideInAnimationOffset(
577 shelf_
->shelf_widget()->GetAlignment()));
578 SetChildBoundsDirect(panel
, bounds
);
580 layer
->SetOpacity(0);
581 if (wm::IsActiveWindow(panel
))
582 wm::DeactivateWindow(panel
);
586 void PanelLayoutManager::RestorePanel(aura::Window
* panel
) {
587 PanelList::iterator found
=
588 std::find(panel_windows_
.begin(), panel_windows_
.end(), panel
);
589 DCHECK(found
!= panel_windows_
.end());
590 found
->slide_in
= true;
594 void PanelLayoutManager::Relayout() {
595 if (!shelf_
|| !shelf_
->shelf_widget())
600 base::AutoReset
<bool> auto_reset_in_layout(&in_layout_
, true);
602 ShelfAlignment alignment
= shelf_
->shelf_widget()->GetAlignment();
603 bool horizontal
= alignment
== SHELF_ALIGNMENT_TOP
||
604 alignment
== SHELF_ALIGNMENT_BOTTOM
;
605 gfx::Rect shelf_bounds
= ash::ScreenUtil::ConvertRectFromScreen(
606 panel_container_
, shelf_
->shelf_widget()->GetWindowBoundsInScreen());
607 int panel_start_bounds
= kPanelIdealSpacing
;
608 int panel_end_bounds
= horizontal
?
609 panel_container_
->bounds().width() - kPanelIdealSpacing
:
610 panel_container_
->bounds().height() - kPanelIdealSpacing
;
611 aura::Window
* active_panel
= NULL
;
612 std::vector
<VisiblePanelPositionInfo
> visible_panels
;
613 for (PanelList::iterator iter
= panel_windows_
.begin();
614 iter
!= panel_windows_
.end(); ++iter
) {
615 aura::Window
* panel
= iter
->window
;
616 iter
->callout_widget
->SetAlignment(alignment
);
618 // Consider the dragged panel as part of the layout as long as it is
619 // touching the shelf.
620 if ((!panel
->IsVisible() && !iter
->slide_in
) ||
621 (panel
== dragged_panel_
&&
622 !BoundsAdjacent(panel
->bounds(), shelf_bounds
))) {
626 // If the shelf is currently hidden (full-screen mode), minimize panel until
627 // full-screen mode is exited. When a panel is dragged from another display
628 // the shelf state does not update before the panel is added so we exclude
629 // the dragged panel.
630 if (panel
!= dragged_panel_
&& restore_windows_on_shelf_visible_
) {
631 wm::GetWindowState(panel
)->Minimize();
632 restore_windows_on_shelf_visible_
->Add(panel
);
636 gfx::Rect icon_bounds
= shelf_
->GetScreenBoundsOfItemIconForWindow(panel
);
638 // If both the icon width and height are 0 then there is no icon in the
639 // shelf. If the shelf is hidden, one of the height or width will be
640 // 0 but the position in the shelf and major dimension is still reported
641 // correctly and the panel can be aligned above where the hidden icon is.
642 if (icon_bounds
.width() == 0 && icon_bounds
.height() == 0)
645 if (panel
->HasFocus() ||
647 aura::client::GetFocusClient(panel
)->GetFocusedWindow())) {
648 DCHECK(!active_panel
);
649 active_panel
= panel
;
651 icon_bounds
= ScreenUtil::ConvertRectFromScreen(panel_container_
,
653 gfx::Point icon_origin
= icon_bounds
.origin();
654 VisiblePanelPositionInfo position_info
;
655 int icon_start
= horizontal
? icon_origin
.x() : icon_origin
.y();
656 int icon_end
= icon_start
+ (horizontal
? icon_bounds
.width() :
657 icon_bounds
.height());
658 position_info
.major_length
= horizontal
?
659 panel
->bounds().width() : panel
->bounds().height();
660 position_info
.min_major
= std::max(
661 panel_start_bounds
+ position_info
.major_length
/ 2,
662 icon_end
- position_info
.major_length
/ 2);
663 position_info
.max_major
= std::min(
664 icon_start
+ position_info
.major_length
/ 2,
665 panel_end_bounds
- position_info
.major_length
/ 2);
666 position_info
.major_pos
= (icon_start
+ icon_end
) / 2;
667 position_info
.window
= panel
;
668 position_info
.slide_in
= iter
->slide_in
;
669 iter
->slide_in
= false;
670 visible_panels
.push_back(position_info
);
673 // Sort panels by their X positions and fan out groups of overlapping panels.
674 // The fan out method may result in new overlapping panels however given that
675 // the panels start at least a full panel width apart this overlap will
676 // never completely obscure a panel.
677 // TODO(flackr): Rearrange panels if new overlaps are introduced.
678 std::sort(visible_panels
.begin(), visible_panels
.end(), CompareWindowMajor
);
679 size_t first_overlapping_panel
= 0;
680 for (size_t i
= 1; i
< visible_panels
.size(); ++i
) {
681 if (visible_panels
[i
- 1].major_pos
+
682 visible_panels
[i
- 1].major_length
/ 2 < visible_panels
[i
].major_pos
-
683 visible_panels
[i
].major_length
/ 2) {
684 FanOutPanels(visible_panels
.begin() + first_overlapping_panel
,
685 visible_panels
.begin() + i
);
686 first_overlapping_panel
= i
;
689 FanOutPanels(visible_panels
.begin() + first_overlapping_panel
,
690 visible_panels
.end());
692 for (size_t i
= 0; i
< visible_panels
.size(); ++i
) {
693 if (visible_panels
[i
].window
== dragged_panel_
)
695 bool slide_in
= visible_panels
[i
].slide_in
;
696 gfx::Rect bounds
= visible_panels
[i
].window
->GetTargetBounds();
698 case SHELF_ALIGNMENT_BOTTOM
:
699 bounds
.set_y(shelf_bounds
.y() - bounds
.height());
701 case SHELF_ALIGNMENT_LEFT
:
702 bounds
.set_x(shelf_bounds
.right());
704 case SHELF_ALIGNMENT_RIGHT
:
705 bounds
.set_x(shelf_bounds
.x() - bounds
.width());
707 case SHELF_ALIGNMENT_TOP
:
708 bounds
.set_y(shelf_bounds
.bottom());
711 bool on_shelf
= visible_panels
[i
].window
->GetTargetBounds() == bounds
;
714 bounds
.set_x(visible_panels
[i
].major_pos
-
715 visible_panels
[i
].major_length
/ 2);
717 bounds
.set_y(visible_panels
[i
].major_pos
-
718 visible_panels
[i
].major_length
/ 2);
721 ui::Layer
* layer
= visible_panels
[i
].window
->layer();
723 // New windows shift up from the shelf into position and fade in.
724 layer
->SetOpacity(0);
725 gfx::Rect
initial_bounds(bounds
);
726 initial_bounds
.Offset(GetSlideInAnimationOffset(alignment
));
727 SetChildBoundsDirect(visible_panels
[i
].window
, initial_bounds
);
728 // Set on shelf so that the panel animates into its target position.
733 ui::ScopedLayerAnimationSettings
panel_slide_settings(
734 layer
->GetAnimator());
735 panel_slide_settings
.SetPreemptionStrategy(
736 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
737 panel_slide_settings
.SetTransitionDuration(
738 base::TimeDelta::FromMilliseconds(kPanelSlideDurationMilliseconds
));
739 SetChildBoundsDirect(visible_panels
[i
].window
, bounds
);
741 layer
->SetOpacity(1);
742 visible_panels
[i
].window
->Show();
745 // If the shelf moved don't animate, move immediately to the new
747 SetChildBoundsDirect(visible_panels
[i
].window
, bounds
);
751 UpdateStacking(active_panel
);
755 void PanelLayoutManager::UpdateStacking(aura::Window
* active_panel
) {
757 if (!last_active_panel_
)
759 active_panel
= last_active_panel_
;
762 ShelfAlignment alignment
= shelf_
->alignment();
763 bool horizontal
= alignment
== SHELF_ALIGNMENT_TOP
||
764 alignment
== SHELF_ALIGNMENT_BOTTOM
;
766 // We want to to stack the panels like a deck of cards:
767 // ,--,--,--,-------.--.--.
771 // We use the middle of each panel to figure out how to stack the panels. This
772 // allows us to update the stacking when a panel is being dragged around by
773 // the titlebar--even though it doesn't update the shelf icon positions, we
774 // still want the visual effect.
775 std::map
<int, aura::Window
*> window_ordering
;
776 for (PanelList::const_iterator it
= panel_windows_
.begin();
777 it
!= panel_windows_
.end(); ++it
) {
778 gfx::Rect bounds
= it
->window
->bounds();
779 window_ordering
.insert(std::make_pair(horizontal
?
780 bounds
.x() + bounds
.width() / 2 :
781 bounds
.y() + bounds
.height() / 2,
785 aura::Window
* previous_panel
= NULL
;
786 for (std::map
<int, aura::Window
*>::const_iterator it
=
787 window_ordering
.begin();
788 it
!= window_ordering
.end() && it
->second
!= active_panel
; ++it
) {
790 panel_container_
->StackChildAbove(it
->second
, previous_panel
);
791 previous_panel
= it
->second
;
794 previous_panel
= NULL
;
795 for (std::map
<int, aura::Window
*>::const_reverse_iterator it
=
796 window_ordering
.rbegin();
797 it
!= window_ordering
.rend() && it
->second
!= active_panel
; ++it
) {
799 panel_container_
->StackChildAbove(it
->second
, previous_panel
);
800 previous_panel
= it
->second
;
803 panel_container_
->StackChildAtTop(active_panel
);
804 if (dragged_panel_
&& dragged_panel_
->parent() == panel_container_
)
805 panel_container_
->StackChildAtTop(dragged_panel_
);
806 last_active_panel_
= active_panel
;
809 void PanelLayoutManager::UpdateCallouts() {
810 ShelfAlignment alignment
= shelf_
->alignment();
811 bool horizontal
= alignment
== SHELF_ALIGNMENT_TOP
||
812 alignment
== SHELF_ALIGNMENT_BOTTOM
;
814 for (PanelList::iterator iter
= panel_windows_
.begin();
815 iter
!= panel_windows_
.end(); ++iter
) {
816 aura::Window
* panel
= iter
->window
;
817 views::Widget
* callout_widget
= iter
->callout_widget
;
819 gfx::Rect current_bounds
= panel
->GetBoundsInScreen();
820 gfx::Rect bounds
= ScreenUtil::ConvertRectToScreen(
822 panel
->GetTargetBounds());
823 gfx::Rect icon_bounds
= shelf_
->GetScreenBoundsOfItemIconForWindow(panel
);
824 if (icon_bounds
.IsEmpty() || !panel
->layer()->GetTargetVisibility() ||
825 panel
== dragged_panel_
|| !show_callout_widgets_
) {
826 callout_widget
->Hide();
827 callout_widget
->GetNativeWindow()->layer()->SetOpacity(0);
831 gfx::Rect callout_bounds
= callout_widget
->GetWindowBoundsInScreen();
832 gfx::Vector2d slide_vector
= bounds
.origin() - current_bounds
.origin();
833 int slide_distance
= horizontal
? slide_vector
.x() : slide_vector
.y();
834 int distance_until_over_panel
= 0;
836 callout_bounds
.set_x(
837 icon_bounds
.x() + (icon_bounds
.width() - callout_bounds
.width()) / 2);
838 distance_until_over_panel
= std::max(
839 current_bounds
.x() - callout_bounds
.x(),
840 callout_bounds
.right() - current_bounds
.right());
842 callout_bounds
.set_y(
843 icon_bounds
.y() + (icon_bounds
.height() -
844 callout_bounds
.height()) / 2);
845 distance_until_over_panel
= std::max(
846 current_bounds
.y() - callout_bounds
.y(),
847 callout_bounds
.bottom() - current_bounds
.bottom());
850 case SHELF_ALIGNMENT_BOTTOM
:
851 callout_bounds
.set_y(bounds
.bottom());
853 case SHELF_ALIGNMENT_LEFT
:
854 callout_bounds
.set_x(bounds
.x() - callout_bounds
.width());
856 case SHELF_ALIGNMENT_RIGHT
:
857 callout_bounds
.set_x(bounds
.right());
859 case SHELF_ALIGNMENT_TOP
:
860 callout_bounds
.set_y(bounds
.y() - callout_bounds
.height());
863 callout_bounds
= ScreenUtil::ConvertRectFromScreen(
864 callout_widget
->GetNativeWindow()->parent(),
867 SetChildBoundsDirect(callout_widget
->GetNativeWindow(), callout_bounds
);
868 panel_container_
->StackChildAbove(callout_widget
->GetNativeWindow(),
871 ui::Layer
* layer
= callout_widget
->GetNativeWindow()->layer();
872 // If the panel is not over the callout position or has just become visible
873 // then fade in the callout.
874 if ((distance_until_over_panel
> 0 || layer
->GetTargetOpacity() < 1)) {
875 if (distance_until_over_panel
> 0 &&
876 slide_distance
>= distance_until_over_panel
) {
877 // If the panel is not yet over the callout, then delay fading in
878 // the callout until after the panel should be over it.
879 int delay
= kPanelSlideDurationMilliseconds
*
880 distance_until_over_panel
/ slide_distance
;
881 layer
->SetOpacity(0);
882 layer
->GetAnimator()->StopAnimating();
883 layer
->GetAnimator()->SchedulePauseForProperties(
884 base::TimeDelta::FromMilliseconds(delay
),
885 ui::LayerAnimationElement::OPACITY
);
887 ui::ScopedLayerAnimationSettings
callout_settings(layer
->GetAnimator());
888 callout_settings
.SetPreemptionStrategy(
889 ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS
);
890 callout_settings
.SetTransitionDuration(
891 base::TimeDelta::FromMilliseconds(
892 kCalloutFadeDurationMilliseconds
));
893 layer
->SetOpacity(1);
896 // Show after changing the opacity animation. This way we don't have a
897 // state where the widget is visible but the opacity is 0.
898 callout_widget
->Show();
902 ////////////////////////////////////////////////////////////////////////////////
903 // keyboard::KeyboardControllerObserver implementation:
905 void PanelLayoutManager::OnKeyboardBoundsChanging(
906 const gfx::Rect
& keyboard_bounds
) {
907 gfx::Rect parent_bounds
= panel_container_
->bounds();
908 int available_space
= parent_bounds
.height() - keyboard_bounds
.height();
909 for (PanelList::iterator iter
= panel_windows_
.begin();
910 iter
!= panel_windows_
.end();
912 aura::Window
* panel
= iter
->window
;
913 wm::WindowState
* panel_state
= wm::GetWindowState(panel
);
914 if (keyboard_bounds
.height() > 0) {
915 // Save existing bounds, so that we can restore them when the keyboard
917 panel_state
->SaveCurrentBoundsForRestore();
919 gfx::Rect panel_bounds
= ScreenUtil::ConvertRectToScreen(
920 panel
->parent(), panel
->GetTargetBounds());
921 int delta
= panel_bounds
.height() - available_space
;
922 // Ensure panels are not pushed above the parent boundaries, shrink any
923 // panels that violate this constraint.
925 SetChildBounds(panel
,
926 gfx::Rect(panel_bounds
.x(),
927 panel_bounds
.y() + delta
,
928 panel_bounds
.width(),
929 panel_bounds
.height() - delta
));
931 } else if (panel_state
->HasRestoreBounds()) {
932 // Keyboard hidden, restore original bounds if they exist.
933 SetChildBounds(panel
, panel_state
->GetRestoreBoundsInScreen());
936 // This bounds change will have caused a change to the Shelf which does not
937 // propogate automatically to this class, so manually recalculate bounds.