1 // Copyright (c) 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 #ifndef UI_VIEWS_ANIMATION_SCROLL_ANIMATOR_H_
6 #define UI_VIEWS_ANIMATION_SCROLL_ANIMATOR_H_
7 
8 #include <memory>
9 
10 #include "base/macros.h"
11 #include "ui/gfx/animation/animation_delegate.h"
12 #include "ui/views/views_export.h"
13 
14 namespace gfx {
15 class SlideAnimation;
16 }
17 
18 namespace views {
19 
20 class VIEWS_EXPORT ScrollDelegate {
21  public:
22   // Returns true if the content was actually scrolled, false otherwise.
23   virtual bool OnScroll(float dx, float dy) = 0;
24 
25  protected:
26   ~ScrollDelegate() = default;
27 };
28 
29 class VIEWS_EXPORT ScrollAnimator : public gfx::AnimationDelegate {
30  public:
31   // The ScrollAnimator does not own the delegate. Uses default acceleration.
32   explicit ScrollAnimator(ScrollDelegate* delegate);
33   ~ScrollAnimator() override;
34 
35   // Use this if you would prefer different acceleration than the default.
set_acceleration(float acceleration)36   void set_acceleration(float acceleration) { acceleration_ = acceleration; }
37 
38   void Start(float velocity_x, float velocity_y);
39   void Stop();
40 
is_scrolling()41   bool is_scrolling() const { return !!animation_.get(); }
42 
43  private:
44   // Implementation of gfx::AnimationDelegate.
45   void AnimationEnded(const gfx::Animation* animation) override;
46   void AnimationProgressed(const gfx::Animation* animation) override;
47   void AnimationCanceled(const gfx::Animation* animation) override;
48 
49   ScrollDelegate* delegate_;
50 
51   float velocity_x_;
52   float velocity_y_;
53   float last_t_;
54   float duration_;
55   float acceleration_;
56 
57   std::unique_ptr<gfx::SlideAnimation> animation_;
58 
59   DISALLOW_COPY_AND_ASSIGN(ScrollAnimator);
60 };
61 
62 }  // namespace views
63 
64 #endif  // UI_VIEWS_ANIMATION_SCROLL_ANIMATOR_H_
65