summaryrefslogtreecommitdiffstats
path: root/widget/SwipeTracker.h
blob: e475cb3b29aea61d160668b0c0c16739625f671d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* -*- 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"
#include "mozilla/RefPtr.h"
#include "mozilla/TimeStamp.h"
#include "nsRefreshObservers.h"
#include "Units.h"

class nsIWidget;
class nsRefreshDriver;

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)

  SwipeTracker(nsIWidget& aWidget, const PanGestureInput& aSwipeStartEvent,
               uint32_t aAllowedDirections, uint32_t aSwipeDirection);

  void Destroy();

  nsEventStatus ProcessEvent(const PanGestureInput& aEvent,
                             bool aProcessingFirstEvent = false);
  void CancelSwipe(const TimeStamp& aTimeStamp);

  static WidgetSimpleGestureEvent CreateSwipeGestureEvent(
      EventMessage aMsg, nsIWidget* aWidget,
      const LayoutDeviceIntPoint& aPosition, const TimeStamp& aTimeStamp);

  // nsARefreshObserver
  void WillRefresh(mozilla::TimeStamp aTime) override;

  static bool CanTriggerSwipe(const PanGestureInput& aPanInput);

 protected:
  ~SwipeTracker();

  bool SwipingInAllowedDirection() const {
    return mAllowedDirections & mSwipeDirection;
  }
  double SwipeSuccessTargetValue() const;
  double ClampToAllowedRange(double aGestureAmount) const;
  bool ComputeSwipeSuccess() const;
  void StartAnimating(double aStartValue, double aTargetValue);
  void SwipeFinished(const TimeStamp& aTimeStamp);
  void UnregisterFromRefreshDriver();
  bool SendSwipeEvent(EventMessage aMsg, uint32_t aDirection, double aDelta,
                      const TimeStamp& aTimeStamp);

  nsIWidget& mWidget;
  RefPtr<nsRefreshDriver> mRefreshDriver;
  layers::AxisPhysicsMSDModel mAxis;
  const LayoutDeviceIntPoint mEventPosition;
  TimeStamp mLastEventTimeStamp;
  TimeStamp mLastAnimationFrameTime;
  const uint32_t mAllowedDirections;
  const uint32_t mSwipeDirection;
  double mGestureAmount = 0.0;
  double mCurrentVelocity = 0.0;
  bool mDeltaTypeIsPage = false;
  bool mEventsAreControllingSwipe = true;
  bool mEventsHaveStartedNewGesture = false;
  bool mRegisteredWithRefreshDriver = false;
};

struct SwipeEventQueue {
  SwipeEventQueue(uint32_t aAllowedDirections, uint64_t aInputBlockId)
      : allowedDirections(aAllowedDirections), inputBlockId(aInputBlockId) {}

  nsTArray<PanGestureInput> queuedEvents;
  uint32_t allowedDirections;
  uint64_t inputBlockId;
};

}  // namespace mozilla

#endif  // SwipeTracker_h