Repository navigation
[Android] Cancel gestures on native ACTION_CANCEL #4589
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b168524
[Android] Cancel gestures on native ACTION_CANCEL
m-bert cd3c5b9
Clear awaitingHandlers
m-bert 98ca89c
Merge branch 'main' into @mbert/detector-native-cancel
m-bert b1e600e
Add Native takeover example and e2e test
m-bert 01cb8c9
Merge branch 'main' into @mbert/detector-native-cancel
m-bert d110d19
Merge branch '@mbert/detector-native-cancel' of github.com:software-m…
m-bert 16ec0b9
Rename example
m-bert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,63 @@ | ||
| # Android only: a native view (nested PagerView) takes over the touch without | ||
| # the root seeing a disallow-intercept request. The tap under it must be | ||
| # cancelled by the framework's ACTION_CANCEL instead of firing on lift. | ||
| steps: | ||
| - echo: Launching Expo Example | ||
| - launch: com.example.ExpoExample | ||
| - await: { visible: { text: "Empty Example" }, timeout: 15000 } | ||
| - when: { platform: android } | ||
| steps: | ||
| - echo: "On the Home list, search for the native intercept cancellation example" | ||
| - type: { text: "intercept", into: { id: "search-examples" } } | ||
| - await: { visible: { text: "Native intercept cancellation" } } | ||
| - echo: "Tap the example to open it" | ||
| - tap: { text: "Native intercept cancellation" } | ||
| - await: { visible: { id: "tap-box" } } | ||
| - await: { idle: true } | ||
|
|
||
| - echo: "Clear console" | ||
| - await: { visible: { id: "open-console-button" } } | ||
| - tap: { id: "open-console-button" } | ||
| - await: { visible: { id: "clear-console-button" } } | ||
| - tap: { id: "clear-console-button" } | ||
| - tap: { id: "close-console-button" } | ||
| - await: { hidden: { id: "close-console-button" } } | ||
| - await: { idle: true } | ||
|
|
||
| - echo: "Swipe the inner pager starting on the tap box" | ||
| - assert: { visible: { text: "Tap count: 0" } } | ||
| - swipe: { from: { id: "tap-box" }, direction: left } | ||
| - await: { visible: { id: "inner-page-2" } } | ||
| - await: { idle: true } | ||
|
|
||
| - echo: "The pager moved and the tap did not fire" | ||
| - tap: { id: "open-console-button" } | ||
| - await: { idle: true } | ||
| - await: { visible: { id: "close-console-button" } } | ||
| - assert: { visible: { text: "1. onBegin" } } | ||
| - assert: { visible: { text: "2. onFinalize" } } | ||
| - assert: { hidden: { text: "onActivate" } } | ||
| - assert: { hidden: { text: "onDeactivate" } } | ||
|
|
||
| - echo: "Clear console" | ||
| - tap: { id: "clear-console-button" } | ||
| - tap: { id: "close-console-button" } | ||
| - await: { hidden: { id: "close-console-button" } } | ||
| - await: { idle: true } | ||
|
|
||
| - echo: "Swipe back to the first page" | ||
| - swipe: { from: { id: "inner-page-2" }, direction: right } | ||
| - await: { visible: { id: "tap-box" } } | ||
| - await: { idle: true } | ||
| - assert: { visible: { text: "Tap count: 0" } } | ||
|
|
||
| - echo: "A plain tap on the box still counts" | ||
| - tap: { id: "tap-box" } | ||
| - await: { visible: { text: "Tap count: 1" } } | ||
| - tap: { id: "open-console-button" } | ||
| - await: { idle: true } | ||
| - await: { visible: { id: "close-console-button" } } | ||
| - assert: { visible: { text: "3. onBegin" } } | ||
| - assert: { visible: { text: "4. onActivate" } } | ||
| - assert: { visible: { text: "5. onDeactivate" } } | ||
| - assert: { visible: { text: "6. onFinalize" } } |
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
197 changes: 197 additions & 0 deletions
197
apps/common-app/src/new_api/tests/nativeInterceptCancellation/index.tsx
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,197 @@ | ||
| import React, { useState } from 'react'; | ||
| import { | ||
| DrawerLayoutAndroid, | ||
| Pressable, | ||
| StyleSheet, | ||
| Text, | ||
| View, | ||
| } from 'react-native'; | ||
| import { GestureDetector, useTapGesture } from 'react-native-gesture-handler'; | ||
| import PagerView from 'react-native-pager-view'; | ||
| import Animated, { | ||
| interpolateColor, | ||
| useAnimatedStyle, | ||
| useSharedValue, | ||
| withTiming, | ||
| } from 'react-native-reanimated'; | ||
| import { scheduleOnRN } from 'react-native-worklets'; | ||
|
|
||
| import { COLORS, useIndexedLogger } from '../../../common'; | ||
|
|
||
| // A native view takes over the touch without the root ever seeing a | ||
| // disallow-intercept request. The framework still sends ACTION_CANCEL to the | ||
| // tap's detector, which is what cancels the tap below. | ||
| // - Pager: swipe the inner pager starting on the box. The inner pager host | ||
| // asks its ancestors not to intercept on DOWN, so the pager's own request on | ||
| // MOVE stops at the already flagged page view and never reaches the root. | ||
| // - Drawer: swipe the drawer open from the right edge starting on the box. | ||
| // DrawerLayout intercepts on MOVE and never requests at all. With gesture | ||
| // navigation the system back swipe takes the edge, use 3-button navigation. | ||
| // In both cases the pager / drawer moves and the tap count stays the same. | ||
|
|
||
| type Mode = 'pager' | 'drawer'; | ||
|
|
||
| type TapBoxProps = { | ||
| count: number; | ||
| onTap: () => void; | ||
| log: (message: string) => void; | ||
| }; | ||
|
|
||
| function TapBox({ count, onTap, log }: TapBoxProps) { | ||
| const colorProgress = useSharedValue(0); | ||
|
|
||
| const animatedStyle = useAnimatedStyle(() => ({ | ||
| backgroundColor: interpolateColor( | ||
| colorProgress.value, | ||
| [0, 1], | ||
| [COLORS.NAVY, COLORS.KINDA_BLUE] | ||
| ), | ||
| })); | ||
|
|
||
| const tap = useTapGesture({ | ||
| onBegin: () => { | ||
| log('onBegin'); | ||
| colorProgress.value = withTiming(1, { duration: 100 }); | ||
| }, | ||
| onActivate: () => { | ||
| log('onActivate'); | ||
| scheduleOnRN(onTap); | ||
| }, | ||
| onDeactivate: () => { | ||
| log('onDeactivate'); | ||
| }, | ||
| onFinalize: () => { | ||
| log('onFinalize'); | ||
| colorProgress.value = withTiming(0, { duration: 100 }); | ||
| }, | ||
| }); | ||
|
|
||
| return ( | ||
| <GestureDetector gesture={tap}> | ||
| <Animated.View testID="tap-box" style={[styles.box, animatedStyle]}> | ||
| <Text style={styles.boxText}>Tap count: {count}</Text> | ||
| </Animated.View> | ||
| </GestureDetector> | ||
| ); | ||
| } | ||
|
|
||
| function PagerMode(props: TapBoxProps) { | ||
| return ( | ||
| <PagerView style={styles.flex} initialPage={0}> | ||
| <View key="outer-1" style={styles.flex}> | ||
| <PagerView style={styles.flex} initialPage={0}> | ||
| <View key="inner-1" style={styles.flex}> | ||
| <TapBox {...props} /> | ||
| </View> | ||
| <View key="inner-2" style={[styles.flex, styles.center]}> | ||
| <Text testID="inner-page-2">Inner page 2</Text> | ||
| </View> | ||
| </PagerView> | ||
| </View> | ||
| <View key="outer-2" style={[styles.flex, styles.center]}> | ||
| <Text>Outer page 2</Text> | ||
| </View> | ||
| </PagerView> | ||
| ); | ||
| } | ||
|
|
||
| function DrawerMode(props: TapBoxProps) { | ||
| return ( | ||
| <DrawerLayoutAndroid | ||
| style={styles.flex} | ||
| drawerWidth={300} | ||
| drawerPosition="right" | ||
| renderNavigationView={() => ( | ||
| <View testID="drawer-panel" style={[styles.flex, styles.center]}> | ||
| <Text>Drawer</Text> | ||
| </View> | ||
| )}> | ||
| <TapBox {...props} /> | ||
| </DrawerLayoutAndroid> | ||
| ); | ||
| } | ||
|
|
||
| export default function NativeInterceptCancellationExample() { | ||
| const [mode, setMode] = useState<Mode>('pager'); | ||
| const [count, setCount] = useState(0); | ||
| const log = useIndexedLogger(); | ||
|
|
||
| const boxProps = { | ||
| count, | ||
| log, | ||
| onTap: () => setCount((c) => c + 1), | ||
| }; | ||
|
|
||
| return ( | ||
| <View style={styles.flex}> | ||
| <View style={styles.header}> | ||
| <Text style={styles.hint}> | ||
| Swipe the pager or drag the drawer open from the right edge, starting | ||
| on the box. The count must not change. | ||
| </Text> | ||
| <View style={styles.buttons}> | ||
| <ModeButton mode="pager" current={mode} onSelect={setMode} /> | ||
| <ModeButton mode="drawer" current={mode} onSelect={setMode} /> | ||
| </View> | ||
| </View> | ||
| {mode === 'pager' ? ( | ||
| <PagerMode {...boxProps} /> | ||
| ) : ( | ||
| <DrawerMode {...boxProps} /> | ||
| )} | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| type ModeButtonProps = { | ||
| mode: Mode; | ||
| current: Mode; | ||
| onSelect: (mode: Mode) => void; | ||
| }; | ||
|
|
||
| function ModeButton({ mode, current, onSelect }: ModeButtonProps) { | ||
| return ( | ||
| <Pressable | ||
| testID={`mode-${mode}`} | ||
| style={[styles.button, mode === current && styles.buttonActive]} | ||
| onPress={() => onSelect(mode)}> | ||
| <Text style={styles.buttonText}> | ||
| {mode === 'pager' ? 'Pager' : 'Drawer'} | ||
| </Text> | ||
| </Pressable> | ||
| ); | ||
| } | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| flex: { flex: 1 }, | ||
| center: { alignItems: 'center', justifyContent: 'center' }, | ||
| header: { | ||
| padding: 12, | ||
| gap: 8, | ||
| backgroundColor: COLORS.offWhite, | ||
| }, | ||
| hint: { | ||
| textAlign: 'center', | ||
| opacity: 0.6, | ||
| fontSize: 14, | ||
| }, | ||
| buttons: { flexDirection: 'row', justifyContent: 'center', gap: 8 }, | ||
| button: { | ||
| paddingVertical: 8, | ||
| paddingHorizontal: 14, | ||
| borderRadius: 6, | ||
| backgroundColor: COLORS.KINDA_YELLOW, | ||
| }, | ||
| buttonActive: { backgroundColor: COLORS.YELLOW }, | ||
| buttonText: { fontWeight: '600', color: COLORS.NAVY }, | ||
| box: { | ||
| flex: 1, | ||
| alignItems: 'center', | ||
| justifyContent: 'center', | ||
| }, | ||
| boxText: { | ||
| fontSize: 20, | ||
| fontWeight: '700', | ||
| color: COLORS.offWhite, | ||
| }, | ||
| }); |
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
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
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.
Oops, something went wrong.
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.