summaryrefslogtreecommitdiffstats
path: root/gfx/layers/apz/src/Overscroll.h
blob: 1fb7c3e4875ff2161ff0a454131570a4347e6966 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* -*- 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 mozilla_layers_Overscroll_h
#define mozilla_layers_Overscroll_h

#include "AsyncPanZoomAnimation.h"
#include "AsyncPanZoomController.h"
#include "mozilla/TimeStamp.h"
#include "nsThreadUtils.h"

namespace mozilla {
namespace layers {

// Animation used by GenericOverscrollEffect.
class OverscrollAnimation : public AsyncPanZoomAnimation {
 public:
  OverscrollAnimation(AsyncPanZoomController& aApzc,
                      const ParentLayerPoint& aVelocity,
                      SideBits aOverscrollSideBits)
      : mApzc(aApzc), mOverscrollSideBits(aOverscrollSideBits) {
    MOZ_ASSERT(
        (mOverscrollSideBits & SideBits::eTopBottom) != SideBits::eTopBottom &&
            (mOverscrollSideBits & SideBits::eLeftRight) !=
                SideBits::eLeftRight,
        "Don't allow overscrolling on both sides at the same time");
    if ((aOverscrollSideBits & SideBits::eLeftRight) != SideBits::eNone) {
      mApzc.mX.StartOverscrollAnimation(aVelocity.x);
    }
    if ((aOverscrollSideBits & SideBits::eTopBottom) != SideBits::eNone) {
      mApzc.mY.StartOverscrollAnimation(aVelocity.y);
    }
  }
  virtual ~OverscrollAnimation() {
    mApzc.mX.EndOverscrollAnimation();
    mApzc.mY.EndOverscrollAnimation();
  }

  virtual bool DoSample(FrameMetrics& aFrameMetrics,
                        const TimeDuration& aDelta) override {
    // Can't inline these variables due to short-circuit evaluation.
    bool continueX = mApzc.mX.IsOverscrollAnimationAlive() &&
                     mApzc.mX.SampleOverscrollAnimation(
                         aDelta, mOverscrollSideBits & SideBits::eLeftRight);
    bool continueY = mApzc.mY.IsOverscrollAnimationAlive() &&
                     mApzc.mY.SampleOverscrollAnimation(
                         aDelta, mOverscrollSideBits & SideBits::eTopBottom);
    if (!continueX && !continueY) {
      // If we got into overscroll from a fling, that fling did not request a
      // fling snap to avoid a resulting scrollTo from cancelling the overscroll
      // animation too early. We do still want to request a fling snap, though,
      // in case the end of the axis at which we're overscrolled is not a valid
      // snap point, so we request one now. If there are no snap points, this
      // will do nothing. If there are snap points, we'll get a scrollTo that
      // snaps us back to the nearest valid snap point. The scroll snapping is
      // done in a deferred task, otherwise the state change to NOTHING caused
      // by the overscroll animation ending would clobber a possible state
      // change to SMOOTH_SCROLL in ScrollSnap().
      mDeferredTasks.AppendElement(NewRunnableMethod<ScrollSnapFlags>(
          "layers::AsyncPanZoomController::ScrollSnap", &mApzc,
          &AsyncPanZoomController::ScrollSnap,
          ScrollSnapFlags::IntendedDirection |
              ScrollSnapFlags::IntendedEndPosition));
      return false;
    }
    return true;
  }

  virtual bool WantsRepaints() override { return false; }

  // Tell the overscroll animation about the pan momentum event. For each axis,
  // the overscroll animation may start, stop, or continue managing that axis in
  // response to the pan momentum event
  void HandlePanMomentum(const ParentLayerPoint& aDisplacement) {
    float xOverscroll = mApzc.mX.GetOverscroll();
    if ((xOverscroll > 0 && aDisplacement.x > 0) ||
        (xOverscroll < 0 && aDisplacement.x < 0)) {
      if (!mApzc.mX.IsOverscrollAnimationRunning()) {
        // Start a new overscroll animation on this axis, if there is no
        // overscroll animation running and if the pan momentum displacement
        // the pan momentum displacement is the same direction of the current
        // overscroll.
        mApzc.mX.StartOverscrollAnimation(mApzc.mX.GetVelocity());
        mOverscrollSideBits |=
            xOverscroll > 0 ? SideBits::eRight : SideBits::eLeft;
      }
    } else if ((xOverscroll > 0 && aDisplacement.x < 0) ||
               (xOverscroll < 0 && aDisplacement.x > 0)) {
      // Otherwise, stop the animation in the direction so that it won't clobber
      // subsequent pan momentum scrolling.
      mApzc.mX.EndOverscrollAnimation();
    }

    // Same as above but for Y axis.
    float yOverscroll = mApzc.mY.GetOverscroll();
    if ((yOverscroll > 0 && aDisplacement.y > 0) ||
        (yOverscroll < 0 && aDisplacement.y < 0)) {
      if (!mApzc.mY.IsOverscrollAnimationRunning()) {
        mApzc.mY.StartOverscrollAnimation(mApzc.mY.GetVelocity());
        mOverscrollSideBits |=
            yOverscroll > 0 ? SideBits::eBottom : SideBits::eTop;
      }
    } else if ((yOverscroll > 0 && aDisplacement.y < 0) ||
               (yOverscroll < 0 && aDisplacement.y > 0)) {
      mApzc.mY.EndOverscrollAnimation();
    }
  }

  ScrollDirections GetDirections() const {
    ScrollDirections directions;
    if (mApzc.mX.IsOverscrollAnimationRunning()) {
      directions += ScrollDirection::eHorizontal;
    }
    if (mApzc.mY.IsOverscrollAnimationRunning()) {
      directions += ScrollDirection::eVertical;
    }
    return directions;
  };

  OverscrollAnimation* AsOverscrollAnimation() override { return this; }

  bool IsManagingXAxis() const {
    return mApzc.mX.IsOverscrollAnimationRunning();
  }
  bool IsManagingYAxis() const {
    return mApzc.mY.IsOverscrollAnimationRunning();
  }

 private:
  AsyncPanZoomController& mApzc;
  SideBits mOverscrollSideBits;
};

// Base class for different overscroll effects;
class OverscrollEffectBase {
 public:
  virtual ~OverscrollEffectBase() = default;

  // Try to increase the amount of overscroll by |aOverscroll|. Limited to
  // directions contained in |aOverscrollableDirections|. Components of
  // |aOverscroll| in directions that are successfully consumed are dropped.
  virtual void ConsumeOverscroll(
      ParentLayerPoint& aOverscroll,
      ScrollDirections aOverscrollableDirections) = 0;

  // Relieve overscroll. Depending on the implementation, the relief may
  // be immediate, or gradual (e.g. after an animation) but this starts
  // the process. |aVelocity| is the current velocity of the APZC, and
  // |aOverscrollSideBits| contains the side(s) at which the APZC is
  // overscrolled.
  virtual void RelieveOverscroll(const ParentLayerPoint& aVelocity,
                                 SideBits aOverscrollSideBits) = 0;

  virtual bool IsOverscrolled() const = 0;

  // Similarly to RelieveOverscroll(), but has immediate effect
  // (no animation).
  virtual void ClearOverscroll() = 0;
};

// A generic overscroll effect, implemented by AsyncPanZoomController itself.
class GenericOverscrollEffect : public OverscrollEffectBase {
 public:
  explicit GenericOverscrollEffect(AsyncPanZoomController& aApzc)
      : mApzc(aApzc) {}

  void ConsumeOverscroll(ParentLayerPoint& aOverscroll,
                         ScrollDirections aOverscrollableDirections) override {
    if (aOverscrollableDirections.contains(ScrollDirection::eHorizontal)) {
      mApzc.mX.OverscrollBy(aOverscroll.x);
      aOverscroll.x = 0;
    }

    if (aOverscrollableDirections.contains(ScrollDirection::eVertical)) {
      mApzc.mY.OverscrollBy(aOverscroll.y);
      aOverscroll.y = 0;
    }

    if (!aOverscrollableDirections.isEmpty()) {
      mApzc.ScheduleComposite();
    }
  }

  void RelieveOverscroll(const ParentLayerPoint& aVelocity,
                         SideBits aOverscrollSideBits) override {
    mApzc.StartOverscrollAnimation(aVelocity, aOverscrollSideBits);
  }

  bool IsOverscrolled() const override {
    return mApzc.IsPhysicallyOverscrolled();
  }

  void ClearOverscroll() override { mApzc.ClearPhysicalOverscroll(); }

 private:
  AsyncPanZoomController& mApzc;
};

// A widget-specific overscroll effect, implemented by the widget via
// GeckoContentController.
class WidgetOverscrollEffect : public OverscrollEffectBase {
 public:
  explicit WidgetOverscrollEffect(AsyncPanZoomController& aApzc)
      : mApzc(aApzc), mIsOverscrolled(false) {}

  void ConsumeOverscroll(ParentLayerPoint& aOverscroll,
                         ScrollDirections aOverscrollableDirections) override {
    RefPtr<GeckoContentController> controller =
        mApzc.GetGeckoContentController();
    if (controller && !aOverscrollableDirections.isEmpty()) {
      mIsOverscrolled = true;
      controller->UpdateOverscrollOffset(mApzc.GetGuid(), aOverscroll.x,
                                         aOverscroll.y, mApzc.IsRootContent());
      aOverscroll = ParentLayerPoint();
    }
  }

  void RelieveOverscroll(const ParentLayerPoint& aVelocity,
                         SideBits aOverscrollSideBits) override {
    RefPtr<GeckoContentController> controller =
        mApzc.GetGeckoContentController();
    // From APZC's point of view, consider it to no longer be overscrolled
    // as soon as RelieveOverscroll() is called. The widget may use a
    // delay or animation until the relieving of the overscroll is complete,
    // but we don't have any insight into that.
    mIsOverscrolled = false;
    if (controller) {
      controller->UpdateOverscrollVelocity(mApzc.GetGuid(), aVelocity.x,
                                           aVelocity.y, mApzc.IsRootContent());
    }
  }

  bool IsOverscrolled() const override { return mIsOverscrolled; }

  void ClearOverscroll() override {
    RelieveOverscroll(ParentLayerPoint(), SideBits() /* ignored */);
  }

 private:
  AsyncPanZoomController& mApzc;
  bool mIsOverscrolled;
};

}  // namespace layers
}  // namespace mozilla

#endif  // mozilla_layers_Overscroll_h