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.
5 #ifndef UI_GFX_ANDROID_SCROLLER_H_
6 #define UI_GFX_ANDROID_SCROLLER_H_
8 #include "base/time/time.h"
9 #include "ui/gfx/gfx_export.h"
13 // Native port of android.widget.Scroller.
14 // * Change-Id: I4365946f890a76fcfa78ca9d69f2a8e0848095a9
15 // * Please update the Change-Id as upstream Android changes are pulled.
16 class GFX_EXPORT Scroller
{
21 // Controls fling deceleration. Defaults to 0.015f.
24 // Controls fling accumulation. Defaults to disabled.
25 bool flywheel_enabled
;
28 explicit Scroller(const Config
& config
);
31 // Start scrolling by providing a starting point and the distance to travel.
32 // The default value of 250 milliseconds will be used for the duration.
33 void StartScroll(float start_x
,
37 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
,
45 base::TimeTicks start_time
,
46 base::TimeDelta duration
);
48 // Start scrolling based on a fling gesture. The distance travelled will
49 // depend on the initial velocity of the fling.
50 void Fling(float start_x
,
58 base::TimeTicks start_time
);
60 // Call this when you want to know the new location. If it returns true,
61 // the animation is not yet finished.
62 bool ComputeScrollOffset(base::TimeTicks time
);
64 // Extend the scroll animation by |extend|. This allows a running animation
65 // to scroll further and longer when used with |SetFinalX()| or |SetFinalY()|.
66 void ExtendDuration(base::TimeDelta extend
);
67 void SetFinalX(float new_x
);
68 void SetFinalY(float new_y
);
70 // Stops the animation. Contrary to |ForceFinished()|, aborting the animation
71 // causes the scroller to move to the final x and y position.
72 void AbortAnimation();
74 // Terminate the scroll without affecting the current x and y positions.
75 void ForceFinished(bool finished
);
77 // Returns whether the scroller has finished scrolling.
78 bool IsFinished() const;
80 // Returns the time elapsed since the beginning of the scrolling.
81 base::TimeDelta
GetTimePassed() const;
83 // Returns how long the scroll event will take.
84 base::TimeDelta
GetDuration() const;
86 float GetStartX() const;
87 float GetStartY() const;
88 float GetCurrX() const;
89 float GetCurrY() const;
90 float GetCurrVelocity() const;
91 float GetCurrVelocityX() const;
92 float GetCurrVelocityY() const;
93 float GetFinalX() const;
94 float GetFinalY() const;
96 bool IsScrollingInDirection(float xvel
, float yvel
) const;
105 void OnDurationChanged();
106 void RecomputeDeltas();
108 double GetSplineDeceleration(float velocity
) const;
109 base::TimeDelta
GetSplineFlingDuration(float velocity
) const;
110 double GetSplineFlingDistance(float velocity
) const;
126 base::TimeTicks start_time_
;
127 base::TimeTicks curr_time_
;
128 base::TimeDelta duration_
;
129 double duration_seconds_reciprocal_
;
135 bool flywheel_enabled_
;
138 float curr_velocity_
;
141 float fling_friction_
;
148 #endif // UI_GFX_ANDROID_SCROLLER_H_