1 // Copyright 2018 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 CC_INPUT_SNAP_FLING_CURVE_H_
6 #define CC_INPUT_SNAP_FLING_CURVE_H_
8 #include "base/time/time.h"
9 #include "cc/cc_export.h"
10 #include "ui/gfx/geometry/rect_f.h"
11 #include "ui/gfx/geometry/vector2d_f.h"
15 // The curve for the snap fling animation. The curve would generate a geometric
16 // sequence of deltas to be scrolled at each frame.
17 class CC_EXPORT SnapFlingCurve {
19 // Creates the curve based on the start offset, target offset, and the first
20 // inertial GSU's time_stamp.
21 SnapFlingCurve(const gfx::Vector2dF& start_offset,
22 const gfx::Vector2dF& target_offset,
23 base::TimeTicks first_gsu_time);
25 virtual ~SnapFlingCurve();
27 // Estimate the total distance that will be scrolled given the first GSU's
29 static gfx::Vector2dF EstimateDisplacement(const gfx::Vector2dF& first_delta);
31 // Returns the delta that should be scrolled at |time|.
32 virtual gfx::Vector2dF GetScrollDelta(base::TimeTicks time);
34 // Updates |current_displacement_|. This sync is necessary because the node
35 // might be scrolled by other calls and the scrolls might be clamped.
36 void UpdateCurrentOffset(const gfx::Vector2dF& current_offset);
38 // Returns true if the scroll has arrived at the snap destination.
39 virtual bool IsFinished() const;
41 base::TimeDelta duration() const { return duration_; }
44 // Returns the curve's current distance at |current_time|.
45 double GetCurrentCurveDistance(base::TimeDelta current_time);
47 // The initial scroll offset of the scroller.
48 const gfx::Vector2dF start_offset_;
50 // The total displacement to the snap position.
51 const gfx::Vector2dF total_displacement_;
52 // 1D representation of |total_displacement_|.
53 const double total_distance_;
55 // The current displacement that has been scrolled.
56 gfx::Vector2dF current_displacement_;
58 // The timestamp of the first GSU.
59 const base::TimeTicks start_time_;
61 // The number of deltas in the curve's geometric sequence.
62 const double total_frames_;
63 // The first delta that defines the curve's geometric sequence.
64 const double first_delta_;
65 // The total milliseconds needed to finish the curve.
66 const base::TimeDelta duration_;
68 bool is_finished_ = false;
70 // |total_displacement_.x| / |total_distance_|
72 // |total_displacement_.y| / |total_distance_|
78 #endif // CC_INPUT_SNAP_FLING_CURVE_H_