1 // Copyright 2012 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_linear_fade.h"
8 #include "cc/layers/layer_impl.h"
12 scoped_ptr
<ScrollbarAnimationControllerLinearFade
>
13 ScrollbarAnimationControllerLinearFade::Create(LayerImpl
* scroll_layer
,
14 base::TimeDelta fadeout_delay
,
15 base::TimeDelta fadeout_length
) {
16 return make_scoped_ptr(new ScrollbarAnimationControllerLinearFade(
17 scroll_layer
, fadeout_delay
, fadeout_length
));
20 ScrollbarAnimationControllerLinearFade::ScrollbarAnimationControllerLinearFade(
21 LayerImpl
* scroll_layer
,
22 base::TimeDelta fadeout_delay
,
23 base::TimeDelta fadeout_length
)
24 : ScrollbarAnimationController(),
25 scroll_layer_(scroll_layer
),
26 scroll_gesture_in_progress_(false),
27 fadeout_delay_(fadeout_delay
),
28 fadeout_length_(fadeout_length
) {}
30 ScrollbarAnimationControllerLinearFade::
31 ~ScrollbarAnimationControllerLinearFade() {}
33 bool ScrollbarAnimationControllerLinearFade::IsScrollGestureInProgress() const {
34 return scroll_gesture_in_progress_
;
37 bool ScrollbarAnimationControllerLinearFade::IsAnimating() const {
38 return !last_awaken_time_
.is_null();
41 base::TimeDelta
ScrollbarAnimationControllerLinearFade::DelayBeforeStart(
42 base::TimeTicks now
) const {
43 if (now
> last_awaken_time_
+ fadeout_delay_
)
44 return base::TimeDelta();
45 return fadeout_delay_
- (now
- last_awaken_time_
);
48 bool ScrollbarAnimationControllerLinearFade::Animate(base::TimeTicks now
) {
49 float opacity
= OpacityAtTime(now
);
50 scroll_layer_
->SetScrollbarOpacity(opacity
);
52 last_awaken_time_
= base::TimeTicks();
53 return IsAnimating() && DelayBeforeStart(now
) == base::TimeDelta();
56 void ScrollbarAnimationControllerLinearFade::DidScrollGestureBegin() {
57 scroll_layer_
->SetScrollbarOpacity(1.0f
);
58 scroll_gesture_in_progress_
= true;
59 last_awaken_time_
= base::TimeTicks();
62 void ScrollbarAnimationControllerLinearFade::DidScrollGestureEnd(
63 base::TimeTicks now
) {
64 scroll_gesture_in_progress_
= false;
65 last_awaken_time_
= now
;
68 void ScrollbarAnimationControllerLinearFade::DidProgrammaticallyUpdateScroll(
69 base::TimeTicks now
) {
70 // Don't set scroll_gesture_in_progress_ as this scroll is not from a gesture
71 // and we won't receive ScrollEnd.
72 scroll_layer_
->SetScrollbarOpacity(1.0f
);
73 last_awaken_time_
= now
;
76 float ScrollbarAnimationControllerLinearFade::OpacityAtTime(
77 base::TimeTicks now
) {
78 if (scroll_gesture_in_progress_
)
81 if (last_awaken_time_
.is_null())
84 base::TimeDelta delta
= now
- last_awaken_time_
;
86 if (delta
<= fadeout_delay_
)
88 if (delta
< fadeout_delay_
+ fadeout_length_
) {
89 return (fadeout_delay_
+ fadeout_length_
- delta
).InSecondsF() /
90 fadeout_length_
.InSecondsF();