<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[188833] trunk/Source/WebKit2</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/188833">188833</a></dd>
<dt>Author</dt> <dd>timothy_horton@apple.com</dd>
<dt>Date</dt> <dd>2015-08-22 21:14:44 -0700 (Sat, 22 Aug 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Factor out and add logging to swipe-start hysteresis code
https://bugs.webkit.org/show_bug.cgi?id=148361

Reviewed by Darin Adler.

* UIProcess/ViewGestureController.cpp:
(WebKit::ViewGestureController::ViewGestureController):
* UIProcess/mac/ViewGestureController.h:
(WebKit::ViewGestureController::wheelEventWasNotHandledByWebCore):
(WebKit::ViewGestureController::shouldIgnorePinnedState):
(WebKit::ViewGestureController::setShouldIgnorePinnedState):
(WebKit::ViewGestureController::PendingSwipeTracker::PendingSwipeTracker):
(WebKit::ViewGestureController::PendingSwipeTracker::shouldIgnorePinnedState):
(WebKit::ViewGestureController::PendingSwipeTracker::setShouldIgnorePinnedState):
Move pending-swipe members into PendingSwipeTracker.

* UIProcess/mac/ViewGestureControllerMac.mm:
(WebKit::scrollEventCanInfluenceSwipe):
(WebKit::deltaShouldCancelSwipe):
(WebKit::ViewGestureController::PendingSwipeTracker::scrollEventCanBecomeSwipe):
(WebKit::ViewGestureController::handleScrollWheelEvent):
(WebKit::ViewGestureController::PendingSwipeTracker::handleEvent):
(WebKit::ViewGestureController::PendingSwipeTracker::eventWasNotHandledByWebCore):
(WebKit::ViewGestureController::PendingSwipeTracker::tryToStartSwipe):
(WebKit::ViewGestureController::PendingSwipeTracker::reset):
(WebKit::ViewGestureController::trackSwipeGesture):
(WebKit::ViewGestureController::setDidMoveSwipeSnapshotCallback):
Move pending-swipe methods into PendingSwipeTracker.
Clean up the logic a bit.
Add some logging.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebKit2ChangeLog">trunk/Source/WebKit2/ChangeLog</a></li>
<li><a href="#trunkSourceWebKit2UIProcessViewGestureControllercpp">trunk/Source/WebKit2/UIProcess/ViewGestureController.cpp</a></li>
<li><a href="#trunkSourceWebKit2UIProcessmacViewGestureControllerh">trunk/Source/WebKit2/UIProcess/mac/ViewGestureController.h</a></li>
<li><a href="#trunkSourceWebKit2UIProcessmacViewGestureControllerMacmm">trunk/Source/WebKit2/UIProcess/mac/ViewGestureControllerMac.mm</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebKit2ChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/ChangeLog (188832 => 188833)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/ChangeLog        2015-08-23 02:28:09 UTC (rev 188832)
+++ trunk/Source/WebKit2/ChangeLog        2015-08-23 04:14:44 UTC (rev 188833)
</span><span class="lines">@@ -1,3 +1,36 @@
</span><ins>+2015-08-22  Tim Horton  &lt;timothy_horton@apple.com&gt;
+
+        Factor out and add logging to swipe-start hysteresis code
+        https://bugs.webkit.org/show_bug.cgi?id=148361
+
+        Reviewed by Darin Adler.
+
+        * UIProcess/ViewGestureController.cpp:
+        (WebKit::ViewGestureController::ViewGestureController):
+        * UIProcess/mac/ViewGestureController.h:
+        (WebKit::ViewGestureController::wheelEventWasNotHandledByWebCore):
+        (WebKit::ViewGestureController::shouldIgnorePinnedState):
+        (WebKit::ViewGestureController::setShouldIgnorePinnedState):
+        (WebKit::ViewGestureController::PendingSwipeTracker::PendingSwipeTracker):
+        (WebKit::ViewGestureController::PendingSwipeTracker::shouldIgnorePinnedState):
+        (WebKit::ViewGestureController::PendingSwipeTracker::setShouldIgnorePinnedState):
+        Move pending-swipe members into PendingSwipeTracker.
+
+        * UIProcess/mac/ViewGestureControllerMac.mm:
+        (WebKit::scrollEventCanInfluenceSwipe):
+        (WebKit::deltaShouldCancelSwipe):
+        (WebKit::ViewGestureController::PendingSwipeTracker::scrollEventCanBecomeSwipe):
+        (WebKit::ViewGestureController::handleScrollWheelEvent):
+        (WebKit::ViewGestureController::PendingSwipeTracker::handleEvent):
+        (WebKit::ViewGestureController::PendingSwipeTracker::eventWasNotHandledByWebCore):
+        (WebKit::ViewGestureController::PendingSwipeTracker::tryToStartSwipe):
+        (WebKit::ViewGestureController::PendingSwipeTracker::reset):
+        (WebKit::ViewGestureController::trackSwipeGesture):
+        (WebKit::ViewGestureController::setDidMoveSwipeSnapshotCallback):
+        Move pending-swipe methods into PendingSwipeTracker.
+        Clean up the logic a bit.
+        Add some logging.
+
</ins><span class="cx"> 2015-08-22  Timothy Horton  &lt;timothy_horton@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         WebKitTestRunner should use WKWebView on OS X and iOS
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessViewGestureControllercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/ViewGestureController.cpp (188832 => 188833)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/ViewGestureController.cpp        2015-08-23 02:28:09 UTC (rev 188832)
+++ trunk/Source/WebKit2/UIProcess/ViewGestureController.cpp        2015-08-23 04:14:44 UTC (rev 188833)
</span><span class="lines">@@ -52,10 +52,12 @@
</span><span class="cx">     return viewGestureControllers.get();
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-
</del><span class="cx"> ViewGestureController::ViewGestureController(WebPageProxy&amp; webPageProxy)
</span><span class="cx">     : m_webPageProxy(webPageProxy)
</span><span class="cx">     , m_swipeActiveLoadMonitoringTimer(RunLoop::main(), this, &amp;ViewGestureController::checkForActiveLoads)
</span><ins>+#if PLATFORM(MAC)
+    , m_pendingSwipeTracker(webPageProxy, std::bind(&amp;ViewGestureController::trackSwipeGesture, this, std::placeholders::_1, std::placeholders::_2))
+#endif
</ins><span class="cx"> {
</span><span class="cx">     m_webPageProxy.process().addMessageReceiver(Messages::ViewGestureController::messageReceiverName(), m_webPageProxy.pageID(), *this);
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessmacViewGestureControllerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/mac/ViewGestureController.h (188832 => 188833)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/mac/ViewGestureController.h        2015-08-23 02:28:09 UTC (rev 188832)
+++ trunk/Source/WebKit2/UIProcess/mac/ViewGestureController.h        2015-08-23 04:14:44 UTC (rev 188833)
</span><span class="lines">@@ -89,12 +89,6 @@
</span><span class="cx">         Forward
</span><span class="cx">     };
</span><span class="cx"> 
</span><del>-    enum class PendingSwipeReason {
-        None,
-        WebCoreMayScroll,
-        InsufficientMagnitude
-    };
-    
</del><span class="cx"> #if PLATFORM(MAC)
</span><span class="cx">     double magnification() const;
</span><span class="cx"> 
</span><span class="lines">@@ -104,15 +98,15 @@
</span><span class="cx">     void handleSmartMagnificationGesture(WebCore::FloatPoint origin);
</span><span class="cx"> 
</span><span class="cx">     bool handleScrollWheelEvent(NSEvent *);
</span><del>-    void wheelEventWasNotHandledByWebCore(NSEvent *);
</del><ins>+    void wheelEventWasNotHandledByWebCore(NSEvent *event) { m_pendingSwipeTracker.eventWasNotHandledByWebCore(event); }
</ins><span class="cx"> 
</span><span class="cx">     void setCustomSwipeViews(Vector&lt;RetainPtr&lt;NSView&gt;&gt; views) { m_customSwipeViews = WTF::move(views); }
</span><span class="cx">     void setCustomSwipeViewsTopContentInset(float topContentInset) { m_customSwipeViewsTopContentInset = topContentInset; }
</span><span class="cx">     WebCore::FloatRect windowRelativeBoundsForCustomSwipeViews() const;
</span><span class="cx">     void setDidMoveSwipeSnapshotCallback(void(^)(CGRect));
</span><span class="cx"> 
</span><del>-    bool shouldIgnorePinnedState() { return m_shouldIgnorePinnedState; }
-    void setShouldIgnorePinnedState(bool ignore) { m_shouldIgnorePinnedState = ignore; }
</del><ins>+    bool shouldIgnorePinnedState() { return m_pendingSwipeTracker.shouldIgnorePinnedState(); }
+    void setShouldIgnorePinnedState(bool ignore) { m_pendingSwipeTracker.setShouldIgnorePinnedState(ignore); }
</ins><span class="cx"> #else
</span><span class="cx">     void installSwipeHandler(UIView *gestureRecognizerView, UIView *swipingView);
</span><span class="cx">     void setAlternateBackForwardListSourceView(WKWebView *);
</span><span class="lines">@@ -195,8 +189,6 @@
</span><span class="cx">     void handleSwipeGesture(WebBackForwardListItem* targetItem, double progress, SwipeDirection);
</span><span class="cx">     void willEndSwipeGesture(WebBackForwardListItem&amp; targetItem, bool cancelled);
</span><span class="cx">     void endSwipeGesture(WebBackForwardListItem* targetItem, bool cancelled);
</span><del>-    bool deltaIsSufficientToBeginSwipe(NSEvent *);
-    bool scrollEventCanBecomeSwipe(NSEvent *, SwipeDirection&amp;);
</del><span class="cx">     bool shouldUseSnapshotForSize(ViewSnapshot&amp;, WebCore::FloatSize swipeLayerSize, float topContentInset);
</span><span class="cx"> 
</span><span class="cx">     CALayer *determineSnapshotLayerParent() const;
</span><span class="lines">@@ -205,6 +197,37 @@
</span><span class="cx">     void didMoveSwipeSnapshotLayer();
</span><span class="cx"> 
</span><span class="cx">     void forceRepaintIfNeeded();
</span><ins>+
+    class PendingSwipeTracker {
+    public:
+        PendingSwipeTracker(WebPageProxy&amp;, std::function&lt;void(NSEvent *, SwipeDirection)&gt; trackSwipeCallback);
+        bool handleEvent(NSEvent *);
+        void eventWasNotHandledByWebCore(NSEvent *);
+
+        void reset(const char* resetReasonForLogging);
+
+        bool shouldIgnorePinnedState() { return m_shouldIgnorePinnedState; }
+        void setShouldIgnorePinnedState(bool ignore) { m_shouldIgnorePinnedState = ignore; }
+
+    private:
+        bool tryToStartSwipe(NSEvent *);
+        bool scrollEventCanBecomeSwipe(NSEvent *, SwipeDirection&amp;);
+
+        enum class State {
+            None,
+            WaitingForWebCore,
+            InsufficientMagnitude
+        };
+
+        State m_state { State::None };
+        SwipeDirection m_direction;
+        WebCore::FloatSize m_cumulativeDelta;
+
+        bool m_shouldIgnorePinnedState { false };
+
+        std::function&lt;void(NSEvent *, SwipeDirection)&gt; m_trackSwipeCallback;
+        WebPageProxy&amp; m_webPageProxy;
+    };
</ins><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx">     WebPageProxy&amp; m_webPageProxy;
</span><span class="lines">@@ -240,16 +263,10 @@
</span><span class="cx">     float m_customSwipeViewsTopContentInset { 0 };
</span><span class="cx">     WebCore::FloatRect m_currentSwipeCustomViewBounds;
</span><span class="cx"> 
</span><del>-    // If we need to wait for content to decide if it is going to consume
-    // the scroll event that would have started a swipe, we'll fill these in.
-    PendingSwipeReason m_pendingSwipeReason { PendingSwipeReason::None };
-    SwipeDirection m_pendingSwipeDirection;
-    WebCore::FloatSize m_cumulativeDeltaForPendingSwipe;
</del><ins>+    PendingSwipeTracker m_pendingSwipeTracker;
</ins><span class="cx"> 
</span><span class="cx">     void (^m_didMoveSwipeSnapshotCallback)(CGRect) { nullptr };
</span><span class="cx"> 
</span><del>-    bool m_shouldIgnorePinnedState { false };
-
</del><span class="cx">     bool m_hasOutstandingRepaintRequest { false };
</span><span class="cx"> #else    
</span><span class="cx">     UIView *m_liveSwipeView { nullptr };
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessmacViewGestureControllerMacmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/mac/ViewGestureControllerMac.mm (188832 => 188833)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/mac/ViewGestureControllerMac.mm        2015-08-23 02:28:09 UTC (rev 188832)
+++ trunk/Source/WebKit2/UIProcess/mac/ViewGestureControllerMac.mm        2015-08-23 04:14:44 UTC (rev 188833)
</span><span class="lines">@@ -29,6 +29,7 @@
</span><span class="cx"> #if PLATFORM(MAC)
</span><span class="cx"> 
</span><span class="cx"> #import &quot;FrameLoadState.h&quot;
</span><ins>+#import &quot;Logging.h&quot;
</ins><span class="cx"> #import &quot;NativeWebWheelEvent.h&quot;
</span><span class="cx"> #import &quot;ViewGestureControllerMessages.h&quot;
</span><span class="cx"> #import &quot;ViewGestureGeometryCollectorMessages.h&quot;
</span><span class="lines">@@ -120,7 +121,7 @@
</span><span class="cx"> 
</span><span class="cx">     // Outside of the extremes, resist further scaling.
</span><span class="cx">     double limit = currentScale &lt; minMagnification ? minMagnification : maxMagnification;
</span><del>-    double scaleDistance = fabs(limit - currentScale);
</del><ins>+    double scaleDistance = std::abs(limit - currentScale);
</ins><span class="cx">     double scalePercent = std::min(std::max(scaleDistance / limit, 0.), 1.);
</span><span class="cx">     double resistance = zoomOutResistance + scalePercent * (0.01 - zoomOutResistance);
</span><span class="cx"> 
</span><span class="lines">@@ -204,7 +205,7 @@
</span><span class="cx"> 
</span><span class="cx"> static float maximumRectangleComponentDelta(FloatRect a, FloatRect b)
</span><span class="cx"> {
</span><del>-    return std::max(fabs(a.x() - b.x()), std::max(fabs(a.y() - b.y()), std::max(fabs(a.width() - b.width()), fabs(a.height() - b.height()))));
</del><ins>+    return std::max(std::abs(a.x() - b.x()), std::max(std::abs(a.y() - b.y()), std::max(std::abs(a.width() - b.width()), std::abs(a.height() - b.height()))));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void ViewGestureController::didCollectGeometryForSmartMagnificationGesture(FloatPoint origin, FloatRect renderRect, FloatRect visibleContentRect, bool isReplacedElement, double viewportMinimumScale, double viewportMaximumScale)
</span><span class="lines">@@ -264,20 +265,33 @@
</span><span class="cx">     m_lastMagnificationGestureWasSmartMagnification = true;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-bool ViewGestureController::scrollEventCanBecomeSwipe(NSEvent *event, ViewGestureController::SwipeDirection&amp; potentialSwipeDirection)
</del><ins>+static bool scrollEventCanInfluenceSwipe(NSEvent *event)
</ins><span class="cx"> {
</span><ins>+    return event.hasPreciseScrollingDeltas &amp;&amp; [NSEvent isSwipeTrackingFromScrollEventsEnabled];
+}
+
+static bool deltaShouldCancelSwipe(float x, float y)
+{
+    return std::abs(y) &gt;= std::abs(x) * minimumScrollEventRatioForSwipe;
+}
+
+ViewGestureController::PendingSwipeTracker::PendingSwipeTracker(WebPageProxy&amp; webPageProxy, std::function&lt;void(NSEvent *, SwipeDirection)&gt; trackSwipeCallback)
+    : m_trackSwipeCallback(WTF::move(trackSwipeCallback))
+    , m_webPageProxy(webPageProxy)
+{
+}
+
+bool ViewGestureController::PendingSwipeTracker::scrollEventCanBecomeSwipe(NSEvent *event, ViewGestureController::SwipeDirection&amp; potentialSwipeDirection)
+{
</ins><span class="cx">     if (event.phase != NSEventPhaseBegan)
</span><span class="cx">         return false;
</span><span class="cx"> 
</span><del>-    if (!event.hasPreciseScrollingDeltas)
</del><ins>+    if (!scrollEventCanInfluenceSwipe(event))
</ins><span class="cx">         return false;
</span><span class="cx"> 
</span><del>-    if (![NSEvent isSwipeTrackingFromScrollEventsEnabled])
</del><ins>+    if (deltaShouldCancelSwipe(event.scrollingDeltaX, event.scrollingDeltaY))
</ins><span class="cx">         return false;
</span><span class="cx"> 
</span><del>-    if (fabs(event.scrollingDeltaX) &lt;= fabs(event.scrollingDeltaY))
-        return false;
-
</del><span class="cx">     bool isPinnedToLeft = m_shouldIgnorePinnedState || m_webPageProxy.isPinnedToLeftSide();
</span><span class="cx">     bool isPinnedToRight = m_shouldIgnorePinnedState || m_webPageProxy.isPinnedToRightSide();
</span><span class="cx"> 
</span><span class="lines">@@ -291,95 +305,91 @@
</span><span class="cx">     return true;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-bool ViewGestureController::deltaIsSufficientToBeginSwipe(NSEvent *event)
</del><ins>+bool ViewGestureController::handleScrollWheelEvent(NSEvent *event)
</ins><span class="cx"> {
</span><del>-    if (m_pendingSwipeReason != PendingSwipeReason::InsufficientMagnitude)
</del><ins>+    if (m_activeGestureType != ViewGestureType::None)
</ins><span class="cx">         return false;
</span><ins>+    return m_pendingSwipeTracker.handleEvent(event);
+}
</ins><span class="cx"> 
</span><del>-    m_cumulativeDeltaForPendingSwipe += FloatSize(event.scrollingDeltaX, event.scrollingDeltaY);
-
-    // If the cumulative delta is ever &quot;too vertical&quot;, we will stop tracking this
-    // as a potential swipe until we get another &quot;begin&quot; event.
-    if (fabs(m_cumulativeDeltaForPendingSwipe.height()) &gt;= fabs(m_cumulativeDeltaForPendingSwipe.width()) * minimumScrollEventRatioForSwipe) {
-        m_pendingSwipeReason = PendingSwipeReason::None;
</del><ins>+bool ViewGestureController::PendingSwipeTracker::handleEvent(NSEvent *event)
+{
+    if (event.phase == NSEventPhaseEnded) {
+        reset(&quot;gesture ended&quot;);
</ins><span class="cx">         return false;
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    if (fabs(m_cumulativeDeltaForPendingSwipe.width()) &lt; minimumHorizontalSwipeDistance)
</del><ins>+    if (m_state == State::None) {
+        if (!scrollEventCanBecomeSwipe(event, m_direction))
+            return false;
+
+        if (!m_shouldIgnorePinnedState &amp;&amp; m_webPageProxy.willHandleHorizontalScrollEvents()) {
+            m_state = State::WaitingForWebCore;
+            LOG(ViewGestures, &quot;Swipe Start Hysteresis - waiting for WebCore to handle event&quot;);
+        }
+    }
+
+    if (m_state == State::WaitingForWebCore)
</ins><span class="cx">         return false;
</span><span class="cx"> 
</span><del>-    return true;
</del><ins>+    return tryToStartSwipe(event);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-void ViewGestureController::setDidMoveSwipeSnapshotCallback(void(^callback)(CGRect))
</del><ins>+void ViewGestureController::PendingSwipeTracker::eventWasNotHandledByWebCore(NSEvent *event)
</ins><span class="cx"> {
</span><del>-    if (m_didMoveSwipeSnapshotCallback)
-        Block_release(m_didMoveSwipeSnapshotCallback);
-    m_didMoveSwipeSnapshotCallback = Block_copy(callback);
</del><ins>+    if (m_state != State::WaitingForWebCore)
+        return;
+
+    LOG(ViewGestures, &quot;Swipe Start Hysteresis - WebCore didn't handle event&quot;);
+    m_state = State::None;
+    m_cumulativeDelta = FloatSize();
+    tryToStartSwipe(event);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-bool ViewGestureController::handleScrollWheelEvent(NSEvent *event)
</del><ins>+bool ViewGestureController::PendingSwipeTracker::tryToStartSwipe(NSEvent *event)
</ins><span class="cx"> {
</span><del>-    if (event.phase == NSEventPhaseEnded) {
-        m_cumulativeDeltaForPendingSwipe = FloatSize();
-        m_pendingSwipeReason = PendingSwipeReason::None;
-    }
</del><ins>+    ASSERT(m_state != State::WaitingForWebCore);
</ins><span class="cx"> 
</span><del>-    if (m_pendingSwipeReason == PendingSwipeReason::InsufficientMagnitude) {
-        if (deltaIsSufficientToBeginSwipe(event)) {
-            trackSwipeGesture(event, m_pendingSwipeDirection);
-            return true;
-        }
</del><ins>+    if (m_state == State::None) {
+        SwipeDirection direction;
+        if (!scrollEventCanBecomeSwipe(event, direction))
+            return false;
</ins><span class="cx">     }
</span><span class="cx"> 
</span><del>-    if (m_activeGestureType != ViewGestureType::None)
</del><ins>+    if (!scrollEventCanInfluenceSwipe(event))
</ins><span class="cx">         return false;
</span><span class="cx"> 
</span><del>-    SwipeDirection direction;
-    if (!scrollEventCanBecomeSwipe(event, direction))
-        return false;
</del><ins>+    m_cumulativeDelta += FloatSize(event.scrollingDeltaX, event.scrollingDeltaY);
+    LOG(ViewGestures, &quot;Swipe Start Hysteresis - consumed event, cumulative delta (%0.2f, %0.2f)&quot;, m_cumulativeDelta.width(), m_cumulativeDelta.height());
</ins><span class="cx"> 
</span><del>-    if (!m_shouldIgnorePinnedState &amp;&amp; m_webPageProxy.willHandleHorizontalScrollEvents()) {
-        m_pendingSwipeReason = PendingSwipeReason::WebCoreMayScroll;
-        m_pendingSwipeDirection = direction;
</del><ins>+    if (deltaShouldCancelSwipe(m_cumulativeDelta.width(), m_cumulativeDelta.height())) {
+        reset(&quot;cumulative delta became too vertical&quot;);
</ins><span class="cx">         return false;
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    if (!deltaIsSufficientToBeginSwipe(event)) {
-        m_pendingSwipeReason = PendingSwipeReason::InsufficientMagnitude;
-        m_pendingSwipeDirection = direction;
-        return true;
-    }
</del><ins>+    if (std::abs(m_cumulativeDelta.width()) &gt;= minimumHorizontalSwipeDistance)
+        m_trackSwipeCallback(event, m_direction);
+    else
+        m_state = State::InsufficientMagnitude;
</ins><span class="cx"> 
</span><del>-    trackSwipeGesture(event, direction);
-
</del><span class="cx">     return true;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void ViewGestureController::wheelEventWasNotHandledByWebCore(NSEvent *event)
</del><ins>+void ViewGestureController::PendingSwipeTracker::reset(const char* resetReasonForLogging)
</ins><span class="cx"> {
</span><del>-    if (m_pendingSwipeReason != PendingSwipeReason::WebCoreMayScroll)
-        return;
</del><ins>+    if (m_state != State::None)
+        LOG(ViewGestures, &quot;Swipe Start Hysteresis - reset; %s&quot;, resetReasonForLogging);
</ins><span class="cx"> 
</span><del>-    m_pendingSwipeReason = PendingSwipeReason::None;
-
-    SwipeDirection direction;
-    if (!scrollEventCanBecomeSwipe(event, direction))
-        return;
-
-    if (!deltaIsSufficientToBeginSwipe(event)) {
-        m_pendingSwipeReason = PendingSwipeReason::InsufficientMagnitude;
-        return;
-    }
-
-    trackSwipeGesture(event, m_pendingSwipeDirection);
</del><ins>+    m_state = State::None;
+    m_cumulativeDelta = FloatSize();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void ViewGestureController::trackSwipeGesture(NSEvent *event, SwipeDirection direction)
</span><span class="cx"> {
</span><span class="cx">     ASSERT(m_activeGestureType == ViewGestureType::None);
</span><del>-    m_pendingSwipeReason = PendingSwipeReason::None;
</del><span class="cx"> 
</span><ins>+    m_pendingSwipeTracker.reset(&quot;starting to track swipe&quot;);
+
</ins><span class="cx">     m_webPageProxy.recordNavigationSnapshot();
</span><span class="cx"> 
</span><span class="cx">     CGFloat maxProgress = (direction == SwipeDirection::Back) ? 1 : 0;
</span><span class="lines">@@ -694,8 +704,8 @@
</span><span class="cx">     dimmingProgress = std::min(1., std::max(dimmingProgress, 0.));
</span><span class="cx">     [m_swipeDimmingLayer setOpacity:dimmingProgress * swipeOverlayDimmingOpacity];
</span><span class="cx"> 
</span><del>-    double absoluteProgress = fabs(progress);
-    double remainingSwipeDistance = width - fabs(absoluteProgress * width);
</del><ins>+    double absoluteProgress = std::abs(progress);
+    double remainingSwipeDistance = width - std::abs(absoluteProgress * width);
</ins><span class="cx">     double shadowFadeDistance = [m_swipeShadowLayer bounds].size.width;
</span><span class="cx">     if (remainingSwipeDistance &lt; shadowFadeDistance)
</span><span class="cx">         [m_swipeShadowLayer setOpacity:(remainingSwipeDistance / shadowFadeDistance) * swipeOverlayShadowOpacity];
</span><span class="lines">@@ -723,6 +733,13 @@
</span><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void ViewGestureController::setDidMoveSwipeSnapshotCallback(void(^callback)(CGRect))
+{
+    if (m_didMoveSwipeSnapshotCallback)
+        Block_release(m_didMoveSwipeSnapshotCallback);
+    m_didMoveSwipeSnapshotCallback = Block_copy(callback);
+}
+
</ins><span class="cx"> void ViewGestureController::didMoveSwipeSnapshotLayer()
</span><span class="cx"> {
</span><span class="cx">     if (!m_didMoveSwipeSnapshotCallback)
</span></span></pre>
</div>
</div>

</body>
</html>