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"
7 #include "base/time/time.h"
8 #include "cc/layers/layer_impl.h"
9 #include "cc/layers/scrollbar_layer_impl_base.h"
13 scoped_ptr<ScrollbarAnimationControllerLinearFade>
14 ScrollbarAnimationControllerLinearFade::Create(LayerImpl* scroll_layer,
15 base::TimeDelta fadeout_delay,
16 base::TimeDelta fadeout_length) {
17 return make_scoped_ptr(new ScrollbarAnimationControllerLinearFade(
18 scroll_layer, fadeout_delay, fadeout_length));
21 ScrollbarAnimationControllerLinearFade::ScrollbarAnimationControllerLinearFade(
22 LayerImpl* scroll_layer,
23 base::TimeDelta fadeout_delay,
24 base::TimeDelta fadeout_length)
25 : ScrollbarAnimationController(),
26 scroll_layer_(scroll_layer),
27 scroll_gesture_in_progress_(false),
28 scroll_gesture_has_scrolled_(false),
29 fadeout_delay_(fadeout_delay),
30 fadeout_length_(fadeout_length) {}
32 ScrollbarAnimationControllerLinearFade::
33 ~ScrollbarAnimationControllerLinearFade() {}
35 bool ScrollbarAnimationControllerLinearFade::IsAnimating() const {
36 return !last_awaken_time_.is_null();
39 base::TimeDelta ScrollbarAnimationControllerLinearFade::DelayBeforeStart(
40 base::TimeTicks now) const {
41 if (now > last_awaken_time_ + fadeout_delay_)
42 return base::TimeDelta();
43 return fadeout_delay_ - (now - last_awaken_time_);
46 bool ScrollbarAnimationControllerLinearFade::Animate(base::TimeTicks now) {
47 float opacity = OpacityAtTime(now);
48 ApplyOpacityToScrollbars(opacity);
50 last_awaken_time_ = base::TimeTicks();
51 return IsAnimating() && DelayBeforeStart(now) == base::TimeDelta();
54 void ScrollbarAnimationControllerLinearFade::DidScrollGestureBegin() {
55 scroll_gesture_in_progress_ = true;
56 scroll_gesture_has_scrolled_ = false;
59 void ScrollbarAnimationControllerLinearFade::DidScrollGestureEnd(
60 base::TimeTicks now) {
61 // The animation should not be triggered if no scrolling has occurred.
62 if (scroll_gesture_has_scrolled_)
63 last_awaken_time_ = now;
64 scroll_gesture_has_scrolled_ = false;
65 scroll_gesture_in_progress_ = false;
68 void ScrollbarAnimationControllerLinearFade::DidMouseMoveOffScrollbar(
69 base::TimeTicks now) {
70 // Ignore mouse move events.
73 bool ScrollbarAnimationControllerLinearFade::DidScrollUpdate(
74 base::TimeTicks now) {
75 ApplyOpacityToScrollbars(1.0f);
76 // The animation should only be activated if the scroll updated occurred
77 // programatically, outside the scope of a scroll gesture.
78 if (scroll_gesture_in_progress_) {
79 last_awaken_time_ = base::TimeTicks();
80 scroll_gesture_has_scrolled_ = true;
84 last_awaken_time_ = now;
88 bool ScrollbarAnimationControllerLinearFade::DidMouseMoveNear(
89 base::TimeTicks now, float distance) {
90 // Ignore mouse move events.
94 float ScrollbarAnimationControllerLinearFade::OpacityAtTime(
95 base::TimeTicks now) {
96 if (scroll_gesture_has_scrolled_)
99 if (last_awaken_time_.is_null())
102 base::TimeDelta delta = now - last_awaken_time_;
104 if (delta <= fadeout_delay_)
106 if (delta < fadeout_delay_ + fadeout_length_) {
107 return (fadeout_delay_ + fadeout_length_ - delta).InSecondsF() /
108 fadeout_length_.InSecondsF();
113 void ScrollbarAnimationControllerLinearFade::ApplyOpacityToScrollbars(
115 if (!scroll_layer_->scrollbars())
118 LayerImpl::ScrollbarSet* scrollbars = scroll_layer_->scrollbars();
119 for (LayerImpl::ScrollbarSet::iterator it = scrollbars->begin();
120 it != scrollbars->end();
122 ScrollbarLayerImplBase* scrollbar = *it;
123 if (scrollbar->is_overlay_scrollbar())
124 scrollbar->SetOpacity(opacity);