DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (dcc6d7a0dc00)

VCS Links

Line Code
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
/* -*- 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/. */

#include "SwipeTracker.h"

#include "InputData.h"
#include "mozilla/TimeStamp.h"
#include "mozilla/TouchEvents.h"
#include "nsAlgorithm.h"
#include "nsChildView.h"
#include "UnitTransforms.h"

// These values were tweaked to make the physics feel similar to the native swipe.
static const double kSpringForce = 250.0;
static const double kVelocityTwitchTolerance = 0.0000001;
static const double kWholePagePixelSize = 1000.0;
static const double kRubberBandResistanceFactor = 4.0;
static const double kSwipeSuccessThreshold = 0.25;
static const double kSwipeSuccessVelocityContribution = 0.3;

namespace mozilla {

static already_AddRefed<nsRefreshDriver>
GetRefreshDriver(nsIWidget& aWidget)
{
  nsIWidgetListener* widgetListener = aWidget.GetWidgetListener();
  nsIPresShell* presShell = widgetListener ? widgetListener->GetPresShell() : nullptr;
  nsPresContext* presContext = presShell ? presShell->GetPresContext() : nullptr;
  RefPtr<nsRefreshDriver> refreshDriver = presContext ? presContext->RefreshDriver() : nullptr;
  return refreshDriver.forget();
}

SwipeTracker::SwipeTracker(nsChildView& aWidget,
                           const PanGestureInput& aSwipeStartEvent,
                           uint32_t aAllowedDirections,
                           uint32_t aSwipeDirection)
  : mWidget(aWidget)
  , mRefreshDriver(GetRefreshDriver(mWidget))
  , mAxis(0.0, 0.0, 0.0, kSpringForce, 1.0)
  , mEventPosition(RoundedToInt(ViewAs<LayoutDevicePixel>(aSwipeStartEvent.mPanStartPoint,
                                  PixelCastJustification::LayoutDeviceIsScreenForUntransformedEvent)))
  , mLastEventTimeStamp(aSwipeStartEvent.mTimeStamp)
  , mAllowedDirections(aAllowedDirections)
  , mSwipeDirection(aSwipeDirection)
  , mGestureAmount(0.0)
  , mCurrentVelocity(0.0)
  , mEventsAreControllingSwipe(true)
  , mEventsHaveStartedNewGesture(false)
  , mRegisteredWithRefreshDriver(false)
{
  SendSwipeEvent(eSwipeGestureStart, 0, 0.0);
  ProcessEvent(aSwipeStartEvent);
}

void
SwipeTracker::Destroy()
{
  UnregisterFromRefreshDriver();
}

SwipeTracker::~SwipeTracker()
{
  MOZ_ASSERT(!mRegisteredWithRefreshDriver, "Destroy needs to be called before deallocating");
}

double
SwipeTracker::SwipeSuccessTargetValue() const
{
  return (mSwipeDirection == nsIDOMSimpleGestureEvent::DIRECTION_RIGHT) ? -1.0 : 1.0;
}

double
SwipeTracker::ClampToAllowedRange(double aGestureAmount) const
{
  // gestureAmount needs to stay between -1 and 0 when swiping right and
  // between 0 and 1 when swiping left.
  double min = (mSwipeDirection == nsIDOMSimpleGestureEvent::DIRECTION_RIGHT) ? -1.0 : 0.0;
  double max = (mSwipeDirection == nsIDOMSimpleGestureEvent::DIRECTION_LEFT) ? 1.0 : 0.0;
  return clamped(aGestureAmount, min, max);
}

bool
SwipeTracker::ComputeSwipeSuccess() const
{
  double targetValue = SwipeSuccessTargetValue();

  // If the fingers were moving away from the target direction when they were
  // lifted from the touchpad, abort the swipe.
  if (mCurrentVelocity * targetValue < -kVelocityTwitchTolerance) {
    return false;
  }

  return (mGestureAmount * targetValue +
          mCurrentVelocity * targetValue * kSwipeSuccessVelocityContribution) >= kSwipeSuccessThreshold;
}

nsEventStatus
SwipeTracker::ProcessEvent(const PanGestureInput& aEvent)
{
  // If the fingers have already been lifted, don't process this event for swiping.
  if (!mEventsAreControllingSwipe) {
    // Return nsEventStatus_eConsumeNoDefault for events from the swipe gesture
    // and nsEventStatus_eIgnore for events of subsequent scroll gestures.
    if (aEvent.mType == PanGestureInput::PANGESTURE_MAYSTART ||
        aEvent.mType == PanGestureInput::PANGESTURE_START) {
      mEventsHaveStartedNewGesture = true;
    }
    return mEventsHaveStartedNewGesture ? nsEventStatus_eIgnore : nsEventStatus_eConsumeNoDefault;
  }

  double delta = -aEvent.mPanDisplacement.x / mWidget.BackingScaleFactor() / kWholePagePixelSize;
  if (!SwipingInAllowedDirection()) {
    delta /= kRubberBandResistanceFactor;
  }
  mGestureAmount = ClampToAllowedRange(mGestureAmount + delta);
  SendSwipeEvent(eSwipeGestureUpdate, 0, mGestureAmount);

  if (aEvent.mType != PanGestureInput::PANGESTURE_END) {
    double elapsedSeconds = std::max(0.008, (aEvent.mTimeStamp - mLastEventTimeStamp).ToSeconds());
    mCurrentVelocity = delta / elapsedSeconds;
    mLastEventTimeStamp = aEvent.mTimeStamp;
  } else {
    mEventsAreControllingSwipe = false;
    bool didSwipeSucceed = SwipingInAllowedDirection() && ComputeSwipeSuccess();
    double targetValue = 0.0;
    if (didSwipeSucceed) {
      SendSwipeEvent(eSwipeGesture, mSwipeDirection, 0.0);
      targetValue = SwipeSuccessTargetValue();
    }
    StartAnimating(targetValue);
  }

  return nsEventStatus_eConsumeNoDefault;
}

void
SwipeTracker::StartAnimating(double aTargetValue)
{
  mAxis.SetPosition(mGestureAmount);
  mAxis.SetDestination(aTargetValue);
  mAxis.SetVelocity(mCurrentVelocity);

  mLastAnimationFrameTime = TimeStamp::Now();

  // Add ourselves as a refresh driver observer. The refresh driver
  // will call WillRefresh for each animation frame until we
  // unregister ourselves.
  MOZ_ASSERT(!mRegisteredWithRefreshDriver);
  if (mRefreshDriver) {
    mRefreshDriver->AddRefreshObserver(this, Flush_Style);
    mRegisteredWithRefreshDriver = true;
  }
}

void
SwipeTracker::WillRefresh(mozilla::TimeStamp aTime)
{
  TimeStamp now = TimeStamp::Now();
  mAxis.Simulate(now - mLastAnimationFrameTime);
  mLastAnimationFrameTime = now;

  bool isFinished = mAxis.IsFinished(1.0 / kWholePagePixelSize);
  mGestureAmount = (isFinished ? mAxis.GetDestination() : mAxis.GetPosition());
  SendSwipeEvent(eSwipeGestureUpdate, 0, mGestureAmount);

  if (isFinished) {
    UnregisterFromRefreshDriver();
    SwipeFinished();
  }
}

void
SwipeTracker::CancelSwipe()
{
  SendSwipeEvent(eSwipeGestureEnd, 0, 0.0);
}

void SwipeTracker::SwipeFinished()
{
  SendSwipeEvent(eSwipeGestureEnd, 0, 0.0);
  mWidget.SwipeFinished();
}

void
SwipeTracker::UnregisterFromRefreshDriver()
{
  if (mRegisteredWithRefreshDriver) {
    MOZ_ASSERT(mRefreshDriver, "How were we able to register, then?");
    mRefreshDriver->RemoveRefreshObserver(this, Flush_Style);
  }
  mRegisteredWithRefreshDriver = false;
}

/* static */ WidgetSimpleGestureEvent
SwipeTracker::CreateSwipeGestureEvent(EventMessage aMsg, nsIWidget* aWidget,
                                      const LayoutDeviceIntPoint& aPosition)
{
  WidgetSimpleGestureEvent geckoEvent(true, aMsg, aWidget);
  geckoEvent.modifiers = 0;
  geckoEvent.timeStamp = TimeStamp::Now();
  geckoEvent.refPoint = aPosition;
  geckoEvent.buttons = 0;
  return geckoEvent;
}

bool
SwipeTracker::SendSwipeEvent(EventMessage aMsg, uint32_t aDirection, double aDelta)
{
  WidgetSimpleGestureEvent geckoEvent =
    CreateSwipeGestureEvent(aMsg, &mWidget, mEventPosition);
  geckoEvent.direction = aDirection;
  geckoEvent.delta = aDelta;
  geckoEvent.allowedDirections = mAllowedDirections;
  return mWidget.DispatchWindowEvent(geckoEvent);
}

} // namespace mozilla