From 1163599989aa6d641f127f3100a01c45f04d2edc Mon Sep 17 00:00:00 2001 From: Joshua Gross Date: Mon, 8 Mar 2021 15:25:50 -0800 Subject: [PATCH] Horizontal/ScrollView: rename reactScrollTo to scrollTo Summary: We can override the `scrollTo` method and it's likely/possible that Android internals are calling scrollTo directly. So, we can capture more cases where the scroll position is changing and needs to be updated in Fabric State. Unfortunately we still cannot override smoothScrollTo because it is marked as final. For now we just keep the custom `reactSmoothScrollTo` method and hope that we can catch more cases with `scrollTo`. Changelog: [Internal] Reviewed By: sammy-SC, mdvacca Differential Revision: D26887028 fbshipit-source-id: e2678f1a20640d598abbec9671d6102635f65bb2 --- .../ReactHorizontalScrollContainerView.java | 2 +- .../scroll/ReactHorizontalScrollView.java | 18 +++++++++++------- .../ReactHorizontalScrollViewManager.java | 8 ++++---- .../react/views/scroll/ReactScrollView.java | 12 ++++++++---- .../views/scroll/ReactScrollViewManager.java | 8 ++++---- 5 files changed, 28 insertions(+), 20 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollContainerView.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollContainerView.java index 178007942d0..831e4555a59 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollContainerView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollContainerView.java @@ -65,7 +65,7 @@ public class ReactHorizontalScrollContainerView extends ReactViewGroup { ReactHorizontalScrollView parent = (ReactHorizontalScrollView) getParent(); // Fix the ScrollX position when using RTL language int offsetX = parent.getScrollX() + getWidth() - mCurrentWidth - parent.getWidth(); - parent.reactScrollTo(offsetX, parent.getScrollY()); + parent.scrollTo(offsetX, parent.getScrollY()); } } mCurrentWidth = getWidth(); 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 c5f672479c2..666ae492e28 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 @@ -291,7 +291,7 @@ public class ReactHorizontalScrollView extends HorizontalScrollView pendingContentOffsetX != UNSET_CONTENT_OFFSET ? pendingContentOffsetX : getScrollX(); int scrollToY = pendingContentOffsetY != UNSET_CONTENT_OFFSET ? pendingContentOffsetY : getScrollY(); - reactScrollTo(scrollToX, scrollToY); + scrollTo(scrollToX, scrollToY); ReactScrollViewHelper.emitLayoutEvent(this); } @@ -1070,17 +1070,21 @@ public class ReactHorizontalScrollView extends HorizontalScrollView } /** - * Calls `reactScrollTo` and updates state. + * Calls `super.scrollTo` and updates state. * - *

`reactScrollTo` changes `contentOffset` and we need to keep `contentOffset` in sync between - * scroll view and state. Calling raw `reactScrollTo` doesn't update state. + *

`super.scrollTo` changes `contentOffset` and we need to keep `contentOffset` in sync between + * scroll view and state. + * + *

Note that while we can override scrollTo, we *cannot* override `smoothScrollTo` because it + * is final. See `reactSmoothScrollTo`. */ - public void reactScrollTo(int x, int y) { + @Override + public void scrollTo(int x, int y) { if (DEBUG_MODE) { - FLog.i(TAG, "reactScrollTo[%d] x %d y %d", getId(), x, y); + FLog.i(TAG, "scrollTo[%d] x %d y %d", getId(), x, y); } - scrollTo(x, y); + super.scrollTo(x, y); updateStateOnScroll(x, y); setPendingContentOffsets(x, y); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollViewManager.java index 9641fc11be9..38f025fbb60 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollViewManager.java @@ -193,7 +193,7 @@ public class ReactHorizontalScrollViewManager extends ViewGroupManager`reactScrollTo` changes `contentOffset` and we need to keep `contentOffset` in sync between * scroll view and state. Calling raw `reactScrollTo` doesn't update state. + * + *

Note that while we can override scrollTo, we *cannot* override `smoothScrollTo` because it + * is final. See `reactSmoothScrollTo`. */ - public void reactScrollTo(int x, int y) { - scrollTo(x, y); + @Override + public void scrollTo(int x, int y) { + super.scrollTo(x, y); updateStateOnScroll(x, y); setPendingContentOffsets(x, y); } @@ -926,7 +930,7 @@ public class ReactScrollView extends ScrollView int currentScrollY = getScrollY(); int maxScrollY = getMaxScrollY(); if (currentScrollY > maxScrollY) { - reactScrollTo(getScrollX(), maxScrollY); + scrollTo(getScrollX(), maxScrollY); } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java index c9733b09697..f8add8ad32a 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewManager.java @@ -206,7 +206,7 @@ public class ReactScrollViewManager extends ViewGroupManager if (data.mAnimated) { scrollView.reactSmoothScrollTo(data.mDestX, data.mDestY); } else { - scrollView.reactScrollTo(data.mDestX, data.mDestY); + scrollView.scrollTo(data.mDestX, data.mDestY); } } @@ -285,7 +285,7 @@ public class ReactScrollViewManager extends ViewGroupManager if (data.mAnimated) { scrollView.reactSmoothScrollTo(scrollView.getScrollX(), bottom); } else { - scrollView.reactScrollTo(scrollView.getScrollX(), bottom); + scrollView.scrollTo(scrollView.getScrollX(), bottom); } } @@ -310,9 +310,9 @@ public class ReactScrollViewManager extends ViewGroupManager if (value != null) { double x = value.hasKey("x") ? value.getDouble("x") : 0; double y = value.hasKey("y") ? value.getDouble("y") : 0; - view.reactScrollTo((int) PixelUtil.toPixelFromDIP(x), (int) PixelUtil.toPixelFromDIP(y)); + view.scrollTo((int) PixelUtil.toPixelFromDIP(x), (int) PixelUtil.toPixelFromDIP(y)); } else { - view.reactScrollTo(0, 0); + view.scrollTo(0, 0); } }