• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "ash/system/tray/tray_item_view.h"
6 
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"
16 
17 namespace {
18 const int kTrayIconHeight = 29;
19 const int kTrayIconWidth = 29;
20 const int kTrayItemAnimationDurationMS = 200;
21 
22 // Animations can be disabled for testing.
23 bool animations_enabled = true;
24 }
25 
26 namespace ash {
27 
TrayItemView(SystemTrayItem * owner)28 TrayItemView::TrayItemView(SystemTrayItem* owner)
29     : owner_(owner),
30       label_(NULL),
31       image_view_(NULL) {
32   SetPaintToLayer(true);
33   SetFillsBoundsOpaquely(false);
34   SetLayoutManager(
35       new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0));
36 }
37 
~TrayItemView()38 TrayItemView::~TrayItemView() {}
39 
40 // static
DisableAnimationsForTest()41 void TrayItemView::DisableAnimationsForTest() {
42   animations_enabled = false;
43 }
44 
CreateLabel()45 void TrayItemView::CreateLabel() {
46   label_ = new views::Label;
47   AddChildView(label_);
48 }
49 
CreateImageView()50 void TrayItemView::CreateImageView() {
51   image_view_ = new views::ImageView;
52   AddChildView(image_view_);
53 }
54 
SetVisible(bool set_visible)55 void TrayItemView::SetVisible(bool set_visible) {
56   if (!GetWidget() || !animations_enabled) {
57     views::View::SetVisible(set_visible);
58     return;
59   }
60 
61   if (!animation_) {
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);
66   }
67 
68   if (!set_visible) {
69     animation_->Hide();
70     AnimationProgressed(animation_.get());
71   } else {
72     animation_->Show();
73     AnimationProgressed(animation_.get());
74     views::View::SetVisible(true);
75   }
76 }
77 
DesiredSize() const78 gfx::Size TrayItemView::DesiredSize() const {
79   return views::View::GetPreferredSize();
80 }
81 
GetAnimationDurationMS()82 int TrayItemView::GetAnimationDurationMS() {
83   return kTrayItemAnimationDurationMS;
84 }
85 
GetPreferredSize() const86 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);
91   else
92     size.set_width(kTrayIconWidth);
93   if (!animation_.get() || !animation_->is_animating())
94     return size;
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())));
99   } else {
100     size.set_height(std::max(1,
101         static_cast<int>(size.height() * animation_->GetCurrentValue())));
102   }
103   return size;
104 }
105 
GetHeightForWidth(int width) const106 int TrayItemView::GetHeightForWidth(int width) const {
107   return GetPreferredSize().height();
108 }
109 
ChildPreferredSizeChanged(views::View * child)110 void TrayItemView::ChildPreferredSizeChanged(views::View* child) {
111   PreferredSizeChanged();
112 }
113 
AnimationProgressed(const gfx::Animation * animation)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.));
120   } else {
121     transform.Translate(animation->CurrentValueBetween(
122         static_cast<double>(width() / 2), 0.), 0);
123   }
124   transform.Scale(animation->GetCurrentValue(),
125                   animation->GetCurrentValue());
126   layer()->SetTransform(transform);
127   PreferredSizeChanged();
128 }
129 
AnimationEnded(const gfx::Animation * animation)130 void TrayItemView::AnimationEnded(const gfx::Animation* animation) {
131   if (animation->GetCurrentValue() < 0.1)
132     views::View::SetVisible(false);
133 }
134 
AnimationCanceled(const gfx::Animation * animation)135 void TrayItemView::AnimationCanceled(const gfx::Animation* animation) {
136   AnimationEnded(animation);
137 }
138 
139 }  // namespace ash
140