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. 4 5 #include "cc/animation/scrollbar_animation_controller_linear_fade.h" 6 7 #include "base/time/time.h" 8 #include "cc/layers/layer_impl.h" 9 #include "cc/layers/scrollbar_layer_impl_base.h" 10 11 namespace cc { 12 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)); 19 } 20 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) {} 31 32 ScrollbarAnimationControllerLinearFade:: 33 ~ScrollbarAnimationControllerLinearFade() {} 34 35 bool ScrollbarAnimationControllerLinearFade::IsAnimating() const { 36 return !last_awaken_time_.is_null(); 37 } 38 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_); 44 } 45 46 bool ScrollbarAnimationControllerLinearFade::Animate(base::TimeTicks now) { 47 float opacity = OpacityAtTime(now); 48 ApplyOpacityToScrollbars(opacity); 49 if (!opacity) 50 last_awaken_time_ = base::TimeTicks(); 51 return IsAnimating() && DelayBeforeStart(now) == base::TimeDelta(); 52 } 53 54 void ScrollbarAnimationControllerLinearFade::DidScrollGestureBegin() { 55 scroll_gesture_in_progress_ = true; 56 scroll_gesture_has_scrolled_ = false; 57 } 58 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; 66 } 67 68 void ScrollbarAnimationControllerLinearFade::DidMouseMoveOffScrollbar( 69 base::TimeTicks now) { 70 // Ignore mouse move events. 71 } 72 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; 81 return false; 82 } 83 84 last_awaken_time_ = now; 85 return true; 86 } 87 88 bool ScrollbarAnimationControllerLinearFade::DidMouseMoveNear( 89 base::TimeTicks now, float distance) { 90 // Ignore mouse move events. 91 return false; 92 } 93 94 float ScrollbarAnimationControllerLinearFade::OpacityAtTime( 95 base::TimeTicks now) { 96 if (scroll_gesture_has_scrolled_) 97 return 1.0f; 98 99 if (last_awaken_time_.is_null()) 100 return 0.0f; 101 102 base::TimeDelta delta = now - last_awaken_time_; 103 104 if (delta <= fadeout_delay_) 105 return 1.0f; 106 if (delta < fadeout_delay_ + fadeout_length_) { 107 return (fadeout_delay_ + fadeout_length_ - delta).InSecondsF() / 108 fadeout_length_.InSecondsF(); 109 } 110 return 0.0f; 111 } 112 113 void ScrollbarAnimationControllerLinearFade::ApplyOpacityToScrollbars( 114 float opacity) { 115 ScrollbarLayerImplBase* horizontal_scrollbar = 116 scroll_layer_->horizontal_scrollbar_layer(); 117 if (horizontal_scrollbar) 118 horizontal_scrollbar->SetOpacity(opacity); 119 120 ScrollbarLayerImplBase* vertical_scrollbar = 121 scroll_layer_->vertical_scrollbar_layer(); 122 if (vertical_scrollbar) 123 vertical_scrollbar->SetOpacity(opacity); 124 } 125 126 } // namespace cc 127