Skip to content
Permalink
Browse files
Share more code between iOS and macOS ViewGestureController
https://bugs.webkit.org/show_bug.cgi?id=163158

Reviewed by Simon Fraser.

Share canSwipeInDirection() and the (unused on Mac) alternate back-forward list mechanism.
Make ViewGestureController operate in terms of WebPageProxy, not WKWebView,
because it shouldn't know anything about WKWebView.
Refactor scrollEventCanBecomeSwipe a bit to be less repetitive.

* UIProcess/API/Cocoa/WKWebView.mm:
(-[WKWebView setAllowsBackForwardNavigationGestures:]):
* UIProcess/Cocoa/ViewGestureController.cpp:
(WebKit::ViewGestureController::ViewGestureController):
(WebKit::ViewGestureController::setAlternateBackForwardListSourcePage):
(WebKit::ViewGestureController::canSwipeInDirection):
(WebKit::ViewGestureController::gestureControllerForPage): Deleted.
* UIProcess/Cocoa/ViewGestureController.h:
* UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::WebPageProxy):
* UIProcess/WebPageProxy.h:
(WebKit::WebPageProxy::createWeakPtr):
* UIProcess/ios/ViewGestureControllerIOS.mm:
(WebKit::ViewGestureController::beginSwipeGesture):
(WebKit::ViewGestureController::setAlternateBackForwardListSourceView): Deleted.
(WebKit::ViewGestureController::canSwipeInDirection): Deleted.
* UIProcess/mac/ViewGestureControllerMac.mm:
(WebKit::ViewGestureController::PendingSwipeTracker::PendingSwipeTracker):
(WebKit::ViewGestureController::PendingSwipeTracker::scrollEventCanBecomeSwipe):
(WebKit::ViewGestureController::PendingSwipeTracker::tryToStartSwipe):


Canonical link: https://commits.webkit.org/180998@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@206950 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
hortont424 committed Oct 8, 2016
1 parent 9110116 commit f5b83271aa6f7450e19b35fbad9aebf8bbb83caf
@@ -1,3 +1,36 @@
2016-10-08 Tim Horton <timothy_horton@apple.com>

Share more code between iOS and macOS ViewGestureController
https://bugs.webkit.org/show_bug.cgi?id=163158

Reviewed by Simon Fraser.

Share canSwipeInDirection() and the (unused on Mac) alternate back-forward list mechanism.
Make ViewGestureController operate in terms of WebPageProxy, not WKWebView,
because it shouldn't know anything about WKWebView.
Refactor scrollEventCanBecomeSwipe a bit to be less repetitive.

* UIProcess/API/Cocoa/WKWebView.mm:
(-[WKWebView setAllowsBackForwardNavigationGestures:]):
* UIProcess/Cocoa/ViewGestureController.cpp:
(WebKit::ViewGestureController::ViewGestureController):
(WebKit::ViewGestureController::setAlternateBackForwardListSourcePage):
(WebKit::ViewGestureController::canSwipeInDirection):
(WebKit::ViewGestureController::gestureControllerForPage): Deleted.
* UIProcess/Cocoa/ViewGestureController.h:
* UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::WebPageProxy):
* UIProcess/WebPageProxy.h:
(WebKit::WebPageProxy::createWeakPtr):
* UIProcess/ios/ViewGestureControllerIOS.mm:
(WebKit::ViewGestureController::beginSwipeGesture):
(WebKit::ViewGestureController::setAlternateBackForwardListSourceView): Deleted.
(WebKit::ViewGestureController::canSwipeInDirection): Deleted.
* UIProcess/mac/ViewGestureControllerMac.mm:
(WebKit::ViewGestureController::PendingSwipeTracker::PendingSwipeTracker):
(WebKit::ViewGestureController::PendingSwipeTracker::scrollEventCanBecomeSwipe):
(WebKit::ViewGestureController::PendingSwipeTracker::tryToStartSwipe):

2016-10-07 Anders Carlsson <andersca@apple.com>

Get rid of API::Session and WKSessionRef
@@ -2198,7 +2198,7 @@ - (void)setAllowsBackForwardNavigationGestures:(BOOL)allowsBackForwardNavigation
if (!_gestureController) {
_gestureController = std::make_unique<WebKit::ViewGestureController>(*_page);
_gestureController->installSwipeHandler(self, [self scrollView]);
_gestureController->setAlternateBackForwardListSourceView([_configuration _alternateWebViewForNavigationGestures]);
_gestureController->setAlternateBackForwardListSourcePage([_configuration _alternateWebViewForNavigationGestures]->_page.get());
}
} else
_gestureController = nullptr;
@@ -29,6 +29,7 @@
#import "Logging.h"
#import "RemoteLayerTreeDrawingAreaProxy.h"
#import "ViewGestureControllerMessages.h"
#import "WebBackForwardList.h"
#import "WebPageProxy.h"
#import "WebProcessProxy.h"
#import <wtf/MathExtras.h>
@@ -59,7 +60,7 @@ ViewGestureController::ViewGestureController(WebPageProxy& webPageProxy)
: m_webPageProxy(webPageProxy)
, m_swipeActiveLoadMonitoringTimer(RunLoop::main(), this, &ViewGestureController::checkForActiveLoads)
#if PLATFORM(MAC)
, m_pendingSwipeTracker(webPageProxy, std::bind(&ViewGestureController::trackSwipeGesture, this, std::placeholders::_1, std::placeholders::_2))
, m_pendingSwipeTracker(webPageProxy, *this)
#endif
{
m_webPageProxy.process().addMessageReceiver(Messages::ViewGestureController::messageReceiverName(), m_webPageProxy.pageID(), *this);
@@ -83,6 +84,24 @@ ViewGestureController* ViewGestureController::gestureControllerForPage(uint64_t
return nullptr;
return gestureControllerIter->value;
}

void ViewGestureController::setAlternateBackForwardListSourcePage(WebPageProxy* page)
{
if (page)
m_alternateBackForwardListSourcePage = page->createWeakPtr();
else
m_alternateBackForwardListSourcePage.clear();
}

bool ViewGestureController::canSwipeInDirection(SwipeDirection direction) const
{
RefPtr<WebPageProxy> alternateBackForwardListSourcePage = m_alternateBackForwardListSourcePage.get();
auto& backForwardList = alternateBackForwardListSourcePage ? alternateBackForwardListSourcePage->backForwardList() : m_webPageProxy.backForwardList();
if (direction == SwipeDirection::Back)
return !!backForwardList.backItem();
return !!backForwardList.forwardItem();
}


void ViewGestureController::didFirstVisuallyNonEmptyLayoutForMainFrame()
{
@@ -28,22 +28,19 @@

#include "MessageReceiver.h"
#include "SameDocumentNavigationType.h"
#include "WeakObjCPtr.h"
#include <WebCore/Color.h>
#include <WebCore/FloatRect.h>
#include <chrono>
#include <wtf/BlockPtr.h>
#include <wtf/RetainPtr.h>
#include <wtf/RunLoop.h>

// FIXME: Move this file out of the mac/ subdirectory.
#include <wtf/WeakPtr.h>

OBJC_CLASS CALayer;

#if PLATFORM(IOS)
OBJC_CLASS UIView;
OBJC_CLASS WKSwipeTransitionController;
OBJC_CLASS WKWebView;
OBJC_CLASS _UINavigationInteractiveTransitionBase;
OBJC_CLASS _UIViewControllerOneToOneTransitionContext;
OBJC_CLASS _UIViewControllerTransitionContext;
@@ -109,13 +106,15 @@ class ViewGestureController : private IPC::MessageReceiver {
bool isPhysicallySwipingLeft(SwipeDirection) const;
#else
void installSwipeHandler(UIView *gestureRecognizerView, UIView *swipingView);
void setAlternateBackForwardListSourceView(WKWebView *);
bool canSwipeInDirection(SwipeDirection);
void beginSwipeGesture(_UINavigationInteractiveTransitionBase *, SwipeDirection);
void endSwipeGesture(WebBackForwardListItem* targetItem, _UIViewControllerTransitionContext *, bool cancelled);
void willCommitPostSwipeTransitionLayerTree(bool);
void setRenderTreeSize(uint64_t);
#endif

void setAlternateBackForwardListSourcePage(WebPageProxy*);

bool canSwipeInDirection(SwipeDirection) const;

WebCore::Color backgroundColorForCurrentSnapshot() const { return m_backgroundColorForCurrentSnapshot; }

@@ -202,7 +201,7 @@ class ViewGestureController : private IPC::MessageReceiver {

class PendingSwipeTracker {
public:
PendingSwipeTracker(WebPageProxy&, std::function<void(NSEvent *, SwipeDirection)> trackSwipeCallback);
PendingSwipeTracker(WebPageProxy&, ViewGestureController&);
bool handleEvent(NSEvent *);
void eventWasNotHandledByWebCore(NSEvent *);

@@ -227,7 +226,7 @@ class ViewGestureController : private IPC::MessageReceiver {

bool m_shouldIgnorePinnedState { false };

std::function<void(NSEvent *, SwipeDirection)> m_trackSwipeCallback;
ViewGestureController& m_viewGestureController;
WebPageProxy& m_webPageProxy;
};
#endif
@@ -238,6 +237,9 @@ class ViewGestureController : private IPC::MessageReceiver {
RunLoop::Timer<ViewGestureController> m_swipeActiveLoadMonitoringTimer;

WebCore::Color m_backgroundColorForCurrentSnapshot;

WeakPtr<WebPageProxy> m_alternateBackForwardListSourcePage;
RefPtr<WebPageProxy> m_webPageProxyForBackForwardListForCurrentSwipe;

#if PLATFORM(MAC)
RefPtr<ViewSnapshot> m_currentSwipeSnapshot;
@@ -277,8 +279,6 @@ class ViewGestureController : private IPC::MessageReceiver {
RetainPtr<WKSwipeTransitionController> m_swipeInteractiveTransitionDelegate;
RetainPtr<_UIViewControllerOneToOneTransitionContext> m_swipeTransitionContext;
uint64_t m_snapshotRemovalTargetRenderTreeSize { 0 };
WeakObjCPtr<WKWebView> m_alternateBackForwardListSourceView;
RefPtr<WebPageProxy> m_webPageProxyForBackForwardListForCurrentSwipe;
uint64_t m_gesturePendingSnapshotRemoval { 0 };
#endif

@@ -450,6 +450,7 @@ WebPageProxy::WebPageProxy(PageClient& pageClient, WebProcessProxy& process, uin
, m_configurationPreferenceValues(m_configuration->preferenceValues())
, m_potentiallyChangedViewStateFlags(ViewState::NoFlags)
, m_viewStateChangeWantsSynchronousReply(false)
, m_weakPtrFactory(this)
{
m_webProcessLifetimeTracker.addObserver(m_visitedLinkStore);
m_webProcessLifetimeTracker.addObserver(m_websiteDataStore);
@@ -1129,6 +1129,8 @@ class WebPageProxy : public API::ObjectImpl<API::Object::Type::Page>
#if ENABLE(GAMEPAD)
void gamepadActivity(const Vector<GamepadData>&);
#endif

WeakPtr<WebPageProxy> createWeakPtr() const { return m_weakPtrFactory.createWeakPtr(); }

private:
WebPageProxy(PageClient&, WebProcessProxy&, uint64_t pageID, Ref<API::PageConfiguration>&&);
@@ -1889,6 +1891,8 @@ class WebPageProxy : public API::ObjectImpl<API::Object::Type::Page>
#if ENABLE(DOWNLOAD_ATTRIBUTE)
bool m_syncNavigationActionHasDownloadAttribute { false };
#endif

WeakPtrFactory<WebPageProxy> m_weakPtrFactory;
};

} // namespace WebKit
@@ -34,6 +34,7 @@
#import "ViewSnapshotStore.h"
#import "WKBackForwardListItemInternal.h"
#import "WKWebViewInternal.h"
#import "WeakObjCPtr.h"
#import "WebBackForwardList.h"
#import "WebPageGroup.h"
#import "WebPageMessages.h"
@@ -140,11 +141,6 @@ - (UIPanGestureRecognizer *)gestureRecognizerForInteractiveTransition:(_UINaviga
[m_swipeInteractiveTransitionDelegate invalidate];
}

void ViewGestureController::setAlternateBackForwardListSourceView(WKWebView *view)
{
m_alternateBackForwardListSourceView = view;
}

void ViewGestureController::installSwipeHandler(UIView *gestureRecognizerView, UIView *swipingView)
{
ASSERT(!m_swipeInteractiveTransitionDelegate);
@@ -159,7 +155,9 @@ - (UIPanGestureRecognizer *)gestureRecognizerForInteractiveTransition:(_UINaviga

m_webPageProxy.recordAutomaticNavigationSnapshot();

m_webPageProxyForBackForwardListForCurrentSwipe = m_alternateBackForwardListSourceView.get() ? m_alternateBackForwardListSourceView.get()->_page : &m_webPageProxy;
RefPtr<WebPageProxy> alternateBackForwardListSourcePage = m_alternateBackForwardListSourcePage.get();
m_webPageProxyForBackForwardListForCurrentSwipe = alternateBackForwardListSourcePage ? alternateBackForwardListSourcePage.get() : &m_webPageProxy;

m_webPageProxyForBackForwardListForCurrentSwipe->navigationGestureDidBegin();
if (&m_webPageProxy != m_webPageProxyForBackForwardListForCurrentSwipe)
m_webPageProxy.navigationGestureDidBegin();
@@ -239,14 +237,6 @@ - (UIPanGestureRecognizer *)gestureRecognizerForInteractiveTransition:(_UINaviga
m_activeGestureType = ViewGestureType::Swipe;
}

bool ViewGestureController::canSwipeInDirection(SwipeDirection direction)
{
auto& backForwardList = m_alternateBackForwardListSourceView.get() ? m_alternateBackForwardListSourceView.get()->_page->backForwardList() : m_webPageProxy.backForwardList();
if (direction == SwipeDirection::Back)
return !!backForwardList.backItem();
return !!backForwardList.forwardItem();
}

void ViewGestureController::endSwipeGesture(WebBackForwardListItem* targetItem, _UIViewControllerTransitionContext *context, bool cancelled)
{
[context _setTransitionIsInFlight:NO];
@@ -272,8 +272,8 @@ static bool deltaShouldCancelSwipe(float x, float y)
return std::abs(y) >= std::abs(x) * minimumScrollEventRatioForSwipe;
}

ViewGestureController::PendingSwipeTracker::PendingSwipeTracker(WebPageProxy& webPageProxy, std::function<void(NSEvent *, SwipeDirection)> trackSwipeCallback)
: m_trackSwipeCallback(WTFMove(trackSwipeCallback))
ViewGestureController::PendingSwipeTracker::PendingSwipeTracker(WebPageProxy& webPageProxy, ViewGestureController& viewGestureController)
: m_viewGestureController(viewGestureController)
, m_webPageProxy(webPageProxy)
{
}
@@ -292,21 +292,16 @@ static bool deltaShouldCancelSwipe(float x, float y)
bool isPinnedToLeft = m_shouldIgnorePinnedState || m_webPageProxy.isPinnedToLeftSide();
bool isPinnedToRight = m_shouldIgnorePinnedState || m_webPageProxy.isPinnedToRightSide();

bool willSwipeBack = false;
bool willSwipeForward = false;
if (m_webPageProxy.userInterfaceLayoutDirection() == WebCore::UserInterfaceLayoutDirection::LTR) {
willSwipeBack = event.scrollingDeltaX > 0 && isPinnedToLeft && m_webPageProxy.backForwardList().backItem();
willSwipeForward = event.scrollingDeltaX < 0 && isPinnedToRight && m_webPageProxy.backForwardList().forwardItem();
} else {
willSwipeBack = event.scrollingDeltaX < 0 && isPinnedToRight && m_webPageProxy.backForwardList().backItem();
willSwipeForward = event.scrollingDeltaX > 0 && isPinnedToLeft && m_webPageProxy.backForwardList().forwardItem();
}
if (!willSwipeBack && !willSwipeForward)
bool tryingToSwipeBack = event.scrollingDeltaX > 0 && isPinnedToLeft;
bool tryingToSwipeForward = event.scrollingDeltaX < 0 && isPinnedToRight;
if (m_webPageProxy.userInterfaceLayoutDirection() != WebCore::UserInterfaceLayoutDirection::LTR)
std::swap(tryingToSwipeBack, tryingToSwipeForward);

if (!tryingToSwipeBack && !tryingToSwipeForward)
return false;

potentialSwipeDirection = willSwipeBack ? ViewGestureController::SwipeDirection::Back : ViewGestureController::SwipeDirection::Forward;

return true;
potentialSwipeDirection = tryingToSwipeBack ? SwipeDirection::Back : SwipeDirection::Forward;
return m_viewGestureController.canSwipeInDirection(potentialSwipeDirection);
}

bool ViewGestureController::handleScrollWheelEvent(NSEvent *event)
@@ -372,7 +367,7 @@ static bool deltaShouldCancelSwipe(float x, float y)
}

if (std::abs(m_cumulativeDelta.width()) >= minimumHorizontalSwipeDistance)
m_trackSwipeCallback(event, m_direction);
m_viewGestureController.trackSwipeGesture(event, m_direction);
else
m_state = State::InsufficientMagnitude;

0 comments on commit f5b8327

Please sign in to comment.