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

Side by Side Diff: content/child/web_gesture_curve_impl.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 unified diff | Download patch
OLDNEW
(Empty)
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
5 #include "content/child/web_gesture_curve_impl.h"
6
7 #include "base/logging.h"
8 #include "third_party/WebKit/public/platform/WebFloatSize.h"
9 #include "third_party/WebKit/public/platform/WebGestureCurveTarget.h"
10 #include "ui/events/gestures/fling_curve.h"
11 #include "ui/gfx/vector2d.h"
12
13 #if defined(OS_ANDROID)
14 #include "ui/events/android/scroller.h"
15 #endif
16
17 using blink::WebGestureCurve;
18
19 namespace content {
20 namespace {
21
22 scoped_ptr<ui::GestureCurve> CreateDefaultPlatformCurve(
23 const gfx::Vector2dF& initial_velocity) {
24 DCHECK(!initial_velocity.IsZero());
25 #if defined(OS_ANDROID)
26 auto scroller = make_scoped_ptr(new ui::Scroller(ui::Scroller::Config()));
27 scroller->Fling(0,
28 0,
29 initial_velocity.x(),
30 initial_velocity.y(),
31 INT_MIN,
32 INT_MAX,
33 INT_MIN,
34 INT_MAX,
35 base::TimeTicks());
36 return scroller.PassAs<ui::GestureCurve>();
37 #else
38 return scoped_ptr<ui::GestureCurve>(
39 new ui::FlingCurve(initial_velocity, base::TimeTicks()));
40 #endif
41 }
42
43 } // namespace
44
45 // static
46 scoped_ptr<WebGestureCurve> WebGestureCurveImpl::CreateFromDefaultPlatformCurve(
47 const gfx::Vector2dF& initial_velocity,
48 const gfx::Vector2dF& initial_offset) {
49 return CreateFrom(CreateDefaultPlatformCurve(initial_velocity),
50 initial_offset);
51 }
52
53 // static
54 scoped_ptr<WebGestureCurve> WebGestureCurveImpl::CreateFrom(
55 scoped_ptr<ui::GestureCurve> curve,
56 const gfx::Vector2dF& initial_offset) {
57 return scoped_ptr<WebGestureCurve>(
58 new WebGestureCurveImpl(curve.Pass(), initial_offset));
59 }
60
61 WebGestureCurveImpl::WebGestureCurveImpl(scoped_ptr<ui::GestureCurve> curve,
62 const gfx::Vector2dF& initial_offset)
63 : curve_(curve.Pass()), last_offset_(initial_offset) {
64 }
65
66 WebGestureCurveImpl::~WebGestureCurveImpl() {
67 }
68
69 bool WebGestureCurveImpl::apply(double time,
70 blink::WebGestureCurveTarget* target) {
71 // If the fling has yet to start, simply return and report true to prevent
72 // fling termination.
73 if (time <= 0)
74 return true;
75
76 const base::TimeTicks time_ticks =
77 base::TimeTicks() + base::TimeDelta::FromSecondsD(time);
78 gfx::Vector2dF offset, velocity;
79 bool still_active =
80 curve_->ComputeScrollOffset(time_ticks, &offset, &velocity);
81
82 gfx::Vector2dF delta = offset - last_offset_;
83 last_offset_ = offset;
84
85 // As successive timestamps can be arbitrarily close (but monotonic!), don't
86 // assume that a zero delta means the curve has terminated.
87 if (delta.IsZero())
88 return still_active;
89
90 // scrollBy() could delete this curve if the animation is over, so don't touch
91 // any member variables after making that call.
92 bool did_scroll = target->scrollBy(blink::WebFloatSize(delta),
93 blink::WebFloatSize(velocity));
94 return did_scroll && still_active;
95 }
96
97 } // namespace content
OLDNEW
« no previous file with comments | « content/child/web_gesture_curve_impl.h ('k') | content/child/web_gesture_curve_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698