Windows should animate when they are about to get docked at screen edges.
[chromium-blink-merge.git] / ash / system / tray / tray_item_view.cc
blobf95ceb935708c20e5b509c1e88b2a47ea74a8f4f
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.
5 #include "ash/system/tray/tray_item_view.h"
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"
17 namespace {
18 const int kTrayIconHeight = 29;
19 const int kTrayIconWidth = 29;
20 const int kTrayItemAnimationDurationMS = 200;
22 // Animations can be disabled for testing.
23 bool animations_enabled = true;
26 namespace ash {
27 namespace internal {
29 TrayItemView::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));
39 TrayItemView::~TrayItemView() {}
41 // static
42 void TrayItemView::DisableAnimationsForTest() {
43 animations_enabled = false;
46 void TrayItemView::CreateLabel() {
47 label_ = new views::Label;
48 AddChildView(label_);
51 void TrayItemView::CreateImageView() {
52 image_view_ = new views::ImageView;
53 AddChildView(image_view_);
56 void TrayItemView::SetVisible(bool set_visible) {
57 if (!GetWidget() || !animations_enabled) {
58 views::View::SetVisible(set_visible);
59 return;
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);
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);
79 gfx::Size TrayItemView::DesiredSize() {
80 return views::View::GetPreferredSize();
83 int TrayItemView::GetAnimationDurationMS() {
84 return kTrayItemAnimationDurationMS;
87 gfx::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;
101 int TrayItemView::GetHeightForWidth(int width) {
102 return GetPreferredSize().height();
105 void TrayItemView::ChildPreferredSizeChanged(views::View* child) {
106 PreferredSizeChanged();
109 void 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();
119 void TrayItemView::AnimationEnded(const ui::Animation* animation) {
120 if (animation->GetCurrentValue() < 0.1)
121 views::View::SetVisible(false);
124 void TrayItemView::AnimationCanceled(const ui::Animation* animation) {
125 AnimationEnded(animation);
128 } // namespace internal
129 } // namespace ash