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