| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/system/tray/tray_item_view.h" | 5 #include "ash/system/tray/tray_item_view.h" |
| 6 | 6 |
| 7 #include "ash/shelf/shelf_types.h" | 7 #include "ash/shelf/shelf_types.h" |
| 8 #include "ash/shell.h" |
| 8 #include "ash/system/tray/system_tray.h" | 9 #include "ash/system/tray/system_tray.h" |
| 9 #include "ash/system/tray/system_tray_item.h" | 10 #include "ash/system/tray/system_tray_item.h" |
| 10 #include "ui/base/animation/slide_animation.h" | 11 #include "ui/base/animation/slide_animation.h" |
| 11 #include "ui/compositor/layer.h" | 12 #include "ui/compositor/layer.h" |
| 12 #include "ui/views/controls/image_view.h" | 13 #include "ui/views/controls/image_view.h" |
| 13 #include "ui/views/controls/label.h" | 14 #include "ui/views/controls/label.h" |
| 14 #include "ui/views/layout/box_layout.h" | 15 #include "ui/views/layout/box_layout.h" |
| 15 #include "ui/views/widget/widget.h" | 16 #include "ui/views/widget/widget.h" |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 const int kTrayIconHeight = 29; | 19 const int kTrayIconHeight = 29; |
| 19 const int kTrayIconWidth = 29; | 20 const int kTrayIconWidth = 29; |
| 20 const int kTrayItemAnimationDurationMS = 200; | 21 const int kTrayItemAnimationDurationMS = 200; |
| 21 | 22 |
| 22 // Animations can be disabled for testing. | 23 // Animations can be disabled for testing. |
| 23 bool animations_enabled = true; | 24 bool animations_enabled = true; |
| 24 } | 25 } |
| 25 | 26 |
| 26 namespace ash { | 27 namespace ash { |
| 27 namespace internal { | 28 namespace internal { |
| 28 | 29 |
| 29 TrayItemView::TrayItemView(SystemTrayItem* owner) | 30 TrayItemView::TrayItemView(SystemTrayItem* owner) |
| 30 : owner_(owner), | 31 : owner_(owner), |
| 31 label_(NULL), | 32 label_(NULL), |
| 32 image_view_(NULL) { | 33 image_view_(NULL), |
| 34 alignment_override_(AUTO) { |
| 33 SetPaintToLayer(true); | 35 SetPaintToLayer(true); |
| 34 SetFillsBoundsOpaquely(false); | 36 SetFillsBoundsOpaquely(false); |
| 35 SetLayoutManager( | 37 SetLayoutAlignment(); |
| 36 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0)); | 38 // Observe any shelf alignment changes to change view alignments. |
| 39 if (ash::Shell::HasInstance()) |
| 40 ash::Shell::GetInstance()->AddShellObserver(this); |
| 37 } | 41 } |
| 38 | 42 |
| 39 TrayItemView::~TrayItemView() {} | 43 TrayItemView::~TrayItemView() { |
| 44 if (ash::Shell::HasInstance()) |
| 45 ash::Shell::GetInstance()->RemoveShellObserver(this); |
| 46 } |
| 40 | 47 |
| 41 // static | 48 // static |
| 42 void TrayItemView::DisableAnimationsForTest() { | 49 void TrayItemView::DisableAnimationsForTest() { |
| 43 animations_enabled = false; | 50 animations_enabled = false; |
| 44 } | 51 } |
| 45 | 52 |
| 46 void TrayItemView::CreateLabel() { | 53 void TrayItemView::CreateLabel() { |
| 47 label_ = new views::Label; | 54 label_ = new views::Label; |
| 48 AddChildView(label_); | 55 AddChildView(label_); |
| 49 } | 56 } |
| 50 | 57 |
| 51 void TrayItemView::CreateImageView() { | 58 void TrayItemView::CreateImageView() { |
| 52 image_view_ = new views::ImageView; | 59 image_view_ = new views::ImageView; |
| 53 AddChildView(image_view_); | 60 AddChildView(image_view_); |
| 54 } | 61 } |
| 55 | 62 |
| 63 void TrayItemView::SetAlignmentOverride(AlignmentOverride alignment_override) { |
| 64 alignment_override_ = alignment_override; |
| 65 SetLayoutAlignment(); |
| 66 } |
| 67 |
| 56 void TrayItemView::SetVisible(bool set_visible) { | 68 void TrayItemView::SetVisible(bool set_visible) { |
| 57 if (!GetWidget() || !animations_enabled) { | 69 if (!GetWidget() || !animations_enabled) { |
| 58 views::View::SetVisible(set_visible); | 70 views::View::SetVisible(set_visible); |
| 59 return; | 71 return; |
| 60 } | 72 } |
| 61 | 73 |
| 62 if (!animation_) { | 74 if (!animation_) { |
| 63 animation_.reset(new ui::SlideAnimation(this)); | 75 animation_.reset(new ui::SlideAnimation(this)); |
| 64 animation_->SetSlideDuration(GetAnimationDurationMS()); | 76 animation_->SetSlideDuration(GetAnimationDurationMS()); |
| 65 animation_->SetTweenType(ui::Tween::LINEAR); | 77 animation_->SetTweenType(ui::Tween::LINEAR); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 95 return size; | 107 return size; |
| 96 size.set_width(std::max(1, | 108 size.set_width(std::max(1, |
| 97 static_cast<int>(size.width() * animation_->GetCurrentValue()))); | 109 static_cast<int>(size.width() * animation_->GetCurrentValue()))); |
| 98 return size; | 110 return size; |
| 99 } | 111 } |
| 100 | 112 |
| 101 int TrayItemView::GetHeightForWidth(int width) { | 113 int TrayItemView::GetHeightForWidth(int width) { |
| 102 return GetPreferredSize().height(); | 114 return GetPreferredSize().height(); |
| 103 } | 115 } |
| 104 | 116 |
| 117 void TrayItemView::OnShelfAlignmentChanged( |
| 118 aura::RootWindow* root_window) { |
| 119 SetLayoutAlignment(); |
| 120 } |
| 121 |
| 105 void TrayItemView::ChildPreferredSizeChanged(views::View* child) { | 122 void TrayItemView::ChildPreferredSizeChanged(views::View* child) { |
| 106 PreferredSizeChanged(); | 123 PreferredSizeChanged(); |
| 107 } | 124 } |
| 108 | 125 |
| 109 void TrayItemView::AnimationProgressed(const ui::Animation* animation) { | 126 void TrayItemView::AnimationProgressed(const ui::Animation* animation) { |
| 110 gfx::Transform transform; | 127 gfx::Transform transform; |
| 111 transform.Translate(0, animation->CurrentValueBetween( | 128 transform.Translate(0, animation->CurrentValueBetween( |
| 112 static_cast<double>(height()) / 2, 0.)); | 129 static_cast<double>(height()) / 2, 0.)); |
| 113 transform.Scale(animation->GetCurrentValue(), | 130 transform.Scale(animation->GetCurrentValue(), |
| 114 animation->GetCurrentValue()); | 131 animation->GetCurrentValue()); |
| 115 layer()->SetTransform(transform); | 132 layer()->SetTransform(transform); |
| 116 PreferredSizeChanged(); | 133 PreferredSizeChanged(); |
| 117 } | 134 } |
| 118 | 135 |
| 119 void TrayItemView::AnimationEnded(const ui::Animation* animation) { | 136 void TrayItemView::AnimationEnded(const ui::Animation* animation) { |
| 120 if (animation->GetCurrentValue() < 0.1) | 137 if (animation->GetCurrentValue() < 0.1) |
| 121 views::View::SetVisible(false); | 138 views::View::SetVisible(false); |
| 122 } | 139 } |
| 123 | 140 |
| 124 void TrayItemView::AnimationCanceled(const ui::Animation* animation) { | 141 void TrayItemView::AnimationCanceled(const ui::Animation* animation) { |
| 125 AnimationEnded(animation); | 142 AnimationEnded(animation); |
| 126 } | 143 } |
| 127 | 144 |
| 145 void TrayItemView::SetLayoutAlignment() { |
| 146 views::BoxLayout::Orientation alignment = views::BoxLayout::kHorizontal; |
| 147 switch (alignment_override_) { |
| 148 case AUTO: |
| 149 switch (owner()->system_tray()->shelf_alignment()) { |
| 150 case ash::SHELF_ALIGNMENT_BOTTOM: |
| 151 case ash::SHELF_ALIGNMENT_TOP: |
| 152 alignment = views::BoxLayout::kHorizontal; |
| 153 break; |
| 154 case ash::SHELF_ALIGNMENT_LEFT: |
| 155 case ash::SHELF_ALIGNMENT_RIGHT: |
| 156 alignment = views::BoxLayout::kVertical; |
| 157 break; |
| 158 } |
| 159 break; |
| 160 case HORIZONTAL: |
| 161 alignment = views::BoxLayout::kHorizontal; |
| 162 break; |
| 163 case VERTICAL: |
| 164 alignment = views::BoxLayout::kVertical; |
| 165 break; |
| 166 } |
| 167 SetLayoutManager(new views::BoxLayout(alignment, 0, 0, 0)); |
| 168 Layout(); |
| 169 } |
| 170 |
| 128 } // namespace internal | 171 } // namespace internal |
| 129 } // namespace ash | 172 } // namespace ash |
| OLD | NEW |