scroll_animator.h 2.1 KB

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