Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(637)

Unified Diff: ui/events/gestures/fling_curve.cc

Issue 634373003: Consolidate content fling implementations (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix win build Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/events/gestures/fling_curve.h ('k') | ui/events/gestures/fling_curve_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/events/gestures/fling_curve.cc
diff --git a/ui/events/gestures/fling_curve.cc b/ui/events/gestures/fling_curve.cc
index 6f63019510ac7e4ba1bb31ddeaafc58d534229c0..00d29a46fd56439cf822beb806376a3c53a7377f 100644
--- a/ui/events/gestures/fling_curve.cc
+++ b/ui/events/gestures/fling_curve.cc
@@ -38,8 +38,10 @@ FlingCurve::FlingCurve(const gfx::Vector2dF& velocity,
base::TimeTicks start_timestamp)
: curve_duration_(GetTimeAtVelocity(0)),
start_timestamp_(start_timestamp),
+ previous_timestamp_(start_timestamp_),
time_offset_(0),
position_offset_(0) {
+ DCHECK(!velocity.IsZero());
float max_start_velocity = std::max(fabs(velocity.x()), fabs(velocity.y()));
if (max_start_velocity > GetVelocityAtTime(0))
max_start_velocity = GetVelocityAtTime(0);
@@ -49,32 +51,57 @@ FlingCurve::FlingCurve(const gfx::Vector2dF& velocity,
velocity.y() / max_start_velocity);
time_offset_ = GetTimeAtVelocity(max_start_velocity);
position_offset_ = GetPositionAtTime(time_offset_);
- last_timestamp_ = start_timestamp_ + base::TimeDelta::FromSecondsD(
- curve_duration_ - time_offset_);
}
FlingCurve::~FlingCurve() {
}
-gfx::Vector2dF FlingCurve::GetScrollAmountAtTime(base::TimeTicks current) {
- if (current < start_timestamp_)
- return gfx::Vector2dF();
+bool FlingCurve::ComputeScrollOffset(base::TimeTicks time,
+ gfx::Vector2dF* offset,
+ gfx::Vector2dF* velocity) {
+ DCHECK(offset);
+ DCHECK(velocity);
+ base::TimeDelta elapsed_time = time - start_timestamp_;
+ if (elapsed_time < base::TimeDelta()) {
+ *offset = gfx::Vector2dF();
+ *velocity = gfx::Vector2dF();
+ return true;
+ }
- float displacement = 0;
- if (current < last_timestamp_) {
- float time = time_offset_ + (current - start_timestamp_).InSecondsF();
- CHECK_LT(time, curve_duration_);
- displacement = GetPositionAtTime(time) - position_offset_;
+ bool still_active = true;
+ float scalar_offset;
+ float scalar_velocity;
+ double offset_time = elapsed_time.InSecondsF() + time_offset_;
+ if (offset_time < curve_duration_) {
+ scalar_offset = GetPositionAtTime(offset_time) - position_offset_;
+ scalar_velocity = GetVelocityAtTime(offset_time);
} else {
- displacement = GetPositionAtTime(curve_duration_) - position_offset_;
+ scalar_offset = GetPositionAtTime(curve_duration_) - position_offset_;
+ scalar_velocity = 0;
+ still_active = false;
}
- gfx::Vector2dF scroll(displacement * displacement_ratio_.x(),
- displacement * displacement_ratio_.y());
- gfx::Vector2dF scroll_increment(scroll.x() - cumulative_scroll_.x(),
- scroll.y() - cumulative_scroll_.y());
- cumulative_scroll_ = scroll;
- return scroll_increment;
+ *offset = gfx::ScaleVector2d(displacement_ratio_, scalar_offset);
+ *velocity = gfx::ScaleVector2d(displacement_ratio_, scalar_velocity);
+ return still_active;
+}
+
+bool FlingCurve::ComputeScrollDeltaAtTime(base::TimeTicks current,
+ gfx::Vector2dF* delta) {
+ DCHECK(delta);
+ if (current <= previous_timestamp_) {
+ *delta = gfx::Vector2dF();
+ return true;
+ }
+
+ previous_timestamp_ = current;
+
+ gfx::Vector2dF offset, velocity;
+ bool still_active = ComputeScrollOffset(current, &offset, &velocity);
+
+ *delta = offset - cumulative_scroll_;
+ cumulative_scroll_ = offset;
+ return still_active;
}
} // namespace ui
« no previous file with comments | « ui/events/gestures/fling_curve.h ('k') | ui/events/gestures/fling_curve_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698