From 8ab2cbb78f689226b3fdb426c07b434ab02d359e Mon Sep 17 00:00:00 2001 From: Xin Chen Date: Tue, 30 Nov 2021 13:13:05 -0800 Subject: [PATCH] Merge scroll changed workflow into scroll helper class Summary: This diff address [comment](https://www.internalfb.com/diff/D32372180 (https://github.com/facebook/react-native/commit/073195991bfdf4c96490c65f9c0cf00d09356188)?dst_version_fbid=444635297227339&transaction_fbid=636262217544650) to merge workflow that notify Fabric state changes when scroll changed. Changelog: [Internal] Reviewed By: javache Differential Revision: D32500423 fbshipit-source-id: 8701f7ac885bf755e026b70554cb4a2ebb1af527 --- .../scroll/ReactHorizontalScrollView.java | 14 +++------- .../react/views/scroll/ReactScrollView.java | 14 +++------- .../views/scroll/ReactScrollViewHelper.java | 28 ++++++++++++++----- 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java index 51066cc02b9..5d5d67f3ff4 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java @@ -432,13 +432,7 @@ public class ReactHorizontalScrollView extends HorizontalScrollView updateClippingRect(); } - // Race an UpdateState with every onScroll. This makes it more likely that, in Fabric, - // when JS processes the scroll event, the C++ ShadowNode representation will have a - // "more correct" scroll position. It will frequently be /incorrect/ but this decreases - // the error as much as possible. - ReactScrollViewHelper.updateStateOnScroll(this); - - ReactScrollViewHelper.emitScrollEvent( + ReactScrollViewHelper.updateStateOnScrollChanged( this, mOnScrollDispatchHelper.getXFlingVelocity(), mOnScrollDispatchHelper.getYFlingVelocity()); @@ -521,7 +515,7 @@ public class ReactHorizontalScrollView extends HorizontalScrollView mVelocityHelper.calculateVelocity(ev); int action = ev.getAction() & MotionEvent.ACTION_MASK; if (action == MotionEvent.ACTION_UP && mDragging) { - ReactScrollViewHelper.updateStateOnScroll(this); + ReactScrollViewHelper.updateFabricScrollState(this); float velocityX = mVelocityHelper.getXVelocity(); float velocityY = mVelocityHelper.getYVelocity(); @@ -769,7 +763,7 @@ public class ReactHorizontalScrollView extends HorizontalScrollView mRunning = true; } else { // There has not been a scroll update since the last time this Runnable executed. - ReactScrollViewHelper.updateStateOnScroll(ReactHorizontalScrollView.this); + ReactScrollViewHelper.updateFabricScrollState(ReactHorizontalScrollView.this); // We keep checking for updates until the ScrollView has "stabilized" and hasn't // scrolled for N consecutive frames. This number is arbitrary: big enough to catch @@ -1178,7 +1172,7 @@ public class ReactHorizontalScrollView extends HorizontalScrollView // to the last item in the list, but that item cannot be move to the start position of the view. final int actualX = getScrollX(); final int actualY = getScrollY(); - ReactScrollViewHelper.updateStateOnScroll(this, actualX, actualY); + ReactScrollViewHelper.updateFabricScrollState(this, actualX, actualY); setPendingContentOffsets(actualX, actualY); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java index cf3de29afa4..044103c7cdb 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java @@ -312,13 +312,7 @@ public class ReactScrollView extends ScrollView updateClippingRect(); } - // Race an UpdateState with every onScroll. This makes it more likely that, in Fabric, - // when JS processes the scroll event, the C++ ShadowNode representation will have a - // "more correct" scroll position. It will frequently be /incorrect/ but this decreases - // the error as much as possible. - ReactScrollViewHelper.updateStateOnScroll(this); - - ReactScrollViewHelper.emitScrollEvent( + ReactScrollViewHelper.updateStateOnScrollChanged( this, mOnScrollDispatchHelper.getXFlingVelocity(), mOnScrollDispatchHelper.getYFlingVelocity()); @@ -359,7 +353,7 @@ public class ReactScrollView extends ScrollView mVelocityHelper.calculateVelocity(ev); int action = ev.getAction() & MotionEvent.ACTION_MASK; if (action == MotionEvent.ACTION_UP && mDragging) { - ReactScrollViewHelper.updateStateOnScroll(this); + ReactScrollViewHelper.updateFabricScrollState(this); float velocityX = mVelocityHelper.getXVelocity(); float velocityY = mVelocityHelper.getYVelocity(); @@ -558,7 +552,7 @@ public class ReactScrollView extends ScrollView mRunning = true; } else { // There has not been a scroll update since the last time this Runnable executed. - ReactScrollViewHelper.updateStateOnScroll(ReactScrollView.this); + ReactScrollViewHelper.updateFabricScrollState(ReactScrollView.this); // We keep checking for updates until the ScrollView has "stabilized" and hasn't // scrolled for N consecutive frames. This number is arbitrary: big enough to catch @@ -953,7 +947,7 @@ public class ReactScrollView extends ScrollView // to the last item in the list, but that item cannot be move to the start position of the view. final int actualX = getScrollX(); final int actualY = getScrollY(); - ReactScrollViewHelper.updateStateOnScroll(this, actualX, actualY); + ReactScrollViewHelper.updateFabricScrollState(this, actualX, actualY); setPendingContentOffsets(actualX, actualY); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.java index 62f3ff94090..03238555f2b 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.java @@ -342,7 +342,7 @@ public class ReactScrollViewHelper { scrollView.startFlingAnimator(scrollY, y); } - updateStateOnScroll(scrollView, x, y); + updateFabricScrollState(scrollView, x, y); } /** Get current (x, y) position or position after current animation finishes, if any. */ @@ -386,8 +386,8 @@ public class ReactScrollViewHelper { T extends ViewGroup & FabricViewStateManager.HasFabricViewStateManager & HasScrollState & HasFlingAnimator> - boolean updateStateOnScroll(final T scrollView) { - return updateStateOnScroll(scrollView, scrollView.getScrollX(), scrollView.getScrollY()); + boolean updateFabricScrollState(final T scrollView) { + return updateFabricScrollState(scrollView, scrollView.getScrollX(), scrollView.getScrollY()); } /** @@ -397,11 +397,11 @@ public class ReactScrollViewHelper { T extends ViewGroup & FabricViewStateManager.HasFabricViewStateManager & HasScrollState & HasFlingAnimator> - boolean updateStateOnScroll(final T scrollView, final int scrollX, final int scrollY) { + boolean updateFabricScrollState(final T scrollView, final int scrollX, final int scrollY) { if (DEBUG_MODE) { FLog.i( TAG, - "updateStateOnScroll[%d] scrollX %d scrollY %d", + "updateFabricScrollState[%d] scrollX %d scrollY %d", scrollView.getId(), scrollX, scrollY); @@ -448,7 +448,7 @@ public class ReactScrollViewHelper { if (DEBUG_MODE) { FLog.i( TAG, - "updateStateOnScroll[%d] scrollX %d scrollY %d fabricScrollX", + "updateFabricScrollState[%d] scrollX %d scrollY %d fabricScrollX", scrollView.getId(), scrollX, scrollY, @@ -471,6 +471,20 @@ public class ReactScrollViewHelper { }); } + public static < + T extends + ViewGroup & FabricViewStateManager.HasFabricViewStateManager & HasScrollState + & HasFlingAnimator> + void updateStateOnScrollChanged( + final T scrollView, final float xVelocity, final float yVelocity) { + // Race an UpdateState with every onScroll. This makes it more likely that, in Fabric, + // when JS processes the scroll event, the C++ ShadowNode representation will have a + // "more correct" scroll position. It will frequently be /incorrect/ but this decreases + // the error as much as possible. + updateFabricScrollState(scrollView); + emitScrollEvent(scrollView, xVelocity, yVelocity); + } + public static < T extends ViewGroup & FabricViewStateManager.HasFabricViewStateManager & HasScrollState @@ -491,7 +505,7 @@ public class ReactScrollViewHelper { @Override public void onAnimationEnd(Animator animator) { scrollView.getReactScrollViewScrollState().setIsFinished(true); - ReactScrollViewHelper.updateStateOnScroll(scrollView); + updateFabricScrollState(scrollView); } @Override