Skip to content

Commit f2007d3

Browse files
authored
scrollview: add option to tag event while drawing (#128)
1 parent 1c02d8e commit f2007d3

File tree

4 files changed

+35
-23
lines changed

4 files changed

+35
-23
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/shell/MainReactPackage.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ constructor(private val config: MainPackageConfig? = null) :
139139
ReactHorizontalScrollViewManager(),
140140
ReactHorizontalScrollContainerViewManager(),
141141
ReactProgressBarViewManager(),
142-
ReactScrollViewManager(),
142+
// ReactScrollViewManager(),
143143
ReactSwitchManager(),
144144
ReactSafeAreaViewManager(),
145145
SwipeRefreshLayoutManager(),
@@ -172,8 +172,8 @@ constructor(private val config: MainPackageConfig? = null) :
172172
ModuleSpec.viewManagerSpec { ReactProgressBarViewManager() },
173173
ReactSafeAreaViewManager.REACT_CLASS to
174174
ModuleSpec.viewManagerSpec { ReactSafeAreaViewManager() },
175-
ReactScrollViewManager.REACT_CLASS to
176-
ModuleSpec.viewManagerSpec { ReactScrollViewManager() },
175+
// ReactScrollViewManager.REACT_CLASS to
176+
// ModuleSpec.viewManagerSpec { ReactScrollViewManager() },
177177
ReactSwitchManager.REACT_CLASS to ModuleSpec.viewManagerSpec { ReactSwitchManager() },
178178
SwipeRefreshLayoutManager.REACT_CLASS to
179179
ModuleSpec.viewManagerSpec { SwipeRefreshLayoutManager() },

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/Event.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public abstract class Event<T : Event<T>> {
4040
public var viewTag: Int = 0
4141
private set
4242

43+
/** @return whether this event is dispatched during a drawing pass */
44+
public var isDrawing: Boolean = false
45+
4346
/**
4447
* @return the time at which the event happened in the [android.os.SystemClock.uptimeMillis] base.
4548
*/

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -463,24 +463,29 @@ private void scrollToChild(View child) {
463463

464464
@Override
465465
protected void onScrollChanged(int x, int y, int oldX, int oldY) {
466-
Systrace.beginSection(Systrace.TRACE_TAG_REACT, "ReactScrollView.onScrollChanged");
467-
try {
468-
super.onScrollChanged(x, y, oldX, oldY);
466+
onScrollChanged(x, y, oldX, oldY, false);
467+
}
469468

470-
mActivelyScrolling = true;
469+
protected void onScrollChanged(int x, int y, int oldX, int oldY, boolean isDrawing) {
470+
Systrace.beginSection(Systrace.TRACE_TAG_REACT, "ReactScrollView.onScrollChanged");
471+
try {
472+
super.onScrollChanged(x, y, oldX, oldY);
471473

472-
if (mOnScrollDispatchHelper.onScrollChanged(x, y)) {
473-
if (mRemoveClippedSubviews) {
474-
updateClippingRect();
475-
}
476-
ReactScrollViewHelper.updateStateOnScrollChanged(
477-
this,
478-
mOnScrollDispatchHelper.getXFlingVelocity(),
479-
mOnScrollDispatchHelper.getYFlingVelocity());
474+
mActivelyScrolling = true;
475+
476+
if (mOnScrollDispatchHelper.onScrollChanged(x, y)) {
477+
if (mRemoveClippedSubviews) {
478+
updateClippingRect();
479+
}
480+
ReactScrollViewHelper.updateStateOnScrollChanged(
481+
this,
482+
mOnScrollDispatchHelper.getXFlingVelocity(),
483+
mOnScrollDispatchHelper.getYFlingVelocity(),
484+
isDrawing);
485+
}
486+
} finally {
487+
Systrace.endSection(Systrace.TRACE_TAG_REACT);
480488
}
481-
} finally {
482-
Systrace.endSection(Systrace.TRACE_TAG_REACT);
483-
}
484489
}
485490

486491
@Override

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollViewHelper.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ public object ReactScrollViewHelper {
6565

6666
/** Shared by [ReactScrollView] and [ReactHorizontalScrollView]. */
6767
@JvmStatic
68-
public fun <T> emitScrollEvent(scrollView: T, xVelocity: Float, yVelocity: Float) where
68+
public fun <T> emitScrollEvent(scrollView: T, xVelocity: Float, yVelocity: Float, isDrawing: Boolean = false) where
6969
T : HasScrollEventThrottle?,
7070
T : ViewGroup {
71-
emitScrollEvent(scrollView, ScrollEventType.SCROLL, xVelocity, yVelocity)
71+
emitScrollEvent(scrollView, ScrollEventType.SCROLL, xVelocity, yVelocity, isDrawing)
7272
}
7373

7474
@JvmStatic
@@ -111,6 +111,7 @@ public object ReactScrollViewHelper {
111111
scrollEventType: ScrollEventType,
112112
xVelocity: Float,
113113
yVelocity: Float,
114+
isDrawing: Boolean = false
114115
) where T : HasScrollEventThrottle?, T : ViewGroup {
115116
val now = System.currentTimeMillis()
116117
// Throttle the scroll event if scrollEventThrottle is set to be equal or more than 17 ms.
@@ -146,7 +147,9 @@ public object ReactScrollViewHelper {
146147
contentView.width,
147148
contentView.height,
148149
scrollView.width,
149-
scrollView.height))
150+
scrollView.height).also {
151+
it.isDrawing = isDrawing
152+
})
150153
if (scrollEventType == ScrollEventType.SCROLL) {
151154
scrollView.lastScrollDispatchTime = now
152155
}
@@ -368,7 +371,8 @@ public object ReactScrollViewHelper {
368371
public fun <T> updateStateOnScrollChanged(
369372
scrollView: T,
370373
xVelocity: Float,
371-
yVelocity: Float
374+
yVelocity: Float,
375+
isDrawing: Boolean = false
372376
) where
373377
T : HasFlingAnimator?,
374378
T : HasScrollEventThrottle?,
@@ -380,7 +384,7 @@ public object ReactScrollViewHelper {
380384
// "more correct" scroll position. It will frequently be /incorrect/ but this decreases
381385
// the error as much as possible.
382386
updateFabricScrollState(scrollView, scrollView.scrollX, scrollView.scrollY)
383-
emitScrollEvent(scrollView, xVelocity, yVelocity)
387+
emitScrollEvent(scrollView, xVelocity, yVelocity, isDrawing)
384388
}
385389

386390
public fun <T> registerFlingAnimator(scrollView: T) where

0 commit comments

Comments
 (0)