Skip to content

Commit 403cafa

Browse files
authored
Make the detector frame cover transformed children (#4532)
## Description On Android, native views (`Switch`, `TextInput`, native `ScrollView`, sliders) inside a `GestureDetector` stop receiving touches once a transform moves them outside the detector's frame. Gestures attached to the detector keep working, since the orchestrator does its own transform-aware traversal (#4251), but the touch never reaches the native child. The detector's frame is the bounding box of its children's layout frames. Transforms are not layout, so a transformed child can end up outside that frame. Android's `ViewGroup` only dispatches a touch to a child whose bounds contain it, so the touch is rejected at the detector before the child's transform is ever considered. This is a general RN Android limitation for any parent view tighter than its transformed content, but layout-only wrappers get flattened and the detector never is, so `GestureDetector` is where users hit it. iOS is unaffected, since the detector forces a non-zero `overflowInset` there and RN's hit test then searches outside the bounds. The fix extends the frame with each child's transformed frame and recomputes `overflowInset` against the resulting frame, since the default value is computed against the zero-size frame of a `display: contents` node. The untransformed frame stays in the union, so the orchestrator's child-space check from #4251 still covers the vacated area. The frame follows a transform only when layout runs: at mount, on React commits, and when something else re-lays out the animated subtree, such as a measurable child like `Switch` or `TextInput`. A purely Reanimated-driven transform with no such trigger keeps a stale frame until the next layout. Following it every frame costs a Yoga pass per detector, measured at 6x worse frame times. Fixes #4529 ### Performance Performance on a Pixel 9 Pro emulator, release build, 1000 detectors animating at once, frame times from `dumpsys gfxinfo` over 10 s: | card content | build | median frame time | 90th percentile | frames rendered | |---|---|---|---|---| | Text | main | 48 ms | 65 ms | 372 | | Text | this PR | 48 ms | 65 ms | 372 | | Switch | main | 200 ms | 200 ms | 101 | | Switch | this PR | 250 ms | 300 ms | 71 | Text cards never re-layout, so the cost is zero. Switch cards make RN run layout every frame on main already, and this PR adds the frame and inset mutation on top. That is the only measured cost and it only applies to many detectors animating simultaneously with measurable leaves inside. ## Test plan <details> <summary>Tested on the following code:</summary> ```tsx import React, { useState } from 'react'; import { Pressable, ScrollView as RNScrollView, StyleSheet, Switch, Text, View, } from 'react-native'; import { GestureDetector, InterceptingGestureDetector, Switch as GHSwitch, usePanGesture, useTapGesture, VirtualGestureDetector, } from 'react-native-gesture-handler'; import Animated, { useAnimatedStyle, useSharedValue, withTiming, } from 'react-native-reanimated'; // Issue #4529, Android only. The v3 `GestureDetector` host view's frame is the // bounding box of its children's layout frames; transforms are not layout, so a // child moved by `translateX` ends up outside the detector's frame. Android's // `ViewGroup.dispatchTouchEvent` only hands a touch to a child whose bounds // contain it, so a touch over the translated content is rejected at the // detector and never reaches the native views inside it (`Switch`, `TextInput`, // sliders...). Gestures attached to the detector still fire, because the // orchestrator does its own transform-aware traversal (#4251). iOS is not // affected: the detector forces a non-zero overflowInset, so RN's hitTest keeps // looking at subviews outside the detector's bounds. // // The tap gestures need 3 taps, so a single tap never activates them and // cannot cancel the native press - what you see is pure touch delivery. // // Steps (Android): single-tap the blue box in each row. // Row 1: Switch inside GestureDetector - stays OFF. Triple-tap the same box: // "detector taps" goes up, so the detector's own gesture still works. // Row 2: same, but the translated View sits in a plain 100x70 View with // collapsable={false} and no detector - dead too. Without // collapsable={false} RN flattens the wrapper away and it works. // Row 3: RN Pressable inside GestureDetector - works (RN's JS touch target // search passes thanks to the detector's oversized overflowInset). // Row 4: Switch inside VirtualGestureDetector (no host view) - toggles. // Row 5: RNGH Switch inside GestureDetector - toggles (touches come // from NativeViewGestureHandler, not from Android dispatch). // Row 6: Reanimated card (pan to drag, or press "slide" to move it by 160) // with an RN Switch inside, in a GestureDetector. Slide it right, // then tap the Switch: stays OFF. Slide back: toggles. // Row 7: card (moved only by its slide button) holding a native horizontal // ScrollView. Slide it and scroll the strip right away: dead. // Row 8: like row 6, but the card slides over a grey sibling block. Slide // it, then tap the Switch: stays OFF. // Expected: every row reacts to the tap. export default function EmptyExample() { const [detectorSwitch, setDetectorSwitch] = useState(false); const [plainSwitch, setPlainSwitch] = useState(false); const [virtualSwitch, setVirtualSwitch] = useState(false); const [ghSwitch, setGhSwitch] = useState(false); const [cardSwitch, setCardSwitch] = useState(false); const cardX = useSharedValue(0); const cardStartX = useSharedValue(0); const stripX = useSharedValue(0); const blockedX = useSharedValue(0); const [blockedSwitch, setBlockedSwitch] = useState(false); const [presses, setPresses] = useState(0); const [detectorTaps, setDetectorTaps] = useState(0); const switchTap = useTapGesture({ numberOfTaps: 3, runOnJS: true, onActivate: () => setDetectorTaps((n) => n + 1), }); const pressableTap = useTapGesture({ numberOfTaps: 3, runOnJS: true, onActivate: () => setDetectorTaps((n) => n + 1), }); const virtualTap = useTapGesture({ numberOfTaps: 3, runOnJS: true, onActivate: () => setDetectorTaps((n) => n + 1), }); const ghSwitchTap = useTapGesture({ numberOfTaps: 3, runOnJS: true, onActivate: () => setDetectorTaps((n) => n + 1), }); const cardPan = usePanGesture({ onActivate: () => { cardStartX.value = cardX.value; }, onUpdate: (e) => { cardX.value = cardStartX.value + e.translationX; }, }); const cardStyle = useAnimatedStyle(() => ({ transform: [{ translateX: cardX.value }], })); const slideCard = () => { cardX.value = withTiming(cardX.value > 80 ? 0 : 160); }; // Tap (3 taps) instead of pan so the native ScrollView keeps its scrolling. const stripTap = useTapGesture({ numberOfTaps: 3, runOnJS: true, onActivate: () => setDetectorTaps((n) => n + 1), }); const stripStyle = useAnimatedStyle(() => ({ transform: [{ translateX: stripX.value }], })); const slideStrip = () => { stripX.value = withTiming(stripX.value > 80 ? 0 : 160); }; const blockedTap = useTapGesture({ numberOfTaps: 3, runOnJS: true, onActivate: () => setDetectorTaps((n) => n + 1), }); const blockedStyle = useAnimatedStyle(() => ({ transform: [{ translateX: blockedX.value }], })); const slideBlocked = () => { blockedX.value = withTiming(blockedX.value > 80 ? 0 : 160); }; return ( <View style={styles.container}> <Text style={styles.status} testID="status"> detector switch: {detectorSwitch ? 'ON' : 'OFF'}, plain switch:{' '} {plainSwitch ? 'ON' : 'OFF'}, virtual switch:{' '} {virtualSwitch ? 'ON' : 'OFF'}, GH switch: {ghSwitch ? 'ON' : 'OFF'},{' '} card switch: {cardSwitch ? 'ON' : 'OFF'}, blocked switch:{' '} {blockedSwitch ? 'ON' : 'OFF'}, presses: {presses}, detector taps:{' '} {detectorTaps} </Text> <Text style={styles.label}>1. Switch inside GestureDetector</Text> <View style={styles.row}> <GestureDetector gesture={switchTap}> <View style={styles.translated} testID="detector-switch-box"> <Switch value={detectorSwitch} onValueChange={setDetectorSwitch} testID="detector-switch" /> </View> </GestureDetector> </View> <Text style={styles.label}> 2. Switch in a plain 100x70 View (no detector) </Text> <View style={styles.row}> <View style={styles.wrapper} collapsable={false} testID="plain-wrapper"> <View style={styles.translated} testID="plain-switch-box"> <Switch value={plainSwitch} onValueChange={setPlainSwitch} testID="plain-switch" /> </View> </View> </View> <Text style={styles.label}>3. RN Pressable inside GestureDetector</Text> <View style={styles.row}> <GestureDetector gesture={pressableTap}> <View style={styles.translated} testID="detector-pressable-box"> <Pressable style={styles.pressable} onPress={() => setPresses((n) => n + 1)} testID="detector-pressable"> <Text style={styles.pressableText}>press</Text> </Pressable> </View> </GestureDetector> </View> <Text style={styles.label}>4. Switch inside VirtualGestureDetector</Text> <InterceptingGestureDetector> <View style={styles.row}> <VirtualGestureDetector gesture={virtualTap}> <View style={styles.translated} testID="virtual-switch-box"> <Switch value={virtualSwitch} onValueChange={setVirtualSwitch} testID="virtual-switch" /> </View> </VirtualGestureDetector> </View> </InterceptingGestureDetector> <Text style={styles.label}>5. RNGH Switch inside GestureDetector</Text> <View style={styles.row}> <GestureDetector gesture={ghSwitchTap}> <View style={styles.translated} testID="gh-switch-box"> <GHSwitch value={ghSwitch} onValueChange={setGhSwitch} testID="gh-switch" /> </View> </GestureDetector> </View> <Text style={styles.label}>6. Reanimated card with RN Switch inside</Text> <View style={styles.row}> <GestureDetector gesture={cardPan}> <Animated.View style={[styles.card, cardStyle]} testID="animated-card"> <Switch value={cardSwitch} onValueChange={setCardSwitch} testID="card-switch" /> </Animated.View> </GestureDetector> <Pressable style={styles.slideButton} onPress={slideCard} testID="slide-card"> <Text style={styles.pressableText}>slide</Text> </Pressable> </View> <Text style={styles.label}> 7. Reanimated card with RN ScrollView inside </Text> <View style={styles.row}> <GestureDetector gesture={stripTap}> <Animated.View style={[styles.card, stripStyle]} testID="animated-strip"> <RNScrollView horizontal style={styles.strip} testID="strip-scroll"> {['#f6b914', '#21a37c', '#001a72', '#ff6259', '#8e44ad'].map( (color) => ( <View key={color} style={[styles.stripItem, { backgroundColor: color }]} /> ) )} </RNScrollView> </Animated.View> </GestureDetector> <Pressable style={styles.slideButton} onPress={slideStrip} testID="slide-strip"> <Text style={styles.pressableText}>slide</Text> </Pressable> </View> <Text style={styles.label}> 8. Card with RN Switch sliding over a sibling </Text> <View style={styles.row}> <View style={styles.blocker} testID="blocker" /> <GestureDetector gesture={blockedTap}> <Animated.View style={[styles.card, blockedStyle]} testID="blocked-card"> <Switch value={blockedSwitch} onValueChange={setBlockedSwitch} testID="blocked-switch" /> </Animated.View> </GestureDetector> <Pressable style={styles.slideButton} onPress={slideBlocked} testID="slide-blocked"> <Text style={styles.pressableText}>slide</Text> </Pressable> </View> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, paddingHorizontal: 16, paddingTop: 8, }, status: { color: '#001a72', marginBottom: 6, }, label: { color: '#001a72', fontWeight: '600', marginBottom: 2, }, row: { height: 70, marginBottom: 4, }, blocker: { position: 'absolute', left: 160, top: 0, width: 100, height: 70, borderRadius: 12, backgroundColor: '#c8c8d2', }, wrapper: { width: 100, height: 70, }, translated: { width: 100, height: 70, justifyContent: 'center', alignItems: 'center', borderRadius: 12, backgroundColor: '#dbeafe', transform: [{ translateX: 160 }], }, card: { width: 100, height: 70, justifyContent: 'center', alignItems: 'center', borderRadius: 12, backgroundColor: '#fde68a', }, strip: { width: 80, height: 40, }, stripItem: { width: 40, height: 40, marginRight: 4, }, slideButton: { position: 'absolute', right: 0, top: 15, paddingHorizontal: 16, paddingVertical: 10, borderRadius: 8, backgroundColor: '#001a72', }, pressable: { paddingHorizontal: 16, paddingVertical: 10, borderRadius: 8, backgroundColor: '#001a72', }, pressableText: { color: 'white', fontWeight: '600', }, }); ``` </details>
1 parent ea28512 commit 403cafa

1 file changed

Lines changed: 41 additions & 11 deletions

File tree

‎packages/react-native-gesture-handler/shared/shadowNodes/react/renderer/components/rngesturehandler_codegen/RNGestureHandlerDetectorShadowNode.cpp‎

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include "RNGestureHandlerDetectorShadowNode.h"
1313

14+
#include <react/renderer/graphics/Transform.h>
15+
1416
#include <limits>
1517

1618
namespace facebook::react {
@@ -85,23 +87,30 @@ void RNGestureHandlerDetectorShadowNode::layout(LayoutContext layoutContext) {
8587
Float maxX = -std::numeric_limits<Float>::infinity();
8688
Float maxY = -std::numeric_limits<Float>::infinity();
8789

90+
const auto extend = [&](const Rect &rect) {
91+
minX = std::min(minX, rect.getMinX());
92+
minY = std::min(minY, rect.getMinY());
93+
maxX = std::max(maxX, rect.getMaxX());
94+
maxY = std::max(maxY, rect.getMaxY());
95+
};
96+
8897
for (const auto &child : children) {
8998
auto yogaChild =
9099
std::static_pointer_cast<const YogaLayoutableShadowNode>(child);
91100
const auto &frame = yogaChild->getLayoutMetrics().frame;
92-
93-
minX = std::min(minX, frame.origin.x);
94-
minY = std::min(minY, frame.origin.y);
95-
maxX = std::max(maxX, frame.origin.x + frame.size.width);
96-
maxY = std::max(maxY, frame.origin.y + frame.size.height);
101+
extend(frame);
102+
103+
#ifdef ANDROID
104+
// Android's ViewGroup only dispatches touches inside a child's bounds, so
105+
// the frame also covers the transformed content. iOS hit-tests outside the
106+
// bounds through the overflowInset override in RNGestureHandlerDetector.mm.
107+
const auto transform = yogaChild->getTransform();
108+
if (transform != Transform::Identity()) {
109+
extend(frame * transform);
110+
}
111+
#endif
97112
}
98113

99-
// Set detector's metrics to the bounding box of all children
100-
auto metrics = getLayoutMetrics();
101-
metrics.frame.origin = Point{minX, minY};
102-
metrics.frame.size = Size{maxX - minX, maxY - minY};
103-
setLayoutMetrics(metrics);
104-
105114
// Shift all children so their positions are relative to the detector's origin
106115
for (const auto &child : children) {
107116
auto yogaChild =
@@ -115,6 +124,27 @@ void RNGestureHandlerDetectorShadowNode::layout(LayoutContext layoutContext) {
115124
childMetrics.frame.origin.y -= minY;
116125
mutableChild->setLayoutMetrics(childMetrics);
117126
}
127+
128+
// Set detector's metrics to the bounding box of all children
129+
auto metrics = getLayoutMetrics();
130+
metrics.frame.origin = Point{minX, minY};
131+
metrics.frame.size = Size{maxX - minX, maxY - minY};
132+
133+
#ifdef ANDROID
134+
// The default layout computed the overflow inset against the zero-sized
135+
// frame Yoga gives a `display: contents` node. Recompute it against the
136+
// real frame; children are already shifted, so their bounds are relative.
137+
const auto contentBounds = getContentBounds();
138+
metrics.overflowInset = EdgeInsets{
139+
.left = std::min(contentBounds.getMinX(), Float{0}),
140+
.top = std::min(contentBounds.getMinY(), Float{0}),
141+
.right = -std::max(
142+
contentBounds.getMaxX() - metrics.frame.size.width, Float{0}),
143+
.bottom = -std::max(
144+
contentBounds.getMaxY() - metrics.frame.size.height, Float{0}),
145+
};
146+
#endif
147+
setLayoutMetrics(metrics);
118148
}
119149

120150
std::shared_ptr<const ShadowNode>

0 commit comments

Comments
 (0)