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_EVENTS_ANDROID_SCROLLER_H_
6 #define UI_EVENTS_ANDROID_SCROLLER_H_
8 #include "base/time/time.h"
9 #include "ui/events/events_base_export.h"
10 #include "ui/events/gesture_curve.h"
11 #include "ui/gfx/geometry/vector2d_f.h"
15 // Native port of android.widget.Scroller.
16 // * Change-Id: I4365946f890a76fcfa78ca9d69f2a8e0848095a9
17 // * Please update the Change-Id as upstream Android changes are pulled.
18 class EVENTS_BASE_EXPORT Scroller
: public GestureCurve
{
23 // Controls fling deceleration. Defaults to 0.015f.
26 // Controls fling accumulation. Defaults to disabled.
27 bool flywheel_enabled
;
30 explicit Scroller(const Config
& config
);
33 // GestureCurve implementation.
34 bool ComputeScrollOffset(base::TimeTicks time
,
35 gfx::Vector2dF
* offset
,
36 gfx::Vector2dF
* velocity
) override
;
38 // Start scrolling by providing a starting point and the distance to travel.
39 // The default value of 250 milliseconds will be used for the duration.
40 void StartScroll(float start_x
,
44 base::TimeTicks start_time
);
46 // Start scrolling by providing a starting point, the distance to travel,
47 // and the duration of the scroll.
48 void StartScroll(float start_x
,
52 base::TimeTicks start_time
,
53 base::TimeDelta duration
);
55 // Start scrolling based on a fling gesture. The distance travelled will
56 // depend on the initial velocity of the fling.
57 void Fling(float start_x
,
65 base::TimeTicks start_time
);
67 // Extend the scroll animation by |extend|. This allows a running animation
68 // to scroll further and longer when used with |SetFinalX()| or |SetFinalY()|.
69 void ExtendDuration(base::TimeDelta extend
);
70 void SetFinalX(float new_x
);
71 void SetFinalY(float new_y
);
73 // Stops the animation. Contrary to |ForceFinished()|, aborting the animation
74 // causes the scroller to move to the final x and y position.
75 void AbortAnimation();
77 // Terminate the scroll without affecting the current x and y positions.
78 void ForceFinished(bool finished
);
80 // Returns whether the scroller has finished scrolling.
81 bool IsFinished() const;
83 // Returns the time elapsed since the beginning of the scrolling.
84 base::TimeDelta
GetTimePassed() const;
86 // Returns how long the scroll event will take.
87 base::TimeDelta
GetDuration() const;
89 float GetStartX() const;
90 float GetStartY() const;
91 float GetCurrX() const;
92 float GetCurrY() const;
93 float GetCurrVelocity() const;
94 float GetCurrVelocityX() const;
95 float GetCurrVelocityY() const;
96 float GetFinalX() const;
97 float GetFinalY() const;
99 bool IsScrollingInDirection(float xvel
, float yvel
) const;
108 bool ComputeScrollOffsetInternal(base::TimeTicks time
);
109 void RecomputeDeltas();
111 double GetSplineDeceleration(float velocity
) const;
112 base::TimeDelta
GetSplineFlingDuration(float velocity
) const;
113 double GetSplineFlingDistance(float velocity
) const;
129 base::TimeTicks start_time_
;
130 base::TimeTicks curr_time_
;
131 base::TimeDelta duration_
;
132 double duration_seconds_reciprocal_
;
138 bool flywheel_enabled_
;
141 float curr_velocity_
;
144 float fling_friction_
;
151 #endif // UI_EVENTS_ANDROID_SCROLLER_H_