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/tray/tray_item_view.h"
7 #include "ash/shelf/shelf_types.h"
8 #include "ash/system/tray/system_tray.h"
9 #include "ash/system/tray/system_tray_item.h"
10 #include "ui/compositor/layer.h"
11 #include "ui/gfx/animation/slide_animation.h"
12 #include "ui/views/controls/image_view.h"
13 #include "ui/views/controls/label.h"
14 #include "ui/views/layout/box_layout.h"
15 #include "ui/views/widget/widget.h"
18 const int kTrayIconHeight
= 29;
19 const int kTrayIconWidth
= 29;
20 const int kTrayItemAnimationDurationMS
= 200;
22 // Animations can be disabled for testing.
23 bool animations_enabled
= true;
28 TrayItemView::TrayItemView(SystemTrayItem
* owner
)
32 SetPaintToLayer(true);
33 SetFillsBoundsOpaquely(false);
35 new views::BoxLayout(views::BoxLayout::kHorizontal
, 0, 0, 0));
38 TrayItemView::~TrayItemView() {}
41 void TrayItemView::DisableAnimationsForTest() {
42 animations_enabled
= false;
45 void TrayItemView::CreateLabel() {
46 label_
= new views::Label
;
50 void TrayItemView::CreateImageView() {
51 image_view_
= new views::ImageView
;
52 AddChildView(image_view_
);
55 void TrayItemView::SetVisible(bool set_visible
) {
56 if (!GetWidget() || !animations_enabled
) {
57 views::View::SetVisible(set_visible
);
62 animation_
.reset(new gfx::SlideAnimation(this));
63 animation_
->SetSlideDuration(GetAnimationDurationMS());
64 animation_
->SetTweenType(gfx::Tween::LINEAR
);
65 animation_
->Reset(visible() ? 1.0 : 0.0);
70 AnimationProgressed(animation_
.get());
73 AnimationProgressed(animation_
.get());
74 views::View::SetVisible(true);
78 gfx::Size
TrayItemView::DesiredSize() const {
79 return views::View::GetPreferredSize();
82 int TrayItemView::GetAnimationDurationMS() {
83 return kTrayItemAnimationDurationMS
;
86 gfx::Size
TrayItemView::GetPreferredSize() const {
87 gfx::Size size
= DesiredSize();
88 if (owner()->system_tray()->shelf_alignment() == SHELF_ALIGNMENT_BOTTOM
||
89 owner()->system_tray()->shelf_alignment() == SHELF_ALIGNMENT_TOP
)
90 size
.set_height(kTrayIconHeight
);
92 size
.set_width(kTrayIconWidth
);
93 if (!animation_
.get() || !animation_
->is_animating())
95 if (owner()->system_tray()->shelf_alignment() == SHELF_ALIGNMENT_BOTTOM
||
96 owner()->system_tray()->shelf_alignment() == SHELF_ALIGNMENT_TOP
) {
97 size
.set_width(std::max(1,
98 static_cast<int>(size
.width() * animation_
->GetCurrentValue())));
100 size
.set_height(std::max(1,
101 static_cast<int>(size
.height() * animation_
->GetCurrentValue())));
106 int TrayItemView::GetHeightForWidth(int width
) const {
107 return GetPreferredSize().height();
110 void TrayItemView::ChildPreferredSizeChanged(views::View
* child
) {
111 PreferredSizeChanged();
114 void TrayItemView::AnimationProgressed(const gfx::Animation
* animation
) {
115 gfx::Transform transform
;
116 if (owner()->system_tray()->shelf_alignment() == SHELF_ALIGNMENT_BOTTOM
||
117 owner()->system_tray()->shelf_alignment() == SHELF_ALIGNMENT_TOP
) {
118 transform
.Translate(0, animation
->CurrentValueBetween(
119 static_cast<double>(height()) / 2, 0.));
121 transform
.Translate(animation
->CurrentValueBetween(
122 static_cast<double>(width() / 2), 0.), 0);
124 transform
.Scale(animation
->GetCurrentValue(),
125 animation
->GetCurrentValue());
126 layer()->SetTransform(transform
);
127 PreferredSizeChanged();
130 void TrayItemView::AnimationEnded(const gfx::Animation
* animation
) {
131 if (animation
->GetCurrentValue() < 0.1)
132 views::View::SetVisible(false);
135 void TrayItemView::AnimationCanceled(const gfx::Animation
* animation
) {
136 AnimationEnded(animation
);