From b45a40a6717bff596b47a30ef9128a26752a9c8a Mon Sep 17 00:00:00 2001 From: Shawn Dempsey <96719+shwanton@users.noreply.github.com> Date: Thu, 1 Jun 2023 09:20:17 -0700 Subject: [PATCH] ScrollView - only send the onScroll event if the document view has actually scrolled (#1838) Co-authored-by: Shawn Dempsey --- React/Views/ScrollView/RCTScrollView.m | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/React/Views/ScrollView/RCTScrollView.m b/React/Views/ScrollView/RCTScrollView.m index 20234eb4eddb3b..1e337aff02e8f9 100644 --- a/React/Views/ScrollView/RCTScrollView.m +++ b/React/Views/ScrollView/RCTScrollView.m @@ -231,10 +231,10 @@ - (void)setContentOffset:(CGPoint)contentOffset #endif // macOS] if (contentView && _centerContent && !CGSizeEqualToSize(contentView.frame.size, CGSizeZero)) { CGSize subviewSize = contentView.frame.size; -#if TARGET_OS_OSX // [macOS - CGSize scrollViewSize = self.contentView.bounds.size; -#else // [macOS +#if !TARGET_OS_OSX // [macOS] CGSize scrollViewSize = self.bounds.size; +#else // [macOS + CGSize scrollViewSize = self.contentView.bounds.size; #endif // macOS] if (subviewSize.width <= scrollViewSize.width) { contentOffset.x = -(scrollViewSize.width - subviewSize.width) / 2.0; @@ -381,6 +381,7 @@ @implementation RCTScrollView { BOOL _allowNextScrollNoMatterWhat; #if TARGET_OS_OSX // [macOS BOOL _notifyDidScroll; + NSPoint _lastDocumentOrigin; #endif // macOS] CGRect _lastClippedToRect; uint16_t _coalescingKey; @@ -475,6 +476,7 @@ - (instancetype)initWithEventDispatcher:(id)eventDis _scrollView.delaysContentTouches = NO; #else // [macOS _scrollView.postsBoundsChangedNotifications = YES; + _lastDocumentOrigin = NSZeroPoint; #endif // macOS] #if !TARGET_OS_OSX // [macOS] @@ -875,8 +877,9 @@ - (void)scrollViewDocumentViewBoundsDidChange:(__unused NSNotification *)notific [_scrollView setContentOffset:_scrollView.contentOffset]; } - // if scrollView is not ready, don't notify with scroll event - if (_notifyDidScroll) { + // only send didScroll event if scrollView is ready or document origin changed + BOOL didScroll = !NSEqualPoints(_scrollView.documentView.frame.origin, _lastDocumentOrigin); + if (_notifyDidScroll && didScroll) { [self scrollViewDidScroll:_scrollView]; } } @@ -922,6 +925,9 @@ - (void)removeScrollListener:(NSObject *)scrollListener - (void)scrollViewDidScroll:(RCTCustomScrollView *)scrollView // [macOS] { +#if TARGET_OS_OSX // [macOS + _lastDocumentOrigin = scrollView.documentView.frame.origin; +#endif // macOS] NSTimeInterval now = CACurrentMediaTime(); [self updateClippedSubviews]; /** @@ -942,9 +948,7 @@ - (void)scrollViewDidScroll:(RCTCustomScrollView *)scrollView // [macOS] } #if !TARGET_OS_OSX // [macOS] RCT_FORWARD_SCROLL_EVENT(scrollViewDidScroll : scrollView); -#else // [macOS - (void) scrollView; -#endif // macOS] +#endif // [macOS] } #if !TARGET_OS_OSX // [macOS]