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 resize_delay_before_starting
,
17 base::TimeDelta duration
)
19 delay_before_starting_(delay_before_starting
),
20 resize_delay_before_starting_(resize_delay_before_starting
),
23 currently_scrolling_(false),
24 scroll_gesture_has_scrolled_(false),
28 ScrollbarAnimationController::~ScrollbarAnimationController() {
31 void ScrollbarAnimationController::Animate(base::TimeTicks now
) {
35 if (last_awaken_time_
.is_null())
36 last_awaken_time_
= now
;
38 float progress
= AnimationProgressAtTime(now
);
39 RunAnimationFrame(progress
);
42 delayed_scrollbar_fade_
.Cancel();
43 client_
->SetNeedsScrollbarAnimationFrame();
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
);
54 void ScrollbarAnimationController::DidScrollBegin() {
55 currently_scrolling_
= true;
58 void ScrollbarAnimationController::DidScrollUpdate(bool on_resize
) {
60 delayed_scrollbar_fade_
.Cancel();
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 PostDelayedFade(on_resize
);
67 scroll_gesture_has_scrolled_
= true;
70 void ScrollbarAnimationController::DidScrollEnd() {
71 if (scroll_gesture_has_scrolled_
) {
72 PostDelayedFade(false);
73 scroll_gesture_has_scrolled_
= false;
76 currently_scrolling_
= false;
79 void ScrollbarAnimationController::PostDelayedFade(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_
->PostDelayedScrollbarFade(delayed_scrollbar_fade_
.callback(), delay
);
88 void ScrollbarAnimationController::StartAnimation() {
89 delayed_scrollbar_fade_
.Cancel();
91 last_awaken_time_
= base::TimeTicks();
92 client_
->SetNeedsScrollbarAnimationFrame();
95 void ScrollbarAnimationController::StopAnimation() {
96 is_animating_
= false;