2015-08-27 23:07:59 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef SwipeTracker_h
|
|
|
|
#define SwipeTracker_h
|
|
|
|
|
|
|
|
#include "EventForwards.h"
|
|
|
|
#include "mozilla/layers/AxisPhysicsMSDModel.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2015-08-27 23:07:59 +03:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2020-08-05 00:17:50 +03:00
|
|
|
#include "nsRefreshObservers.h"
|
2015-08-27 23:07:59 +03:00
|
|
|
#include "Units.h"
|
|
|
|
|
2018-11-14 20:03:36 +03:00
|
|
|
class nsIWidget;
|
2020-08-05 00:17:50 +03:00
|
|
|
class nsRefreshDriver;
|
2015-08-27 23:07:59 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
class PanGestureInput;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SwipeTracker turns PanGestureInput events into swipe events
|
|
|
|
* (WidgetSimpleGestureEvent) and dispatches them into Gecko.
|
|
|
|
* The swiping behavior mirrors the behavior of the Cocoa API
|
|
|
|
* -[NSEvent
|
|
|
|
* trackSwipeEventWithOptions:dampenAmountThresholdMin:max:usingHandler:].
|
|
|
|
* The advantage of using this class over the Cocoa API is that this class
|
|
|
|
* properly supports submitting queued up events to it, and that it hopefully
|
|
|
|
* doesn't intermittently break scrolling the way the Cocoa API does (bug
|
|
|
|
* 927702).
|
|
|
|
*
|
|
|
|
* The swipe direction is either left or right. It is determined before the
|
|
|
|
* SwipeTracker is created and stays fixed during the swipe.
|
|
|
|
* During the swipe, the swipe has a current "value" which is between 0 and the
|
|
|
|
* target value. The target value is either 1 (swiping left) or -1 (swiping
|
|
|
|
* right) - see SwipeSuccessTargetValue().
|
|
|
|
* A swipe can either succeed or fail. If it succeeds, the swipe animation
|
|
|
|
* animates towards the success target value; if it fails, it animates back to
|
|
|
|
* a value of 0. A swipe can only succeed if the user is swiping in an allowed
|
|
|
|
* direction. (Since both the allowed directions and the swipe direction are
|
|
|
|
* known at swipe start time, it's clear from the beginning whether a swipe is
|
|
|
|
* doomed to fail. In that case, the purpose of the SwipeTracker is to simulate
|
|
|
|
* a bounce-back animation.)
|
|
|
|
*/
|
|
|
|
class SwipeTracker final : public nsARefreshObserver {
|
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_REFCOUNTING(SwipeTracker, override)
|
|
|
|
|
2022-01-05 12:08:05 +03:00
|
|
|
SwipeTracker(nsIWidget& aWidget, const PanGestureInput& aSwipeStartEvent,
|
2015-08-27 23:07:59 +03:00
|
|
|
uint32_t aAllowedDirections, uint32_t aSwipeDirection);
|
|
|
|
|
|
|
|
void Destroy();
|
|
|
|
|
2022-05-26 05:29:10 +03:00
|
|
|
nsEventStatus ProcessEvent(const PanGestureInput& aEvent,
|
|
|
|
bool aProcessingFirstEvent = false);
|
2016-12-20 14:58:04 +03:00
|
|
|
void CancelSwipe(const TimeStamp& aTimeStamp);
|
2015-08-27 23:07:59 +03:00
|
|
|
|
|
|
|
static WidgetSimpleGestureEvent CreateSwipeGestureEvent(
|
|
|
|
EventMessage aMsg, nsIWidget* aWidget,
|
2016-12-20 14:58:04 +03:00
|
|
|
const LayoutDeviceIntPoint& aPosition, const TimeStamp& aTimeStamp);
|
2015-08-27 23:07:59 +03:00
|
|
|
|
|
|
|
// nsARefreshObserver
|
|
|
|
void WillRefresh(mozilla::TimeStamp aTime) override;
|
|
|
|
|
2022-01-05 12:08:07 +03:00
|
|
|
static bool CanTriggerSwipe(const PanGestureInput& aPanInput);
|
|
|
|
|
2015-08-27 23:07:59 +03:00
|
|
|
protected:
|
|
|
|
~SwipeTracker();
|
|
|
|
|
|
|
|
bool SwipingInAllowedDirection() const {
|
|
|
|
return mAllowedDirections & mSwipeDirection;
|
|
|
|
}
|
|
|
|
double SwipeSuccessTargetValue() const;
|
|
|
|
double ClampToAllowedRange(double aGestureAmount) const;
|
|
|
|
bool ComputeSwipeSuccess() const;
|
2022-08-03 13:50:41 +03:00
|
|
|
void StartAnimating(double aStartValue, double aTargetValue);
|
2016-12-20 14:58:04 +03:00
|
|
|
void SwipeFinished(const TimeStamp& aTimeStamp);
|
2015-08-27 23:07:59 +03:00
|
|
|
void UnregisterFromRefreshDriver();
|
2016-12-20 14:58:04 +03:00
|
|
|
bool SendSwipeEvent(EventMessage aMsg, uint32_t aDirection, double aDelta,
|
|
|
|
const TimeStamp& aTimeStamp);
|
2015-08-27 23:07:59 +03:00
|
|
|
|
2022-01-05 12:08:05 +03:00
|
|
|
nsIWidget& mWidget;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsRefreshDriver> mRefreshDriver;
|
2015-08-27 23:07:59 +03:00
|
|
|
layers::AxisPhysicsMSDModel mAxis;
|
|
|
|
const LayoutDeviceIntPoint mEventPosition;
|
|
|
|
TimeStamp mLastEventTimeStamp;
|
|
|
|
TimeStamp mLastAnimationFrameTime;
|
|
|
|
const uint32_t mAllowedDirections;
|
|
|
|
const uint32_t mSwipeDirection;
|
2023-08-15 03:20:11 +03:00
|
|
|
double mGestureAmount = 0.0;
|
|
|
|
double mCurrentVelocity = 0.0;
|
|
|
|
bool mDeltaTypeIsPage = false;
|
|
|
|
bool mEventsAreControllingSwipe = true;
|
|
|
|
bool mEventsHaveStartedNewGesture = false;
|
2023-08-15 03:22:57 +03:00
|
|
|
bool mRegisteredWithRefreshDriver = false;
|
2015-08-27 23:07:59 +03:00
|
|
|
};
|
|
|
|
|
2022-01-05 12:08:06 +03:00
|
|
|
struct SwipeEventQueue {
|
|
|
|
SwipeEventQueue(uint32_t aAllowedDirections, uint64_t aInputBlockId)
|
|
|
|
: allowedDirections(aAllowedDirections), inputBlockId(aInputBlockId) {}
|
|
|
|
|
|
|
|
nsTArray<PanGestureInput> queuedEvents;
|
|
|
|
uint32_t allowedDirections;
|
|
|
|
uint64_t inputBlockId;
|
|
|
|
};
|
|
|
|
|
2015-08-27 23:07:59 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // SwipeTracker_h
|