1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
10 #include "EventForwards.h"
11 #include "mozilla/layers/AxisPhysicsMSDModel.h"
12 #include "mozilla/RefPtr.h"
13 #include "mozilla/TimeStamp.h"
14 #include "nsRefreshObservers.h"
18 class nsRefreshDriver
;
22 class PanGestureInput
;
25 * SwipeTracker turns PanGestureInput events into swipe events
26 * (WidgetSimpleGestureEvent) and dispatches them into Gecko.
27 * The swiping behavior mirrors the behavior of the Cocoa API
29 * trackSwipeEventWithOptions:dampenAmountThresholdMin:max:usingHandler:].
30 * The advantage of using this class over the Cocoa API is that this class
31 * properly supports submitting queued up events to it, and that it hopefully
32 * doesn't intermittently break scrolling the way the Cocoa API does (bug
35 * The swipe direction is either left or right. It is determined before the
36 * SwipeTracker is created and stays fixed during the swipe.
37 * During the swipe, the swipe has a current "value" which is between 0 and the
38 * target value. The target value is either 1 (swiping left) or -1 (swiping
39 * right) - see SwipeSuccessTargetValue().
40 * A swipe can either succeed or fail. If it succeeds, the swipe animation
41 * animates towards the success target value; if it fails, it animates back to
42 * a value of 0. A swipe can only succeed if the user is swiping in an allowed
43 * direction. (Since both the allowed directions and the swipe direction are
44 * known at swipe start time, it's clear from the beginning whether a swipe is
45 * doomed to fail. In that case, the purpose of the SwipeTracker is to simulate
46 * a bounce-back animation.)
48 class SwipeTracker final
: public nsARefreshObserver
{
50 NS_INLINE_DECL_REFCOUNTING(SwipeTracker
, override
)
52 SwipeTracker(nsIWidget
& aWidget
, const PanGestureInput
& aSwipeStartEvent
,
53 uint32_t aAllowedDirections
, uint32_t aSwipeDirection
);
57 nsEventStatus
ProcessEvent(const PanGestureInput
& aEvent
,
58 bool aProcessingFirstEvent
= false);
59 void CancelSwipe(const TimeStamp
& aTimeStamp
);
61 static WidgetSimpleGestureEvent
CreateSwipeGestureEvent(
62 EventMessage aMsg
, nsIWidget
* aWidget
,
63 const LayoutDeviceIntPoint
& aPosition
, const TimeStamp
& aTimeStamp
);
66 void WillRefresh(mozilla::TimeStamp aTime
) override
;
68 static bool CanTriggerSwipe(const PanGestureInput
& aPanInput
);
73 bool SwipingInAllowedDirection() const {
74 return mAllowedDirections
& mSwipeDirection
;
76 double SwipeSuccessTargetValue() const;
77 double ClampToAllowedRange(double aGestureAmount
) const;
78 bool ComputeSwipeSuccess() const;
79 void StartAnimating(double aStartValue
, double aTargetValue
);
80 void SwipeFinished(const TimeStamp
& aTimeStamp
);
81 void UnregisterFromRefreshDriver();
82 bool SendSwipeEvent(EventMessage aMsg
, uint32_t aDirection
, double aDelta
,
83 const TimeStamp
& aTimeStamp
);
86 RefPtr
<nsRefreshDriver
> mRefreshDriver
;
87 layers::AxisPhysicsMSDModel mAxis
;
88 const LayoutDeviceIntPoint mEventPosition
;
89 TimeStamp mLastEventTimeStamp
;
90 TimeStamp mLastAnimationFrameTime
;
91 const uint32_t mAllowedDirections
;
92 const uint32_t mSwipeDirection
;
93 double mGestureAmount
;
94 double mCurrentVelocity
;
95 bool mEventsAreControllingSwipe
;
96 bool mEventsHaveStartedNewGesture
;
97 bool mRegisteredWithRefreshDriver
;
100 struct SwipeEventQueue
{
101 SwipeEventQueue(uint32_t aAllowedDirections
, uint64_t aInputBlockId
)
102 : allowedDirections(aAllowedDirections
), inputBlockId(aInputBlockId
) {}
104 nsTArray
<PanGestureInput
> queuedEvents
;
105 uint32_t allowedDirections
;
106 uint64_t inputBlockId
;
109 } // namespace mozilla
111 #endif // SwipeTracker_h