Skip to content
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

Fix Android native buttons emitting 2 press events when talkbalk is enabled #3002

Merged
merged 6 commits into from
Jul 24, 2024
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import android.widget.ScrollView
import com.facebook.react.views.scroll.ReactScrollView
import com.facebook.react.views.swiperefresh.ReactSwipeRefreshLayout
import com.facebook.react.views.textinput.ReactEditText
import com.swmansion.gesturehandler.react.RNGestureHandlerButtonViewManager
import com.swmansion.gesturehandler.react.isScreenReaderOn

class NativeViewGestureHandler : GestureHandler<NativeViewGestureHandler>() {
private var shouldActivateOnStart = false
Expand Down Expand Up @@ -82,6 +84,17 @@ class NativeViewGestureHandler : GestureHandler<NativeViewGestureHandler>() {

override fun onHandle(event: MotionEvent, sourceEvent: MotionEvent) {
val view = view!!

val isTouchExplorationEnabled = view.context.isScreenReaderOn()

if (view is RNGestureHandlerButtonViewManager.ButtonViewGroup && isTouchExplorationEnabled) {
// Fix for: https://github.com/software-mansion/react-native-gesture-handler/issues/2808
// When TalkBack is enabled, two identical press events are sent, while only one is expected.
// The unexpected one is caught by looking at the state of the current handler,
// which is UNDETERMINED when receiving the invalid event, and BEGAN when receiving the valid one.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update this comment as it doesn't check state anymore 😅

return
}

if (event.actionMasked == MotionEvent.ACTION_UP) {
if (state == STATE_UNDETERMINED && !hook.canBegin(event)) {
cancel()
Expand All @@ -106,13 +119,16 @@ class NativeViewGestureHandler : GestureHandler<NativeViewGestureHandler>() {
view.onTouchEvent(event)
activate()
}

tryIntercept(view, event) -> {
view.onTouchEvent(event)
activate()
}

hook.wantsToHandleEventBeforeActivation() -> {
hook.handleEventBeforeActivation(event)
}

state != STATE_BEGAN -> {
if (hook.canBegin(event)) {
begin()
Expand Down
Loading