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 void CancelSwipe(const TimeStamp
& aTimeStamp
);
60 static WidgetSimpleGestureEvent
CreateSwipeGestureEvent(
61 EventMessage aMsg
, nsIWidget
* aWidget
,
62 const LayoutDeviceIntPoint
& aPosition
, const TimeStamp
& aTimeStamp
);
65 void WillRefresh(mozilla::TimeStamp aTime
) override
;
67 static bool CanTriggerSwipe(const PanGestureInput
& aPanInput
);
72 bool SwipingInAllowedDirection() const {
73 return mAllowedDirections
& mSwipeDirection
;
75 double SwipeSuccessTargetValue() const;
76 double ClampToAllowedRange(double aGestureAmount
) const;
77 bool ComputeSwipeSuccess() const;
78 void StartAnimating(double aTargetValue
);
79 void SwipeFinished(const TimeStamp
& aTimeStamp
);
80 void UnregisterFromRefreshDriver();
81 bool SendSwipeEvent(EventMessage aMsg
, uint32_t aDirection
, double aDelta
,
82 const TimeStamp
& aTimeStamp
);
85 RefPtr
<nsRefreshDriver
> mRefreshDriver
;
86 layers::AxisPhysicsMSDModel mAxis
;
87 const LayoutDeviceIntPoint mEventPosition
;
88 TimeStamp mLastEventTimeStamp
;
89 TimeStamp mLastAnimationFrameTime
;
90 const uint32_t mAllowedDirections
;
91 const uint32_t mSwipeDirection
;
92 double mGestureAmount
;
93 double mCurrentVelocity
;
94 bool mEventsAreControllingSwipe
;
95 bool mEventsHaveStartedNewGesture
;
96 bool mRegisteredWithRefreshDriver
;
99 struct SwipeEventQueue
{
100 SwipeEventQueue(uint32_t aAllowedDirections
, uint64_t aInputBlockId
)
101 : allowedDirections(aAllowedDirections
), inputBlockId(aInputBlockId
) {}
103 nsTArray
<PanGestureInput
> queuedEvents
;
104 uint32_t allowedDirections
;
105 uint64_t inputBlockId
;
108 } // namespace mozilla
110 #endif // SwipeTracker_h