Swipe snapshot removed too early (jumps around) on arstechnica and NYT
[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/FloatRect.h>
33 #include <wtf/RetainPtr.h>
34 #include <wtf/RunLoop.h>
35
36 OBJC_CLASS CALayer;
37
38 #if PLATFORM(IOS)
39 OBJC_CLASS UIView;
40 OBJC_CLASS WKSwipeTransitionController;
41 OBJC_CLASS WKWebView;
42 OBJC_CLASS _UINavigationInteractiveTransitionBase;
43 OBJC_CLASS _UIViewControllerOneToOneTransitionContext;
44 OBJC_CLASS _UIViewControllerTransitionContext;
45 #else
46 OBJC_CLASS NSEvent;
47 OBJC_CLASS NSView;
48 OBJC_CLASS WKSwipeCancellationTracker;
49 #endif
50
51 namespace WebCore {
52 class IOSurface;
53 }
54
55 namespace WebKit {
56
57 class ViewSnapshot;
58 class WebBackForwardListItem;
59 class WebPageProxy;
60
61 class ViewGestureController : private IPC::MessageReceiver {
62     WTF_MAKE_NONCOPYABLE(ViewGestureController);
63 public:
64     ViewGestureController(WebPageProxy&);
65     ~ViewGestureController();
66     
67     enum class ViewGestureType {
68         None,
69 #if PLATFORM(MAC)
70         Magnification,
71         SmartMagnification,
72 #endif
73         Swipe
74     };
75     
76     enum class SwipeTransitionStyle {
77         Overlap,
78         Push
79     };
80     
81     enum class SwipeDirection {
82         Left,
83         Right
84     };
85
86     enum class PendingSwipeReason {
87         None,
88         WebCoreMayScroll,
89         InsufficientMagnitude
90     };
91     
92 #if PLATFORM(MAC)
93     double magnification() const;
94
95     void handleMagnificationGesture(double scale, WebCore::FloatPoint origin);
96     void handleSmartMagnificationGesture(WebCore::FloatPoint origin);
97
98     bool handleScrollWheelEvent(NSEvent *);
99     void wheelEventWasNotHandledByWebCore(NSEvent *);
100
101     void setCustomSwipeViews(Vector<RetainPtr<NSView>> views) { m_customSwipeViews = WTF::move(views); }
102     void setCustomSwipeViewsTopContentInset(float topContentInset) { m_customSwipeViewsTopContentInset = topContentInset; }
103     WebCore::FloatRect windowRelativeBoundsForCustomSwipeViews() const;
104     void setDidMoveSwipeSnapshotCallback(void(^)(CGRect));
105
106     void endActiveGesture();
107
108     bool shouldIgnorePinnedState() { return m_shouldIgnorePinnedState; }
109     void setShouldIgnorePinnedState(bool ignore) { m_shouldIgnorePinnedState = ignore; }
110
111     void didFirstVisuallyNonEmptyLayoutForMainFrame();
112 #else
113     void installSwipeHandler(UIView *gestureRecognizerView, UIView *swipingView);
114     void setAlternateBackForwardListSourceView(WKWebView *);
115     bool canSwipeInDirection(SwipeDirection);
116     void beginSwipeGesture(_UINavigationInteractiveTransitionBase *, SwipeDirection);
117     void endSwipeGesture(WebBackForwardListItem* targetItem, _UIViewControllerTransitionContext *, bool cancelled);
118     void willCommitPostSwipeTransitionLayerTree(bool);
119     void setRenderTreeSize(uint64_t);
120     void didRestoreScrollPosition();
121 #endif
122
123     void didFinishLoadForMainFrame();
124     void removeSwipeSnapshot();
125     void didSameDocumentNavigationForMainFrame(SameDocumentNavigationType);
126
127 private:
128     // IPC::MessageReceiver.
129     virtual void didReceiveMessage(IPC::Connection&, IPC::MessageDecoder&) override;
130
131     void swipeSnapshotWatchdogTimerFired();
132     void activeLoadMonitoringTimerFired();
133
134 #if PLATFORM(MAC)
135     // Message handlers.
136     void didCollectGeometryForMagnificationGesture(WebCore::FloatRect visibleContentBounds, bool frameHandlesMagnificationGesture);
137     void didCollectGeometryForSmartMagnificationGesture(WebCore::FloatPoint origin, WebCore::FloatRect renderRect, WebCore::FloatRect visibleContentBounds, bool isReplacedElement, double viewportMinimumScale, double viewportMaximumScale);
138     void didHitRenderTreeSizeThreshold();
139     void removeSwipeSnapshotAfterRepaint();
140
141     void endMagnificationGesture();
142     WebCore::FloatPoint scaledMagnificationOrigin(WebCore::FloatPoint origin, double scale);
143
144     void trackSwipeGesture(NSEvent *, SwipeDirection);
145     void beginSwipeGesture(WebBackForwardListItem* targetItem, SwipeDirection);
146     void handleSwipeGesture(WebBackForwardListItem* targetItem, double progress, SwipeDirection);
147     void endSwipeGesture(WebBackForwardListItem* targetItem, bool cancelled);
148     bool deltaIsSufficientToBeginSwipe(NSEvent *);
149     bool scrollEventCanBecomeSwipe(NSEvent *, SwipeDirection&);
150     bool shouldUseSnapshotForSize(ViewSnapshot&, WebCore::FloatSize swipeLayerSize, float topContentInset);
151
152     CALayer *determineSnapshotLayerParent() const;
153     CALayer *determineLayerAdjacentToSnapshotForParent(SwipeDirection, CALayer *snapshotLayerParent) const;
154     void applyDebuggingPropertiesToSwipeViews();
155     void didMoveSwipeSnapshotLayer();
156 #else
157     void removeSwipeSnapshotIfReady();
158 #endif
159
160     WebPageProxy& m_webPageProxy;
161     ViewGestureType m_activeGestureType { ViewGestureType::None };
162
163     RunLoop::Timer<ViewGestureController> m_swipeWatchdogTimer;
164     RunLoop::Timer<ViewGestureController> m_swipeActiveLoadMonitoringTimer;
165
166 #if PLATFORM(MAC)
167     RefPtr<ViewSnapshot> m_currentSwipeSnapshot;
168
169     RunLoop::Timer<ViewGestureController> m_swipeWatchdogAfterFirstVisuallyNonEmptyLayoutTimer;
170
171     double m_magnification;
172     WebCore::FloatPoint m_magnificationOrigin;
173
174     WebCore::FloatRect m_lastSmartMagnificationUnscaledTargetRect;
175     bool m_lastMagnificationGestureWasSmartMagnification { false };
176     WebCore::FloatPoint m_lastSmartMagnificationOrigin;
177
178     WebCore::FloatRect m_visibleContentRect;
179     bool m_visibleContentRectIsValid { false };
180     bool m_frameHandlesMagnificationGesture { false };
181
182     RetainPtr<WKSwipeCancellationTracker> m_swipeCancellationTracker;
183     RetainPtr<CALayer> m_swipeLayer;
184     RetainPtr<CALayer> m_swipeSnapshotLayer;
185     Vector<RetainPtr<CALayer>> m_currentSwipeLiveLayers;
186
187     SwipeTransitionStyle m_swipeTransitionStyle { SwipeTransitionStyle::Overlap };
188     Vector<RetainPtr<NSView>> m_customSwipeViews;
189     float m_customSwipeViewsTopContentInset { 0 };
190     WebCore::FloatRect m_currentSwipeCustomViewBounds;
191
192     // If we need to wait for content to decide if it is going to consume
193     // the scroll event that would have started a swipe, we'll fill these in.
194     PendingSwipeReason m_pendingSwipeReason { PendingSwipeReason::None };
195     SwipeDirection m_pendingSwipeDirection;
196     WebCore::FloatSize m_cumulativeDeltaForPendingSwipe;
197
198     void (^m_didMoveSwipeSnapshotCallback)(CGRect) { nullptr };
199
200     bool m_shouldIgnorePinnedState { false };
201     bool m_swipeInProgress { false };
202 #else    
203     UIView *m_liveSwipeView { nullptr };
204     RetainPtr<UIView> m_liveSwipeViewClippingView;
205     RetainPtr<UIView> m_snapshotView;
206     RetainPtr<UIView> m_transitionContainerView;
207     RetainPtr<WKSwipeTransitionController> m_swipeInteractiveTransitionDelegate;
208     RetainPtr<_UIViewControllerOneToOneTransitionContext> m_swipeTransitionContext;
209     uint64_t m_snapshotRemovalTargetRenderTreeSize { 0 };
210     WeakObjCPtr<WKWebView> m_alternateBackForwardListSourceView;
211     RefPtr<WebPageProxy> m_webPageProxyForBackForwardListForCurrentSwipe;
212     uint64_t m_gesturePendingSnapshotRemoval { 0 };
213 #endif
214
215     bool m_swipeWaitingForVisuallyNonEmptyLayout { false };
216     bool m_swipeWaitingForRenderTreeSizeThreshold { false };
217     bool m_swipeWaitingForRepaint { false };
218     bool m_swipeWaitingForDidFinishLoad { false };
219     bool m_swipeWaitingForSubresourceLoads { false };
220     bool m_swipeWaitingForScrollPositionRestoration { false };
221 };
222
223 } // namespace WebKit
224
225 #endif // ViewGestureController_h