summaryrefslogtreecommitdiff
path: root/chromium/ash/system/tray/tray_item_view.cc
blob: 9a7a75c437891ce2c2f7008a23bfc7cd4e2573d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/system/tray/tray_item_view.h"

#include "ash/shelf/shelf_types.h"
#include "ash/system/tray/system_tray.h"
#include "ash/system/tray/system_tray_item.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/animation/slide_animation.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/widget/widget.h"

namespace {
const int kTrayIconHeight = 29;
const int kTrayIconWidth = 29;
const int kTrayItemAnimationDurationMS = 200;

// Animations can be disabled for testing.
bool animations_enabled = true;
}

namespace ash {
namespace internal {

TrayItemView::TrayItemView(SystemTrayItem* owner)
    : owner_(owner),
      label_(NULL),
      image_view_(NULL) {
  SetPaintToLayer(true);
  SetFillsBoundsOpaquely(false);
  SetLayoutManager(
      new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0));
}

TrayItemView::~TrayItemView() {}

// static
void TrayItemView::DisableAnimationsForTest() {
  animations_enabled = false;
}

void TrayItemView::CreateLabel() {
  label_ = new views::Label;
  AddChildView(label_);
}

void TrayItemView::CreateImageView() {
  image_view_ = new views::ImageView;
  AddChildView(image_view_);
}

void TrayItemView::SetVisible(bool set_visible) {
  if (!GetWidget() || !animations_enabled) {
    views::View::SetVisible(set_visible);
    return;
  }

  if (!animation_) {
    animation_.reset(new gfx::SlideAnimation(this));
    animation_->SetSlideDuration(GetAnimationDurationMS());
    animation_->SetTweenType(gfx::Tween::LINEAR);
    animation_->Reset(visible() ? 1.0 : 0.0);
  }

  if (!set_visible) {
    animation_->Hide();
    AnimationProgressed(animation_.get());
  } else {
    animation_->Show();
    AnimationProgressed(animation_.get());
    views::View::SetVisible(true);
  }
}

gfx::Size TrayItemView::DesiredSize() {
  return views::View::GetPreferredSize();
}

int TrayItemView::GetAnimationDurationMS() {
  return kTrayItemAnimationDurationMS;
}

gfx::Size TrayItemView::GetPreferredSize() {
  gfx::Size size = DesiredSize();
  if (owner()->system_tray()->shelf_alignment() == SHELF_ALIGNMENT_BOTTOM ||
      owner()->system_tray()->shelf_alignment() == SHELF_ALIGNMENT_TOP)
    size.set_height(kTrayIconHeight);
  else
    size.set_width(kTrayIconWidth);
  if (!animation_.get() || !animation_->is_animating())
    return size;
  if (owner()->system_tray()->shelf_alignment() == SHELF_ALIGNMENT_BOTTOM ||
      owner()->system_tray()->shelf_alignment() == SHELF_ALIGNMENT_TOP) {
    size.set_width(std::max(1,
        static_cast<int>(size.width() * animation_->GetCurrentValue())));
  } else {
    size.set_height(std::max(1,
        static_cast<int>(size.height() * animation_->GetCurrentValue())));
  }
  return size;
}

int TrayItemView::GetHeightForWidth(int width) {
  return GetPreferredSize().height();
}

void TrayItemView::ChildPreferredSizeChanged(views::View* child) {
  PreferredSizeChanged();
}

void TrayItemView::AnimationProgressed(const gfx::Animation* animation) {
  gfx::Transform transform;
  if (owner()->system_tray()->shelf_alignment() == SHELF_ALIGNMENT_BOTTOM ||
      owner()->system_tray()->shelf_alignment() == SHELF_ALIGNMENT_TOP) {
    transform.Translate(0, animation->CurrentValueBetween(
        static_cast<double>(height()) / 2, 0.));
  } else {
    transform.Translate(animation->CurrentValueBetween(
        static_cast<double>(width() / 2), 0.), 0);
  }
  transform.Scale(animation->GetCurrentValue(),
                  animation->GetCurrentValue());
  layer()->SetTransform(transform);
  PreferredSizeChanged();
}

void TrayItemView::AnimationEnded(const gfx::Animation* animation) {
  if (animation->GetCurrentValue() < 0.1)
    views::View::SetVisible(false);
}

void TrayItemView::AnimationCanceled(const gfx::Animation* animation) {
  AnimationEnded(animation);
}

}  // namespace internal
}  // namespace ash