ScrollingTreeLatchingController.cpp [plain text]
#include "config.h"
#include "ScrollingTreeLatchingController.h"
#if ENABLE(ASYNC_SCROLLING)
#include "FloatPoint.h"
#include "Logging.h"
#include "PlatformWheelEvent.h"
#include "ScrollingThread.h"
#include "ScrollingTree.h"
namespace WebCore {
static const Seconds resetLatchedStateTimeout { 100_ms };
ScrollingTreeLatchingController::ScrollingTreeLatchingController() = default;
void ScrollingTreeLatchingController::receivedWheelEvent(const PlatformWheelEvent& wheelEvent, bool allowLatching)
{
if (!allowLatching)
return;
LockHolder locker(m_latchedNodeMutex);
if (wheelEvent.shouldConsiderLatching()) {
if (m_latchedNodeID) {
auto secondsSinceLastInteraction = MonotonicTime::now() - m_lastLatchedNodeInterationTime;
if (secondsSinceLastInteraction > resetLatchedStateTimeout) {
LOG_WITH_STREAM(ScrollLatching, stream << "ScrollingTreeLatchingController " << this << " receivedWheelEvent - " << secondsSinceLastInteraction.milliseconds() << "ms since last event, clearing latched node");
m_latchedNodeID.reset();
}
}
}
}
Optional<ScrollingNodeID> ScrollingTreeLatchingController::latchedNodeForEvent(const PlatformWheelEvent& wheelEvent, bool allowLatching) const
{
if (!allowLatching)
return WTF::nullopt;
LockHolder locker(m_latchedNodeMutex);
if (wheelEvent.useLatchedEventElement() && m_latchedNodeID) {
LOG_WITH_STREAM(ScrollLatching, stream << "ScrollingTreeLatchingController " << this << " latchedNodeForEvent: returning " << m_latchedNodeID);
return m_latchedNodeID.value();
}
return WTF::nullopt;
}
Optional<ScrollingNodeID> ScrollingTreeLatchingController::latchedNodeID() const
{
LockHolder locker(m_latchedNodeMutex);
return m_latchedNodeID;
}
void ScrollingTreeLatchingController::nodeDidHandleEvent(ScrollingNodeID scrollingNodeID, const PlatformWheelEvent& wheelEvent, bool allowLatching)
{
if (!allowLatching)
return;
LockHolder locker(m_latchedNodeMutex);
if (wheelEvent.useLatchedEventElement() && m_latchedNodeID == scrollingNodeID) {
m_lastLatchedNodeInterationTime = MonotonicTime::now();
return;
}
if (wheelEvent.delta().isZero() || !wheelEvent.shouldConsiderLatching())
return;
LOG_WITH_STREAM(ScrollLatching, stream << "ScrollingTreeLatchingController " << this << " nodeDidHandleEvent: latching to " << scrollingNodeID);
m_latchedNodeID = scrollingNodeID;
m_lastLatchedNodeInterationTime = MonotonicTime::now();
}
void ScrollingTreeLatchingController::nodeWasRemoved(ScrollingNodeID nodeID)
{
LockHolder locker(m_latchedNodeMutex);
if (nodeID == m_latchedNodeID)
m_latchedNodeID.reset();
}
void ScrollingTreeLatchingController::clearLatchedNode()
{
LockHolder locker(m_latchedNodeMutex);
LOG_WITH_STREAM(ScrollLatching, stream << "ScrollingTreeLatchingController " << this << " clearLatchedNode (was " << m_latchedNodeID << ")");
m_latchedNodeID.reset();
}
};
#endif // ENABLE(ASYNC_SCROLLING)