[iOS WK2] REGRESSION (r242687): Programmatic scroll of overflow scroll results in...
[WebKit-https.git] / Source / WebKit / UIProcess / RemoteLayerTree / ios / ScrollingTreeScrollingNodeDelegateIOS.mm
1 /*
2  * Copyright (C) 2017 Igalia S.L. 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 #import "config.h"
27 #import "ScrollingTreeScrollingNodeDelegateIOS.h"
28
29 #if PLATFORM(IOS_FAMILY) && ENABLE(ASYNC_SCROLLING)
30
31 #import "RemoteScrollingCoordinatorProxy.h"
32 #import "RemoteScrollingTree.h"
33 #import "UIKitSPI.h"
34 #import "WebPageProxy.h"
35 #import <QuartzCore/QuartzCore.h>
36 #import <UIKit/UIPanGestureRecognizer.h>
37 #import <UIKit/UIScrollView.h>
38 #import <WebCore/ScrollingStateOverflowScrollingNode.h>
39 #import <WebCore/ScrollingTree.h>
40 #import <WebCore/ScrollingTreeFrameScrollingNode.h>
41 #import <WebCore/ScrollingTreeScrollingNode.h>
42 #import <wtf/BlockObjCExceptions.h>
43 #import <wtf/SetForScope.h>
44
45 #if ENABLE(CSS_SCROLL_SNAP)
46 #import <WebCore/AxisScrollSnapOffsets.h>
47 #import <WebCore/ScrollSnapOffsetsInfo.h>
48 #endif
49
50 #if ENABLE(POINTER_EVENTS)
51 #import "PageClient.h"
52 #endif
53
54 @implementation WKScrollingNodeScrollViewDelegate
55
56 - (instancetype)initWithScrollingTreeNodeDelegate:(WebKit::ScrollingTreeScrollingNodeDelegateIOS*)delegate
57 {
58     if ((self = [super init]))
59         _scrollingTreeNodeDelegate = delegate;
60
61     return self;
62 }
63
64 - (void)scrollViewDidScroll:(UIScrollView *)scrollView
65 {
66     _scrollingTreeNodeDelegate->scrollViewDidScroll(scrollView.contentOffset, _inUserInteraction);
67 }
68
69 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
70 {
71     _inUserInteraction = YES;
72
73     if (scrollView.panGestureRecognizer.state == UIGestureRecognizerStateBegan)
74         _scrollingTreeNodeDelegate->scrollViewWillStartPanGesture();
75     _scrollingTreeNodeDelegate->scrollWillStart();
76 }
77
78 - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
79 {
80 #if ENABLE(POINTER_EVENTS)
81     if (![scrollView isZooming]) {
82         if (auto touchActionData = _scrollingTreeNodeDelegate->touchActionData()) {
83             auto touchActions = touchActionData->touchActions;
84             if (touchActions != WebCore::TouchAction::Auto && touchActions != WebCore::TouchAction::Manipulation) {
85                 bool canPanX = true;
86                 bool canPanY = true;
87                 if (!touchActions.contains(WebCore::TouchAction::PanX)) {
88                     canPanX = false;
89                     targetContentOffset->x = scrollView.contentOffset.x;
90                 }
91                 if (!touchActions.contains(WebCore::TouchAction::PanY)) {
92                     canPanY = false;
93                     targetContentOffset->y = scrollView.contentOffset.y;
94                 }
95             }
96         }
97     }
98 #endif
99
100 #if ENABLE(CSS_SCROLL_SNAP)
101     CGFloat horizontalTarget = targetContentOffset->x;
102     CGFloat verticalTarget = targetContentOffset->y;
103
104     unsigned originalHorizontalSnapPosition = _scrollingTreeNodeDelegate->scrollingNode().currentHorizontalSnapPointIndex();
105     unsigned originalVerticalSnapPosition = _scrollingTreeNodeDelegate->scrollingNode().currentVerticalSnapPointIndex();
106
107     if (!_scrollingTreeNodeDelegate->scrollingNode().horizontalSnapOffsets().isEmpty()) {
108         unsigned index;
109         float potentialSnapPosition = WebCore::closestSnapOffset(_scrollingTreeNodeDelegate->scrollingNode().horizontalSnapOffsets(), _scrollingTreeNodeDelegate->scrollingNode().horizontalSnapOffsetRanges(), horizontalTarget, velocity.x, index);
110         _scrollingTreeNodeDelegate->scrollingNode().setCurrentHorizontalSnapPointIndex(index);
111         if (horizontalTarget >= 0 && horizontalTarget <= scrollView.contentSize.width)
112             targetContentOffset->x = potentialSnapPosition;
113     }
114
115     if (!_scrollingTreeNodeDelegate->scrollingNode().verticalSnapOffsets().isEmpty()) {
116         unsigned index;
117         float potentialSnapPosition = WebCore::closestSnapOffset(_scrollingTreeNodeDelegate->scrollingNode().verticalSnapOffsets(), _scrollingTreeNodeDelegate->scrollingNode().verticalSnapOffsetRanges(), verticalTarget, velocity.y, index);
118         _scrollingTreeNodeDelegate->scrollingNode().setCurrentVerticalSnapPointIndex(index);
119         if (verticalTarget >= 0 && verticalTarget <= scrollView.contentSize.height)
120             targetContentOffset->y = potentialSnapPosition;
121     }
122
123     if (originalHorizontalSnapPosition != _scrollingTreeNodeDelegate->scrollingNode().currentHorizontalSnapPointIndex()
124         || originalVerticalSnapPosition != _scrollingTreeNodeDelegate->scrollingNode().currentVerticalSnapPointIndex()) {
125         _scrollingTreeNodeDelegate->currentSnapPointIndicesDidChange(_scrollingTreeNodeDelegate->scrollingNode().currentHorizontalSnapPointIndex(), _scrollingTreeNodeDelegate->scrollingNode().currentVerticalSnapPointIndex());
126     }
127 #endif
128 }
129
130 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)willDecelerate
131 {
132     if (_inUserInteraction && !willDecelerate) {
133         _inUserInteraction = NO;
134         _scrollingTreeNodeDelegate->scrollViewDidScroll(scrollView.contentOffset, _inUserInteraction);
135         _scrollingTreeNodeDelegate->scrollDidEnd();
136     }
137 }
138
139 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
140 {
141     if (_inUserInteraction) {
142         _inUserInteraction = NO;
143         _scrollingTreeNodeDelegate->scrollViewDidScroll(scrollView.contentOffset, _inUserInteraction);
144         _scrollingTreeNodeDelegate->scrollDidEnd();
145     }
146 }
147
148 #if ENABLE(POINTER_EVENTS)
149 - (CGPoint)_scrollView:(UIScrollView *)scrollView adjustedOffsetForOffset:(CGPoint)offset translation:(CGPoint)translation startPoint:(CGPoint)start locationInView:(CGPoint)locationInView horizontalVelocity:(inout double *)hv verticalVelocity:(inout double *)vv
150 {
151     auto touchActionData = _scrollingTreeNodeDelegate->touchActionData();
152     if (!touchActionData) {
153         [self cancelPointersForGestureRecognizer:scrollView.panGestureRecognizer];
154         return offset;
155     }
156
157     auto touchActions = touchActionData->touchActions;
158     if (touchActions == WebCore::TouchAction::Auto || touchActions == WebCore::TouchAction::Manipulation)
159         return offset;
160
161     CGPoint adjustedContentOffset = CGPointMake(offset.x, offset.y);
162
163     if (!touchActions.contains(WebCore::TouchAction::PanX))
164         adjustedContentOffset.x = start.x;
165     if (!touchActions.contains(WebCore::TouchAction::PanY))
166         adjustedContentOffset.y = start.y;
167
168     if ((touchActions.contains(WebCore::TouchAction::PanX) && adjustedContentOffset.x != start.x)
169         || (touchActions.contains(WebCore::TouchAction::PanY) && adjustedContentOffset.y != start.y)) {
170         [self cancelPointersForGestureRecognizer:scrollView.panGestureRecognizer];
171     }
172
173     return adjustedContentOffset;
174 }
175
176 - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view
177 {
178     [self cancelPointersForGestureRecognizer:scrollView.pinchGestureRecognizer];
179 }
180
181 - (void)cancelPointersForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
182 {
183     _scrollingTreeNodeDelegate->cancelPointersForGestureRecognizer(gestureRecognizer);
184 }
185 #endif
186
187 @end
188
189 namespace WebKit {
190 using namespace WebCore;
191
192 ScrollingTreeScrollingNodeDelegateIOS::ScrollingTreeScrollingNodeDelegateIOS(ScrollingTreeScrollingNode& scrollingNode)
193     : ScrollingTreeScrollingNodeDelegate(scrollingNode)
194 {
195 }
196
197 ScrollingTreeScrollingNodeDelegateIOS::~ScrollingTreeScrollingNodeDelegateIOS()
198 {
199     BEGIN_BLOCK_OBJC_EXCEPTIONS
200     if (UIScrollView *scrollView = (UIScrollView *)[scrollLayer() delegate]) {
201         ASSERT([scrollView isKindOfClass:[UIScrollView self]]);
202         // The scrollView may have been adopted by another node, so only clear the delegate if it's ours.
203         if (scrollView.delegate == m_scrollViewDelegate.get())
204             scrollView.delegate = nil;
205     }
206     END_BLOCK_OBJC_EXCEPTIONS
207 }
208
209 void ScrollingTreeScrollingNodeDelegateIOS::resetScrollViewDelegate()
210 {
211     BEGIN_BLOCK_OBJC_EXCEPTIONS
212     if (UIScrollView *scrollView = (UIScrollView *)[scrollLayer() delegate]) {
213         ASSERT([scrollView isKindOfClass:[UIScrollView self]]);
214         scrollView.delegate = nil;
215     }
216     END_BLOCK_OBJC_EXCEPTIONS
217 }
218
219 void ScrollingTreeScrollingNodeDelegateIOS::commitStateBeforeChildren(const ScrollingStateScrollingNode& scrollingStateNode)
220 {
221     if (scrollingStateNode.hasChangedProperty(ScrollingStateScrollingNode::ScrollContainerLayer))
222         m_scrollLayer = scrollingStateNode.scrollContainerLayer();
223 }
224
225 void ScrollingTreeScrollingNodeDelegateIOS::commitStateAfterChildren(const ScrollingStateScrollingNode& scrollingStateNode)
226 {
227     SetForScope<bool> updatingChange(m_updatingFromStateNode, true);
228
229     if (scrollingStateNode.hasChangedProperty(ScrollingStateScrollingNode::ScrollContainerLayer)
230         || scrollingStateNode.hasChangedProperty(ScrollingStateScrollingNode::TotalContentsSize)
231         || scrollingStateNode.hasChangedProperty(ScrollingStateScrollingNode::ReachableContentsSize)
232         || scrollingStateNode.hasChangedProperty(ScrollingStateScrollingNode::ScrollPosition)
233         || scrollingStateNode.hasChangedProperty(ScrollingStateScrollingNode::ScrollOrigin)) {
234         BEGIN_BLOCK_OBJC_EXCEPTIONS
235         UIScrollView *scrollView = (UIScrollView *)[scrollLayer() delegate];
236         ASSERT([scrollView isKindOfClass:[UIScrollView self]]);
237
238         if (scrollingStateNode.hasChangedProperty(ScrollingStateScrollingNode::ScrollContainerLayer)) {
239             if (!m_scrollViewDelegate)
240                 m_scrollViewDelegate = adoptNS([[WKScrollingNodeScrollViewDelegate alloc] initWithScrollingTreeNodeDelegate:this]);
241
242             scrollView.scrollsToTop = NO;
243             scrollView.delegate = m_scrollViewDelegate.get();
244         }
245
246         bool recomputeInsets = scrollingStateNode.hasChangedProperty(ScrollingStateScrollingNode::TotalContentsSize);
247         if (scrollingStateNode.hasChangedProperty(ScrollingStateScrollingNode::ReachableContentsSize)) {
248             scrollView.contentSize = scrollingStateNode.reachableContentsSize();
249             recomputeInsets = true;
250         }
251
252         if (scrollingStateNode.hasChangedProperty(ScrollingStateScrollingNode::ScrollOrigin))
253             recomputeInsets = true;
254
255         if (recomputeInsets) {
256             UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, 0, 0);
257             // With RTL or bottom-to-top scrolling (non-zero origin), we need extra space on the left or top.
258             if (scrollOrigin().x())
259                 insets.left = reachableContentsSize().width() - totalContentsSize().width();
260
261             if (scrollOrigin().y())
262                 insets.top = reachableContentsSize().height() - totalContentsSize().height();
263
264             scrollView.contentInset = insets;
265         }
266         END_BLOCK_OBJC_EXCEPTIONS
267     }
268
269 #if ENABLE(CSS_SCROLL_SNAP)
270     // FIXME: If only one axis snaps in 2D scrolling, the other axis will decelerate fast as well. Is this what we want?
271     if (scrollingStateNode.hasChangedProperty(ScrollingStateScrollingNode::HorizontalSnapOffsets) || scrollingStateNode.hasChangedProperty(ScrollingStateScrollingNode::VerticalSnapOffsets)) {
272         BEGIN_BLOCK_OBJC_EXCEPTIONS
273         UIScrollView *scrollView = (UIScrollView *)[scrollLayer() delegate];
274         ASSERT([scrollView isKindOfClass:[UIScrollView self]]);
275
276         scrollView.decelerationRate = scrollingNode().horizontalSnapOffsets().size() || scrollingNode().verticalSnapOffsets().size() ? UIScrollViewDecelerationRateFast : UIScrollViewDecelerationRateNormal;
277 #endif
278         END_BLOCK_OBJC_EXCEPTIONS
279     }
280
281     if (scrollingStateNode.hasChangedProperty(ScrollingStateScrollingNode::ScrollableAreaParams)) {
282         BEGIN_BLOCK_OBJC_EXCEPTIONS
283         UIScrollView *scrollView = (UIScrollView *)[scrollLayer() delegate];
284         ASSERT([scrollView isKindOfClass:[UIScrollView self]]);
285
286         [scrollView setShowsHorizontalScrollIndicator:!scrollingNode().horizontalScrollbarHiddenByStyle()];
287         [scrollView setShowsVerticalScrollIndicator:!scrollingNode().verticalScrollbarHiddenByStyle()];
288
289         END_BLOCK_OBJC_EXCEPTIONS
290     }
291
292     if (scrollingStateNode.hasChangedProperty(ScrollingStateScrollingNode::RequestedScrollPosition)) {
293         auto scrollType = scrollingStateNode.requestedScrollPositionRepresentsProgrammaticScroll() ? ScrollType::Programmatic : ScrollType::User;
294         scrollingNode().scrollTo(scrollingStateNode.requestedScrollPosition(), scrollType);
295     }
296 }
297
298 void ScrollingTreeScrollingNodeDelegateIOS::repositionScrollingLayers()
299 {
300     auto scrollPosition = scrollingNode().currentScrollPosition();
301
302     BEGIN_BLOCK_OBJC_EXCEPTIONS
303     UIScrollView *scrollView = (UIScrollView *)[scrollLayer() delegate];
304     ASSERT([scrollView isKindOfClass:[UIScrollView self]]);
305     [scrollView setContentOffset:scrollPosition];
306     END_BLOCK_OBJC_EXCEPTIONS
307 }
308
309 void ScrollingTreeScrollingNodeDelegateIOS::scrollWillStart() const
310 {
311     scrollingTree().scrollingTreeNodeWillStartScroll();
312 }
313
314 void ScrollingTreeScrollingNodeDelegateIOS::scrollDidEnd() const
315 {
316     scrollingTree().scrollingTreeNodeDidEndScroll();
317 }
318
319 void ScrollingTreeScrollingNodeDelegateIOS::scrollViewWillStartPanGesture() const
320 {
321     scrollingTree().scrollingTreeNodeWillStartPanGesture();
322 }
323
324 void ScrollingTreeScrollingNodeDelegateIOS::scrollViewDidScroll(const FloatPoint& scrollPosition, bool inUserInteraction)
325 {
326     if (m_updatingFromStateNode)
327         return;
328
329     scrollingNode().wasScrolledByDelegatedScrolling(scrollPosition);
330 }
331
332 void ScrollingTreeScrollingNodeDelegateIOS::currentSnapPointIndicesDidChange(unsigned horizontal, unsigned vertical) const
333 {
334     if (m_updatingFromStateNode)
335         return;
336
337     scrollingTree().currentSnapPointIndicesDidChange(scrollingNode().scrollingNodeID(), horizontal, vertical);
338 }
339
340 #if ENABLE(POINTER_EVENTS)
341 Optional<TouchActionData> ScrollingTreeScrollingNodeDelegateIOS::touchActionData() const
342 {
343     return downcast<RemoteScrollingTree>(scrollingTree()).scrollingCoordinatorProxy().touchActionDataForScrollNodeID(scrollingNode().scrollingNodeID());
344 }
345
346 void ScrollingTreeScrollingNodeDelegateIOS::cancelPointersForGestureRecognizer(UIGestureRecognizer* gestureRecognizer)
347 {
348     downcast<RemoteScrollingTree>(scrollingTree()).scrollingCoordinatorProxy().webPageProxy().pageClient().cancelPointersForGestureRecognizer(gestureRecognizer);
349 }
350 #endif
351
352 } // namespace WebKit
353
354 #endif // PLATFORM(IOS_FAMILY) && ENABLE(ASYNC_SCROLLING)