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/system/status_area_widget_delegate.h"
7 #include "ash/ash_export.h"
8 #include "ash/ash_switches.h"
9 #include "ash/focus_cycler.h"
10 #include "ash/shell.h"
11 #include "ash/shell_window_ids.h"
12 #include "ash/system/tray/tray_constants.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "ui/aura/window_event_dispatcher.h"
16 #include "ui/compositor/layer.h"
17 #include "ui/compositor/scoped_layer_animation_settings.h"
18 #include "ui/gfx/animation/tween.h"
19 #include "ui/gfx/canvas.h"
20 #include "ui/gfx/image/image.h"
21 #include "ui/views/accessible_pane_view.h"
22 #include "ui/views/layout/grid_layout.h"
23 #include "ui/views/widget/widget.h"
27 const int kAnimationDurationMs
= 250;
29 class StatusAreaWidgetDelegateAnimationSettings
30 : public ui::ScopedLayerAnimationSettings
{
32 explicit StatusAreaWidgetDelegateAnimationSettings(ui::Layer
* layer
)
33 : ui::ScopedLayerAnimationSettings(layer
->GetAnimator()) {
34 SetTransitionDuration(
35 base::TimeDelta::FromMilliseconds(kAnimationDurationMs
));
36 SetPreemptionStrategy(
37 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
38 SetTweenType(gfx::Tween::EASE_IN_OUT
);
41 ~StatusAreaWidgetDelegateAnimationSettings() override
{}
44 DISALLOW_COPY_AND_ASSIGN(StatusAreaWidgetDelegateAnimationSettings
);
51 StatusAreaWidgetDelegate::StatusAreaWidgetDelegate()
52 : focus_cycler_for_testing_(NULL
),
53 alignment_(SHELF_ALIGNMENT_BOTTOM
) {
54 // Allow the launcher to surrender the focus to another window upon
55 // navigation completion by the user.
56 set_allow_deactivate_on_esc(true);
57 SetPaintToLayer(true);
58 SetFillsBoundsOpaquely(false);
61 StatusAreaWidgetDelegate::~StatusAreaWidgetDelegate() {
64 void StatusAreaWidgetDelegate::SetFocusCyclerForTesting(
65 const FocusCycler
* focus_cycler
) {
66 focus_cycler_for_testing_
= focus_cycler
;
69 views::View
* StatusAreaWidgetDelegate::GetDefaultFocusableChild() {
73 views::Widget
* StatusAreaWidgetDelegate::GetWidget() {
74 return View::GetWidget();
77 const views::Widget
* StatusAreaWidgetDelegate::GetWidget() const {
78 return View::GetWidget();
81 void StatusAreaWidgetDelegate::OnGestureEvent(ui::GestureEvent
* event
) {
82 aura::Window
* target_window
= static_cast<views::View
*>(event
->target())
85 if (gesture_handler_
.ProcessGestureEvent(*event
, target_window
))
86 event
->StopPropagation();
88 views::AccessiblePaneView::OnGestureEvent(event
);
91 bool StatusAreaWidgetDelegate::CanActivate() const {
92 // We don't want mouse clicks to activate us, but we need to allow
93 // activation when the user is using the keyboard (FocusCycler).
94 const FocusCycler
* focus_cycler
= focus_cycler_for_testing_
?
95 focus_cycler_for_testing_
: Shell::GetInstance()->focus_cycler();
96 return focus_cycler
->widget_activating() == GetWidget();
99 void StatusAreaWidgetDelegate::DeleteDelegate() {
102 void StatusAreaWidgetDelegate::AddTray(views::View
* tray
) {
103 SetLayoutManager(NULL
); // Reset layout manager before adding a child.
105 // Set the layout manager with the new list of children.
109 void StatusAreaWidgetDelegate::UpdateLayout() {
110 // Use a grid layout so that the trays can be centered in each cell, and
111 // so that the widget gets laid out correctly when tray sizes change.
112 views::GridLayout
* layout
= new views::GridLayout(this);
113 SetLayoutManager(layout
);
115 views::ColumnSet
* columns
= layout
->AddColumnSet(0);
116 if (alignment_
== SHELF_ALIGNMENT_BOTTOM
||
117 alignment_
== SHELF_ALIGNMENT_TOP
) {
118 bool is_first_visible_child
= true;
119 for (int c
= 0; c
< child_count(); ++c
) {
120 views::View
* child
= child_at(c
);
121 if (!child
->visible())
123 if (!is_first_visible_child
)
124 columns
->AddPaddingColumn(0, kTraySpacing
);
125 is_first_visible_child
= false;
126 columns
->AddColumn(views::GridLayout::CENTER
, views::GridLayout::FILL
,
127 0, /* resize percent */
128 views::GridLayout::USE_PREF
, 0, 0);
130 layout
->StartRow(0, 0);
131 for (int c
= child_count() - 1; c
>= 0; --c
) {
132 views::View
* child
= child_at(c
);
133 if (child
->visible())
134 layout
->AddView(child
);
137 columns
->AddColumn(views::GridLayout::FILL
, views::GridLayout::CENTER
,
138 0, /* resize percent */
139 views::GridLayout::USE_PREF
, 0, 0);
140 bool is_first_visible_child
= true;
141 for (int c
= child_count() - 1; c
>= 0; --c
) {
142 views::View
* child
= child_at(c
);
143 if (!child
->visible())
145 if (!is_first_visible_child
)
146 layout
->AddPaddingRow(0, kTraySpacing
);
147 is_first_visible_child
= false;
148 layout
->StartRow(0, 0);
149 layout
->AddView(child
);
153 layer()->GetAnimator()->StopAnimating();
154 StatusAreaWidgetDelegateAnimationSettings
settings(layer());
160 void StatusAreaWidgetDelegate::ChildPreferredSizeChanged(View
* child
) {
161 // Need to resize the window when trays or items are added/removed.
162 StatusAreaWidgetDelegateAnimationSettings
settings(layer());
166 void StatusAreaWidgetDelegate::ChildVisibilityChanged(View
* child
) {
170 void StatusAreaWidgetDelegate::UpdateWidgetSize() {
172 GetWidget()->SetSize(GetPreferredSize());