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, OptionSet<WheelEventProcessingSteps> processingSteps, bool allowLatching)
{
if (!allowLatching)
return;
LockHolder locker(m_latchedNodeMutex);
if (wheelEvent.isGestureStart() && !latchedNodeIsRelevant()) {
if (m_latchedNodeAndSteps) {
LOG_WITH_STREAM(ScrollLatching, stream << "ScrollingTreeLatchingController " << this << " receivedWheelEvent - " << (MonotonicTime::now() - m_lastLatchedNodeInterationTime).milliseconds() << "ms since last event, clearing latched node");
m_latchedNodeAndSteps.reset();
}
m_processingStepsForCurrentGesture = processingSteps;
}
}
Optional<ScrollingTreeLatchingController::ScrollingNodeAndProcessingSteps> ScrollingTreeLatchingController::latchingDataForEvent(const PlatformWheelEvent& wheelEvent, bool allowLatching) const
{
if (!allowLatching)
return WTF::nullopt;
LockHolder locker(m_latchedNodeMutex);
if (wheelEvent.useLatchedEventElement() && m_latchedNodeAndSteps && latchedNodeIsRelevant()) {
LOG_WITH_STREAM(ScrollLatching, stream << "ScrollingTreeLatchingController " << this << " latchedNodeForEvent: returning " << m_latchedNodeAndSteps->scrollingNodeID);
return m_latchedNodeAndSteps;
}
return WTF::nullopt;
}
Optional<ScrollingNodeID> ScrollingTreeLatchingController::latchedNodeID() const
{
LockHolder locker(m_latchedNodeMutex);
if (m_latchedNodeAndSteps)
return m_latchedNodeAndSteps->scrollingNodeID;
return WTF::nullopt;
}
Optional<ScrollingTreeLatchingController::ScrollingNodeAndProcessingSteps> ScrollingTreeLatchingController::latchedNodeAndSteps() const
{
LockHolder locker(m_latchedNodeMutex);
return m_latchedNodeAndSteps;
}
void ScrollingTreeLatchingController::nodeDidHandleEvent(ScrollingNodeID scrollingNodeID, OptionSet<WheelEventProcessingSteps> processingSteps, const PlatformWheelEvent& wheelEvent, bool allowLatching)
{
if (!allowLatching)
return;
LockHolder locker(m_latchedNodeMutex);
if (wheelEvent.useLatchedEventElement() && m_latchedNodeAndSteps && m_latchedNodeAndSteps->scrollingNodeID == scrollingNodeID) {
if (wheelEvent.isEndOfMomentumScroll())
m_lastLatchedNodeInterationTime = { };
else
m_lastLatchedNodeInterationTime = MonotonicTime::now();
return;
}
auto shouldLatch = [&]() {
if (wheelEvent.delta().isZero())
return false;
if (wheelEvent.isGestureStart())
return true;
if (!wheelEvent.isGestureContinuation())
return false;
if (m_processingStepsForCurrentGesture.valueOr(OptionSet<WheelEventProcessingSteps> { }).contains(WheelEventProcessingSteps::MainThreadForScrolling) && processingSteps.contains(WheelEventProcessingSteps::ScrollingThread))
return true;
return false;
};
if (!shouldLatch())
return;
m_processingStepsForCurrentGesture = processingSteps;
LOG_WITH_STREAM(ScrollLatching, stream << "ScrollingTreeLatchingController " << this << " nodeDidHandleEvent: latching to " << scrollingNodeID);
m_latchedNodeAndSteps = ScrollingNodeAndProcessingSteps { scrollingNodeID, processingSteps };
m_lastLatchedNodeInterationTime = MonotonicTime::now();
}
void ScrollingTreeLatchingController::nodeWasRemoved(ScrollingNodeID nodeID)
{
LockHolder locker(m_latchedNodeMutex);
if (m_latchedNodeAndSteps && m_latchedNodeAndSteps->scrollingNodeID == nodeID)
m_latchedNodeAndSteps.reset();
}
void ScrollingTreeLatchingController::clearLatchedNode()
{
LockHolder locker(m_latchedNodeMutex);
LOG_WITH_STREAM(ScrollLatching, stream << "ScrollingTreeLatchingController " << this << " clearLatchedNode");
m_latchedNodeAndSteps.reset();
}
bool ScrollingTreeLatchingController::latchedNodeIsRelevant() const
{
auto secondsSinceLastInteraction = MonotonicTime::now() - m_lastLatchedNodeInterationTime;
return secondsSinceLastInteraction < resetLatchedStateTimeout;
}
}
#endif // ENABLE(ASYNC_SCROLLING)