tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

DesktopFlingPhysics.h (2321B)


      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/. */
      6 
      7 #ifndef mozilla_layers_DesktopFlingPhysics_h_
      8 #define mozilla_layers_DesktopFlingPhysics_h_
      9 
     10 #include "AsyncPanZoomController.h"
     11 #include "Units.h"
     12 #include "mozilla/StaticPrefs_apz.h"
     13 
     14 namespace mozilla {
     15 namespace layers {
     16 
     17 class DesktopFlingPhysics {
     18 public:
     19  void Init(const ParentLayerPoint& aStartingVelocity,
     20            float aPLPPI /* unused */) {
     21    mVelocity = aStartingVelocity;
     22  }
     23  void Sample(const TimeDuration& aDelta, ParentLayerPoint* aOutVelocity,
     24              ParentLayerPoint* aOutOffset) {
     25    float friction = StaticPrefs::apz_fling_friction();
     26    float threshold = StaticPrefs::apz_fling_stopped_threshold();
     27 
     28    mVelocity = ParentLayerPoint(
     29        ApplyFrictionOrCancel(mVelocity.x, aDelta, friction, threshold),
     30        ApplyFrictionOrCancel(mVelocity.y, aDelta, friction, threshold));
     31 
     32    *aOutVelocity = mVelocity;
     33    *aOutOffset = mVelocity * aDelta.ToMilliseconds();
     34  }
     35 
     36 private:
     37  /**
     38   * Applies friction to the given velocity and returns the result, or
     39   * returns zero if the velocity is too low.
     40   * |aVelocity| is the incoming velocity.
     41   * |aDelta| is the amount of time that has passed since the last time
     42   * friction was applied.
     43   * |aFriction| is the amount of friction to apply.
     44   * |aThreshold| is the velocity below which the fling is cancelled.
     45   */
     46  static float ApplyFrictionOrCancel(float aVelocity,
     47                                     const TimeDuration& aDelta,
     48                                     float aFriction, float aThreshold) {
     49    if (fabsf(aVelocity) <= aThreshold) {
     50      // If the velocity is very low, just set it to 0 and stop the fling,
     51      // otherwise we'll just asymptotically approach 0 and the user won't
     52      // actually see any changes.
     53      return 0.0f;
     54    }
     55 
     56    aVelocity *= pow(1.0f - aFriction, float(aDelta.ToMilliseconds()));
     57    return aVelocity;
     58  }
     59 
     60  ParentLayerPoint mVelocity;
     61 };
     62 
     63 }  // namespace layers
     64 }  // namespace mozilla
     65 
     66 #endif  // mozilla_layers_DesktopFlingPhysics_h_