From a871c68b3205abc9fc79fa858b7008ce8fbd3228 Mon Sep 17 00:00:00 2001 From: Hiroyuki Ikezoe Date: Tue, 12 Sep 2023 01:33:18 +0000 Subject: [PATCH] Bug 1768393 - Introduce SnapPoint to wrap a pair of Maybe. r=botond Differential Revision: https://phabricator.services.mozilla.com/D184337 --- gfx/layers/ipc/LayersMessageUtils.h | 20 +++++++++++--- layout/generic/ScrollSnap.cpp | 52 ++++++++++++++++++------------------- layout/generic/ScrollSnapInfo.cpp | 12 ++++----- layout/generic/ScrollSnapInfo.h | 27 +++++++++++++------ 4 files changed, 67 insertions(+), 44 deletions(-) diff --git a/gfx/layers/ipc/LayersMessageUtils.h b/gfx/layers/ipc/LayersMessageUtils.h index cba1d3fedd31..bc4b02b43f37 100644 --- a/gfx/layers/ipc/LayersMessageUtils.h +++ b/gfx/layers/ipc/LayersMessageUtils.h @@ -409,20 +409,32 @@ struct ParamTraits : public PlainOldDataSerializer {}; template <> +struct ParamTraits { + typedef mozilla::SnapPoint paramType; + + static void Write(MessageWriter* aWriter, const paramType& aParam) { + WriteParam(aWriter, aParam.mX); + WriteParam(aWriter, aParam.mY); + } + + static bool Read(MessageReader* aReader, paramType* aResult) { + return ReadParam(aReader, &aResult->mX) && ReadParam(aReader, &aResult->mY); + } +}; + +template <> struct ParamTraits { typedef mozilla::ScrollSnapInfo::SnapTarget paramType; static void Write(MessageWriter* aWriter, const paramType& aParam) { - WriteParam(aWriter, aParam.mSnapPositionX); - WriteParam(aWriter, aParam.mSnapPositionY); + WriteParam(aWriter, aParam.mSnapPoint); WriteParam(aWriter, aParam.mSnapArea); WriteParam(aWriter, aParam.mScrollSnapStop); WriteParam(aWriter, aParam.mTargetId); } static bool Read(MessageReader* aReader, paramType* aResult) { - return ReadParam(aReader, &aResult->mSnapPositionX) && - ReadParam(aReader, &aResult->mSnapPositionY) && + return ReadParam(aReader, &aResult->mSnapPoint) && ReadParam(aReader, &aResult->mSnapArea) && ReadParam(aReader, &aResult->mScrollSnapStop) && ReadParam(aReader, &aResult->mTargetId); diff --git a/layout/generic/ScrollSnap.cpp b/layout/generic/ScrollSnap.cpp index 5b027dd0376e..e2fcc0e19540 100644 --- a/layout/generic/ScrollSnap.cpp +++ b/layout/generic/ScrollSnap.cpp @@ -301,15 +301,15 @@ static void ProcessSnapPositions(CalcSnapPoints& aCalcSnapPoints, const ScrollSnapInfo& aSnapInfo) { aSnapInfo.ForEachValidTargetFor( aCalcSnapPoints.Destination(), [&](const auto& aTarget) -> bool { - if (aTarget.mSnapPositionX && aSnapInfo.mScrollSnapStrictnessX != - StyleScrollSnapStrictness::None) { - aCalcSnapPoints.AddVerticalEdge({*aTarget.mSnapPositionX, + if (aTarget.mSnapPoint.mX && aSnapInfo.mScrollSnapStrictnessX != + StyleScrollSnapStrictness::None) { + aCalcSnapPoints.AddVerticalEdge({*aTarget.mSnapPoint.mX, aTarget.mScrollSnapStop, aTarget.mTargetId}); } - if (aTarget.mSnapPositionY && aSnapInfo.mScrollSnapStrictnessY != - StyleScrollSnapStrictness::None) { - aCalcSnapPoints.AddHorizontalEdge({*aTarget.mSnapPositionY, + if (aTarget.mSnapPoint.mY && aSnapInfo.mScrollSnapStrictnessY != + StyleScrollSnapStrictness::None) { + aCalcSnapPoints.AddHorizontalEdge({*aTarget.mSnapPoint.mY, aTarget.mScrollSnapStop, aTarget.mTargetId}); } @@ -410,22 +410,22 @@ static std::pair, Maybe> GetCandidateInLastTargets( Maybe x, y; aSnapInfo.ForEachValidTargetFor( aCurrentPosition, [&](const auto& aTarget) -> bool { - if (aTarget.mSnapPositionX && aSnapInfo.mScrollSnapStrictnessX != - StyleScrollSnapStrictness::None) { + if (aTarget.mSnapPoint.mX && aSnapInfo.mScrollSnapStrictnessX != + StyleScrollSnapStrictness::None) { if (aLastSnapTargetIds->mIdsOnX.Contains(aTarget.mTargetId)) { if (targetIdForFocusedContent == aTarget.mTargetId) { // If we've already found the candidate on Y axis, but if snapping // to the point results this target is scrolled out, we can't use // it. if ((y && !aTarget.mSnapArea.Intersects( - nsRect(nsPoint(*aTarget.mSnapPositionX, *y), + nsRect(nsPoint(*aTarget.mSnapPoint.mX, *y), aSnapInfo.mSnapportSize)))) { y.reset(); } focusedTarget = &aTarget; // If the focused one is valid, then it's the candidate. - x = aTarget.mSnapPositionX; + x = aTarget.mSnapPoint.mX; } if (!x) { @@ -436,15 +436,15 @@ static std::pair, Maybe> GetCandidateInLastTargets( // candidate position result the target element is visible // inside the snapport. if (!y || (y && aTarget.mSnapArea.Intersects( - nsRect(nsPoint(*aTarget.mSnapPositionX, *y), + nsRect(nsPoint(*aTarget.mSnapPoint.mX, *y), aSnapInfo.mSnapportSize)))) { - x = aTarget.mSnapPositionX; + x = aTarget.mSnapPoint.mX; } } } } - if (aTarget.mSnapPositionY && aSnapInfo.mScrollSnapStrictnessY != - StyleScrollSnapStrictness::None) { + if (aTarget.mSnapPoint.mY && aSnapInfo.mScrollSnapStrictnessY != + StyleScrollSnapStrictness::None) { if (aLastSnapTargetIds->mIdsOnY.Contains(aTarget.mTargetId)) { if (targetIdForFocusedContent == aTarget.mTargetId) { NS_ASSERTION( @@ -456,20 +456,20 @@ static std::pair, Maybe> GetCandidateInLastTargets( // is scrolled out, we can't use it. if (!focusedTarget && (x && !aTarget.mSnapArea.Intersects( - nsRect(nsPoint(*x, *aTarget.mSnapPositionY), + nsRect(nsPoint(*x, *aTarget.mSnapPoint.mY), aSnapInfo.mSnapportSize)))) { x.reset(); } focusedTarget = &aTarget; - y = aTarget.mSnapPositionY; + y = aTarget.mSnapPoint.mY; } if (!y) { if (!x || (x && aTarget.mSnapArea.Intersects( - nsRect(nsPoint(*x, *aTarget.mSnapPositionY), + nsRect(nsPoint(*x, *aTarget.mSnapPoint.mY), aSnapInfo.mSnapportSize)))) { - y = aTarget.mSnapPositionY; + y = aTarget.mSnapPoint.mY; } } } @@ -523,17 +523,17 @@ Maybe ScrollSnapUtils::GetSnapPointForResnap( aSnapInfo.ForEachValidTargetFor( newPosition, [&, &x = x, &y = y](const auto& aTarget) -> bool { - if (!x && aTarget.mSnapPositionX && + if (!x && aTarget.mSnapPoint.mX && aSnapInfo.mScrollSnapStrictnessX != StyleScrollSnapStrictness::None) { - calcSnapPoints.AddVerticalEdge({*aTarget.mSnapPositionX, + calcSnapPoints.AddVerticalEdge({*aTarget.mSnapPoint.mX, aTarget.mScrollSnapStop, aTarget.mTargetId}); } - if (!y && aTarget.mSnapPositionY && + if (!y && aTarget.mSnapPoint.mY && aSnapInfo.mScrollSnapStrictnessY != StyleScrollSnapStrictness::None) { - calcSnapPoints.AddHorizontalEdge({*aTarget.mSnapPositionY, + calcSnapPoints.AddHorizontalEdge({*aTarget.mSnapPoint.mY, aTarget.mScrollSnapStop, aTarget.mTargetId}); } @@ -554,17 +554,17 @@ Maybe ScrollSnapUtils::GetSnapPointForResnap( // position. aSnapInfo.ForEachValidTargetFor( snapTarget.mPosition, [&, &x = x, &y = y](const auto& aTarget) -> bool { - if (aTarget.mSnapPositionX && + if (aTarget.mSnapPoint.mX && aSnapInfo.mScrollSnapStrictnessX != StyleScrollSnapStrictness::None && - aTarget.mSnapPositionX == x) { + aTarget.mSnapPoint.mX == x) { snapTarget.mTargetIds.mIdsOnX.AppendElement(aTarget.mTargetId); } - if (aTarget.mSnapPositionY && + if (aTarget.mSnapPoint.mY && aSnapInfo.mScrollSnapStrictnessY != StyleScrollSnapStrictness::None && - aTarget.mSnapPositionY == y) { + aTarget.mSnapPoint.mY == y) { snapTarget.mTargetIds.mIdsOnY.AppendElement(aTarget.mTargetId); } return true; diff --git a/layout/generic/ScrollSnapInfo.cpp b/layout/generic/ScrollSnapInfo.cpp index acbaf71906cd..7f9f410c3367 100644 --- a/layout/generic/ScrollSnapInfo.cpp +++ b/layout/generic/ScrollSnapInfo.cpp @@ -28,9 +28,9 @@ bool ScrollSnapInfo::HasSnapPositions() const { } for (const auto& target : mSnapTargets) { - if ((target.mSnapPositionX && + if ((target.mSnapPoint.mX && mScrollSnapStrictnessX != StyleScrollSnapStrictness::None) || - (target.mSnapPositionY && + (target.mSnapPoint.mY && mScrollSnapStrictnessY != StyleScrollSnapStrictness::None)) { return true; } @@ -81,12 +81,12 @@ void ScrollSnapInfo::ForEachValidTargetFor( for (const auto& target : mSnapTargets) { nsPoint snapPoint( mScrollSnapStrictnessX != StyleScrollSnapStrictness::None && - target.mSnapPositionX - ? *target.mSnapPositionX + target.mSnapPoint.mX + ? *target.mSnapPoint.mX : aDestination.x, mScrollSnapStrictnessY != StyleScrollSnapStrictness::None && - target.mSnapPositionY - ? *target.mSnapPositionY + target.mSnapPoint.mY + ? *target.mSnapPoint.mY : aDestination.y); nsRect snappedPort = nsRect(snapPoint, mSnapportSize); // Ignore snap points if snapping to the point would leave the snap area diff --git a/layout/generic/ScrollSnapInfo.h b/layout/generic/ScrollSnapInfo.h index d2125c8b35dd..666bad1610d9 100644 --- a/layout/generic/ScrollSnapInfo.h +++ b/layout/generic/ScrollSnapInfo.h @@ -12,10 +12,10 @@ #include "mozilla/ScrollSnapTargetId.h" #include "mozilla/ServoStyleConsts.h" #include "mozilla/Maybe.h" +#include "nsPoint.h" class nsIContent; class nsIFrame; -struct nsPoint; struct nsRect; struct nsSize; struct nsStyleDisplay; @@ -25,6 +25,21 @@ namespace mozilla { enum class StyleScrollSnapStrictness : uint8_t; class WritingMode; +struct SnapPoint { + SnapPoint() = default; + explicit SnapPoint(const nsPoint& aPoint) + : mX(Some(aPoint.x)), mY(Some(aPoint.y)) {} + SnapPoint(Maybe&& aX, Maybe&& aY) + : mX(std::move(aX)), mY(std::move(aY)) {} + + bool operator==(const SnapPoint& aOther) const { + return mX == aOther.mX && mY == aOther.mY; + } + + Maybe mX; + Maybe mY; +}; + struct ScrollSnapInfo { ScrollSnapInfo(); @@ -49,8 +64,7 @@ struct ScrollSnapInfo { struct SnapTarget { // The scroll positions corresponding to scroll-snap-align values. - Maybe mSnapPositionX; - Maybe mSnapPositionY; + SnapPoint mSnapPoint; // https://drafts.csswg.org/css-scroll-snap/#scroll-snap-area nsRect mSnapArea; @@ -66,16 +80,13 @@ struct ScrollSnapInfo { SnapTarget(Maybe&& aSnapPositionX, Maybe&& aSnapPositionY, nsRect&& aSnapArea, StyleScrollSnapStop aScrollSnapStop, ScrollSnapTargetId aTargetId) - : mSnapPositionX(std::move(aSnapPositionX)), - mSnapPositionY(std::move(aSnapPositionY)), + : mSnapPoint(std::move(aSnapPositionX), std::move(aSnapPositionY)), mSnapArea(aSnapArea), mScrollSnapStop(aScrollSnapStop), mTargetId(aTargetId) {} bool operator==(const SnapTarget& aOther) const { - return mSnapPositionX == aOther.mSnapPositionX && - mSnapPositionY == aOther.mSnapPositionY && - mSnapArea == aOther.mSnapArea && + return mSnapPoint == aOther.mSnapPoint && mSnapArea == aOther.mSnapArea && mScrollSnapStop == aOther.mScrollSnapStop && mTargetId == aOther.mTargetId; } -- 2.11.4.GIT