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
17namespace {
18const int kTrayIconHeight = 29;
19const int kTrayIconWidth = 29;
20const int kTrayItemAnimationDurationMS = 200;
21
22// Animations can be disabled for testing.
23bool animations_enabled = true;
24}
25
26namespace ash {
27
28TrayItemView::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
38TrayItemView::~TrayItemView() {}
39
40// static
41void TrayItemView::DisableAnimationsForTest() {
42  animations_enabled = false;
43}
44
45void TrayItemView::CreateLabel() {
46  label_ = new views::Label;
47  AddChildView(label_);
48}
49
50void TrayItemView::CreateImageView() {
51  image_view_ = new views::ImageView;
52  AddChildView(image_view_);
53}
54
55void 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
78gfx::Size TrayItemView::DesiredSize() const {
79  return views::View::GetPreferredSize();
80}
81
82int TrayItemView::GetAnimationDurationMS() {
83  return kTrayItemAnimationDurationMS;
84}
85
86gfx::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
106int TrayItemView::GetHeightForWidth(int width) const {
107  return GetPreferredSize().height();
108}
109
110void TrayItemView::ChildPreferredSizeChanged(views::View* child) {
111  PreferredSizeChanged();
112}
113
114void 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
130void TrayItemView::AnimationEnded(const gfx::Animation* animation) {
131  if (animation->GetCurrentValue() < 0.1)
132    views::View::SetVisible(false);
133}
134
135void TrayItemView::AnimationCanceled(const gfx::Animation* animation) {
136  AnimationEnded(animation);
137}
138
139}  // namespace ash
140