Home | History | Annotate | Download | only in animation
      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 
     10 namespace cc {
     11 
     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));
     18 }
     19 
     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) {}
     29 
     30 ScrollbarAnimationControllerLinearFade::
     31     ~ScrollbarAnimationControllerLinearFade() {}
     32 
     33 bool ScrollbarAnimationControllerLinearFade::IsScrollGestureInProgress() const {
     34   return scroll_gesture_in_progress_;
     35 }
     36 
     37 bool ScrollbarAnimationControllerLinearFade::IsAnimating() const {
     38   return !last_awaken_time_.is_null();
     39 }
     40 
     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_);
     46 }
     47 
     48 bool ScrollbarAnimationControllerLinearFade::Animate(base::TimeTicks now) {
     49   float opacity = OpacityAtTime(now);
     50   scroll_layer_->SetScrollbarOpacity(opacity);
     51   if (!opacity)
     52     last_awaken_time_ = base::TimeTicks();
     53   return IsAnimating() && DelayBeforeStart(now) == base::TimeDelta();
     54 }
     55 
     56 void ScrollbarAnimationControllerLinearFade::DidScrollGestureBegin() {
     57   scroll_layer_->SetScrollbarOpacity(1.0f);
     58   scroll_gesture_in_progress_ = true;
     59   last_awaken_time_ = base::TimeTicks();
     60 }
     61 
     62 void ScrollbarAnimationControllerLinearFade::DidScrollGestureEnd(
     63     base::TimeTicks now) {
     64   scroll_gesture_in_progress_ = false;
     65   last_awaken_time_ = now;
     66 }
     67 
     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;
     74 }
     75 
     76 float ScrollbarAnimationControllerLinearFade::OpacityAtTime(
     77     base::TimeTicks now) {
     78   if (scroll_gesture_in_progress_)
     79     return 1.0f;
     80 
     81   if (last_awaken_time_.is_null())
     82     return 0.0f;
     83 
     84   base::TimeDelta delta = now - last_awaken_time_;
     85 
     86   if (delta <= fadeout_delay_)
     87     return 1.0f;
     88   if (delta < fadeout_delay_ + fadeout_length_) {
     89     return (fadeout_delay_ + fadeout_length_ - delta).InSecondsF() /
     90            fadeout_length_.InSecondsF();
     91   }
     92   return 0.0f;
     93 }
     94 
     95 }  // namespace cc
     96