mobile_scroller.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2014 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_EVENTS_MOBILE_SCROLLER_H_
  5. #define UI_EVENTS_MOBILE_SCROLLER_H_
  6. #include "base/time/time.h"
  7. #include "ui/events/events_base_export.h"
  8. #include "ui/events/gesture_curve.h"
  9. #include "ui/gfx/geometry/vector2d_f.h"
  10. namespace ui {
  11. // Native port of android.widget.Scroller.
  12. // * Change-Id: I4365946f890a76fcfa78ca9d69f2a8e0848095a9
  13. // * Please update the Change-Id as upstream Android changes are pulled.
  14. class EVENTS_BASE_EXPORT MobileScroller : public GestureCurve {
  15. public:
  16. struct EVENTS_BASE_EXPORT Config {
  17. Config();
  18. // Controls fling deceleration. Defaults to 0.015f.
  19. float fling_friction;
  20. // Controls fling accumulation. Defaults to disabled.
  21. bool flywheel_enabled;
  22. // Controls whether to use chromecast optimized
  23. // scrolling. Defaults to false, mimic normal Android scrolling.
  24. bool chromecast_optimized;
  25. };
  26. explicit MobileScroller(const Config& config);
  27. ~MobileScroller() override;
  28. // GestureCurve implementation.
  29. bool ComputeScrollOffset(base::TimeTicks time,
  30. gfx::Vector2dF* offset,
  31. gfx::Vector2dF* velocity) override;
  32. // Start scrolling by providing a starting point and the distance to travel.
  33. // The default value of 250 milliseconds will be used for the duration.
  34. void StartScroll(float start_x,
  35. float start_y,
  36. float dx,
  37. float dy,
  38. base::TimeTicks start_time);
  39. // Start scrolling by providing a starting point, the distance to travel,
  40. // and the duration of the scroll.
  41. void StartScroll(float start_x,
  42. float start_y,
  43. float dx,
  44. float dy,
  45. base::TimeTicks start_time,
  46. base::TimeDelta duration);
  47. // Start scrolling based on a fling gesture. The distance travelled will
  48. // depend on the initial velocity of the fling.
  49. void Fling(float start_x,
  50. float start_y,
  51. float velocity_x,
  52. float velocity_y,
  53. float min_x,
  54. float max_x,
  55. float min_y,
  56. float max_y,
  57. base::TimeTicks start_time);
  58. // Extend the scroll animation by |extend|. This allows a running animation
  59. // to scroll further and longer when used with |SetFinalX()| or |SetFinalY()|.
  60. void ExtendDuration(base::TimeDelta extend);
  61. void SetFinalX(float new_x);
  62. void SetFinalY(float new_y);
  63. // Stops the animation. Contrary to |ForceFinished()|, aborting the animation
  64. // causes the scroller to move to the final x and y position.
  65. void AbortAnimation();
  66. // Terminate the scroll without affecting the current x and y positions.
  67. void ForceFinished(bool finished);
  68. // Returns whether the scroller has finished scrolling.
  69. bool IsFinished() const;
  70. // Returns the time elapsed since the beginning of the scrolling.
  71. base::TimeDelta GetTimePassed() const;
  72. // Returns how long the scroll event will take.
  73. base::TimeDelta GetDuration() const;
  74. float GetStartX() const;
  75. float GetStartY() const;
  76. float GetCurrX() const;
  77. float GetCurrY() const;
  78. float GetCurrVelocity() const;
  79. float GetCurrVelocityX() const;
  80. float GetCurrVelocityY() const;
  81. float GetFinalX() const;
  82. float GetFinalY() const;
  83. bool IsScrollingInDirection(float xvel, float yvel) const;
  84. private:
  85. enum Mode {
  86. UNDEFINED,
  87. SCROLL_MODE,
  88. FLING_MODE,
  89. };
  90. bool ComputeScrollOffsetInternal(base::TimeTicks time);
  91. void RecomputeDeltas();
  92. double GetSplineDeceleration(float velocity) const;
  93. base::TimeDelta GetSplineFlingDuration(float velocity) const;
  94. double GetSplineFlingDistance(float velocity) const;
  95. Mode mode_;
  96. float start_x_;
  97. float start_y_;
  98. float final_x_;
  99. float final_y_;
  100. float min_x_;
  101. float max_x_;
  102. float min_y_;
  103. float max_y_;
  104. float curr_x_;
  105. float curr_y_;
  106. base::TimeTicks start_time_;
  107. base::TimeTicks curr_time_;
  108. base::TimeDelta duration_;
  109. double duration_seconds_reciprocal_;
  110. float delta_x_;
  111. float delta_x_norm_;
  112. float delta_y_;
  113. float delta_y_norm_;
  114. bool finished_;
  115. bool flywheel_enabled_;
  116. float velocity_;
  117. float curr_velocity_;
  118. float distance_;
  119. float fling_friction_;
  120. float deceleration_;
  121. float tuning_coeff_;
  122. };
  123. } // namespace ui
  124. #endif // UI_EVENTS_MOBILE_SCROLLER_H_