| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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 "ui/views/controls/animated_icon_view.h" |
| 6 |
| 7 #include "ui/compositor/compositor.h" |
| 8 #include "ui/gfx/canvas.h" |
| 9 #include "ui/gfx/color_palette.h" |
| 10 #include "ui/gfx/paint_vector_icon.h" |
| 11 #include "ui/views/widget/widget.h" |
| 12 |
| 13 namespace views { |
| 14 |
| 15 AnimatedIconView::AnimatedIconView(const gfx::VectorIcon& icon) |
| 16 : icon_(icon), |
| 17 color_(gfx::kPlaceholderColor), |
| 18 duration_(gfx::GetDurationOfAnimation(icon)) { |
| 19 UpdateStaticImage(); |
| 20 } |
| 21 |
| 22 AnimatedIconView::~AnimatedIconView() {} |
| 23 |
| 24 void AnimatedIconView::Animate(State target) { |
| 25 SetState(target); |
| 26 if (!IsAnimating()) |
| 27 GetWidget()->GetCompositor()->AddAnimationObserver(this); |
| 28 start_time_ = base::TimeTicks::Now(); |
| 29 } |
| 30 |
| 31 void AnimatedIconView::SetState(State state) { |
| 32 state_ = state; |
| 33 UpdateStaticImage(); |
| 34 } |
| 35 |
| 36 void AnimatedIconView::OnPaint(gfx::Canvas* canvas) { |
| 37 if (!IsAnimating()) { |
| 38 views::ImageView::OnPaint(canvas); |
| 39 return; |
| 40 } |
| 41 |
| 42 auto timestamp = base::TimeTicks::Now(); |
| 43 base::TimeDelta elapsed = timestamp - start_time_; |
| 44 if (state_ == END) |
| 45 elapsed = start_time_ + duration_ - timestamp; |
| 46 |
| 47 canvas->Translate(GetImageBounds().OffsetFromOrigin()); |
| 48 gfx::PaintVectorIcon(canvas, icon_, color_, elapsed); |
| 49 } |
| 50 |
| 51 void AnimatedIconView::OnAnimationStep(base::TimeTicks timestamp) { |
| 52 base::TimeDelta elapsed = timestamp - start_time_; |
| 53 if (elapsed > duration_) { |
| 54 GetWidget()->GetCompositor()->RemoveAnimationObserver(this); |
| 55 start_time_ = base::TimeTicks(); |
| 56 } |
| 57 |
| 58 SchedulePaint(); |
| 59 } |
| 60 |
| 61 void AnimatedIconView::OnCompositingShuttingDown(ui::Compositor* compositor) {} |
| 62 |
| 63 bool AnimatedIconView::IsAnimating() { |
| 64 return start_time_ != base::TimeTicks(); |
| 65 } |
| 66 |
| 67 void AnimatedIconView::UpdateStaticImage() { |
| 68 gfx::IconDescription description( |
| 69 icon_, 0, color_, state_ == START ? base::TimeDelta() : duration_, |
| 70 gfx::kNoneIcon); |
| 71 SetImage(gfx::CreateVectorIcon(description)); |
| 72 } |
| 73 |
| 74 } // namespace views |
| OLD | NEW |