Provide a mechanism through the legacy SPI to know when swipe gestures begin and end
[WebKit-https.git] / Source / WebKit2 / UIProcess / mac / ViewGestureController.h
1 /*
2  * Copyright (C) 2013, 2014 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #ifndef ViewGestureController_h
27 #define ViewGestureController_h
28
29 #include "MessageReceiver.h"
30 #include "SameDocumentNavigationType.h"
31 #include "WeakObjCPtr.h"
32 #include <WebCore/Color.h>
33 #include <WebCore/FloatRect.h>
34 #include <wtf/RetainPtr.h>
35 #include <wtf/RunLoop.h>
36
37 OBJC_CLASS CALayer;
38
39 #if PLATFORM(IOS)
40 OBJC_CLASS UIView;
41 OBJC_CLASS WKSwipeTransitionController;
42 OBJC_CLASS WKWebView;
43 OBJC_CLASS _UINavigationInteractiveTransitionBase;
44 OBJC_CLASS _UIViewControllerOneToOneTransitionContext;
45 OBJC_CLASS _UIViewControllerTransitionContext;
46 #else
47 OBJC_CLASS NSEvent;
48 OBJC_CLASS NSView;
49 OBJC_CLASS WKSwipeCancellationTracker;
50 #endif
51
52 namespace WebCore {
53 class IOSurface;
54 }
55
56 namespace WebKit {
57
58 class ViewSnapshot;
59 class WebBackForwardListItem;
60 class WebPageProxy;
61
62 class ViewGestureController : private IPC::MessageReceiver {
63     WTF_MAKE_NONCOPYABLE(ViewGestureController);
64 public:
65     ViewGestureController(WebPageProxy&);
66     ~ViewGestureController();
67     
68     enum class ViewGestureType {
69         None,
70 #if PLATFORM(MAC)
71         Magnification,
72         SmartMagnification,
73 #endif
74         Swipe
75     };
76     
77     enum class SwipeTransitionStyle {
78         Overlap,
79         Push
80     };
81     
82     enum class SwipeDirection {
83         Back,
84         Forward
85     };
86
87     enum class PendingSwipeReason {
88         None,
89         WebCoreMayScroll,
90         InsufficientMagnitude
91     };
92     
93 #if PLATFORM(MAC)
94     double magnification() const;
95
96     void handleMagnificationGesture(double scale, WebCore::FloatPoint origin);
97     void endMagnificationGesture();
98
99     void handleSmartMagnificationGesture(WebCore::FloatPoint origin);
100
101     bool handleScrollWheelEvent(NSEvent *);
102     void wheelEventWasNotHandledByWebCore(NSEvent *);
103
104     void setCustomSwipeViews(Vector<RetainPtr<NSView>> views) { m_customSwipeViews = WTF::move(views); }
105     void setCustomSwipeViewsTopContentInset(float topContentInset) { m_customSwipeViewsTopContentInset = topContentInset; }
106     WebCore::FloatRect windowRelativeBoundsForCustomSwipeViews() const;
107     void setDidMoveSwipeSnapshotCallback(void(^)(CGRect));
108
109     bool shouldIgnorePinnedState() { return m_shouldIgnorePinnedState; }
110     void setShouldIgnorePinnedState(bool ignore) { m_shouldIgnorePinnedState = ignore; }
111
112     void didFirstVisuallyNonEmptyLayoutForMainFrame();
113 #else
114     void installSwipeHandler(UIView *gestureRecognizerView, UIView *swipingView);
115     void setAlternateBackForwardListSourceView(WKWebView *);
116     bool canSwipeInDirection(SwipeDirection);
117     void beginSwipeGesture(_UINavigationInteractiveTransitionBase *, SwipeDirection);
118     void endSwipeGesture(WebBackForwardListItem* targetItem, _UIViewControllerTransitionContext *, bool cancelled);
119     void willCommitPostSwipeTransitionLayerTree(bool);
120     void setRenderTreeSize(uint64_t);
121     void didRestoreScrollPosition();
122 #endif
123
124     void didFinishLoadForMainFrame();
125     void removeSwipeSnapshot();
126     void didSameDocumentNavigationForMainFrame(SameDocumentNavigationType);
127
128     WebCore::Color backgroundColorForCurrentSnapshot() const { return m_backgroundColorForCurrentSnapshot; }
129
130 private:
131     // IPC::MessageReceiver.
132     virtual void didReceiveMessage(IPC::Connection&, IPC::MessageDecoder&) override;
133
134     void swipeSnapshotWatchdogTimerFired();
135     void activeLoadMonitoringTimerFired();
136
137 #if PLATFORM(MAC)
138     // Message handlers.
139     void didCollectGeometryForMagnificationGesture(WebCore::FloatRect visibleContentBounds, bool frameHandlesMagnificationGesture);
140     void didCollectGeometryForSmartMagnificationGesture(WebCore::FloatPoint origin, WebCore::FloatRect renderRect, WebCore::FloatRect visibleContentBounds, bool isReplacedElement, double viewportMinimumScale, double viewportMaximumScale);
141     void didHitRenderTreeSizeThreshold();
142     void removeSwipeSnapshotAfterRepaint();
143
144     WebCore::FloatPoint scaledMagnificationOrigin(WebCore::FloatPoint origin, double scale);
145
146     void trackSwipeGesture(NSEvent *, SwipeDirection);
147     void beginSwipeGesture(WebBackForwardListItem* targetItem, SwipeDirection);
148     void handleSwipeGesture(WebBackForwardListItem* targetItem, double progress, SwipeDirection);
149     void willEndSwipeGesture(WebBackForwardListItem& targetItem, bool cancelled);
150     void endSwipeGesture(WebBackForwardListItem* targetItem, bool cancelled);
151     bool deltaIsSufficientToBeginSwipe(NSEvent *);
152     bool scrollEventCanBecomeSwipe(NSEvent *, SwipeDirection&);
153     bool shouldUseSnapshotForSize(ViewSnapshot&, WebCore::FloatSize swipeLayerSize, float topContentInset);
154
155     CALayer *determineSnapshotLayerParent() const;
156     CALayer *determineLayerAdjacentToSnapshotForParent(SwipeDirection, CALayer *snapshotLayerParent) const;
157     void applyDebuggingPropertiesToSwipeViews();
158     void didMoveSwipeSnapshotLayer();
159 #else
160     void removeSwipeSnapshotIfReady();
161 #endif
162
163     WebPageProxy& m_webPageProxy;
164     ViewGestureType m_activeGestureType { ViewGestureType::None };
165
166     RunLoop::Timer<ViewGestureController> m_swipeWatchdogTimer;
167     RunLoop::Timer<ViewGestureController> m_swipeActiveLoadMonitoringTimer;
168
169     WebCore::Color m_backgroundColorForCurrentSnapshot;
170
171 #if PLATFORM(MAC)
172     RefPtr<ViewSnapshot> m_currentSwipeSnapshot;
173
174     RunLoop::Timer<ViewGestureController> m_swipeWatchdogAfterFirstVisuallyNonEmptyLayoutTimer;
175
176     double m_magnification;
177     WebCore::FloatPoint m_magnificationOrigin;
178
179     WebCore::FloatRect m_lastSmartMagnificationUnscaledTargetRect;
180     bool m_lastMagnificationGestureWasSmartMagnification { false };
181     WebCore::FloatPoint m_lastSmartMagnificationOrigin;
182
183     WebCore::FloatRect m_visibleContentRect;
184     bool m_visibleContentRectIsValid { false };
185     bool m_frameHandlesMagnificationGesture { false };
186
187     RetainPtr<WKSwipeCancellationTracker> m_swipeCancellationTracker;
188     RetainPtr<CALayer> m_swipeLayer;
189     RetainPtr<CALayer> m_swipeSnapshotLayer;
190     RetainPtr<CALayer> m_swipeShadowLayer;
191     RetainPtr<CALayer> m_swipeDimmingLayer;
192     Vector<RetainPtr<CALayer>> m_currentSwipeLiveLayers;
193
194     SwipeTransitionStyle m_swipeTransitionStyle { SwipeTransitionStyle::Overlap };
195     Vector<RetainPtr<NSView>> m_customSwipeViews;
196     float m_customSwipeViewsTopContentInset { 0 };
197     WebCore::FloatRect m_currentSwipeCustomViewBounds;
198
199     // If we need to wait for content to decide if it is going to consume
200     // the scroll event that would have started a swipe, we'll fill these in.
201     PendingSwipeReason m_pendingSwipeReason { PendingSwipeReason::None };
202     SwipeDirection m_pendingSwipeDirection;
203     WebCore::FloatSize m_cumulativeDeltaForPendingSwipe;
204
205     void (^m_didMoveSwipeSnapshotCallback)(CGRect) { nullptr };
206
207     bool m_shouldIgnorePinnedState { false };
208     bool m_swipeInProgress { false };
209 #else    
210     UIView *m_liveSwipeView { nullptr };
211     RetainPtr<UIView> m_liveSwipeViewClippingView;
212     RetainPtr<UIView> m_snapshotView;
213     RetainPtr<UIView> m_transitionContainerView;
214     RetainPtr<WKSwipeTransitionController> m_swipeInteractiveTransitionDelegate;
215     RetainPtr<_UIViewControllerOneToOneTransitionContext> m_swipeTransitionContext;
216     uint64_t m_snapshotRemovalTargetRenderTreeSize { 0 };
217     WeakObjCPtr<WKWebView> m_alternateBackForwardListSourceView;
218     RefPtr<WebPageProxy> m_webPageProxyForBackForwardListForCurrentSwipe;
219     uint64_t m_gesturePendingSnapshotRemoval { 0 };
220 #endif
221
222     bool m_swipeWaitingForVisuallyNonEmptyLayout { false };
223     bool m_swipeWaitingForRenderTreeSizeThreshold { false };
224     bool m_swipeWaitingForRepaint { false };
225     bool m_swipeWaitingForDidFinishLoad { false };
226     bool m_swipeWaitingForSubresourceLoads { false };
227     bool m_swipeWaitingForScrollPositionRestoration { false };
228 };
229
230 } // namespace WebKit
231
232 #endif // ViewGestureController_h