Skip to content

Commit 55e061e

Browse files
authored
Dispatch paired momentum scroll events for animated programmatic scrolls
Summary: On iOS, an animated `scrollTo`/`scrollToEnd` dispatched `onMomentumScrollEnd` without a preceding `onMomentumScrollBegin`, and a non-animated one dispatched a lone `onMomentumScrollEnd`. Android dispatches a begin/end pair for animated programmatic scrolls and nothing for non-animated ones. An earlier attempt dispatched `onMomentumScrollBegin` after starting the animation. When UIKit finished the animation synchronously, or never reported its end, the begin event arrived after its end event or was never matched. `ScrollView` treats a begin newer than the last end as "still animating" and captures every touch, so its children stopped responding. Behind the `fixProgrammaticScrollMomentumEventsIOS` feature flag (off by default, on for the OSS canary release level), track an in-flight animated programmatic scroll and guarantee ordering and pairing: - Dispatch `onMomentumScrollBegin` before calling `setContentOffset:animated:`. - Dispatch `onMomentumScrollEnd` when the animation ends, when UIKit applies the offset without animating (no window, or offset applied immediately), when a drag interrupts the animation, when the view leaves the window, or from a fallback one second later if UIKit never reports the end. - Non-animated scrolls, and animated `zoomToRect`, dispatch neither event. With the flag off, behavior is unchanged. Changelog: [iOS][Added] - `fixProgrammaticScrollMomentumEventsIOS` feature flag: `onMomentumScrollBegin` and `onMomentumScrollEnd` fire as a pair for animated `scrollTo`/`scrollToEnd`, and neither fires for non-animated scrolls Reviewed By: andrewdacenko Differential Revision: D131264148 Pull Request resolved: #60162
1 parent e103a4e commit 55e061e

23 files changed

Lines changed: 248 additions & 61 deletions

‎packages/react-native/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollViewComponentView.mm‎

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
static NSString *kOnScrollEndEvent = @"onScrollEnded";
3434

3535
static const CGFloat kClippingLeeway = 44.0;
36+
// Well past UIKit's fixed-duration `setContentOffset:animated:` animation.
37+
static const NSTimeInterval kProgrammaticScrollEndFallbackDelay = 1.0;
3638

3739
static UIScrollViewKeyboardDismissMode RCTUIKeyboardDismissModeFromProps(const ScrollViewProps &props)
3840
{
@@ -112,6 +114,11 @@ @implementation RCTScrollViewComponentView {
112114
// This helps to only update state from `scrollViewDidScroll` in case
113115
// some other part of the system scrolls scroll view.
114116
BOOL _isUserTriggeredScrolling;
117+
// Whether an animated programmatic scroll has dispatched `onMomentumScrollBegin` and still owes
118+
// its `onMomentumScrollEnd`.
119+
BOOL _isProgrammaticScrollAnimating;
120+
// Identifies the latest animated programmatic scroll, so a stale end fallback is ignored.
121+
NSUInteger _programmaticScrollGeneration;
115122
BOOL _shouldUpdateContentInsetAdjustmentBehavior;
116123
BOOL _automaticallyAdjustKeyboardInsets;
117124

@@ -731,6 +738,8 @@ - (void)prepareForRecycle
731738
_shouldUpdateContentInsetAdjustmentBehavior = YES;
732739
_automaticallyAdjustKeyboardInsets = NO;
733740
_isUserTriggeredScrolling = NO;
741+
_isProgrammaticScrollAnimating = NO;
742+
_programmaticScrollGeneration++;
734743
CGRect oldFrame = self.frame;
735744
self.frame = CGRectZero;
736745
self.frame = oldFrame;
@@ -816,10 +825,17 @@ - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
816825
{
817826
[self _forceDispatchNextScrollEvent];
818827

828+
// A drag interrupts an animated programmatic scroll without `scrollViewDidEndScrollingAnimation`.
829+
BOOL interruptedProgrammaticScroll = _isProgrammaticScrollAnimating;
830+
_isProgrammaticScrollAnimating = NO;
831+
819832
if (!_eventEmitter) {
820833
return;
821834
}
822835

836+
if (interruptedProgrammaticScroll) {
837+
static_cast<const ScrollViewEventEmitter &>(*_eventEmitter).onMomentumScrollEnd([self _scrollViewMetrics]);
838+
}
823839
static_cast<const ScrollViewEventEmitter &>(*_eventEmitter).onScrollBeginDrag([self _scrollViewMetrics]);
824840
_isUserTriggeredScrolling = YES;
825841
}
@@ -945,6 +961,7 @@ - (void)_updateContentViewSafeAreaPadding
945961
- (void)_handleScrollEndIfNeeded
946962
{
947963
if (_scrollView.isDecelerating || !_scrollView.isTracking) {
964+
_isProgrammaticScrollAnimating = NO;
948965
if (!_eventEmitter) {
949966
return;
950967
}
@@ -960,11 +977,16 @@ - (void)_handleFinishedScrolling:(UIScrollView *)scrollView
960977
[self _forceDispatchNextScrollEvent];
961978
[self scrollViewDidScroll:scrollView];
962979

980+
BOOL finishedProgrammaticScroll = _isProgrammaticScrollAnimating;
981+
_isProgrammaticScrollAnimating = NO;
982+
963983
if (!_eventEmitter) {
964984
return;
965985
}
966986

967-
static_cast<const ScrollViewEventEmitter &>(*_eventEmitter).onMomentumScrollEnd([self _scrollViewMetrics]);
987+
if (finishedProgrammaticScroll || !ReactNativeFeatureFlags::fixProgrammaticScrollMomentumEventsIOS()) {
988+
static_cast<const ScrollViewEventEmitter &>(*_eventEmitter).onMomentumScrollEnd([self _scrollViewMetrics]);
989+
}
968990
[self _updateStateWithContentOffset];
969991
}
970992

@@ -1119,12 +1141,54 @@ - (void)scrollToOffset:(CGPoint)offset animated:(BOOL)animated
11191141

11201142
[self _forceDispatchNextScrollEvent];
11211143

1122-
[_scrollView setContentOffset:offset animated:animated];
1144+
if (!ReactNativeFeatureFlags::fixProgrammaticScrollMomentumEventsIOS()) {
1145+
[_scrollView setContentOffset:offset animated:animated];
1146+
if (!animated) {
1147+
[self _handleFinishedScrolling:_scrollView];
1148+
}
1149+
return;
1150+
}
11231151

11241152
if (!animated) {
1153+
[_scrollView setContentOffset:offset animated:NO];
11251154
// When not animated, the expected workflow in ``scrollViewDidEndScrollingAnimation`` after scrolling is not going
11261155
// to get triggered. We will need to manually execute here.
11271156
[self _handleFinishedScrolling:_scrollView];
1157+
return;
1158+
}
1159+
1160+
// Dispatch begin before starting the animation: UIKit can finish it synchronously, and the end event must never
1161+
// precede its begin event.
1162+
if (!_isProgrammaticScrollAnimating && _eventEmitter) {
1163+
static_cast<const ScrollViewEventEmitter &>(*_eventEmitter).onMomentumScrollBegin([self _scrollViewMetrics]);
1164+
}
1165+
_isProgrammaticScrollAnimating = YES;
1166+
NSUInteger generation = ++_programmaticScrollGeneration;
1167+
1168+
[_scrollView setContentOffset:offset animated:YES];
1169+
1170+
if (_isProgrammaticScrollAnimating &&
1171+
(self.window == nil || CGPointEqualToPoint(_scrollView.contentOffset, offset))) {
1172+
// UIKit applied the offset without animating, so ``scrollViewDidEndScrollingAnimation`` will not be called.
1173+
[self _handleFinishedScrolling:_scrollView];
1174+
return;
1175+
}
1176+
1177+
// An unmatched begin event makes the JS scroll view capture every touch, so guarantee the end event even if UIKit
1178+
// stops the animation without calling ``scrollViewDidEndScrollingAnimation``.
1179+
__weak RCTScrollViewComponentView *weakSelf = self;
1180+
dispatch_after(
1181+
dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kProgrammaticScrollEndFallbackDelay * NSEC_PER_SEC)),
1182+
dispatch_get_main_queue(),
1183+
^{
1184+
[weakSelf _finishProgrammaticScrollIfStalled:generation];
1185+
});
1186+
}
1187+
1188+
- (void)_finishProgrammaticScrollIfStalled:(NSUInteger)generation
1189+
{
1190+
if (_isProgrammaticScrollAnimating && generation == _programmaticScrollGeneration) {
1191+
[self _handleFinishedScrolling:_scrollView];
11281192
}
11291193
}
11301194

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlags.kt‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<c129d87ae4a503699e961bbefd90ff47>>
7+
* @generated SignedSource<<99c0c14cf6e3eca24e7bf1c7eaf64d07>>
88
*/
99

1010
/**
@@ -402,6 +402,12 @@ public object ReactNativeFeatureFlags {
402402
@JvmStatic
403403
public fun fixMappingOfEventPrioritiesBetweenFabricAndReact(): Boolean = accessor.fixMappingOfEventPrioritiesBetweenFabricAndReact()
404404

405+
/**
406+
* Dispatches `onMomentumScrollBegin` and `onMomentumScrollEnd` as an ordered pair for animated programmatic scrolls on iOS, and neither for non-animated programmatic scrolls.
407+
*/
408+
@JvmStatic
409+
public fun fixProgrammaticScrollMomentumEventsIOS(): Boolean = accessor.fixProgrammaticScrollMomentumEventsIOS()
410+
405411
/**
406412
* Grows lines of text with `lineHeight` to fit taller inline views instead of letting them overflow, keeps lines after an inline view at `lineHeight` on Android, and aligns inline views with the text baseline on iOS.
407413
*/

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxAccessor.kt‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<bb70e27d2c7bd68046613d2b4dda836c>>
7+
* @generated SignedSource<<cee48ed67cacf26a8625a146d6bd4fbd>>
88
*/
99

1010
/**
@@ -82,6 +82,7 @@ internal class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAcces
8282
private var enableVirtualViewContainerStateExperimentalCache: Boolean? = null
8383
private var fixBorderlessRippleAndroidCache: Boolean? = null
8484
private var fixMappingOfEventPrioritiesBetweenFabricAndReactCache: Boolean? = null
85+
private var fixProgrammaticScrollMomentumEventsIOSCache: Boolean? = null
8586
private var fixTextInlineViewLineHeightCache: Boolean? = null
8687
private var fixTextLayoutOnPixelDensityChangeCache: Boolean? = null
8788
private var fixYogaFlexBasisFitContentInMainAxisCache: Boolean? = null
@@ -678,6 +679,15 @@ internal class ReactNativeFeatureFlagsCxxAccessor : ReactNativeFeatureFlagsAcces
678679
return cached
679680
}
680681

682+
override fun fixProgrammaticScrollMomentumEventsIOS(): Boolean {
683+
var cached = fixProgrammaticScrollMomentumEventsIOSCache
684+
if (cached == null) {
685+
cached = ReactNativeFeatureFlagsCxxInterop.fixProgrammaticScrollMomentumEventsIOS()
686+
fixProgrammaticScrollMomentumEventsIOSCache = cached
687+
}
688+
return cached
689+
}
690+
681691
override fun fixTextInlineViewLineHeight(): Boolean {
682692
var cached = fixTextInlineViewLineHeightCache
683693
if (cached == null) {

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsCxxInterop.kt‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<c283b77ee5b5929aa51edef8acaa1dd2>>
7+
* @generated SignedSource<<ef7526b6b18e20db71fe54d43218ab69>>
88
*/
99

1010
/**
@@ -152,6 +152,8 @@ public object ReactNativeFeatureFlagsCxxInterop {
152152

153153
@DoNotStrip @JvmStatic public external fun fixMappingOfEventPrioritiesBetweenFabricAndReact(): Boolean
154154

155+
@DoNotStrip @JvmStatic public external fun fixProgrammaticScrollMomentumEventsIOS(): Boolean
156+
155157
@DoNotStrip @JvmStatic public external fun fixTextInlineViewLineHeight(): Boolean
156158

157159
@DoNotStrip @JvmStatic public external fun fixTextLayoutOnPixelDensityChange(): Boolean

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsDefaults.kt‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<a6e573f156e7cde50177a92173623b32>>
7+
* @generated SignedSource<<26569f7cbc370133de7e8948ee2d4151>>
88
*/
99

1010
/**
@@ -147,6 +147,8 @@ public open class ReactNativeFeatureFlagsDefaults : ReactNativeFeatureFlagsProvi
147147

148148
override fun fixMappingOfEventPrioritiesBetweenFabricAndReact(): Boolean = false
149149

150+
override fun fixProgrammaticScrollMomentumEventsIOS(): Boolean = false
151+
150152
override fun fixTextInlineViewLineHeight(): Boolean = false
151153

152154
override fun fixTextLayoutOnPixelDensityChange(): Boolean = false

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsLocalAccessor.kt‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<4d0de8e6c65d709e98e638afb976a665>>
7+
* @generated SignedSource<<f20f5ce06d72bb7256dbc280dbe8b76f>>
88
*/
99

1010
/**
@@ -86,6 +86,7 @@ internal class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcc
8686
private var enableVirtualViewContainerStateExperimentalCache: Boolean? = null
8787
private var fixBorderlessRippleAndroidCache: Boolean? = null
8888
private var fixMappingOfEventPrioritiesBetweenFabricAndReactCache: Boolean? = null
89+
private var fixProgrammaticScrollMomentumEventsIOSCache: Boolean? = null
8990
private var fixTextInlineViewLineHeightCache: Boolean? = null
9091
private var fixTextLayoutOnPixelDensityChangeCache: Boolean? = null
9192
private var fixYogaFlexBasisFitContentInMainAxisCache: Boolean? = null
@@ -744,6 +745,16 @@ internal class ReactNativeFeatureFlagsLocalAccessor : ReactNativeFeatureFlagsAcc
744745
return cached
745746
}
746747

748+
override fun fixProgrammaticScrollMomentumEventsIOS(): Boolean {
749+
var cached = fixProgrammaticScrollMomentumEventsIOSCache
750+
if (cached == null) {
751+
cached = currentProvider.fixProgrammaticScrollMomentumEventsIOS()
752+
accessedFeatureFlags.add("fixProgrammaticScrollMomentumEventsIOS")
753+
fixProgrammaticScrollMomentumEventsIOSCache = cached
754+
}
755+
return cached
756+
}
757+
747758
override fun fixTextInlineViewLineHeight(): Boolean {
748759
var cached = fixTextInlineViewLineHeightCache
749760
if (cached == null) {

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsOverrides_RNOSS_Canary_Android.kt‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<0aa6021a35e690e00686c95c387017f6>>
7+
* @generated SignedSource<<f516272583021236cf5a1594b80104b2>>
88
*/
99

1010
/**
@@ -33,6 +33,8 @@ public open class ReactNativeFeatureFlagsOverrides_RNOSS_Canary_Android : ReactN
3333

3434
override fun fixBorderlessRippleAndroid(): Boolean = true
3535

36+
override fun fixProgrammaticScrollMomentumEventsIOS(): Boolean = true
37+
3638
override fun fixTextInlineViewLineHeight(): Boolean = true
3739

3840
override fun optimizedAnimatedPropUpdates(): Boolean = true

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/internal/featureflags/ReactNativeFeatureFlagsProvider.kt‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<337c8d0d14616dccbf881c443ffb0519>>
7+
* @generated SignedSource<<8e0059eadd2b19c49b64bb57121ba723>>
88
*/
99

1010
/**
@@ -147,6 +147,8 @@ public interface ReactNativeFeatureFlagsProvider {
147147

148148
@DoNotStrip public fun fixMappingOfEventPrioritiesBetweenFabricAndReact(): Boolean
149149

150+
@DoNotStrip public fun fixProgrammaticScrollMomentumEventsIOS(): Boolean
151+
150152
@DoNotStrip public fun fixTextInlineViewLineHeight(): Boolean
151153

152154
@DoNotStrip public fun fixTextLayoutOnPixelDensityChange(): Boolean

‎packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.cpp‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<9ef15f2ab900b3afa8d01fcdbf9de178>>
7+
* @generated SignedSource<<53efcbcc0b7fb4f8da094449e96e1584>>
88
*/
99

1010
/**
@@ -411,6 +411,12 @@ class ReactNativeFeatureFlagsJavaProvider
411411
return method(javaProvider_);
412412
}
413413

414+
bool fixProgrammaticScrollMomentumEventsIOS() override {
415+
static const auto method =
416+
getReactNativeFeatureFlagsProviderJavaClass()->getMethod<jboolean()>("fixProgrammaticScrollMomentumEventsIOS");
417+
return method(javaProvider_);
418+
}
419+
414420
bool fixTextInlineViewLineHeight() override {
415421
static const auto method =
416422
getReactNativeFeatureFlagsProviderJavaClass()->getMethod<jboolean()>("fixTextInlineViewLineHeight");
@@ -947,6 +953,11 @@ bool JReactNativeFeatureFlagsCxxInterop::fixMappingOfEventPrioritiesBetweenFabri
947953
return ReactNativeFeatureFlags::fixMappingOfEventPrioritiesBetweenFabricAndReact();
948954
}
949955

956+
bool JReactNativeFeatureFlagsCxxInterop::fixProgrammaticScrollMomentumEventsIOS(
957+
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop> /*unused*/) {
958+
return ReactNativeFeatureFlags::fixProgrammaticScrollMomentumEventsIOS();
959+
}
960+
950961
bool JReactNativeFeatureFlagsCxxInterop::fixTextInlineViewLineHeight(
951962
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop> /*unused*/) {
952963
return ReactNativeFeatureFlags::fixTextInlineViewLineHeight();
@@ -1349,6 +1360,9 @@ void JReactNativeFeatureFlagsCxxInterop::registerNatives() {
13491360
makeNativeMethod(
13501361
"fixMappingOfEventPrioritiesBetweenFabricAndReact",
13511362
JReactNativeFeatureFlagsCxxInterop::fixMappingOfEventPrioritiesBetweenFabricAndReact),
1363+
makeNativeMethod(
1364+
"fixProgrammaticScrollMomentumEventsIOS",
1365+
JReactNativeFeatureFlagsCxxInterop::fixProgrammaticScrollMomentumEventsIOS),
13521366
makeNativeMethod(
13531367
"fixTextInlineViewLineHeight",
13541368
JReactNativeFeatureFlagsCxxInterop::fixTextInlineViewLineHeight),

‎packages/react-native/ReactAndroid/src/main/jni/react/featureflags/JReactNativeFeatureFlagsCxxInterop.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<c7e5acf7c692554ccb953fc404797ba4>>
7+
* @generated SignedSource<<ec2699ae9f8ee37d8776217d3d974dac>>
88
*/
99

1010
/**
@@ -216,6 +216,9 @@ class JReactNativeFeatureFlagsCxxInterop
216216
static bool fixMappingOfEventPrioritiesBetweenFabricAndReact(
217217
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop>);
218218

219+
static bool fixProgrammaticScrollMomentumEventsIOS(
220+
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop>);
221+
219222
static bool fixTextInlineViewLineHeight(
220223
facebook::jni::alias_ref<JReactNativeFeatureFlagsCxxInterop>);
221224

0 commit comments

Comments
 (0)