OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_RENDERER_INPUT_INPUT_SCROLL_ELASTICITY_CONTROLLER_H_ | 5 #ifndef CONTENT_RENDERER_INPUT_INPUT_SCROLL_ELASTICITY_CONTROLLER_H_ |
6 #define CONTENT_RENDERER_INPUT_INPUT_SCROLL_ELASTICITY_CONTROLLER_H_ | 6 #define CONTENT_RENDERER_INPUT_INPUT_SCROLL_ELASTICITY_CONTROLLER_H_ |
7 | 7 |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/time/time.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "cc/input/scroll_elasticity_helper.h" |
10 #include "third_party/WebKit/public/web/WebInputEvent.h" | 11 #include "third_party/WebKit/public/web/WebInputEvent.h" |
11 #include "ui/gfx/geometry/vector2d_f.h" | |
12 | 12 |
13 // ScrollElasticityController and ScrollElasticityControllerClient are based on | 13 // InputScrollElasticityController is based on |
14 // WebKit/Source/platform/mac/ScrollElasticityController.h | 14 // WebKit/Source/platform/mac/ScrollElasticityController.h |
15 /* | 15 /* |
16 * Copyright (C) 2011 Apple Inc. All rights reserved. | 16 * Copyright (C) 2011 Apple Inc. All rights reserved. |
17 * | 17 * |
18 * Redistribution and use in source and binary forms, with or without | 18 * Redistribution and use in source and binary forms, with or without |
19 * modification, are permitted provided that the following conditions | 19 * modification, are permitted provided that the following conditions |
20 * are met: | 20 * are met: |
21 * 1. Redistributions of source code must retain the above copyright | 21 * 1. Redistributions of source code must retain the above copyright |
22 * notice, this list of conditions and the following disclaimer. | 22 * notice, this list of conditions and the following disclaimer. |
23 * 2. Redistributions in binary form must reproduce the above copyright | 23 * 2. Redistributions in binary form must reproduce the above copyright |
24 * notice, this list of conditions and the following disclaimer in the | 24 * notice, this list of conditions and the following disclaimer in the |
25 * documentation and/or other materials provided with the distribution. | 25 * documentation and/or other materials provided with the distribution. |
26 * | 26 * |
27 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' | 27 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
28 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | 28 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
29 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 29 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
37 * THE POSSIBILITY OF SUCH DAMAGE. | 37 * THE POSSIBILITY OF SUCH DAMAGE. |
38 */ | 38 */ |
39 | 39 |
| 40 namespace cc { |
| 41 struct InputHandlerScrollResult; |
| 42 } // namespace cc |
| 43 |
40 namespace content { | 44 namespace content { |
41 | 45 |
42 class ScrollElasticityControllerClient { | 46 class InputScrollElasticityController { |
43 protected: | 47 public: |
44 virtual ~ScrollElasticityControllerClient() {} | 48 explicit InputScrollElasticityController(cc::ScrollElasticityHelper* helper); |
| 49 virtual ~InputScrollElasticityController(); |
45 | 50 |
46 public: | 51 base::WeakPtr<InputScrollElasticityController> GetWeakPtr(); |
47 virtual bool AllowsHorizontalStretching() = 0; | 52 void ObserveWheelEventAndResult( |
48 virtual bool AllowsVerticalStretching() = 0; | 53 const blink::WebMouseWheelEvent& wheel_event, |
49 // The amount that the view is stretched past the normal allowable bounds. | 54 const cc::InputHandlerScrollResult& scroll_result); |
50 // The "overhang" amount. | |
51 virtual gfx::Vector2dF StretchAmount() = 0; | |
52 virtual bool PinnedInDirection(const gfx::Vector2dF& direction) = 0; | |
53 virtual bool CanScrollHorizontally() = 0; | |
54 virtual bool CanScrollVertically() = 0; | |
55 | 55 |
56 // Return the absolute scroll position, not relative to the scroll origin. | 56 void WillShutdown(); |
57 virtual gfx::Vector2dF AbsoluteScrollPosition() = 0; | 57 void Animate(base::TimeTicks time); |
58 | 58 |
59 virtual void ImmediateScrollBy(const gfx::Vector2dF& scroll) = 0; | 59 private: |
60 virtual void ImmediateScrollByWithoutContentEdgeConstraints( | |
61 const gfx::Vector2dF& scroll) = 0; | |
62 virtual void StartSnapRubberbandTimer() = 0; | |
63 virtual void StopSnapRubberbandTimer() = 0; | |
64 | |
65 // If the current scroll position is within the overhang area, this function | |
66 // will cause | |
67 // the page to scroll to the nearest boundary point. | |
68 virtual void AdjustScrollPositionToBoundsIfNecessary() = 0; | |
69 }; | |
70 | |
71 class ScrollElasticityController { | |
72 public: | |
73 explicit ScrollElasticityController(ScrollElasticityControllerClient*); | |
74 | |
75 // This method is responsible for both scrolling and rubber-banding. | 60 // This method is responsible for both scrolling and rubber-banding. |
76 // | 61 // |
77 // Events are passed by IPC from the embedder. Events on Mac are grouped | 62 // Events are passed by IPC from the embedder. Events on Mac are grouped |
78 // into "gestures". If this method returns 'true', then this object has | 63 // into "gestures". If this method returns 'true', then this object has |
79 // handled the event. It expects the embedder to continue to forward events | 64 // handled the event. It expects the embedder to continue to forward events |
80 // from the gesture. | 65 // from the gesture. |
81 // | 66 // |
82 // This method makes the assumption that there is only 1 input device being | 67 // This method makes the assumption that there is only 1 input device being |
83 // used at a time. If the user simultaneously uses multiple input devices, | 68 // used at a time. If the user simultaneously uses multiple input devices, |
84 // Cocoa does not correctly pass all the gestureBegin/End events. The state | 69 // Cocoa does not correctly pass all the gestureBegin/End events. The state |
85 // of this class is guaranteed to become eventually consistent, once the | 70 // of this class is guaranteed to become eventually consistent, once the |
86 // user stops using multiple input devices. | 71 // user stops using multiple input devices. |
87 bool HandleWheelEvent(const blink::WebMouseWheelEvent& wheel_event); | 72 bool HandleWheelEvent(const blink::WebMouseWheelEvent& wheel_event); |
88 void SnapRubberbandTimerFired(); | 73 void SnapRubberbandTimerFired(); |
89 | 74 |
90 bool IsRubberbandInProgress() const; | 75 bool IsRubberbandInProgress() const; |
91 | 76 |
92 private: | |
93 void StopSnapRubberbandTimer(); | 77 void StopSnapRubberbandTimer(); |
94 void SnapRubberband(); | 78 void SnapRubberband(); |
95 | 79 |
96 // This method determines whether a given event should be handled. The | 80 // This method determines whether a given event should be handled. The |
97 // logic for control events of gestures (PhaseBegan, PhaseEnded) is handled | 81 // logic for control events of gestures (PhaseBegan, PhaseEnded) is handled |
98 // elsewhere. | 82 // elsewhere. |
99 // | 83 // |
100 // This class handles almost all wheel events. All of the following | 84 // This class handles almost all wheel events. All of the following |
101 // conditions must be met for this class to ignore an event: | 85 // conditions must be met for this class to ignore an event: |
102 // + No previous events in this gesture have caused any scrolling or rubber | 86 // + No previous events in this gesture have caused any scrolling or rubber |
103 // banding. | 87 // banding. |
104 // + The event contains a horizontal component. | 88 // + The event contains a horizontal component. |
105 // + The client's view is pinned in the horizontal direction of the event. | 89 // + The helper's view is pinned in the horizontal direction of the event. |
106 // + The wheel event disallows rubber banding in the horizontal direction | 90 // + The wheel event disallows rubber banding in the horizontal direction |
107 // of the event. | 91 // of the event. |
108 bool ShouldHandleEvent(const blink::WebMouseWheelEvent& wheel_event); | 92 bool ShouldHandleEvent(const blink::WebMouseWheelEvent& wheel_event); |
109 | 93 |
110 ScrollElasticityControllerClient* client_; | 94 cc::ScrollElasticityHelper* helper_; |
111 | 95 |
112 // There is an active scroll gesture event. This parameter only gets set to | 96 // There is an active scroll gesture event. This parameter only gets set to |
113 // false after the rubber band has been snapped, and before a new gesture | 97 // false after the rubber band has been snapped, and before a new gesture |
114 // has begun. A careful audit of the code may deprecate the need for this | 98 // has begun. A careful audit of the code may deprecate the need for this |
115 // parameter. | 99 // parameter. |
116 bool in_scroll_gesture_; | 100 bool in_scroll_gesture_; |
117 // At least one event in the current gesture has been consumed and has | 101 // At least one event in the current gesture has been consumed and has |
118 // caused the view to scroll or rubber band. All future events in this | 102 // caused the view to scroll or rubber band. All future events in this |
119 // gesture will be consumed and overscrolls will cause rubberbanding. | 103 // gesture will be consumed and overscrolls will cause rubberbanding. |
120 bool has_scrolled_; | 104 bool has_scrolled_; |
121 bool momentum_scroll_in_progress_; | 105 bool momentum_scroll_in_progress_; |
122 bool ignore_momentum_scrolls_; | 106 bool ignore_momentum_scrolls_; |
123 | 107 |
124 // Used with blink::WebInputEvent::timeStampSeconds, in seconds since epoch. | 108 // Used with blink::WebInputEvent::timeStampSeconds, in seconds since epoch. |
125 double last_momentum_scroll_timestamp_; | 109 double last_momentum_scroll_timestamp_; |
126 gfx::Vector2dF overflow_scroll_delta_; | 110 gfx::Vector2dF overflow_scroll_delta_; |
127 gfx::Vector2dF stretch_scroll_force_; | 111 gfx::Vector2dF stretch_scroll_force_; |
128 gfx::Vector2dF momentum_velocity_; | 112 gfx::Vector2dF momentum_velocity_; |
129 | 113 |
130 // Rubber band state. | 114 // Rubber band state. |
131 base::Time start_time_; | 115 base::Time start_time_; |
132 gfx::Vector2dF start_stretch_; | 116 gfx::Vector2dF start_stretch_; |
133 gfx::Vector2dF orig_origin_; | 117 gfx::Vector2dF orig_origin_; |
134 gfx::Vector2dF orig_velocity_; | 118 gfx::Vector2dF orig_velocity_; |
135 | 119 |
136 bool snap_rubberband_timer_is_active_; | 120 bool snap_rubberband_timer_is_active_; |
137 | 121 |
138 DISALLOW_COPY_AND_ASSIGN(ScrollElasticityController); | 122 base::WeakPtrFactory<InputScrollElasticityController> weak_factory_; |
| 123 DISALLOW_COPY_AND_ASSIGN(InputScrollElasticityController); |
139 }; | 124 }; |
140 | 125 |
141 } // namespace content | 126 } // namespace content |
142 | 127 |
143 #endif // CONTENT_RENDERER_INPUT_INPUT_SCROLL_ELASTICITY_CONTROLLER_H_ | 128 #endif // CONTENT_RENDERER_INPUT_INPUT_SCROLL_ELASTICITY_CONTROLLER_H_ |
OLD | NEW |