Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CONTENT_BROWSER_RENDERER_HOST_OVERSCROLL_CONTROLLER_H_ |
| 6 | #define CONTENT_BROWSER_RENDERER_HOST_OVERSCROLL_CONTROLLER_H_ |
| 7 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 8 | #include <optional> |
| 9 | |
Daniel Cheng | 224569ee | 2018-04-25 05:45:06 | [diff] [blame] | 10 | #include "base/time/time.h" |
Mohsen Izadi | 8018941 | 2018-03-22 17:10:18 | [diff] [blame] | 11 | #include "cc/input/overscroll_behavior.h" |
mcnee | 19fd249 | 2017-06-01 14:42:43 | [diff] [blame] | 12 | #include "content/common/content_export.h" |
Dave Tapuska | 129cef8 | 2019-12-19 16:36:48 | [diff] [blame] | 13 | #include "third_party/blink/public/common/input/web_gesture_event.h" |
| 14 | #include "third_party/blink/public/common/input/web_input_event.h" |
Sandra Sun | cbd6da4 | 2018-01-05 15:29:36 | [diff] [blame] | 15 | #include "ui/events/blink/did_overscroll_params.h" |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 16 | |
| 17 | namespace content { |
| 18 | |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 19 | class OverscrollControllerDelegate; |
Mohsen Izadi | 9830436c | 2017-12-21 18:02:46 | [diff] [blame] | 20 | class OverscrollControllerTest; |
| 21 | class RenderWidgetHostViewAuraOverscrollTest; |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 22 | |
[email protected] | 888878e8 | 2013-07-24 22:49:40 | [diff] [blame] | 23 | // Indicates the direction that the scroll is heading in relative to the screen, |
| 24 | // with the top being NORTH. |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 25 | enum OverscrollMode { |
| 26 | OVERSCROLL_NONE, |
| 27 | OVERSCROLL_NORTH, |
| 28 | OVERSCROLL_SOUTH, |
| 29 | OVERSCROLL_WEST, |
mfomitchev | 27e68f6e | 2015-04-14 15:18:14 | [diff] [blame] | 30 | OVERSCROLL_EAST |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 31 | }; |
| 32 | |
mfomitchev | 09f0d64a | 2017-03-02 19:40:07 | [diff] [blame] | 33 | // Indicates the source device that was used to trigger the overscroll gesture. |
| 34 | enum class OverscrollSource { |
| 35 | NONE, |
| 36 | TOUCHPAD, |
| 37 | TOUCHSCREEN, |
| 38 | }; |
| 39 | |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 40 | // When a page is scrolled beyond the scrollable region, it will trigger an |
| 41 | // overscroll gesture. This controller receives the events that are dispatched |
| 42 | // to the renderer, and the ACKs of events, and updates the overscroll gesture |
| 43 | // status accordingly. |
Mohsen Izadi | 9830436c | 2017-12-21 18:02:46 | [diff] [blame] | 44 | // Exported for testing. |
Jose Dapena Paz | db116c0 | 2017-11-28 15:58:21 | [diff] [blame] | 45 | class CONTENT_EXPORT OverscrollController { |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 46 | public: |
Jose Dapena Paz | db116c0 | 2017-11-28 15:58:21 | [diff] [blame] | 47 | OverscrollController(); |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 48 | |
| 49 | OverscrollController(const OverscrollController&) = delete; |
| 50 | OverscrollController& operator=(const OverscrollController&) = delete; |
| 51 | |
Jose Dapena Paz | db116c0 | 2017-11-28 15:58:21 | [diff] [blame] | 52 | virtual ~OverscrollController(); |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 53 | |
| 54 | // This must be called when dispatching any event from the |
| 55 | // RenderWidgetHostView so that the state of the overscroll gesture can be |
[email protected] | 277857a | 2014-06-03 10:38:01 | [diff] [blame] | 56 | // updated properly. Returns true if the event was handled, in which case |
| 57 | // further processing should cease. |
| 58 | bool WillHandleEvent(const blink::WebInputEvent& event); |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 59 | |
Sandra Sun | cbd6da4 | 2018-01-05 15:29:36 | [diff] [blame] | 60 | // This is called whenever an overscroll event is generated on the renderer |
| 61 | // side. This is called before ReceivedEventAck. The params contains an |
| 62 | // OverscrollBehavior that can prevent overscroll navigation. |
| 63 | void OnDidOverscroll(const ui::DidOverscrollParams& params); |
| 64 | |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 65 | // This must be called when the ACK for any event comes in. This updates the |
| 66 | // overscroll gesture status as appropriate. |
Kevin McNee | 2b9fb07 | 2019-12-04 00:36:23 | [diff] [blame] | 67 | void ReceivedEventACK(const blink::WebInputEvent& event, bool processed); |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 68 | |
| 69 | OverscrollMode overscroll_mode() const { return overscroll_mode_; } |
| 70 | |
Robert Flack | c367019 | 2021-03-30 16:18:09 | [diff] [blame] | 71 | void set_delegate(base::WeakPtr<OverscrollControllerDelegate> delegate) { |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 72 | delegate_ = delegate; |
| 73 | } |
| 74 | |
| 75 | // Resets internal states. |
| 76 | void Reset(); |
| 77 | |
[email protected] | 63ad7383 | 2013-06-17 15:41:04 | [diff] [blame] | 78 | // Cancels any in-progress overscroll (and calls OnOverscrollModeChange on the |
| 79 | // delegate if necessary), and resets internal states. |
| 80 | void Cancel(); |
| 81 | |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 82 | private: |
Mohsen Izadi | 9830436c | 2017-12-21 18:02:46 | [diff] [blame] | 83 | friend class OverscrollControllerTest; |
[email protected] | 277857a | 2014-06-03 10:38:01 | [diff] [blame] | 84 | friend class RenderWidgetHostViewAuraOverscrollTest; |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 85 | |
[email protected] | a414164 | 2013-05-23 04:10:58 | [diff] [blame] | 86 | // Different scrolling states. |
Mohsen Izadi | 9830436c | 2017-12-21 18:02:46 | [diff] [blame] | 87 | enum class ScrollState { |
| 88 | NONE, |
| 89 | |
| 90 | // Either a mouse-wheel or a gesture-scroll-update event is consumed by the |
| 91 | // renderer in which case no overscroll should be initiated until the end of |
| 92 | // the user interaction. |
| 93 | CONTENT_CONSUMING, |
| 94 | |
| 95 | // Overscroll controller has initiated overscrolling and will consume all |
| 96 | // subsequent gesture-scroll-update events, preventing them from being |
| 97 | // forwarded to the renderer. |
| 98 | OVERSCROLLING, |
[email protected] | a414164 | 2013-05-23 04:10:58 | [diff] [blame] | 99 | }; |
| 100 | |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 101 | // Returns true if the event indicates that the in-progress overscroll gesture |
| 102 | // can now be completed. |
chaopeng | 1d469b0 | 2018-04-05 15:41:07 | [diff] [blame] | 103 | bool DispatchEventCompletesAction(const blink::WebInputEvent& event) const; |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 104 | |
| 105 | // Returns true to indicate that dispatching the event should reset the |
| 106 | // overscroll gesture status. |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 107 | bool DispatchEventResetsState(const blink::WebInputEvent& event) const; |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 108 | |
[email protected] | 2422e5c | 2013-09-13 19:05:02 | [diff] [blame] | 109 | // Processes an event to update the internal state for overscroll. Returns |
| 110 | // true if the state is updated, false otherwise. |
[email protected] | 180ef24 | 2013-11-07 06:50:46 | [diff] [blame] | 111 | bool ProcessEventForOverscroll(const blink::WebInputEvent& event); |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 112 | |
| 113 | // Processes horizontal overscroll. This can update both the overscroll mode |
| 114 | // and the over scroll amount (i.e. |overscroll_mode_|, |overscroll_delta_x_| |
[email protected] | bfab3d0 | 2014-08-20 03:16:55 | [diff] [blame] | 115 | // and |overscroll_delta_y_|). Returns true if overscroll was handled by the |
| 116 | // delegate. |
chaopeng | 9736c52 | 2018-05-08 18:32:27 | [diff] [blame] | 117 | bool ProcessOverscroll(float delta_x, |
| 118 | float delta_y, |
| 119 | bool is_touchpad, |
| 120 | bool is_inertial); |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 121 | |
| 122 | // Completes the desired action from the current gesture. |
| 123 | void CompleteAction(); |
| 124 | |
mfomitchev | 09f0d64a | 2017-03-02 19:40:07 | [diff] [blame] | 125 | // Sets the overscroll mode and triggers callback in the delegate when |
| 126 | // appropriate. When a new overscroll is started (i.e. when |new_mode| is not |
| 127 | // equal to OVERSCROLL_NONE), |source| will be set to the device that |
| 128 | // triggered the overscroll gesture. |
| 129 | void SetOverscrollMode(OverscrollMode new_mode, OverscrollSource source); |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 130 | |
chaopeng | a7585d79 | 2018-04-10 18:02:38 | [diff] [blame] | 131 | // Whether this inertial event should be filtered out by the controller. |
| 132 | bool ShouldIgnoreInertialEvent(const blink::WebInputEvent& event) const; |
| 133 | |
dtapuska | 19ebfa51 | 2016-02-19 22:27:40 | [diff] [blame] | 134 | // Whether this event should be processed or not handled by the controller. |
| 135 | bool ShouldProcessEvent(const blink::WebInputEvent& event); |
| 136 | |
Mohsen Izadi | 50180f39 | 2017-09-08 19:29:12 | [diff] [blame] | 137 | // Helper function to reset |scroll_state_| and |locked_mode_|. |
| 138 | void ResetScrollState(); |
| 139 | |
Mohsen Izadi | 8018941 | 2018-03-22 17:10:18 | [diff] [blame] | 140 | // Current value of overscroll-behavior CSS property for the root element of |
| 141 | // the page. |
| 142 | cc::OverscrollBehavior behavior_; |
| 143 | |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 144 | // The current state of overscroll gesture. |
Mohsen Izadi | 38795d5d | 2017-06-30 17:10:48 | [diff] [blame] | 145 | OverscrollMode overscroll_mode_ = OVERSCROLL_NONE; |
| 146 | |
Mohsen Izadi | f589f4b | 2017-08-24 02:55:48 | [diff] [blame] | 147 | // When set to something other than OVERSCROLL_NONE, the overscroll cannot |
| 148 | // switch to any other mode, except to OVERSCROLL_NONE. This is set when an |
| 149 | // overscroll is started until the touch sequence is completed. |
| 150 | OverscrollMode locked_mode_ = OVERSCROLL_NONE; |
| 151 | |
Mohsen Izadi | 38795d5d | 2017-06-30 17:10:48 | [diff] [blame] | 152 | // Source of the current overscroll gesture. |
| 153 | OverscrollSource overscroll_source_ = OverscrollSource::NONE; |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 154 | |
Mohsen Izadi | 9830436c | 2017-12-21 18:02:46 | [diff] [blame] | 155 | // Current scrolling state. |
| 156 | ScrollState scroll_state_ = ScrollState::NONE; |
[email protected] | a414164 | 2013-05-23 04:10:58 | [diff] [blame] | 157 | |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 158 | // The amount of overscroll in progress. These values are invalid when |
| 159 | // |overscroll_mode_| is set to OVERSCROLL_NONE. |
Mohsen Izadi | 38795d5d | 2017-06-30 17:10:48 | [diff] [blame] | 160 | float overscroll_delta_x_ = 0.f; |
| 161 | float overscroll_delta_y_ = 0.f; |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 162 | |
| 163 | // The delegate that receives the overscroll updates. The delegate is not |
| 164 | // owned by this controller. |
Robert Flack | c367019 | 2021-03-30 16:18:09 | [diff] [blame] | 165 | base::WeakPtr<OverscrollControllerDelegate> delegate_; |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 166 | |
chaopeng | 1d469b0 | 2018-04-05 15:41:07 | [diff] [blame] | 167 | // A inertial scroll (fling) event may complete an overscroll gesture and |
chaopeng | 9736c52 | 2018-05-08 18:32:27 | [diff] [blame] | 168 | // navigate to a new page or cancel the overscroll animation. In both cases |
| 169 | // inertial scroll can continue to generate scroll-update events. These events |
| 170 | // need to be ignored. |
chaopeng | 1d469b0 | 2018-04-05 15:41:07 | [diff] [blame] | 171 | bool ignore_following_inertial_events_ = false; |
| 172 | |
Mohsen Izadi | 5a658351 | 2018-04-23 18:31:19 | [diff] [blame] | 173 | // Specifies whether last overscroll was ignored, either due to a command line |
| 174 | // flag or because cool off period had not passed. |
| 175 | bool overscroll_ignored_ = false; |
| 176 | |
Daniel Cheng | 224569ee | 2018-04-25 05:45:06 | [diff] [blame] | 177 | // Timestamp for the end of the last ignored scroll sequence. |
| 178 | base::TimeTicks last_ignored_scroll_time_; |
Mohsen Izadi | 5a658351 | 2018-04-23 18:31:19 | [diff] [blame] | 179 | |
| 180 | // Time between the end of the last ignored scroll sequence and the beginning |
Daniel Cheng | 224569ee | 2018-04-25 05:45:06 | [diff] [blame] | 181 | // of the current one. |
| 182 | base::TimeDelta time_since_last_ignored_scroll_; |
Mohsen Izadi | 5a658351 | 2018-04-23 18:31:19 | [diff] [blame] | 183 | |
chaopeng | 9736c52 | 2018-05-08 18:32:27 | [diff] [blame] | 184 | // On Windows, we don't generate the inertial events (fling) but receive them |
| 185 | // from Win API. In some cases, we get a long tail of inertial events for a |
| 186 | // couple of seconds. The overscroll animation feels like stuck in these |
| 187 | // cases. So we only process 0.3 second inertial events then cancel the |
| 188 | // overscroll if it is not completed yet. |
| 189 | // Timestamp for the first inertial event (fling) in current stream. |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 190 | std::optional<base::TimeTicks> first_inertial_event_time_; |
[email protected] | b4df9df | 2012-11-16 01:58:58 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | } // namespace content |
| 194 | |
| 195 | #endif // CONTENT_BROWSER_RENDERER_HOST_OVERSCROLL_CONTROLLER_H_ |