| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 "cc/animation/scrollbar_animation_controller.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/time/time.h" | |
| 10 #include "cc/trees/layer_tree_impl.h" | |
| 11 | |
| 12 namespace cc { | |
| 13 | |
| 14 ScrollbarAnimationController::ScrollbarAnimationController( | |
| 15 LayerImpl* scroll_layer, | |
| 16 ScrollbarAnimationControllerClient* client, | |
| 17 base::TimeDelta delay_before_starting, | |
| 18 base::TimeDelta resize_delay_before_starting, | |
| 19 base::TimeDelta duration) | |
| 20 : scroll_layer_(scroll_layer), | |
| 21 client_(client), | |
| 22 delay_before_starting_(delay_before_starting), | |
| 23 resize_delay_before_starting_(resize_delay_before_starting), | |
| 24 duration_(duration), | |
| 25 is_animating_(false), | |
| 26 currently_scrolling_(false), | |
| 27 scroll_gesture_has_scrolled_(false), | |
| 28 weak_factory_(this) { | |
| 29 } | |
| 30 | |
| 31 ScrollbarAnimationController::~ScrollbarAnimationController() { | |
| 32 if (is_animating_) | |
| 33 client_->StopAnimatingScrollbarAnimationController(this); | |
| 34 } | |
| 35 | |
| 36 void ScrollbarAnimationController::Animate(base::TimeTicks now) { | |
| 37 if (!is_animating_) | |
| 38 return; | |
| 39 | |
| 40 if (last_awaken_time_.is_null()) | |
| 41 last_awaken_time_ = now; | |
| 42 | |
| 43 float progress = AnimationProgressAtTime(now); | |
| 44 RunAnimationFrame(progress); | |
| 45 } | |
| 46 | |
| 47 float ScrollbarAnimationController::AnimationProgressAtTime( | |
| 48 base::TimeTicks now) { | |
| 49 base::TimeDelta delta = now - last_awaken_time_; | |
| 50 float progress = delta.InSecondsF() / duration_.InSecondsF(); | |
| 51 return std::max(std::min(progress, 1.f), 0.f); | |
| 52 } | |
| 53 | |
| 54 void ScrollbarAnimationController::DidScrollBegin() { | |
| 55 currently_scrolling_ = true; | |
| 56 } | |
| 57 | |
| 58 void ScrollbarAnimationController::DidScrollUpdate(bool on_resize) { | |
| 59 StopAnimation(); | |
| 60 delayed_scrollbar_fade_.Cancel(); | |
| 61 | |
| 62 // As an optimization, we avoid spamming fade delay tasks during active fast | |
| 63 // scrolls. But if we're not within one, we need to post every scroll update. | |
| 64 if (!currently_scrolling_) | |
| 65 PostDelayedAnimationTask(on_resize); | |
| 66 else | |
| 67 scroll_gesture_has_scrolled_ = true; | |
| 68 } | |
| 69 | |
| 70 void ScrollbarAnimationController::DidScrollEnd() { | |
| 71 if (scroll_gesture_has_scrolled_) { | |
| 72 PostDelayedAnimationTask(false); | |
| 73 scroll_gesture_has_scrolled_ = false; | |
| 74 } | |
| 75 | |
| 76 currently_scrolling_ = false; | |
| 77 } | |
| 78 | |
| 79 void ScrollbarAnimationController::PostDelayedAnimationTask(bool on_resize) { | |
| 80 base::TimeDelta delay = | |
| 81 on_resize ? resize_delay_before_starting_ : delay_before_starting_; | |
| 82 delayed_scrollbar_fade_.Reset( | |
| 83 base::Bind(&ScrollbarAnimationController::StartAnimation, | |
| 84 weak_factory_.GetWeakPtr())); | |
| 85 client_->PostDelayedScrollbarAnimationTask(delayed_scrollbar_fade_.callback(), | |
| 86 delay); | |
| 87 } | |
| 88 | |
| 89 void ScrollbarAnimationController::StartAnimation() { | |
| 90 delayed_scrollbar_fade_.Cancel(); | |
| 91 is_animating_ = true; | |
| 92 last_awaken_time_ = base::TimeTicks(); | |
| 93 client_->StartAnimatingScrollbarAnimationController(this); | |
| 94 } | |
| 95 | |
| 96 void ScrollbarAnimationController::StopAnimation() { | |
| 97 is_animating_ = false; | |
| 98 client_->StopAnimatingScrollbarAnimationController(this); | |
| 99 } | |
| 100 | |
| 101 } // namespace cc | |
| OLD | NEW |