2017-10-27 20:33:53 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2016-03-22 04:32:28 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2018-03-07 21:55:26 +03:00
|
|
|
#include "ScrollSnap.h"
|
2018-03-07 02:59:36 +03:00
|
|
|
|
|
|
|
#include "FrameMetrics.h"
|
2016-03-19 04:15:45 +03:00
|
|
|
#include "gfxPrefs.h"
|
2016-03-22 04:32:28 +03:00
|
|
|
#include "mozilla/Maybe.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "nsLineLayout.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using layers::ScrollSnapInfo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Keeps track of the current best edge to snap to. The criteria for
|
|
|
|
* adding an edge depends on the scrolling unit.
|
|
|
|
*/
|
2019-02-12 02:45:17 +03:00
|
|
|
class CalcSnapPoints final {
|
2016-03-22 04:32:28 +03:00
|
|
|
public:
|
|
|
|
CalcSnapPoints(nsIScrollableFrame::ScrollUnit aUnit,
|
|
|
|
const nsPoint& aDestination, const nsPoint& aStartPos);
|
2019-02-12 02:45:17 +03:00
|
|
|
void AddHorizontalEdge(nscoord aEdge);
|
|
|
|
void AddVerticalEdge(nscoord aEdge);
|
|
|
|
void AddHorizontalEdgeInterval(const nsRect& aScrollRange, nscoord aInterval,
|
|
|
|
nscoord aOffset);
|
|
|
|
void AddVerticalEdgeInterval(const nsRect& aScrollRange, nscoord aInterval,
|
|
|
|
nscoord aOffset);
|
2016-03-22 04:32:28 +03:00
|
|
|
void AddEdge(nscoord aEdge, nscoord aDestination, nscoord aStartPos,
|
|
|
|
nscoord aScrollingDirection, nscoord* aBestEdge,
|
|
|
|
bool* aEdgeFound);
|
|
|
|
void AddEdgeInterval(nscoord aInterval, nscoord aMinPos, nscoord aMaxPos,
|
|
|
|
nscoord aOffset, nscoord aDestination, nscoord aStartPos,
|
|
|
|
nscoord aScrollingDirection, nscoord* aBestEdge,
|
|
|
|
bool* aEdgeFound);
|
|
|
|
nsPoint GetBestEdge() const;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-03-22 04:32:28 +03:00
|
|
|
protected:
|
|
|
|
nsIScrollableFrame::ScrollUnit mUnit;
|
2018-12-05 21:44:05 +03:00
|
|
|
nsPoint mDestination; // gives the position after scrolling but before
|
|
|
|
// snapping
|
2018-12-06 02:04:51 +03:00
|
|
|
nsPoint mStartPos; // gives the position before scrolling
|
2016-03-22 04:32:28 +03:00
|
|
|
nsIntPoint mScrollingDirection; // always -1, 0, or 1
|
|
|
|
nsPoint mBestEdge; // keeps track of the position of the current best edge
|
|
|
|
bool mHorizontalEdgeFound; // true if mBestEdge.x is storing a valid
|
|
|
|
// horizontal edge
|
|
|
|
bool mVerticalEdgeFound; // true if mBestEdge.y is storing a valid vertical
|
|
|
|
// edge
|
|
|
|
};
|
|
|
|
|
|
|
|
CalcSnapPoints::CalcSnapPoints(nsIScrollableFrame::ScrollUnit aUnit,
|
|
|
|
const nsPoint& aDestination,
|
|
|
|
const nsPoint& aStartPos) {
|
|
|
|
mUnit = aUnit;
|
|
|
|
mDestination = aDestination;
|
|
|
|
mStartPos = aStartPos;
|
|
|
|
|
|
|
|
nsPoint direction = aDestination - aStartPos;
|
|
|
|
mScrollingDirection = nsIntPoint(0, 0);
|
|
|
|
if (direction.x < 0) {
|
|
|
|
mScrollingDirection.x = -1;
|
|
|
|
}
|
|
|
|
if (direction.x > 0) {
|
|
|
|
mScrollingDirection.x = 1;
|
|
|
|
}
|
|
|
|
if (direction.y < 0) {
|
|
|
|
mScrollingDirection.y = -1;
|
|
|
|
}
|
|
|
|
if (direction.y > 0) {
|
|
|
|
mScrollingDirection.y = 1;
|
|
|
|
}
|
|
|
|
mBestEdge = aDestination;
|
|
|
|
mHorizontalEdgeFound = false;
|
|
|
|
mVerticalEdgeFound = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPoint CalcSnapPoints::GetBestEdge() const {
|
|
|
|
return nsPoint(mVerticalEdgeFound ? mBestEdge.x : mStartPos.x,
|
|
|
|
mHorizontalEdgeFound ? mBestEdge.y : mStartPos.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CalcSnapPoints::AddHorizontalEdge(nscoord aEdge) {
|
|
|
|
AddEdge(aEdge, mDestination.y, mStartPos.y, mScrollingDirection.y,
|
|
|
|
&mBestEdge.y, &mHorizontalEdgeFound);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CalcSnapPoints::AddVerticalEdge(nscoord aEdge) {
|
|
|
|
AddEdge(aEdge, mDestination.x, mStartPos.x, mScrollingDirection.x,
|
|
|
|
&mBestEdge.x, &mVerticalEdgeFound);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CalcSnapPoints::AddHorizontalEdgeInterval(const nsRect& aScrollRange,
|
|
|
|
nscoord aInterval,
|
|
|
|
nscoord aOffset) {
|
|
|
|
AddEdgeInterval(aInterval, aScrollRange.y, aScrollRange.YMost(), aOffset,
|
|
|
|
mDestination.y, mStartPos.y, mScrollingDirection.y,
|
|
|
|
&mBestEdge.y, &mHorizontalEdgeFound);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CalcSnapPoints::AddVerticalEdgeInterval(const nsRect& aScrollRange,
|
|
|
|
nscoord aInterval,
|
|
|
|
nscoord aOffset) {
|
|
|
|
AddEdgeInterval(aInterval, aScrollRange.x, aScrollRange.XMost(), aOffset,
|
|
|
|
mDestination.x, mStartPos.x, mScrollingDirection.x,
|
|
|
|
&mBestEdge.x, &mVerticalEdgeFound);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CalcSnapPoints::AddEdge(nscoord aEdge, nscoord aDestination,
|
|
|
|
nscoord aStartPos, nscoord aScrollingDirection,
|
|
|
|
nscoord* aBestEdge, bool* aEdgeFound) {
|
|
|
|
// nsIScrollableFrame::DEVICE_PIXELS indicates that we are releasing a drag
|
|
|
|
// gesture or any other user input event that sets an absolute scroll
|
|
|
|
// position. In this case, scroll snapping is expected to travel in any
|
|
|
|
// direction. Otherwise, we will restrict the direction of the scroll
|
|
|
|
// snapping movement based on aScrollingDirection.
|
|
|
|
if (mUnit != nsIScrollableFrame::DEVICE_PIXELS) {
|
|
|
|
// Unless DEVICE_PIXELS, we only want to snap to points ahead of the
|
|
|
|
// direction we are scrolling
|
|
|
|
if (aScrollingDirection == 0) {
|
|
|
|
// The scroll direction is neutral - will not hit a snap point.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// nsIScrollableFrame::WHOLE indicates that we are navigating to "home" or
|
|
|
|
// "end". In this case, we will always select the first or last snap point
|
|
|
|
// regardless of the direction of the scroll. Otherwise, we will select
|
|
|
|
// scroll snapping points only in the direction specified by
|
|
|
|
// aScrollingDirection.
|
|
|
|
if (mUnit != nsIScrollableFrame::WHOLE) {
|
|
|
|
// Direction of the edge from the current position (before scrolling) in
|
|
|
|
// the direction of scrolling
|
|
|
|
nscoord direction = (aEdge - aStartPos) * aScrollingDirection;
|
|
|
|
if (direction <= 0) {
|
|
|
|
// The edge is not in the direction we are scrolling, skip it.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!*aEdgeFound) {
|
|
|
|
*aBestEdge = aEdge;
|
|
|
|
*aEdgeFound = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mUnit == nsIScrollableFrame::DEVICE_PIXELS ||
|
|
|
|
mUnit == nsIScrollableFrame::LINES) {
|
|
|
|
if (std::abs(aEdge - aDestination) < std::abs(*aBestEdge - aDestination)) {
|
|
|
|
*aBestEdge = aEdge;
|
|
|
|
}
|
|
|
|
} else if (mUnit == nsIScrollableFrame::PAGES) {
|
|
|
|
// distance to the edge from the scrolling destination in the direction of
|
|
|
|
// scrolling
|
|
|
|
nscoord overshoot = (aEdge - aDestination) * aScrollingDirection;
|
|
|
|
// distance to the current best edge from the scrolling destination in the
|
|
|
|
// direction of scrolling
|
|
|
|
nscoord curOvershoot = (*aBestEdge - aDestination) * aScrollingDirection;
|
|
|
|
|
|
|
|
// edges between the current position and the scrolling destination are
|
|
|
|
// favoured to preserve context
|
|
|
|
if (overshoot < 0 && (overshoot > curOvershoot || curOvershoot >= 0)) {
|
|
|
|
*aBestEdge = aEdge;
|
|
|
|
}
|
|
|
|
// if there are no edges between the current position and the scrolling
|
|
|
|
// destination the closest edge beyond the destination is used
|
|
|
|
if (overshoot > 0 && overshoot < curOvershoot) {
|
|
|
|
*aBestEdge = aEdge;
|
|
|
|
}
|
|
|
|
} else if (mUnit == nsIScrollableFrame::WHOLE) {
|
|
|
|
// the edge closest to the top/bottom/left/right is used, depending on
|
|
|
|
// scrolling direction
|
|
|
|
if (aScrollingDirection > 0 && aEdge > *aBestEdge) {
|
|
|
|
*aBestEdge = aEdge;
|
|
|
|
} else if (aScrollingDirection < 0 && aEdge < *aBestEdge) {
|
|
|
|
*aBestEdge = aEdge;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
NS_ERROR("Invalid scroll mode");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CalcSnapPoints::AddEdgeInterval(nscoord aInterval, nscoord aMinPos,
|
|
|
|
nscoord aMaxPos, nscoord aOffset,
|
|
|
|
nscoord aDestination, nscoord aStartPos,
|
|
|
|
nscoord aScrollingDirection,
|
|
|
|
nscoord* aBestEdge, bool* aEdgeFound) {
|
|
|
|
if (aInterval == 0) {
|
|
|
|
// When interval is 0, there are no scroll snap points.
|
|
|
|
// Avoid division by zero and bail.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The only possible candidate interval snap points are the edges immediately
|
|
|
|
// surrounding aDestination.
|
|
|
|
|
|
|
|
// aDestination must be clamped to the scroll
|
|
|
|
// range in order to handle cases where the best matching snap point would
|
|
|
|
// result in scrolling out of bounds. This clamping must be prior to
|
|
|
|
// selecting the two interval edges.
|
|
|
|
nscoord clamped = std::max(std::min(aDestination, aMaxPos), aMinPos);
|
|
|
|
|
|
|
|
// Add each edge in the interval immediately before aTarget and after aTarget
|
|
|
|
// Do not add edges that are out of range.
|
|
|
|
nscoord r = (clamped + aOffset) % aInterval;
|
|
|
|
if (r < aMinPos) {
|
|
|
|
r += aInterval;
|
|
|
|
}
|
|
|
|
nscoord edge = clamped - r;
|
|
|
|
if (edge >= aMinPos && edge <= aMaxPos) {
|
|
|
|
AddEdge(edge, aDestination, aStartPos, aScrollingDirection, aBestEdge,
|
|
|
|
aEdgeFound);
|
|
|
|
}
|
|
|
|
edge += aInterval;
|
|
|
|
if (edge >= aMinPos && edge <= aMaxPos) {
|
|
|
|
AddEdge(edge, aDestination, aStartPos, aScrollingDirection, aBestEdge,
|
|
|
|
aEdgeFound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ProcessScrollSnapCoordinates(
|
2019-02-12 02:45:17 +03:00
|
|
|
CalcSnapPoints& aCalcSnapPoint,
|
2016-03-22 04:32:28 +03:00
|
|
|
const nsTArray<nsPoint>& aScrollSnapCoordinates,
|
|
|
|
const nsPoint& aScrollSnapDestination) {
|
|
|
|
for (nsPoint snapCoords : aScrollSnapCoordinates) {
|
|
|
|
// Make them relative to the scroll snap destination.
|
|
|
|
snapCoords -= aScrollSnapDestination;
|
|
|
|
|
2019-02-12 02:45:17 +03:00
|
|
|
aCalcSnapPoint.AddVerticalEdge(snapCoords.x);
|
|
|
|
aCalcSnapPoint.AddHorizontalEdge(snapCoords.y);
|
2016-03-22 04:32:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Maybe<nsPoint> ScrollSnapUtils::GetSnapPointForDestination(
|
|
|
|
const ScrollSnapInfo& aSnapInfo, nsIScrollableFrame::ScrollUnit aUnit,
|
2019-02-12 02:44:53 +03:00
|
|
|
const nsRect& aScrollRange, const nsPoint& aStartPos,
|
|
|
|
const nsPoint& aDestination) {
|
2019-04-11 09:19:13 +03:00
|
|
|
if (aSnapInfo.mScrollSnapTypeY == StyleScrollSnapStrictness::None &&
|
|
|
|
aSnapInfo.mScrollSnapTypeX == StyleScrollSnapStrictness::None) {
|
2016-03-22 04:32:28 +03:00
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPoint destPos = aSnapInfo.mScrollSnapDestination;
|
|
|
|
|
|
|
|
CalcSnapPoints calcSnapPoints(aUnit, aDestination, aStartPos);
|
|
|
|
|
|
|
|
if (aSnapInfo.mScrollSnapIntervalX.isSome()) {
|
|
|
|
nscoord interval = aSnapInfo.mScrollSnapIntervalX.value();
|
|
|
|
calcSnapPoints.AddVerticalEdgeInterval(aScrollRange, interval, destPos.x);
|
|
|
|
}
|
|
|
|
if (aSnapInfo.mScrollSnapIntervalY.isSome()) {
|
|
|
|
nscoord interval = aSnapInfo.mScrollSnapIntervalY.value();
|
|
|
|
calcSnapPoints.AddHorizontalEdgeInterval(aScrollRange, interval, destPos.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
ProcessScrollSnapCoordinates(calcSnapPoints, aSnapInfo.mScrollSnapCoordinates,
|
|
|
|
destPos);
|
|
|
|
bool snapped = false;
|
|
|
|
nsPoint finalPos = calcSnapPoints.GetBestEdge();
|
2016-03-19 04:15:45 +03:00
|
|
|
nscoord proximityThreshold = gfxPrefs::ScrollSnapProximityThreshold();
|
2016-03-22 04:32:28 +03:00
|
|
|
proximityThreshold = nsPresContext::CSSPixelsToAppUnits(proximityThreshold);
|
2019-04-11 09:19:13 +03:00
|
|
|
if (aSnapInfo.mScrollSnapTypeY == StyleScrollSnapStrictness::Proximity &&
|
2016-03-22 04:32:28 +03:00
|
|
|
std::abs(aDestination.y - finalPos.y) > proximityThreshold) {
|
|
|
|
finalPos.y = aDestination.y;
|
|
|
|
} else {
|
|
|
|
snapped = true;
|
|
|
|
}
|
2019-04-11 09:19:13 +03:00
|
|
|
if (aSnapInfo.mScrollSnapTypeX == StyleScrollSnapStrictness::Proximity &&
|
2016-03-22 04:32:28 +03:00
|
|
|
std::abs(aDestination.x - finalPos.x) > proximityThreshold) {
|
|
|
|
finalPos.x = aDestination.x;
|
|
|
|
} else {
|
|
|
|
snapped = true;
|
|
|
|
}
|
|
|
|
return snapped ? Some(finalPos) : Nothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|