AxisScrollSnapOffsets.h [plain text]
#ifndef AxisScrollSnapOffsets_h
#define AxisScrollSnapOffsets_h
#if ENABLE(CSS_SCROLL_SNAP)
#include "ScrollTypes.h"
#include <wtf/Vector.h>
namespace WebCore {
class HTMLElement;
class RenderBox;
class RenderStyle;
class ScrollableArea;
void updateSnapOffsetsForScrollableArea(ScrollableArea&, HTMLElement& scrollingElement, const RenderBox& scrollingElementBox, const RenderStyle& scrollingElementStyle);
template <typename LayoutType, typename VelocityType>
LayoutType closestSnapOffset(const Vector<LayoutType>& snapOffsets, LayoutType scrollDestination, VelocityType velocity, unsigned& activeSnapIndex)
{
ASSERT(snapOffsets.size());
activeSnapIndex = 0;
if (scrollDestination <= snapOffsets.first())
return snapOffsets.first();
activeSnapIndex = snapOffsets.size() - 1;
if (scrollDestination >= snapOffsets.last())
return snapOffsets.last();
size_t lowerIndex = 0;
size_t upperIndex = snapOffsets.size() - 1;
while (lowerIndex < upperIndex - 1) {
size_t middleIndex = (lowerIndex + upperIndex) / 2;
if (scrollDestination < snapOffsets[middleIndex])
upperIndex = middleIndex;
else if (scrollDestination > snapOffsets[middleIndex])
lowerIndex = middleIndex;
else {
upperIndex = middleIndex;
lowerIndex = middleIndex;
break;
}
}
LayoutType lowerSnapPosition = snapOffsets[lowerIndex];
LayoutType upperSnapPosition = snapOffsets[upperIndex];
if (velocity) {
activeSnapIndex = (velocity < 0) ? lowerIndex : upperIndex;
return velocity < 0 ? lowerSnapPosition : upperSnapPosition;
}
bool isCloserToLowerSnapPosition = scrollDestination - lowerSnapPosition <= upperSnapPosition - scrollDestination;
activeSnapIndex = isCloserToLowerSnapPosition ? lowerIndex : upperIndex;
return isCloserToLowerSnapPosition ? lowerSnapPosition : upperSnapPosition;
}
}
#endif // CSS_SCROLL_SNAP
#endif // AxisScrollSnapOffsets_h