Skip to content

Commit

Permalink
ScrollView - only send the onScroll event if the document view has ac…
Browse files Browse the repository at this point in the history
…tually scrolled
  • Loading branch information
Shawn Dempsey committed Jun 1, 2023
1 parent f469e72 commit c502094
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions React/Views/ScrollView/RCTScrollView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -381,6 +381,7 @@ @implementation RCTScrollView {
BOOL _allowNextScrollNoMatterWhat;
#if TARGET_OS_OSX // [macOS
BOOL _notifyDidScroll;
NSPoint _lastDocumentOrigin;
#endif // macOS]
CGRect _lastClippedToRect;
uint16_t _coalescingKey;
Expand Down Expand Up @@ -475,6 +476,7 @@ - (instancetype)initWithEventDispatcher:(id<RCTEventDispatcherProtocol>)eventDis
_scrollView.delaysContentTouches = NO;
#else // [macOS
_scrollView.postsBoundsChangedNotifications = YES;
_lastDocumentOrigin = NSZeroPoint;
#endif // macOS]

#if !TARGET_OS_OSX // [macOS]
Expand Down Expand Up @@ -874,8 +876,9 @@ - (void)scrollViewDocumentViewBoundsDidChange:(__unused NSNotification *)notific
_scrollView.contentOffset = _scrollView.contentOffset; // necessary for content centering when _centerContent == YES
}

// 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];
}
}
Expand Down Expand Up @@ -921,6 +924,9 @@ - (void)removeScrollListener:(NSObject<UIScrollViewDelegate> *)scrollListener

- (void)scrollViewDidScroll:(RCTCustomScrollView *)scrollView // [macOS]
{
#if TARGET_OS_OSX // [macOS
_lastDocumentOrigin = scrollView.documentView.frame.origin;
#endif // macOS]
NSTimeInterval now = CACurrentMediaTime();
[self updateClippedSubviews];
/**
Expand All @@ -941,9 +947,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]
Expand Down

0 comments on commit c502094

Please sign in to comment.