Skip to content

Commit

Permalink
Merge scroll changed workflow into scroll helper class
Browse files Browse the repository at this point in the history
Summary:
This diff address [comment](https://www.internalfb.com/diff/D32372180 (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
  • Loading branch information
ryancat authored and facebook-github-bot committed Nov 30, 2021
1 parent f70018b commit 8ab2cbb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
Expand Up @@ -432,13 +432,7 @@ protected void onScrollChanged(int x, int y, int oldX, int oldY) {
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());
Expand Down Expand Up @@ -521,7 +515,7 @@ public boolean onTouchEvent(MotionEvent ev) {
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();
Expand Down Expand Up @@ -769,7 +763,7 @@ public void run() {
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
Expand Down Expand Up @@ -1178,7 +1172,7 @@ public void scrollTo(int x, int y) {
// 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);
}

Expand Down
Expand Up @@ -312,13 +312,7 @@ protected void onScrollChanged(int x, int y, int oldX, int oldY) {
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());
Expand Down Expand Up @@ -359,7 +353,7 @@ public boolean onTouchEvent(MotionEvent ev) {
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();
Expand Down Expand Up @@ -558,7 +552,7 @@ public void run() {
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
Expand Down Expand Up @@ -953,7 +947,7 @@ public void scrollTo(int x, int y) {
// 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);
}

Expand Down
Expand Up @@ -342,7 +342,7 @@ void smoothScrollTo(final T scrollView, final int x, final int y) {
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. */
Expand Down Expand Up @@ -386,8 +386,8 @@ Point getPostAnimationScroll(final T scrollView, final boolean isPositiveVelocit
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());
}

/**
Expand All @@ -397,11 +397,11 @@ boolean updateStateOnScroll(final T scrollView) {
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);
Expand Down Expand Up @@ -448,7 +448,7 @@ void forceUpdateState(final T scrollView) {
if (DEBUG_MODE) {
FLog.i(
TAG,
"updateStateOnScroll[%d] scrollX %d scrollY %d fabricScrollX",
"updateFabricScrollState[%d] scrollX %d scrollY %d fabricScrollX",
scrollView.getId(),
scrollX,
scrollY,
Expand All @@ -471,6 +471,20 @@ public WritableMap getStateUpdate() {
});
}

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
Expand All @@ -491,7 +505,7 @@ public void onAnimationStart(Animator animator) {
@Override
public void onAnimationEnd(Animator animator) {
scrollView.getReactScrollViewScrollState().setIsFinished(true);
ReactScrollViewHelper.updateStateOnScroll(scrollView);
updateFabricScrollState(scrollView);
}

@Override
Expand Down

0 comments on commit 8ab2cbb

Please sign in to comment.