| OLD | NEW |
| (Empty) |
| 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/common/shelf/shelf_types.h" | |
| 8 #include "ash/common/shelf/wm_shelf_util.h" | |
| 9 #include "ash/shelf/shelf_util.h" | |
| 10 #include "ash/system/tray/system_tray.h" | |
| 11 #include "ash/system/tray/system_tray_item.h" | |
| 12 #include "ui/compositor/layer.h" | |
| 13 #include "ui/gfx/animation/slide_animation.h" | |
| 14 #include "ui/views/controls/image_view.h" | |
| 15 #include "ui/views/controls/label.h" | |
| 16 #include "ui/views/layout/box_layout.h" | |
| 17 #include "ui/views/widget/widget.h" | |
| 18 | |
| 19 namespace { | |
| 20 const int kTrayIconHeight = 29; | |
| 21 const int kTrayIconWidth = 29; | |
| 22 const int kTrayItemAnimationDurationMS = 200; | |
| 23 | |
| 24 // Animations can be disabled for testing. | |
| 25 bool animations_enabled = true; | |
| 26 } | |
| 27 | |
| 28 namespace ash { | |
| 29 | |
| 30 TrayItemView::TrayItemView(SystemTrayItem* owner) | |
| 31 : owner_(owner), | |
| 32 label_(NULL), | |
| 33 image_view_(NULL) { | |
| 34 SetPaintToLayer(true); | |
| 35 layer()->SetFillsBoundsOpaquely(false); | |
| 36 SetLayoutManager( | |
| 37 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0)); | |
| 38 } | |
| 39 | |
| 40 TrayItemView::~TrayItemView() {} | |
| 41 | |
| 42 // static | |
| 43 void TrayItemView::DisableAnimationsForTest() { | |
| 44 animations_enabled = false; | |
| 45 } | |
| 46 | |
| 47 void TrayItemView::CreateLabel() { | |
| 48 label_ = new views::Label; | |
| 49 AddChildView(label_); | |
| 50 } | |
| 51 | |
| 52 void TrayItemView::CreateImageView() { | |
| 53 image_view_ = new views::ImageView; | |
| 54 AddChildView(image_view_); | |
| 55 } | |
| 56 | |
| 57 void TrayItemView::SetVisible(bool set_visible) { | |
| 58 if (!GetWidget() || !animations_enabled) { | |
| 59 views::View::SetVisible(set_visible); | |
| 60 return; | |
| 61 } | |
| 62 | |
| 63 if (!animation_) { | |
| 64 animation_.reset(new gfx::SlideAnimation(this)); | |
| 65 animation_->SetSlideDuration(GetAnimationDurationMS()); | |
| 66 animation_->SetTweenType(gfx::Tween::LINEAR); | |
| 67 animation_->Reset(visible() ? 1.0 : 0.0); | |
| 68 } | |
| 69 | |
| 70 if (!set_visible) { | |
| 71 animation_->Hide(); | |
| 72 AnimationProgressed(animation_.get()); | |
| 73 } else { | |
| 74 animation_->Show(); | |
| 75 AnimationProgressed(animation_.get()); | |
| 76 views::View::SetVisible(true); | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 gfx::Size TrayItemView::DesiredSize() const { | |
| 81 return views::View::GetPreferredSize(); | |
| 82 } | |
| 83 | |
| 84 int TrayItemView::GetAnimationDurationMS() { | |
| 85 return kTrayItemAnimationDurationMS; | |
| 86 } | |
| 87 | |
| 88 gfx::Size TrayItemView::GetPreferredSize() const { | |
| 89 gfx::Size size = DesiredSize(); | |
| 90 if (IsHorizontalAlignment(owner()->system_tray()->shelf_alignment())) | |
| 91 size.set_height(kTrayIconHeight); | |
| 92 else | |
| 93 size.set_width(kTrayIconWidth); | |
| 94 if (!animation_.get() || !animation_->is_animating()) | |
| 95 return size; | |
| 96 if (IsHorizontalAlignment(owner()->system_tray()->shelf_alignment())) { | |
| 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 | |
| 106 int TrayItemView::GetHeightForWidth(int width) const { | |
| 107 return GetPreferredSize().height(); | |
| 108 } | |
| 109 | |
| 110 void TrayItemView::ChildPreferredSizeChanged(views::View* child) { | |
| 111 PreferredSizeChanged(); | |
| 112 } | |
| 113 | |
| 114 void TrayItemView::AnimationProgressed(const gfx::Animation* animation) { | |
| 115 gfx::Transform transform; | |
| 116 if (IsHorizontalAlignment(owner()->system_tray()->shelf_alignment())) { | |
| 117 transform.Translate(0, animation->CurrentValueBetween( | |
| 118 static_cast<double>(height()) / 2, 0.)); | |
| 119 } else { | |
| 120 transform.Translate(animation->CurrentValueBetween( | |
| 121 static_cast<double>(width() / 2), 0.), 0); | |
| 122 } | |
| 123 transform.Scale(animation->GetCurrentValue(), | |
| 124 animation->GetCurrentValue()); | |
| 125 layer()->SetTransform(transform); | |
| 126 PreferredSizeChanged(); | |
| 127 } | |
| 128 | |
| 129 void TrayItemView::AnimationEnded(const gfx::Animation* animation) { | |
| 130 if (animation->GetCurrentValue() < 0.1) | |
| 131 views::View::SetVisible(false); | |
| 132 } | |
| 133 | |
| 134 void TrayItemView::AnimationCanceled(const gfx::Animation* animation) { | |
| 135 AnimationEnded(animation); | |
| 136 } | |
| 137 | |
| 138 } // namespace ash | |
| OLD | NEW |