tray_item_view.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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/base/animation/slide_animation.h"
11#include "ui/compositor/layer.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 {
27namespace internal {
28
29TrayItemView::TrayItemView(SystemTrayItem* owner)
30    : owner_(owner),
31      label_(NULL),
32      image_view_(NULL) {
33  SetPaintToLayer(true);
34  SetFillsBoundsOpaquely(false);
35  SetLayoutManager(
36      new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0));
37}
38
39TrayItemView::~TrayItemView() {}
40
41// static
42void TrayItemView::DisableAnimationsForTest() {
43  animations_enabled = false;
44}
45
46void TrayItemView::CreateLabel() {
47  label_ = new views::Label;
48  AddChildView(label_);
49}
50
51void TrayItemView::CreateImageView() {
52  image_view_ = new views::ImageView;
53  AddChildView(image_view_);
54}
55
56void TrayItemView::SetVisible(bool set_visible) {
57  if (!GetWidget() || !animations_enabled) {
58    views::View::SetVisible(set_visible);
59    return;
60  }
61
62  if (!animation_) {
63    animation_.reset(new ui::SlideAnimation(this));
64    animation_->SetSlideDuration(GetAnimationDurationMS());
65    animation_->SetTweenType(ui::Tween::LINEAR);
66    animation_->Reset(visible() ? 1.0 : 0.0);
67  }
68
69  if (!set_visible) {
70    animation_->Hide();
71    AnimationProgressed(animation_.get());
72  } else {
73    animation_->Show();
74    AnimationProgressed(animation_.get());
75    views::View::SetVisible(true);
76  }
77}
78
79gfx::Size TrayItemView::DesiredSize() {
80  return views::View::GetPreferredSize();
81}
82
83int TrayItemView::GetAnimationDurationMS() {
84  return kTrayItemAnimationDurationMS;
85}
86
87gfx::Size TrayItemView::GetPreferredSize() {
88  gfx::Size size = DesiredSize();
89  if (owner()->system_tray()->shelf_alignment() == SHELF_ALIGNMENT_BOTTOM ||
90      owner()->system_tray()->shelf_alignment() == SHELF_ALIGNMENT_TOP)
91    size.set_height(kTrayIconHeight);
92  else
93    size.set_width(kTrayIconWidth);
94  if (!animation_.get() || !animation_->is_animating())
95    return size;
96  size.set_width(std::max(1,
97      static_cast<int>(size.width() * animation_->GetCurrentValue())));
98  return size;
99}
100
101int TrayItemView::GetHeightForWidth(int width) {
102  return GetPreferredSize().height();
103}
104
105void TrayItemView::ChildPreferredSizeChanged(views::View* child) {
106  PreferredSizeChanged();
107}
108
109void TrayItemView::AnimationProgressed(const ui::Animation* animation) {
110  gfx::Transform transform;
111  transform.Translate(0, animation->CurrentValueBetween(
112      static_cast<double>(height()) / 2, 0.));
113  transform.Scale(animation->GetCurrentValue(),
114                  animation->GetCurrentValue());
115  layer()->SetTransform(transform);
116  PreferredSizeChanged();
117}
118
119void TrayItemView::AnimationEnded(const ui::Animation* animation) {
120  if (animation->GetCurrentValue() < 0.1)
121    views::View::SetVisible(false);
122}
123
124void TrayItemView::AnimationCanceled(const ui::Animation* animation) {
125  AnimationEnded(animation);
126}
127
128}  // namespace internal
129}  // namespace ash
130