Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 25.2k
Fix to make taps on views outside parent bounds work on Android#29039
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Uh oh!
There was an error while loading. Please reload this page.
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b09e768
Fix to make taps on views outside parent bounds work on Android
hsource d24abc0
Reused mTempPoint per original implementation
hsource 749df7e
Added fully-outside box to the overflow test case
hsource 1b63402
Made bounds checks also run on `overflow: scroll` Views
hsource 3548de7
Fixed behavior for ReactCompoundViewGroups for react-native-svg
hsource 0f2b2f8
Added better comments to example and Java code
hsource ee51bd4
Updated ExampleBox to use setState
hsource 4de4ad6
Fixed flow error by extracting component from JSX
hsource f988b95
Fixed ExampleBox in test case, added more new example
hsource bf06fc8
Merge branch 'main' into harry-android-overflow
JoshuaGross File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26 ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactOverflowView.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * Copyright (c) Facebook, Inc. and its affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
| package com.facebook.react.uimanager; | ||
| import android.graphics.Rect; | ||
| import android.view.View; | ||
| import androidx.annotation.Nullable; | ||
| /** | ||
| * Interface that should be implemented by {@link View} subclasses that support {@code | ||
| * overflow} style. This allows the overflow information to be used by {@link TouchTargetHelper} | ||
| * to determine if a View is touchable. | ||
| */ | ||
| public interface ReactOverflowView { | ||
| /** | ||
| * Gets the overflow state of a view. If set, this should be one of {@link ViewProps#HIDDEN}, | ||
| * {@link ViewProps#VISIBLE} or {@link ViewProps#SCROLL}. | ||
| */ | ||
| @Nullable String getOverflow(); | ||
| } | ||
174 changes: 109 additions & 65 deletions
174 ReactAndroid/src/main/java/com/facebook/react/uimanager/TouchTargetHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -18,6 +18,8 @@ | ||
| import com.facebook.react.bridge.UiThreadUtil; | ||
| import com.facebook.react.touch.ReactHitSlopView; | ||
| import java.util.EnumSet; | ||
| /** | ||
| * Class responsible for identifying which react view should handle a given {@link MotionEvent}. It | ||
| * uses the event coordinates to traverse the view hierarchy and return a suitable view. | ||
| @@ -80,7 +82,7 @@ public static int findTargetTagAndCoordinatesForTouch( | ||
| // Store eventCoords in array so that they are modified to be relative to the targetView found. | ||
| viewCoords[0] = eventX; | ||
| viewCoords[1] = eventY; | ||
| View nativeTargetView = findTouchTargetView(viewCoords, viewGroup); | ||
| View nativeTargetView = findTouchTargetViewWithPointerEvents(viewCoords, viewGroup); | ||
| if (nativeTargetView != null) { | ||
| View reactTargetView = findClosestReactAncestor(nativeTargetView); | ||
| if (reactTargetView != null) { | ||
| @@ -100,6 +102,20 @@ private static View findClosestReactAncestor(View view) { | ||
| return view; | ||
| } | ||
| /** | ||
| * Types of allowed return values from {@link #findTouchTargetView}. | ||
| */ | ||
| private enum TouchTargetReturnType { | ||
| /** | ||
| * Allow returning the view passed in through the parameters. | ||
| */ | ||
| SELF, | ||
| /** | ||
| * Allow returning children of the view passed in through parameters. | ||
| */ | ||
| CHILD, | ||
| } | ||
| /** | ||
| * Returns the touch target View that is either viewGroup or one if its descendants. This is a | ||
| * recursive DFS since view the entire tree must be parsed until the target is found. If the | ||
| @@ -111,43 +127,91 @@ private static View findClosestReactAncestor(View view) { | ||
| * be relative to the current viewGroup. When the method returns, it will contain the eventCoords | ||
| * relative to the targetView found. | ||
| */ | ||
| private static View findTouchTargetView(float[] eventCoords, ViewGroup viewGroup) { | ||
| int childrenCount = viewGroup.getChildCount(); | ||
| // Consider z-index when determining the touch target. | ||
| ReactZIndexedViewGroup zIndexedViewGroup = | ||
| private static View findTouchTargetView( | ||
| float[] eventCoords, View view, EnumSet<TouchTargetReturnType> allowReturnTouchTargetTypes) { | ||
| // We prefer returning a child, so we check for a child that can handle the touch first | ||
| if (allowReturnTouchTargetTypes.contains(TouchTargetReturnType.CHILD) | ||
hsource marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| && view instanceof ViewGroup) { | ||
| ViewGroup viewGroup = (ViewGroup) view; | ||
| int childrenCount = viewGroup.getChildCount(); | ||
| // Consider z-index when determining the touch target. | ||
| ReactZIndexedViewGroup zIndexedViewGroup = | ||
| viewGroup instanceof ReactZIndexedViewGroup ? (ReactZIndexedViewGroup) viewGroup : null; | ||
| for (int i = childrenCount - 1; i >= 0; i--) { | ||
| int childIndex = | ||
| for (int i = childrenCount - 1; i >= 0; i--) { | ||
| int childIndex = | ||
| zIndexedViewGroup != null ? zIndexedViewGroup.getZIndexMappedChildIndex(i) : i; | ||
| View child = viewGroup.getChildAt(childIndex); | ||
| PointF childPoint = mTempPoint; | ||
| if (isTransformedTouchPointInView( | ||
| eventCoords[0], eventCoords[1], viewGroup, child, childPoint)) { | ||
| // If it is contained within the child View, the childPoint value will contain the view | ||
| // coordinates relative to the child | ||
| View child = viewGroup.getChildAt(childIndex); | ||
| PointF childPoint = mTempPoint; | ||
| getChildPoint(eventCoords[0], eventCoords[1], viewGroup, child, childPoint); | ||
| // The childPoint value will contain the view coordinates relative to the child. | ||
| // We need to store the existing X,Y for the viewGroup away as it is possible this child | ||
| // will not actually be the target and so we restore them if not | ||
| float restoreX = eventCoords[0]; | ||
| float restoreY = eventCoords[1]; | ||
| eventCoords[0] = childPoint.x; | ||
| eventCoords[1] = childPoint.y; | ||
| View targetView = findTouchTargetViewWithPointerEvents(eventCoords, child); | ||
| if (targetView != null) { | ||
| return targetView; | ||
| // We don't allow touches on views that are outside the bounds of an `overflow: hidden` | ||
| // View | ||
| boolean inOverflowBounds = true; | ||
| if (viewGroup instanceof ReactOverflowView) { | ||
| @Nullable String overflow = ((ReactOverflowView) viewGroup).getOverflow(); | ||
| if ((ViewProps.HIDDEN.equals(overflow) || ViewProps.SCROLL.equals(overflow)) | ||
| && !isTouchPointInView(restoreX, restoreY, view)) { | ||
| inOverflowBounds = false; | ||
| } | ||
| } | ||
| if (inOverflowBounds) { | ||
| return targetView; | ||
| } | ||
| } | ||
| eventCoords[0] = restoreX; | ||
| eventCoords[1] = restoreY; | ||
| } | ||
| } | ||
| return viewGroup; | ||
| // Check if parent can handle the touch after the children | ||
| if (allowReturnTouchTargetTypes.contains(TouchTargetReturnType.SELF) | ||
| && isTouchPointInView(eventCoords[0], eventCoords[1], view)) { | ||
| return view; | ||
| } | ||
| return null; | ||
| } | ||
| /** | ||
| * Checks whether a touch at {@code x} and {@code y} are within the bounds of the View. Both | ||
| * {@code x} and {@code y} must be relative to the top-left corner of the view. | ||
| */ | ||
| private static boolean isTouchPointInView(float x, float y, View view) { | ||
| if (view instanceof ReactHitSlopView && ((ReactHitSlopView) view).getHitSlopRect() != null) { | ||
| Rect hitSlopRect = ((ReactHitSlopView) view).getHitSlopRect(); | ||
| if ((x >= -hitSlopRect.left | ||
| && x < (view.getRight() - view.getLeft()) + hitSlopRect.right) | ||
| && (y >= -hitSlopRect.top | ||
| && y < (view.getBottom() - view.getTop()) + hitSlopRect.bottom)) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } else { | ||
| if ((x >= 0 && x < (view.getRight() - view.getLeft())) | ||
| && (y >= 0 && y < (view.getBottom() - view.getTop()))) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
| /** | ||
| * Returns whether the touch point is within the child View It is transform aware and will invert | ||
| * Returns the coordinates of a touch in the child View. It is transform aware and will invert | ||
| * the transform Matrix to find the true local points This code is taken from {@link | ||
| * ViewGroup#isTransformedTouchPointInView()} | ||
| */ | ||
| private static boolean isTransformedTouchPointInView( | ||
| private static void getChildPoint( | ||
| float x, float y, ViewGroup parent, View child, PointF outLocalPoint) { | ||
| float localX = x + parent.getScrollX() - child.getLeft(); | ||
| float localY = y + parent.getScrollY() - child.getTop(); | ||
| @@ -162,26 +226,7 @@ private static boolean isTransformedTouchPointInView( | ||
| localX = localXY[0]; | ||
| localY = localXY[1]; | ||
| } | ||
| if (child instanceof ReactHitSlopView && ((ReactHitSlopView) child).getHitSlopRect() != null) { | ||
| Rect hitSlopRect = ((ReactHitSlopView) child).getHitSlopRect(); | ||
| if ((localX >= -hitSlopRect.left | ||
| && localX < (child.getRight() - child.getLeft()) + hitSlopRect.right) | ||
| && (localY >= -hitSlopRect.top | ||
| && localY < (child.getBottom() - child.getTop()) + hitSlopRect.bottom)) { | ||
| outLocalPoint.set(localX, localY); | ||
| return true; | ||
| } | ||
| return false; | ||
| } else { | ||
| if ((localX >= 0 && localX < (child.getRight() - child.getLeft())) | ||
| && (localY >= 0 && localY < (child.getBottom() - child.getTop()))) { | ||
| outLocalPoint.set(localX, localY); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| outLocalPoint.set(localX, localY); | ||
| } | ||
| /** | ||
| @@ -211,45 +256,44 @@ private static boolean isTransformedTouchPointInView( | ||
| return null; | ||
| } else if (pointerEvents == PointerEvents.BOX_ONLY) { | ||
| // This view is the target, its children don't matter | ||
| return view; | ||
| // This view may be the target, its children don't matter | ||
| return findTouchTargetView(eventCoords, view, EnumSet.of(TouchTargetReturnType.SELF)); | ||
| } else if (pointerEvents == PointerEvents.BOX_NONE) { | ||
| // This view can't be the target, but its children might. | ||
| if (view instanceof ViewGroup) { | ||
| View targetView = findTouchTargetView(eventCoords, (ViewGroup) view); | ||
| if (targetView != view) { | ||
| return targetView; | ||
| } | ||
| View targetView = | ||
| findTouchTargetView(eventCoords, view, EnumSet.of(TouchTargetReturnType.CHILD)); | ||
| if (targetView != null) { | ||
| return targetView; | ||
| } | ||
| // PointerEvents.BOX_NONE means that this react element cannot receive pointer events. | ||
| // However, there might be virtual children that can receive pointer events, in which case | ||
| // we still want to return this View and dispatch a pointer event to the virtual element. | ||
| // Note that this currently only applies to Nodes/FlatViewGroup as it's the only class that | ||
| // is both a ViewGroup and ReactCompoundView (ReactTextView is a ReactCompoundView but not a | ||
| // ViewGroup). | ||
| if (view instanceof ReactCompoundView) { | ||
| int reactTag = | ||
| ((ReactCompoundView) view).reactTagForTouch(eventCoords[0], eventCoords[1]); | ||
| if (reactTag != view.getId()) { | ||
| // make sure we exclude the View itself because of the PointerEvents.BOX_NONE | ||
| return view; | ||
| } | ||
| // PointerEvents.BOX_NONE means that this react element cannot receive pointer events. | ||
| // However, there might be virtual children that can receive pointer events, in which case | ||
| // we still want to return this View and dispatch a pointer event to the virtual element. | ||
| // Note that this currently only applies to Nodes/FlatViewGroup as it's the only class that | ||
| // is both a ViewGroup and ReactCompoundView (ReactTextView is a ReactCompoundView but not a | ||
| // ViewGroup). | ||
| if (view instanceof ReactCompoundView | ||
| && isTouchPointInView(eventCoords[0], eventCoords[1], view)) { | ||
| int reactTag = | ||
| ((ReactCompoundView) view).reactTagForTouch(eventCoords[0], eventCoords[1]); | ||
| if (reactTag != view.getId()) { | ||
| // make sure we exclude the View itself because of the PointerEvents.BOX_NONE | ||
| return view; | ||
| } | ||
| } | ||
| return null; | ||
| } else if (pointerEvents == PointerEvents.AUTO) { | ||
| // Either this view or one of its children is the target | ||
| if (view instanceof ReactCompoundViewGroup) { | ||
| if (((ReactCompoundViewGroup) view).interceptsTouchEvent(eventCoords[0], eventCoords[1])) { | ||
| return view; | ||
| } | ||
| if (view instanceof ReactCompoundViewGroup | ||
| && isTouchPointInView(eventCoords[0], eventCoords[1], view) | ||
| && ((ReactCompoundViewGroup) view).interceptsTouchEvent(eventCoords[0], eventCoords[1])) { | ||
| return view; | ||
| } | ||
| if (view instanceof ViewGroup) { | ||
| return findTouchTargetView(eventCoords, (ViewGroup) view); | ||
| } | ||
| return view; | ||
| return findTouchTargetView(eventCoords, view, | ||
| EnumSet.of(TouchTargetReturnType.SELF, TouchTargetReturnType.CHILD)); | ||
| } else { | ||
| throw new JSApplicationIllegalArgumentException( | ||
10 changes: 9 additions & 1 deletion
10 ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 8 additions & 1 deletion
9 ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 4 additions & 1 deletion
5 ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.