ScrollPositionUpdate.cpp (5078B)
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 #include "ScrollPositionUpdate.h" 6 7 #include <ostream> 8 9 #include "mozilla/Assertions.h" 10 11 namespace mozilla { 12 13 static ScrollGenerationCounter sGenerationCounter; 14 15 ScrollPositionUpdate::ScrollPositionUpdate() 16 : mType(ScrollUpdateType::Absolute), 17 mScrollMode(ScrollMode::Normal), 18 mScrollOrigin(ScrollOrigin::None), 19 mTriggeredByScript(ScrollTriggeredByScript::No) {} 20 21 /*static*/ 22 ScrollPositionUpdate ScrollPositionUpdate::NewScrollframe( 23 nsPoint aInitialPosition) { 24 ScrollPositionUpdate ret; 25 ret.mScrollGeneration = sGenerationCounter.NewMainThreadGeneration(); 26 ret.mScrollMode = ScrollMode::Instant; 27 ret.mDestination = CSSPoint::FromAppUnits(aInitialPosition); 28 return ret; 29 } 30 31 /*static*/ 32 ScrollPositionUpdate ScrollPositionUpdate::NewScroll(ScrollOrigin aOrigin, 33 nsPoint aDestination) { 34 MOZ_ASSERT(aOrigin != ScrollOrigin::NotSpecified); 35 MOZ_ASSERT(aOrigin != ScrollOrigin::None); 36 37 ScrollPositionUpdate ret; 38 ret.mScrollGeneration = sGenerationCounter.NewMainThreadGeneration(); 39 ret.mType = ScrollUpdateType::Absolute; 40 ret.mScrollMode = ScrollMode::Instant; 41 ret.mScrollOrigin = aOrigin; 42 ret.mDestination = CSSPoint::FromAppUnits(aDestination); 43 return ret; 44 } 45 46 /*static*/ 47 ScrollPositionUpdate ScrollPositionUpdate::NewRelativeScroll( 48 nsPoint aSource, nsPoint aDestination) { 49 ScrollPositionUpdate ret; 50 ret.mScrollGeneration = sGenerationCounter.NewMainThreadGeneration(); 51 ret.mType = ScrollUpdateType::Relative; 52 ret.mScrollMode = ScrollMode::Instant; 53 ret.mScrollOrigin = ScrollOrigin::Relative; 54 ret.mDestination = CSSPoint::FromAppUnits(aDestination); 55 ret.mSource = CSSPoint::FromAppUnits(aSource); 56 return ret; 57 } 58 59 /*static*/ 60 ScrollPositionUpdate ScrollPositionUpdate::NewSmoothScroll( 61 ScrollMode aMode, ScrollOrigin aOrigin, nsPoint aDestination, 62 ScrollTriggeredByScript aTriggeredByScript, 63 UniquePtr<ScrollSnapTargetIds> aSnapTargetIds, 64 ViewportType aViewportToScroll) { 65 MOZ_ASSERT(aOrigin != ScrollOrigin::NotSpecified); 66 MOZ_ASSERT(aOrigin != ScrollOrigin::None); 67 MOZ_ASSERT(aMode == ScrollMode::Smooth || aMode == ScrollMode::SmoothMsd); 68 69 ScrollPositionUpdate ret; 70 ret.mScrollGeneration = sGenerationCounter.NewMainThreadGeneration(); 71 ret.mType = ScrollUpdateType::Absolute; 72 ret.mScrollMode = aMode; 73 ret.mScrollOrigin = aOrigin; 74 ret.mDestination = CSSPoint::FromAppUnits(aDestination); 75 ret.mViewportType = aViewportToScroll; 76 ret.mTriggeredByScript = aTriggeredByScript; 77 if (aSnapTargetIds) { 78 ret.mSnapTargetIds = *aSnapTargetIds; 79 } 80 return ret; 81 } 82 83 /*static*/ 84 ScrollPositionUpdate ScrollPositionUpdate::NewPureRelativeScroll( 85 ScrollOrigin aOrigin, ScrollMode aMode, const nsPoint& aDelta) { 86 MOZ_ASSERT(aOrigin != ScrollOrigin::NotSpecified); 87 MOZ_ASSERT(aOrigin != ScrollOrigin::None); 88 89 ScrollPositionUpdate ret; 90 ret.mScrollGeneration = sGenerationCounter.NewMainThreadGeneration(); 91 ret.mType = ScrollUpdateType::PureRelative; 92 ret.mScrollMode = aMode; 93 // Pure relative scrolls always apply to the visual viewport. 94 // FIXME: Should they? (Bug 1984224) 95 ret.mViewportType = ViewportType::Visual; 96 ret.mScrollOrigin = aOrigin; 97 ret.mDelta = CSSPoint::FromAppUnits(aDelta); 98 return ret; 99 } 100 101 bool ScrollPositionUpdate::operator==( 102 const ScrollPositionUpdate& aOther) const { 103 // instances are immutable, and all the fields are set when the generation 104 // is set. So if the generation matches, these instances are identical. 105 return mScrollGeneration == aOther.mScrollGeneration; 106 } 107 108 MainThreadScrollGeneration ScrollPositionUpdate::GetGeneration() const { 109 return mScrollGeneration; 110 } 111 112 ScrollUpdateType ScrollPositionUpdate::GetType() const { return mType; } 113 114 ScrollMode ScrollPositionUpdate::GetMode() const { return mScrollMode; } 115 116 ScrollOrigin ScrollPositionUpdate::GetOrigin() const { return mScrollOrigin; } 117 118 CSSPoint ScrollPositionUpdate::GetDestination() const { 119 MOZ_ASSERT(mType == ScrollUpdateType::Absolute || 120 mType == ScrollUpdateType::Relative); 121 return mDestination; 122 } 123 124 CSSPoint ScrollPositionUpdate::GetSource() const { 125 MOZ_ASSERT(mType == ScrollUpdateType::Relative); 126 return mSource; 127 } 128 129 CSSPoint ScrollPositionUpdate::GetDelta() const { 130 MOZ_ASSERT(mType == ScrollUpdateType::PureRelative); 131 return mDelta; 132 } 133 134 std::ostream& operator<<(std::ostream& aStream, 135 const ScrollPositionUpdate& aUpdate) { 136 aStream << "{ gen=" << aUpdate.mScrollGeneration << ", type=" << aUpdate.mType 137 << ", mode=" << aUpdate.mScrollMode 138 << ", origin=" << aUpdate.mScrollOrigin 139 << ", dst=" << aUpdate.mDestination << ", src=" << aUpdate.mSource 140 << ", delta=" << aUpdate.mDelta 141 << ", triggered by script=" << aUpdate.WasTriggeredByScript() << " }"; 142 return aStream; 143 } 144 145 } // namespace mozilla