SmoothScrollAnimation.h (4374B)
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_SmoothScrollAnimation_h_ 8 #define mozilla_layers_SmoothScrollAnimation_h_ 9 10 #include "AsyncPanZoomAnimation.h" 11 #include "InputData.h" 12 #include "ScrollPositionUpdate.h" 13 #include "mozilla/AlreadyAddRefed.h" 14 #include "mozilla/RelativeTo.h" 15 #include "mozilla/ScrollOrigin.h" 16 #include "mozilla/layers/APZPublicUtils.h" 17 #include "mozilla/layers/KeyboardScrollAction.h" 18 19 namespace mozilla { 20 21 class ScrollAnimationPhysics; 22 23 namespace layers { 24 25 class AsyncPanZoomController; 26 27 class SmoothScrollAnimation : public AsyncPanZoomAnimation { 28 public: 29 using ScrollAnimationKind = apz::ScrollAnimationKind; 30 31 // Create a SmoothScrollAnimation of kind Smooth or SmoothMsd. 32 // The origin is ignored for SmoothMsd animations. 33 static already_AddRefed<SmoothScrollAnimation> Create( 34 AsyncPanZoomController& aApzc, ScrollAnimationKind aKind, 35 ViewportType aViewportToScroll, ScrollOrigin aOrigin); 36 // Create a SmoothScrollAnimation of kind Keyboard. 37 static already_AddRefed<SmoothScrollAnimation> CreateForKeyboard( 38 AsyncPanZoomController& aApzc, ScrollOrigin aOrigin); 39 // Create a SmoothScrollAnimation of kind Wheel. 40 static already_AddRefed<SmoothScrollAnimation> CreateForWheel( 41 AsyncPanZoomController& aApzc, 42 ScrollWheelInput::ScrollDeltaType aDeltaType); 43 44 void UpdateDestinationAndSnapTargets( 45 TimeStamp aTime, const nsPoint& aDestination, 46 const nsSize& aCurrentVelocity, ScrollSnapTargetIds&& aSnapTargetIds, 47 ScrollTriggeredByScript aTriggeredByScript); 48 49 SmoothScrollAnimation* AsSmoothScrollAnimation() override; 50 bool WasTriggeredByScript() const override { 51 return mTriggeredByScript == ScrollTriggeredByScript::Yes; 52 } 53 ScrollAnimationKind Kind() const { return mKind; } 54 ViewportType ViewportToScroll() const { return mViewportToScroll; } 55 ScrollSnapTargetIds TakeSnapTargetIds() { return std::move(mSnapTargetIds); } 56 ScrollOrigin GetScrollOrigin() const; 57 static ScrollOrigin GetScrollOriginForAction( 58 KeyboardScrollAction::KeyboardScrollActionType aAction); 59 60 bool DoSample(FrameMetrics& aFrameMetrics, 61 const TimeDuration& aDelta) override; 62 63 bool HandleScrollOffsetUpdate(const Maybe<CSSPoint>& aRelativeDelta) override; 64 65 void UpdateDelta(TimeStamp aTime, const nsPoint& aDelta, 66 const nsSize& aCurrentVelocity); 67 void UpdateDestination(TimeStamp aTime, const nsPoint& aDestination, 68 const nsSize& aCurrentVelocity); 69 70 CSSPoint GetDestination() const { 71 return CSSPoint::FromAppUnits(mFinalDestination); 72 } 73 74 // If we need to perform an animation of the same kind and the specified 75 // parameters, can we extend this existing animation? 76 bool CanExtend(ViewportType aViewportToScroll, ScrollOrigin aOrigin) const; 77 78 private: 79 SmoothScrollAnimation(ScrollAnimationKind aKind, 80 AsyncPanZoomController& aApzc, 81 ViewportType aViewportToScroll, ScrollOrigin aOrigin); 82 83 void Update(TimeStamp aTime, const nsSize& aCurrentVelocity); 84 CSSPoint GetViewportOffset(const FrameMetrics& aMetrics) const; 85 86 ScrollAnimationKind mKind; 87 // Whether the animation is scroling the visual viewport or the layout 88 // viewport. 89 ViewportType mViewportToScroll; 90 AsyncPanZoomController& mApzc; 91 UniquePtr<ScrollAnimationPhysics> mAnimationPhysics; 92 nsPoint mFinalDestination; 93 // If a direction is forced to overscroll, it means it's axis in that 94 // direction is locked, and scroll in that direction is treated as overscroll 95 // of an equal amount, which, for example, may then bubble up a scroll action 96 // to its parent, or may behave as whatever an overscroll occurence requires 97 // to behave 98 Maybe<ScrollDirection> mDirectionForcedToOverscroll; 99 ScrollOrigin mOrigin; 100 101 // These fields are only used for animations of kind Smooth and SmoothMsd. 102 ScrollSnapTargetIds mSnapTargetIds; 103 ScrollTriggeredByScript mTriggeredByScript; 104 }; 105 106 } // namespace layers 107 } // namespace mozilla 108 109 #endif // mozilla_layers_SmoothScrollAnimation_h_