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