Add magnify and rotate gesture event support for Mac
[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 <chrono>
35 #include <wtf/RetainPtr.h>
36 #include <wtf/RunLoop.h>
37
38 // FIXME: Move this file out of the mac/ subdirectory.
39
40 OBJC_CLASS CALayer;
41
42 #if PLATFORM(IOS)
43 OBJC_CLASS UIView;
44 OBJC_CLASS WKSwipeTransitionController;
45 OBJC_CLASS WKWebView;
46 OBJC_CLASS _UINavigationInteractiveTransitionBase;
47 OBJC_CLASS _UIViewControllerOneToOneTransitionContext;
48 OBJC_CLASS _UIViewControllerTransitionContext;
49 #else
50 OBJC_CLASS CAGradientLayer;
51 OBJC_CLASS NSEvent;
52 OBJC_CLASS NSView;
53 OBJC_CLASS WKSwipeCancellationTracker;
54 #endif
55
56 namespace WebCore {
57 class IOSurface;
58 }
59
60 namespace WebKit {
61
62 class ViewSnapshot;
63 class WebBackForwardListItem;
64 class WebPageProxy;
65
66 class ViewGestureController : private IPC::MessageReceiver {
67     WTF_MAKE_NONCOPYABLE(ViewGestureController);
68 public:
69     ViewGestureController(WebPageProxy&);
70     ~ViewGestureController();
71     void platformTeardown();
72     
73     enum class ViewGestureType {
74         None,
75 #if PLATFORM(MAC)
76         Magnification,
77         SmartMagnification,
78 #endif
79         Swipe
80     };
81     
82     enum class SwipeTransitionStyle {
83         Overlap,
84         Push
85     };
86     
87     enum class SwipeDirection {
88         Back,
89         Forward
90     };
91
92 #if PLATFORM(MAC)
93     double magnification() const;
94
95     void handleMagnificationGestureEvent(NSEvent *, WebCore::FloatPoint origin);
96
97     bool hasActiveMagnificationGesture() const { return m_activeGestureType == ViewGestureType::Magnification; }
98
99     void handleSmartMagnificationGesture(WebCore::FloatPoint origin);
100
101     bool handleScrollWheelEvent(NSEvent *);
102     void wheelEventWasNotHandledByWebCore(NSEvent *event) { m_pendingSwipeTracker.eventWasNotHandledByWebCore(event); }
103     void gestureEventWasNotHandledByWebCore(NSEvent *, WebCore::FloatPoint origin);
104
105     void setCustomSwipeViews(Vector<RetainPtr<NSView>> views) { m_customSwipeViews = WTF::move(views); }
106     void setCustomSwipeViewsTopContentInset(float topContentInset) { m_customSwipeViewsTopContentInset = topContentInset; }
107     WebCore::FloatRect windowRelativeBoundsForCustomSwipeViews() const;
108     void setDidMoveSwipeSnapshotCallback(void(^)(CGRect));
109
110     bool shouldIgnorePinnedState() { return m_pendingSwipeTracker.shouldIgnorePinnedState(); }
111     void setShouldIgnorePinnedState(bool ignore) { m_pendingSwipeTracker.setShouldIgnorePinnedState(ignore); }
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 #endif
121
122     WebCore::Color backgroundColorForCurrentSnapshot() const { return m_backgroundColorForCurrentSnapshot; }
123
124     void didFinishLoadForMainFrame() { didReachMainFrameLoadTerminalState(); }
125     void didFailLoadForMainFrame() { didReachMainFrameLoadTerminalState(); }
126     void didFirstVisuallyNonEmptyLayoutForMainFrame();
127     void didRepaintAfterNavigation();
128     void didHitRenderTreeSizeThreshold();
129     void didRestoreScrollPosition();
130     void didReachMainFrameLoadTerminalState();
131     void didSameDocumentNavigationForMainFrame(SameDocumentNavigationType);
132
133     void checkForActiveLoads();
134
135     void removeSwipeSnapshot();
136
137 private:
138     // IPC::MessageReceiver.
139     virtual void didReceiveMessage(IPC::Connection&, IPC::MessageDecoder&) override;
140
141     static ViewGestureController* gestureControllerForPage(uint64_t);
142
143     class SnapshotRemovalTracker {
144     public:
145         enum Event : uint8_t {
146             VisuallyNonEmptyLayout = 1 << 0,
147             RenderTreeSizeThreshold = 1 << 1,
148             RepaintAfterNavigation = 1 << 2,
149             MainFrameLoad = 1 << 3,
150             SubresourceLoads = 1 << 4,
151             ScrollPositionRestoration = 1 << 5
152         };
153         typedef uint8_t Events;
154
155         SnapshotRemovalTracker();
156
157         void start(Events, std::function<void()>);
158         void reset();
159
160         bool eventOccurred(Events);
161         bool cancelOutstandingEvent(Events);
162
163         void startWatchdog(std::chrono::seconds);
164
165     private:
166         static String eventsDescription(Events);
167         void log(const String&) const;
168
169         void fireRemovalCallbackImmediately();
170         void fireRemovalCallbackIfPossible();
171         void watchdogTimerFired();
172
173         bool stopWaitingForEvent(Events, const String& logReason);
174
175         Events m_outstandingEvents { 0 };
176         std::function<void()> m_removalCallback;
177         std::chrono::steady_clock::time_point m_startTime;
178
179         RunLoop::Timer<SnapshotRemovalTracker> m_watchdogTimer;
180     };
181
182 #if PLATFORM(MAC)
183     // Message handlers.
184     void didCollectGeometryForMagnificationGesture(WebCore::FloatRect visibleContentBounds, bool frameHandlesMagnificationGesture);
185     void didCollectGeometryForSmartMagnificationGesture(WebCore::FloatPoint origin, WebCore::FloatRect renderRect, WebCore::FloatRect visibleContentBounds, bool isReplacedElement, double viewportMinimumScale, double viewportMaximumScale);
186
187     void endMagnificationGesture();
188
189     WebCore::FloatPoint scaledMagnificationOrigin(WebCore::FloatPoint origin, double scale);
190
191     void trackSwipeGesture(NSEvent *, SwipeDirection);
192     void beginSwipeGesture(WebBackForwardListItem* targetItem, SwipeDirection);
193     void handleSwipeGesture(WebBackForwardListItem* targetItem, double progress, SwipeDirection);
194     void willEndSwipeGesture(WebBackForwardListItem& targetItem, bool cancelled);
195     void endSwipeGesture(WebBackForwardListItem* targetItem, bool cancelled);
196     bool shouldUseSnapshotForSize(ViewSnapshot&, WebCore::FloatSize swipeLayerSize, float topContentInset);
197
198     CALayer *determineSnapshotLayerParent() const;
199     CALayer *determineLayerAdjacentToSnapshotForParent(SwipeDirection, CALayer *snapshotLayerParent) const;
200     void applyDebuggingPropertiesToSwipeViews();
201     void didMoveSwipeSnapshotLayer();
202
203     void forceRepaintIfNeeded();
204
205     class PendingSwipeTracker {
206     public:
207         PendingSwipeTracker(WebPageProxy&, std::function<void(NSEvent *, SwipeDirection)> trackSwipeCallback);
208         bool handleEvent(NSEvent *);
209         void eventWasNotHandledByWebCore(NSEvent *);
210
211         void reset(const char* resetReasonForLogging);
212
213         bool shouldIgnorePinnedState() { return m_shouldIgnorePinnedState; }
214         void setShouldIgnorePinnedState(bool ignore) { m_shouldIgnorePinnedState = ignore; }
215
216     private:
217         bool tryToStartSwipe(NSEvent *);
218         bool scrollEventCanBecomeSwipe(NSEvent *, SwipeDirection&);
219
220         enum class State {
221             None,
222             WaitingForWebCore,
223             InsufficientMagnitude
224         };
225
226         State m_state { State::None };
227         SwipeDirection m_direction;
228         WebCore::FloatSize m_cumulativeDelta;
229
230         bool m_shouldIgnorePinnedState { false };
231
232         std::function<void(NSEvent *, SwipeDirection)> m_trackSwipeCallback;
233         WebPageProxy& m_webPageProxy;
234     };
235 #endif
236
237     WebPageProxy& m_webPageProxy;
238     ViewGestureType m_activeGestureType { ViewGestureType::None };
239
240     RunLoop::Timer<ViewGestureController> m_swipeActiveLoadMonitoringTimer;
241
242     WebCore::Color m_backgroundColorForCurrentSnapshot;
243
244 #if PLATFORM(MAC)
245     RefPtr<ViewSnapshot> m_currentSwipeSnapshot;
246
247     double m_magnification;
248     WebCore::FloatPoint m_magnificationOrigin;
249
250     WebCore::FloatRect m_lastSmartMagnificationUnscaledTargetRect;
251     bool m_lastMagnificationGestureWasSmartMagnification { false };
252     WebCore::FloatPoint m_lastSmartMagnificationOrigin;
253
254     WebCore::FloatRect m_visibleContentRect;
255     bool m_visibleContentRectIsValid { false };
256     bool m_frameHandlesMagnificationGesture { false };
257
258     RetainPtr<WKSwipeCancellationTracker> m_swipeCancellationTracker;
259     RetainPtr<CALayer> m_swipeLayer;
260     RetainPtr<CALayer> m_swipeSnapshotLayer;
261     RetainPtr<CAGradientLayer> m_swipeShadowLayer;
262     RetainPtr<CALayer> m_swipeDimmingLayer;
263     Vector<RetainPtr<CALayer>> m_currentSwipeLiveLayers;
264
265     SwipeTransitionStyle m_swipeTransitionStyle { SwipeTransitionStyle::Overlap };
266     Vector<RetainPtr<NSView>> m_customSwipeViews;
267     float m_customSwipeViewsTopContentInset { 0 };
268     WebCore::FloatRect m_currentSwipeCustomViewBounds;
269
270     PendingSwipeTracker m_pendingSwipeTracker;
271
272     void (^m_didMoveSwipeSnapshotCallback)(CGRect) { nullptr };
273
274     bool m_hasOutstandingRepaintRequest { false };
275 #else    
276     UIView *m_liveSwipeView { nullptr };
277     RetainPtr<UIView> m_liveSwipeViewClippingView;
278     RetainPtr<UIView> m_snapshotView;
279     RetainPtr<UIView> m_transitionContainerView;
280     RetainPtr<WKSwipeTransitionController> m_swipeInteractiveTransitionDelegate;
281     RetainPtr<_UIViewControllerOneToOneTransitionContext> m_swipeTransitionContext;
282     uint64_t m_snapshotRemovalTargetRenderTreeSize { 0 };
283     WeakObjCPtr<WKWebView> m_alternateBackForwardListSourceView;
284     RefPtr<WebPageProxy> m_webPageProxyForBackForwardListForCurrentSwipe;
285     uint64_t m_gesturePendingSnapshotRemoval { 0 };
286 #endif
287
288     SnapshotRemovalTracker m_snapshotRemovalTracker;
289 };
290
291 } // namespace WebKit
292
293 #endif // ViewGestureController_h