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 CONTENT_RENDERER_INPUT_INPUT_SCROLL_ELASTICITY_CONTROLLER_H_
6 #define CONTENT_RENDERER_INPUT_INPUT_SCROLL_ELASTICITY_CONTROLLER_H_
8 #include "base/macros.h"
9 #include "base/memory/weak_ptr.h"
10 #include "cc/input/scroll_elasticity_helper.h"
11 #include "content/common/content_export.h"
12 #include "third_party/WebKit/public/web/WebInputEvent.h"
14 // InputScrollElasticityController is based on
15 // WebKit/Source/platform/mac/ScrollElasticityController.h
17 * Copyright (C) 2011 Apple Inc. All rights reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
28 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
30 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
38 * THE POSSIBILITY OF SUCH DAMAGE.
42 struct InputHandlerScrollResult
;
47 class CONTENT_EXPORT InputScrollElasticityController
{
49 explicit InputScrollElasticityController(cc::ScrollElasticityHelper
* helper
);
50 virtual ~InputScrollElasticityController();
52 base::WeakPtr
<InputScrollElasticityController
> GetWeakPtr();
54 // Update the overscroll state based a wheel event that has been processed.
55 // Note that this assumes that all events are coming from a single input
56 // device. If the user simultaneously uses multiple input devices, Cocoa may
57 // not correctly pass all the gesture begin and end events. In this case,
58 // this class may disregard some scrolls that come in at unexpected times.
59 void ObserveWheelEventAndResult(
60 const blink::WebMouseWheelEvent
& wheel_event
,
61 const cc::InputHandlerScrollResult
& scroll_result
);
62 void Animate(base::TimeTicks time
);
64 void ReconcileStretchAndScroll();
68 // The initial state, during which the overscroll amount is zero and
69 // there are no active or momentum scroll events coming in. This state
70 // is briefly returned to between the active and momentum phases of a
71 // scroll (if there is no overscroll).
73 // The state between receiving PhaseBegan/MayBegin and PhaseEnd/Cancelled,
74 // corresponding to the period during which the user has fingers on the
75 // trackpad. The overscroll amount is updated as input events are received.
76 // When PhaseEnd is received, the state transitions to Inactive if there is
77 // no overscroll and MomentumAnimated if there is non-zero overscroll.
79 // The state between receiving a momentum PhaseBegan and PhaseEnd, while
80 // there is no overscroll. The overscroll amount is updated as input events
81 // are received. If the overscroll is ever non-zero then the state
82 // immediately transitions to kStateMomentumAnimated.
84 // The state while the overscroll amount is updated by an animation. If
85 // the user places fingers on the trackpad (a PhaseMayBegin is received)
86 // then the state transition to kStateActiveScroll. Otherwise the state
87 // transitions to Inactive when the overscroll amount becomes zero.
88 kStateMomentumAnimated
,
91 void UpdateVelocity(const gfx::Vector2dF
& event_delta
,
92 const base::TimeTicks
& event_timestamp
);
93 void Overscroll(const gfx::Vector2dF
& input_delta
,
94 const gfx::Vector2dF
& overscroll_delta
);
95 void EnterStateMomentumAnimated(
96 const base::TimeTicks
& triggering_event_timestamp
);
97 void EnterStateInactive();
99 // Returns true if |direction| is pointing in a direction in which it is not
100 // possible to scroll any farther horizontally (or vertically). It is only in
101 // this circumstance that an overscroll in that direction may begin.
102 bool PinnedHorizontally(float direction
) const;
103 bool PinnedVertically(float direction
) const;
104 // Whether or not the content of the page is scrollable horizontaly (or
106 bool CanScrollHorizontally() const;
107 bool CanScrollVertically() const;
109 cc::ScrollElasticityHelper
* helper_
;
112 // If there is no overscroll, require a minimum overscroll delta before
113 // starting the rubber-band effect. Track the amount of scrolling that has
114 // has occurred but has not yet caused rubber-band stretching in
115 // |pending_overscroll_delta_|.
116 gfx::Vector2dF pending_overscroll_delta_
;
118 // Maintain a calculation of the velocity of the scroll, based on the input
119 // scroll delta divide by the time between input events. Track this velocity
120 // in |scroll_velocity| and the previous input event timestamp for finite
121 // differencing in |last_scroll_event_timestamp_|.
122 gfx::Vector2dF scroll_velocity
;
123 base::TimeTicks last_scroll_event_timestamp_
;
125 // The force of the rubber-band spring. This is equal to the cumulative sum
126 // of all overscroll offsets since entering a non-Inactive state. This is
127 // reset to zero only when entering the Inactive state.
128 gfx::Vector2dF stretch_scroll_force_
;
130 // Momentum animation state. This state is valid only while the state is
131 // MomentumAnimated, and is initialized in EnterStateMomentumAnimated.
132 base::TimeTicks momentum_animation_start_time_
;
133 gfx::Vector2dF momentum_animation_initial_stretch_
;
134 gfx::Vector2dF momentum_animation_initial_velocity_
;
136 // This is set in response to a scroll (most likely programmatic) occuring
137 // while animating the momentum phase. In this case, re-set the initial
138 // velocity, stretch, and start time at the next frame (this is the same
139 // behavior as would happen if the scroll were caused by an active scroll).
140 bool momentum_animation_reset_at_next_frame_
;
142 base::WeakPtrFactory
<InputScrollElasticityController
> weak_factory_
;
143 DISALLOW_COPY_AND_ASSIGN(InputScrollElasticityController
);
146 } // namespace content
148 #endif // CONTENT_RENDERER_INPUT_INPUT_SCROLL_ELASTICITY_CONTROLLER_H_