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.
5 #include "cc/animation/scrollbar_animation_controller.h"
9 #include "base/time/time.h"
13 ScrollbarAnimationController::ScrollbarAnimationController(
14 ScrollbarAnimationControllerClient
* client
,
15 base::TimeDelta delay_before_starting
,
16 base::TimeDelta duration
)
18 delay_before_starting_(delay_before_starting
),
21 currently_scrolling_(false),
22 scroll_gesture_has_scrolled_(false),
26 ScrollbarAnimationController::~ScrollbarAnimationController() {
29 void ScrollbarAnimationController::Animate(base::TimeTicks now
) {
33 if (last_awaken_time_
.is_null())
34 last_awaken_time_
= now
;
36 float progress
= AnimationProgressAtTime(now
);
37 RunAnimationFrame(progress
);
40 delayed_scrollbar_fade_
.Cancel();
41 client_
->SetNeedsScrollbarAnimationFrame();
45 float ScrollbarAnimationController::AnimationProgressAtTime(
46 base::TimeTicks now
) {
47 base::TimeDelta delta
= now
- last_awaken_time_
;
48 float progress
= delta
.InSecondsF() / duration_
.InSecondsF();
49 return std::max(std::min(progress
, 1.f
), 0.f
);
52 void ScrollbarAnimationController::DidScrollBegin() {
53 currently_scrolling_
= true;
56 void ScrollbarAnimationController::DidScrollUpdate() {
58 delayed_scrollbar_fade_
.Cancel();
60 // As an optimization, we avoid spamming fade delay tasks during active fast
61 // scrolls. But if we're not within one, we need to post every scroll update.
62 if (!currently_scrolling_
)
65 scroll_gesture_has_scrolled_
= true;
68 void ScrollbarAnimationController::DidScrollEnd() {
69 if (scroll_gesture_has_scrolled_
) {
71 scroll_gesture_has_scrolled_
= false;
74 currently_scrolling_
= false;
77 void ScrollbarAnimationController::PostDelayedFade() {
78 delayed_scrollbar_fade_
.Reset(
79 base::Bind(&ScrollbarAnimationController::StartAnimation
,
80 weak_factory_
.GetWeakPtr()));
81 client_
->PostDelayedScrollbarFade(delayed_scrollbar_fade_
.callback(),
82 delay_before_starting_
);
85 void ScrollbarAnimationController::StartAnimation() {
86 delayed_scrollbar_fade_
.Cancel();
88 last_awaken_time_
= base::TimeTicks();
89 client_
->SetNeedsScrollbarAnimationFrame();
92 void ScrollbarAnimationController::StopAnimation() {
93 is_animating_
= false;