OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/shelf/shelf_button.h" | 5 #include "ash/shelf/shelf_button.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "ash/common/ash_constants.h" | 9 #include "ash/common/ash_constants.h" |
10 #include "ash/common/ash_switches.h" | 10 #include "ash/common/ash_switches.h" |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 | 78 |
79 protected: | 79 protected: |
80 virtual ~Observer() {} | 80 virtual ~Observer() {} |
81 }; | 81 }; |
82 | 82 |
83 static ShelfButtonAnimation* GetInstance() { | 83 static ShelfButtonAnimation* GetInstance() { |
84 static ShelfButtonAnimation* s_instance = new ShelfButtonAnimation(); | 84 static ShelfButtonAnimation* s_instance = new ShelfButtonAnimation(); |
85 return s_instance; | 85 return s_instance; |
86 } | 86 } |
87 | 87 |
88 void AddObserver(Observer* observer) { | 88 void AddObserver(Observer* observer) { observers_.AddObserver(observer); } |
89 observers_.AddObserver(observer); | |
90 } | |
91 | 89 |
92 void RemoveObserver(Observer* observer) { | 90 void RemoveObserver(Observer* observer) { |
93 observers_.RemoveObserver(observer); | 91 observers_.RemoveObserver(observer); |
94 if (!observers_.might_have_observers()) | 92 if (!observers_.might_have_observers()) |
95 animation_.Stop(); | 93 animation_.Stop(); |
96 } | 94 } |
97 | 95 |
98 int GetAlpha() { | 96 int GetAlpha() { return GetThrobAnimation().CurrentValueBetween(0, 255); } |
99 return GetThrobAnimation().CurrentValueBetween(0, 255); | |
100 } | |
101 | 97 |
102 double GetAnimation() { | 98 double GetAnimation() { return GetThrobAnimation().GetCurrentValue(); } |
103 return GetThrobAnimation().GetCurrentValue(); | |
104 } | |
105 | 99 |
106 private: | 100 private: |
107 ShelfButtonAnimation() | 101 ShelfButtonAnimation() : animation_(this) { |
108 : animation_(this) { | |
109 animation_.SetThrobDuration(kAttentionThrobDurationMS); | 102 animation_.SetThrobDuration(kAttentionThrobDurationMS); |
110 animation_.SetTweenType(gfx::Tween::SMOOTH_IN_OUT); | 103 animation_.SetTweenType(gfx::Tween::SMOOTH_IN_OUT); |
111 } | 104 } |
112 | 105 |
113 ~ShelfButtonAnimation() override {} | 106 ~ShelfButtonAnimation() override {} |
114 | 107 |
115 gfx::ThrobAnimation& GetThrobAnimation() { | 108 gfx::ThrobAnimation& GetThrobAnimation() { |
116 if (!animation_.is_animating()) { | 109 if (!animation_.is_animating()) { |
117 animation_.Reset(); | 110 animation_.Reset(); |
118 animation_.StartThrobbing(-1 /*throb indefinitely*/); | 111 animation_.StartThrobbing(-1 /*throb indefinitely*/); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 void SetBarBoundsRect(const gfx::Rect& bounds) { | 174 void SetBarBoundsRect(const gfx::Rect& bounds) { |
182 base_bounds_ = bounds; | 175 base_bounds_ = bounds; |
183 UpdateBounds(); | 176 UpdateBounds(); |
184 } | 177 } |
185 | 178 |
186 void ShowAttention(bool show) { | 179 void ShowAttention(bool show) { |
187 if (show_attention_ != show) { | 180 if (show_attention_ != show) { |
188 show_attention_ = show; | 181 show_attention_ = show; |
189 if (show_attention_) { | 182 if (show_attention_) { |
190 animating_ = true; | 183 animating_ = true; |
191 animation_end_time_ = base::TimeTicks::Now() + | 184 animation_end_time_ = |
| 185 base::TimeTicks::Now() + |
192 base::TimeDelta::FromSeconds(kMaxAnimationSeconds); | 186 base::TimeDelta::FromSeconds(kMaxAnimationSeconds); |
193 ShelfButtonAnimation::GetInstance()->AddObserver(this); | 187 ShelfButtonAnimation::GetInstance()->AddObserver(this); |
194 } else { | 188 } else { |
195 animating_ = false; | 189 animating_ = false; |
196 ShelfButtonAnimation::GetInstance()->RemoveObserver(this); | 190 ShelfButtonAnimation::GetInstance()->RemoveObserver(this); |
197 } | 191 } |
198 } | 192 } |
199 UpdateBounds(); | 193 UpdateBounds(); |
200 } | 194 } |
201 | 195 |
202 private: | 196 private: |
203 void UpdateBounds() { | 197 void UpdateBounds() { |
204 gfx::Rect bounds = base_bounds_; | 198 gfx::Rect bounds = base_bounds_; |
205 if (show_attention_) { | 199 if (show_attention_) { |
206 // Scale from .35 to 1.0 of the total width (which is wider than the | 200 // Scale from .35 to 1.0 of the total width (which is wider than the |
207 // visible width of the image), so the animation "rests" briefly at full | 201 // visible width of the image), so the animation "rests" briefly at full |
208 // visible width. Cap bounds length at kIconSize to prevent visual | 202 // visible width. Cap bounds length at kIconSize to prevent visual |
209 // flutter while centering bar within further expanding bounds. | 203 // flutter while centering bar within further expanding bounds. |
210 double animation = animating_ ? | 204 double animation = |
211 ShelfButtonAnimation::GetInstance()->GetAnimation() : 1.0; | 205 animating_ ? ShelfButtonAnimation::GetInstance()->GetAnimation() |
| 206 : 1.0; |
212 double scale = .35 + .65 * animation; | 207 double scale = .35 + .65 * animation; |
213 if (shelf_->IsHorizontalAlignment()) { | 208 if (shelf_->IsHorizontalAlignment()) { |
214 int width = base_bounds_.width() * scale; | 209 int width = base_bounds_.width() * scale; |
215 bounds.set_width(std::min(width, kIconSize)); | 210 bounds.set_width(std::min(width, kIconSize)); |
216 int x_offset = (base_bounds_.width() - bounds.width()) / 2; | 211 int x_offset = (base_bounds_.width() - bounds.width()) / 2; |
217 bounds.set_x(base_bounds_.x() + x_offset); | 212 bounds.set_x(base_bounds_.x() + x_offset); |
218 UpdateAnimating(bounds.width() == kIconSize); | 213 UpdateAnimating(bounds.width() == kIconSize); |
219 } else { | 214 } else { |
220 int height = base_bounds_.height() * scale; | 215 int height = base_bounds_.height() * scale; |
221 bounds.set_height(std::min(height, kIconSize)); | 216 bounds.set_height(std::min(height, kIconSize)); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 if (width > kIconSize) { | 305 if (width > kIconSize) { |
311 width = kIconSize; | 306 width = kIconSize; |
312 height = static_cast<int>(width / aspect_ratio); | 307 height = static_cast<int>(width / aspect_ratio); |
313 } | 308 } |
314 | 309 |
315 if (width == image.width() && height == image.height()) { | 310 if (width == image.width() && height == image.height()) { |
316 SetShadowedImage(image); | 311 SetShadowedImage(image); |
317 return; | 312 return; |
318 } | 313 } |
319 | 314 |
320 SetShadowedImage(gfx::ImageSkiaOperations::CreateResizedImage(image, | 315 SetShadowedImage(gfx::ImageSkiaOperations::CreateResizedImage( |
321 skia::ImageOperations::RESIZE_BEST, gfx::Size(width, height))); | 316 image, skia::ImageOperations::RESIZE_BEST, gfx::Size(width, height))); |
322 } | 317 } |
323 | 318 |
324 const gfx::ImageSkia& ShelfButton::GetImage() const { | 319 const gfx::ImageSkia& ShelfButton::GetImage() const { |
325 return icon_view_->GetImage(); | 320 return icon_view_->GetImage(); |
326 } | 321 } |
327 | 322 |
328 void ShelfButton::AddState(State state) { | 323 void ShelfButton::AddState(State state) { |
329 if (!(state_ & state)) { | 324 if (!(state_ & state)) { |
330 state_ |= state; | 325 state_ |= state; |
331 Layout(); | 326 Layout(); |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
577 views::ImageView::TRAILING)); | 572 views::ImageView::TRAILING)); |
578 bar_->SetVerticalAlignment(shelf->SelectValueForShelfAlignment( | 573 bar_->SetVerticalAlignment(shelf->SelectValueForShelfAlignment( |
579 views::ImageView::TRAILING, views::ImageView::CENTER, | 574 views::ImageView::TRAILING, views::ImageView::CENTER, |
580 views::ImageView::CENTER)); | 575 views::ImageView::CENTER)); |
581 bar_->SchedulePaint(); | 576 bar_->SchedulePaint(); |
582 } | 577 } |
583 bar_->SetVisible(bar_id != 0 && state_ != STATE_NORMAL); | 578 bar_->SetVisible(bar_id != 0 && state_ != STATE_NORMAL); |
584 } | 579 } |
585 | 580 |
586 } // namespace ash | 581 } // namespace ash |
OLD | NEW |