WheelEventDeltaFilter.cpp [plain text]
#include "config.h"
#include "WheelEventDeltaFilter.h"
#if HAVE(NSSCROLLING_FILTERS)
#include "WheelEventDeltaFilterMac.h"
#endif
#include "FloatSize.h"
#include "Logging.h"
#include "TextStream.h"
namespace WebCore {
WheelEventDeltaFilter::WheelEventDeltaFilter()
{
}
WheelEventDeltaFilter::~WheelEventDeltaFilter()
{
}
std::unique_ptr<WheelEventDeltaFilter> WheelEventDeltaFilter::create()
{
#if HAVE(NSSCROLLING_FILTERS)
return std::make_unique<WheelEventDeltaFilterMac>();
#else
return std::make_unique<BasicWheelEventDeltaFilter>();
#endif
}
bool WheelEventDeltaFilter::isFilteringDeltas() const
{
return m_isFilteringDeltas;
}
FloatSize WheelEventDeltaFilter::filteredDelta() const
{
return m_currentFilteredDelta;
}
FloatSize WheelEventDeltaFilter::filteredVelocity() const
{
return m_currentFilteredVelocity;
}
BasicWheelEventDeltaFilter::BasicWheelEventDeltaFilter()
: WheelEventDeltaFilter()
{
}
const size_t basicWheelEventDeltaFilterWindowSize = 3;
void BasicWheelEventDeltaFilter::updateFromDelta(const FloatSize& delta)
{
m_currentFilteredDelta = delta;
if (!m_isFilteringDeltas)
return;
m_recentWheelEventDeltas.append(delta);
if (m_recentWheelEventDeltas.size() > basicWheelEventDeltaFilterWindowSize)
m_recentWheelEventDeltas.removeFirst();
DominantScrollGestureDirection scrollDirection = dominantScrollGestureDirection();
if (scrollDirection == DominantScrollGestureDirection::Vertical)
m_currentFilteredDelta.setWidth(0);
else if (scrollDirection == DominantScrollGestureDirection::Horizontal)
m_currentFilteredDelta.setHeight(0);
}
void BasicWheelEventDeltaFilter::beginFilteringDeltas()
{
m_recentWheelEventDeltas.clear();
m_isFilteringDeltas = true;
}
void BasicWheelEventDeltaFilter::endFilteringDeltas()
{
m_currentFilteredDelta = FloatSize(0, 0);
m_isFilteringDeltas = false;
}
static inline bool deltaIsPredominantlyVertical(const FloatSize& delta)
{
return fabs(delta.height()) > fabs(delta.width());
}
DominantScrollGestureDirection BasicWheelEventDeltaFilter::dominantScrollGestureDirection() const
{
bool allVertical = m_recentWheelEventDeltas.size();
bool allHorizontal = m_recentWheelEventDeltas.size();
for (const auto& delta : m_recentWheelEventDeltas) {
bool isVertical = deltaIsPredominantlyVertical(delta);
allVertical &= isVertical;
allHorizontal &= !isVertical;
}
if (allVertical)
return DominantScrollGestureDirection::Vertical;
if (allHorizontal)
return DominantScrollGestureDirection::Horizontal;
return DominantScrollGestureDirection::None;
}
};