tor-browser

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

ScrollPositionUpdate.h (5912B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #ifndef mozilla_ScrollPositionUpdate_h_
      6 #define mozilla_ScrollPositionUpdate_h_
      7 
      8 #include <cstdint>
      9 #include <iosfwd>
     10 
     11 #include "Units.h"
     12 #include "mozilla/DefineEnum.h"
     13 #include "mozilla/RelativeTo.h"
     14 #include "mozilla/ScrollGeneration.h"
     15 #include "mozilla/ScrollOrigin.h"
     16 #include "mozilla/ScrollSnapTargetId.h"
     17 #include "mozilla/ScrollTypes.h"
     18 #include "nsPoint.h"
     19 
     20 namespace IPC {
     21 template <typename T>
     22 struct ParamTraits;
     23 }  // namespace IPC
     24 
     25 namespace mozilla {
     26 
     27 MOZ_DEFINE_ENUM_CLASS_WITH_BASE_AND_TOSTRING(
     28    ScrollUpdateType, uint8_t,
     29    (
     30        // A scroll update to a specific destination, regardless of the current
     31        // scroll position.
     32        Absolute,
     33        // A scroll update by a specific amount, based off a given starting
     34        // scroll position. XXX Fold this into PureRelative, it should be
     35        // relatively straightforward after bug 1655733.
     36        Relative,
     37        // A scroll update by a specific amount, where only the delta is
     38        // provided. The delta should be applied to whatever the current scroll
     39        // position is on the receiver side.
     40        PureRelative));
     41 
     42 enum class ScrollTriggeredByScript : bool { No, Yes };
     43 
     44 /**
     45 * This class represents an update to the scroll position that is initiated by
     46 * something on the main thread. A list of these classes is accumulated by
     47 * scrollframes on the main thread, and the list is sent over as part of a
     48 * paint transaction to the compositor. The compositor can then iterate through
     49 * the scroll updates and apply/merge them with scrolling that has already
     50 * occurred independently on the compositor side.
     51 */
     52 class ScrollPositionUpdate {
     53  friend struct IPC::ParamTraits<mozilla::ScrollPositionUpdate>;
     54 
     55 public:
     56  // Constructor for IPC use only.
     57  explicit ScrollPositionUpdate();
     58 
     59  // Create a ScrollPositionUpdate for a newly created (or reconstructed)
     60  // scrollframe.
     61  static ScrollPositionUpdate NewScrollframe(nsPoint aInitialPosition);
     62  // Create a ScrollPositionUpdate for a new absolute/instant scroll, to
     63  // the given destination.
     64  static ScrollPositionUpdate NewScroll(ScrollOrigin aOrigin,
     65                                        nsPoint aDestination);
     66  // Create a ScrollPositionUpdate for a new relative/instant scroll, with
     67  // the given source/destination.
     68  static ScrollPositionUpdate NewRelativeScroll(nsPoint aSource,
     69                                                nsPoint aDestination);
     70  // Create a ScrollPositionUpdate for a new absolute/smooth scroll, which
     71  // animates smoothly to the given destination from whatever the current
     72  // scroll position is in the receiver.
     73  // If the smooth operation involves snapping to |aDestination|,
     74  // |aSnapTargetIds| has snap-target-ids for snapping. Once after this smooth
     75  // scroll finished on the target APZC, the ids will be reported back to the
     76  // main-thread as the last snap target ids which will be used for re-snapping
     77  // to the same snapped element(s).
     78  static ScrollPositionUpdate NewSmoothScroll(
     79      ScrollMode aMode, ScrollOrigin aOrigin, nsPoint aDestination,
     80      ScrollTriggeredByScript aTriggeredByScript,
     81      UniquePtr<ScrollSnapTargetIds> aSnapTargetIds,
     82      ViewportType aViewportToScroll);
     83  // Create a ScrollPositionUpdate for a new pure-relative scroll. The
     84  // aMode parameter controls whether or not this is a smooth animation or
     85  // instantaneous scroll.
     86  static ScrollPositionUpdate NewPureRelativeScroll(ScrollOrigin aOrigin,
     87                                                    ScrollMode aMode,
     88                                                    const nsPoint& aDelta);
     89 
     90  bool operator==(const ScrollPositionUpdate& aOther) const;
     91 
     92  MainThreadScrollGeneration GetGeneration() const;
     93  ScrollUpdateType GetType() const;
     94  ScrollMode GetMode() const;
     95  ScrollOrigin GetOrigin() const;
     96  // GetDestination is only valid for Absolute and Relative types; it asserts
     97  // otherwise.
     98  CSSPoint GetDestination() const;
     99  // GetSource is only valid for the Relative type; it asserts otherwise.
    100  CSSPoint GetSource() const;
    101  // GetDelta is only valid for the PureRelative type; it asserts otherwise.
    102  CSSPoint GetDelta() const;
    103 
    104  ViewportType GetViewportType() const { return mViewportType; }
    105  ScrollTriggeredByScript GetScrollTriggeredByScript() const {
    106    return mTriggeredByScript;
    107  }
    108  bool WasTriggeredByScript() const {
    109    return mTriggeredByScript == ScrollTriggeredByScript::Yes;
    110  }
    111  const ScrollSnapTargetIds& GetSnapTargetIds() const { return mSnapTargetIds; }
    112 
    113  friend std::ostream& operator<<(std::ostream& aStream,
    114                                  const ScrollPositionUpdate& aUpdate);
    115 
    116 private:
    117  MainThreadScrollGeneration mScrollGeneration;
    118  // Refer to the ScrollUpdateType documentation for what the types mean.
    119  // All fields are populated for all types, except as noted below.
    120  ScrollUpdateType mType;
    121  ScrollMode mScrollMode;
    122  ScrollOrigin mScrollOrigin;
    123  // mDestination is not populated when mType == PureRelative.
    124  CSSPoint mDestination;
    125  // mSource is not populated when mType == Absolute || mType == PureRelative.
    126  CSSPoint mSource;
    127  // mDelta is not populated when mType == Absolute || mType == Relative.
    128  CSSPoint mDelta;
    129  // Specifies whether mDestination, mSource, and mDelta should be interpreted
    130  // as applying to the layout viewport or the visual viewport.
    131  // Currently, updates to the visual viewport offset are only supported for
    132  // smooth scroll updates (mScrollMode is Smooth or SmoothMsd).
    133  ViewportType mViewportType = ViewportType::Layout;
    134  ScrollTriggeredByScript mTriggeredByScript;
    135  ScrollSnapTargetIds mSnapTargetIds;
    136 };
    137 
    138 }  // namespace mozilla
    139 
    140 #endif  // mozilla_ScrollPositionUpdate_h_