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/common/system/tray/tray_item_view.h" | |
6 | |
7 #include "ash/common/material_design/material_design_controller.h" | |
8 #include "ash/common/shelf/wm_shelf_util.h" | |
9 #include "ash/common/system/tray/system_tray.h" | |
10 #include "ash/common/system/tray/system_tray_item.h" | |
11 #include "ash/common/system/tray/tray_constants.h" | |
12 #include "ash/public/cpp/shelf_types.h" | |
13 #include "ui/compositor/layer.h" | |
14 #include "ui/gfx/animation/slide_animation.h" | |
15 #include "ui/views/controls/image_view.h" | |
16 #include "ui/views/controls/label.h" | |
17 #include "ui/views/layout/fill_layout.h" | |
18 #include "ui/views/widget/widget.h" | |
19 | |
20 namespace { | |
21 const int kTrayIconHeight = 29; | |
22 const int kTrayIconWidth = 29; | |
23 const int kTrayItemAnimationDurationMS = 200; | |
24 | |
25 // Animations can be disabled for testing. | |
26 bool animations_enabled = true; | |
27 } | |
28 | |
29 namespace ash { | |
30 | |
31 TrayItemView::TrayItemView(SystemTrayItem* owner) | |
32 : owner_(owner), label_(NULL), image_view_(NULL) { | |
33 SetPaintToLayer(); | |
34 layer()->SetFillsBoundsOpaquely(false); | |
35 SetLayoutManager(new views::FillLayout()); | |
36 } | |
37 | |
38 TrayItemView::~TrayItemView() {} | |
39 | |
40 // static | |
41 void TrayItemView::DisableAnimationsForTest() { | |
42 animations_enabled = false; | |
43 } | |
44 | |
45 void TrayItemView::CreateLabel() { | |
46 label_ = new views::Label; | |
47 AddChildView(label_); | |
48 PreferredSizeChanged(); | |
49 } | |
50 | |
51 void TrayItemView::CreateImageView() { | |
52 image_view_ = new views::ImageView; | |
53 AddChildView(image_view_); | |
54 PreferredSizeChanged(); | |
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 // static | |
81 bool TrayItemView::UseMd() { | |
82 return MaterialDesignController::UseMaterialDesignSystemIcons(); | |
83 } | |
84 | |
85 int TrayItemView::GetAnimationDurationMS() { | |
86 return kTrayItemAnimationDurationMS; | |
87 } | |
88 | |
89 gfx::Size TrayItemView::GetPreferredSize() const { | |
90 DCHECK_EQ(1, child_count()); | |
91 gfx::Size size; | |
92 if (UseMd()) { | |
93 gfx::Size inner_size = views::View::GetPreferredSize(); | |
94 if (image_view_) | |
95 inner_size = gfx::Size(kTrayIconSize, kTrayIconSize); | |
96 gfx::Rect rect(inner_size); | |
97 rect.Inset(gfx::Insets(-kTrayImageItemPadding)); | |
98 size = rect.size(); | |
99 } else { | |
100 size = views::View::GetPreferredSize(); | |
101 if (IsHorizontalAlignment(owner()->system_tray()->shelf_alignment())) | |
102 size.set_height(kTrayIconHeight); | |
103 else | |
104 size.set_width(kTrayIconWidth); | |
105 } | |
106 if (!animation_.get() || !animation_->is_animating()) | |
107 return size; | |
108 if (IsHorizontalAlignment(owner()->system_tray()->shelf_alignment())) { | |
109 size.set_width(std::max( | |
110 1, static_cast<int>(size.width() * animation_->GetCurrentValue()))); | |
111 } else { | |
112 size.set_height(std::max( | |
113 1, static_cast<int>(size.height() * animation_->GetCurrentValue()))); | |
114 } | |
115 return size; | |
116 } | |
117 | |
118 int TrayItemView::GetHeightForWidth(int width) const { | |
119 return GetPreferredSize().height(); | |
120 } | |
121 | |
122 void TrayItemView::ChildPreferredSizeChanged(views::View* child) { | |
123 PreferredSizeChanged(); | |
124 } | |
125 | |
126 void TrayItemView::AnimationProgressed(const gfx::Animation* animation) { | |
127 gfx::Transform transform; | |
128 if (IsHorizontalAlignment(owner()->system_tray()->shelf_alignment())) { | |
129 transform.Translate(0, animation->CurrentValueBetween( | |
130 static_cast<double>(height()) / 2, 0.)); | |
131 } else { | |
132 transform.Translate( | |
133 animation->CurrentValueBetween(static_cast<double>(width() / 2), 0.), | |
134 0); | |
135 } | |
136 transform.Scale(animation->GetCurrentValue(), animation->GetCurrentValue()); | |
137 layer()->SetTransform(transform); | |
138 PreferredSizeChanged(); | |
139 } | |
140 | |
141 void TrayItemView::AnimationEnded(const gfx::Animation* animation) { | |
142 if (animation->GetCurrentValue() < 0.1) | |
143 views::View::SetVisible(false); | |
144 } | |
145 | |
146 void TrayItemView::AnimationCanceled(const gfx::Animation* animation) { | |
147 AnimationEnded(animation); | |
148 } | |
149 | |
150 } // namespace ash | |
OLD | NEW |