From 40cb9adf7f7254b3947c5a69deb74c2b9d1b36ad Mon Sep 17 00:00:00 2001 From: Jakub Kasprzyk Date: Wed, 18 Nov 2026 23:01:05 +0100 Subject: [PATCH] Deliver hover to disabled ScrollView content and box-only views on Android A ScrollView with scrollEnabled={false} returned false from dispatchGenericMotionEvent for every event, so hover never reached its content. TalkBack's touch exploration and stylus hover both arrive as hover events, so nothing inside could be focused or hovered. Only the mouse wheel is now ignored while scrolling is disabled. A View with pointerEvents="box-only" dropped its own hover the same way. It now receives hover itself through onInterceptHoverEvent, while its children still get none. Closes #60108 Closes #59966 --- .../ReactAndroid/api/ReactAndroid.api | 1 + .../views/scroll/ReactHorizontalScrollView.kt | 7 +- .../views/scroll/ReactNestedScrollView.kt | 3 +- .../react/views/scroll/ReactScrollView.kt | 3 +- .../react/views/view/ReactViewGroup.kt | 24 ++- .../ReactScrollViewDisabledHoverTest.kt | 185 ++++++++++++++++++ .../react/views/view/ReactViewGroupTest.kt | 76 +++++++ 7 files changed, 292 insertions(+), 7 deletions(-) create mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/scroll/ReactScrollViewDisabledHoverTest.kt diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index 9f62f0c9b496..c0aa0e2f00ac 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -6468,6 +6468,7 @@ public class com/facebook/react/views/view/ReactViewGroup : android/view/ViewGro protected fun onAttachedToWindow ()V protected fun onConfigurationChanged (Landroid/content/res/Configuration;)V public fun onHoverEvent (Landroid/view/MotionEvent;)Z + public fun onInterceptHoverEvent (Landroid/view/MotionEvent;)Z public fun onInterceptTouchEvent (Landroid/view/MotionEvent;)Z protected fun onLayout (ZIIII)V protected fun onMeasure (II)V diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.kt index 39fabc840aa4..01fa8e255d01 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.kt @@ -779,14 +779,15 @@ constructor(context: Context, private val fpsListener: FpsListener? = null) : } override fun dispatchGenericMotionEvent(ev: MotionEvent): Boolean { - // Ignore generic motion events (joystick, mouse wheel, trackpad) if scrolling is disabled - if (!scrollEnabled) return false - // We do not dispatch the motion event if its children are not supposed to receive it if (!PointerEvents.canChildrenBeTouchTarget(pointerEvents)) return false // Handle ACTION_SCROLL events (mouse wheel, trackpad, joystick) if (ev.actionMasked == MotionEvent.ACTION_SCROLL) { + // Ignore them if scrolling is disabled. Hover events still reach the content, which + // TalkBack's touch exploration relies on. + if (!scrollEnabled) return false + val hScroll = ev.getAxisValue(MotionEvent.AXIS_HSCROLL) if (hScroll != 0f) { // Perform the scroll diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactNestedScrollView.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactNestedScrollView.kt index 01cb197ff7b5..1739ce364ec0 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactNestedScrollView.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactNestedScrollView.kt @@ -631,10 +631,11 @@ constructor(context: Context, private val fpsListener: FpsListener? = null) : } override fun dispatchGenericMotionEvent(ev: MotionEvent): Boolean { - if (!scrollEnabled) return false if (!PointerEvents.canChildrenBeTouchTarget(pointerEvents)) return false if (ev.actionMasked == MotionEvent.ACTION_SCROLL) { + // Hover events still reach the content, which TalkBack's touch exploration relies on. + if (!scrollEnabled) return false val vScroll = ev.getAxisValue(MotionEvent.AXIS_VSCROLL) if (vScroll != 0f) { enableFpsListener() diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.kt index fca6538d0774..1a8b91c90696 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactScrollView.kt @@ -623,10 +623,11 @@ constructor(context: Context, private val fpsListener: FpsListener? = null) : } override fun dispatchGenericMotionEvent(ev: MotionEvent): Boolean { - if (!scrollEnabled) return false if (!PointerEvents.canChildrenBeTouchTarget(pointerEvents)) return false if (ev.actionMasked == MotionEvent.ACTION_SCROLL) { + // Hover events still reach the content, which TalkBack's touch exploration relies on. + if (!scrollEnabled) return false val vScroll = ev.getAxisValue(MotionEvent.AXIS_VSCROLL) if (vScroll != 0f) { enableFpsListener() diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.kt index 437baa1f2591..407058b75e36 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.kt @@ -317,9 +317,21 @@ public open class ReactViewGroup public constructor(context: Context?) : return super.onHoverEvent(event) } - override fun dispatchGenericMotionEvent(ev: MotionEvent): Boolean { - // We do not dispatch the motion event if its children are not supposed to receive it + override fun onInterceptHoverEvent(event: MotionEvent): Boolean { + // We keep the hover event for this view if the children are not supposed to receive it. if (!canChildrenBeTouchTarget(pointerEvents)) { + return true + } + return super.onInterceptHoverEvent(event) + } + + override fun dispatchGenericMotionEvent(ev: MotionEvent): Boolean { + // We do not dispatch the motion event if its children are not supposed to receive it. A hover + // event still reaches a view that is a touch target, and onInterceptHoverEvent keeps it there. + if ( + !canChildrenBeTouchTarget(pointerEvents) && + !(isHoverEvent(ev) && canBeTouchTarget(pointerEvents)) + ) { return false } @@ -1155,5 +1167,13 @@ public open class ReactViewGroup public constructor(context: Context?) : private fun setViewClipped(view: View, clipped: Boolean) { view.setTag(R.id.view_clipped, clipped) } + + private fun isHoverEvent(event: MotionEvent): Boolean = + when (event.actionMasked) { + MotionEvent.ACTION_HOVER_ENTER, + MotionEvent.ACTION_HOVER_MOVE, + MotionEvent.ACTION_HOVER_EXIT -> true + else -> false + } } } diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/scroll/ReactScrollViewDisabledHoverTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/scroll/ReactScrollViewDisabledHoverTest.kt new file mode 100644 index 000000000000..2017068ba431 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/scroll/ReactScrollViewDisabledHoverTest.kt @@ -0,0 +1,185 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +// The deprecated APIs exercised here remain public and required by this Robolectric setup. +@file:Suppress("DEPRECATION") + +package com.facebook.react.views.scroll + +import android.annotation.SuppressLint +import android.util.DisplayMetrics +import android.view.InputDevice +import android.view.MotionEvent +import android.view.View +import android.view.ViewGroup +import com.facebook.react.bridge.ReactTestHelper.createTestReactApplicationContext +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests +import com.facebook.react.uimanager.DisplayMetricsHolder +import com.facebook.react.uimanager.ThemedReactContext +import com.facebook.react.uimanager.UIManagerHelper +import com.facebook.react.uimanager.events.EventDispatcher +import com.facebook.soloader.SoLoader +import com.facebook.testutils.shadows.ShadowSoLoader +import org.assertj.core.api.Assertions.assertThat +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.MockedStatic +import org.mockito.Mockito.mockStatic +import org.mockito.kotlin.any +import org.mockito.kotlin.mock +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +/** Verifies that a ScrollView with scrolling disabled ignores the mouse wheel but not hover. */ +@SuppressLint("DeprecatedClass", "DeprecatedMethod") +@RunWith(RobolectricTestRunner::class) +@Config(shadows = [ShadowSoLoader::class]) +class ReactScrollViewDisabledHoverTest { + + private lateinit var themedContext: ThemedReactContext + private lateinit var uiManagerHelperMock: MockedStatic + + @Before + fun setup() { + ReactNativeFeatureFlagsForTests.setUp() + SoLoader.setInTestMode() + val context = createTestReactApplicationContext() + themedContext = ThemedReactContext(context, context, null, -1) + DisplayMetricsHolder.setScreenDisplayMetrics(DisplayMetrics()) + uiManagerHelperMock = mockStatic(UIManagerHelper::class.java) + uiManagerHelperMock + .`when` { UIManagerHelper.getEventDispatcher(any()) } + .thenReturn(mock()) + } + + @After + fun teardown() { + uiManagerHelperMock.close() + DisplayMetricsHolder.setScreenDisplayMetrics(null) + } + + @Test + fun testScrollViewWithScrollDisabledDispatchesHoverToContent() { + val view = ReactScrollViewManager().createViewInstance(themedContext) + view.scrollEnabled = false + + assertThat(hoverReachesContent(view)).isTrue() + } + + @Test + fun testHorizontalScrollViewWithScrollDisabledDispatchesHoverToContent() { + val view = ReactHorizontalScrollViewManager().createViewInstance(themedContext) + view.scrollEnabled = false + + assertThat(hoverReachesContent(view)).isTrue() + } + + @Test + fun testNestedScrollViewWithScrollDisabledDispatchesHoverToContent() { + val view = ReactNestedScrollView(themedContext) + view.scrollEnabled = false + + assertThat(hoverReachesContent(view)).isTrue() + } + + @Test + fun testScrollViewWithScrollDisabledIgnoresWheel() { + val view = ReactScrollViewManager().createViewInstance(themedContext) + view.scrollEnabled = false + addContent(view) + + val handled = dispatchAndRecycle(view, obtainWheelScroll(MotionEvent.AXIS_VSCROLL, -1f)) + + assertThat(handled).isFalse() + assertThat(view.scrollY).isZero() + } + + @Test + fun testHorizontalScrollViewWithScrollDisabledIgnoresWheel() { + val view = ReactHorizontalScrollViewManager().createViewInstance(themedContext) + view.scrollEnabled = false + addContent(view) + + val handled = dispatchAndRecycle(view, obtainWheelScroll(MotionEvent.AXIS_HSCROLL, 1f)) + + assertThat(handled).isFalse() + assertThat(view.scrollX).isZero() + } + + @Test + fun testNestedScrollViewWithScrollDisabledIgnoresWheel() { + val view = ReactNestedScrollView(themedContext) + view.scrollEnabled = false + addContent(view) + + val handled = dispatchAndRecycle(view, obtainWheelScroll(MotionEvent.AXIS_VSCROLL, -1f)) + + assertThat(handled).isFalse() + assertThat(view.scrollY).isZero() + } + + private fun hoverReachesContent(view: ViewGroup): Boolean { + var hovered = false + addContent(view).setOnHoverListener { _, _ -> + hovered = true + true + } + dispatchAndRecycle(view, obtainStylusHover(MotionEvent.ACTION_HOVER_ENTER)) + dispatchAndRecycle(view, obtainStylusHover(MotionEvent.ACTION_HOVER_MOVE)) + return hovered + } + + private fun addContent(view: ViewGroup): View { + val content = View(themedContext) + view.addView(content) + content.layout(0, 0, CONTENT_SIZE, CONTENT_SIZE) + view.layout(0, 0, VIEWPORT_SIZE, VIEWPORT_SIZE) + return content + } + + private fun dispatchAndRecycle(view: View, event: MotionEvent): Boolean = + try { + view.dispatchGenericMotionEvent(event) + } finally { + event.recycle() + } + + private fun obtainStylusHover(action: Int): MotionEvent { + val event = MotionEvent.obtain(0L, 0L, action, VIEWPORT_SIZE / 2f, VIEWPORT_SIZE / 2f, 0) + event.source = InputDevice.SOURCE_STYLUS + return event + } + + private fun obtainWheelScroll(axis: Int, value: Float): MotionEvent { + val properties = + MotionEvent.PointerProperties().apply { toolType = MotionEvent.TOOL_TYPE_MOUSE } + val coords = MotionEvent.PointerCoords().apply { setAxisValue(axis, value) } + return MotionEvent.obtain( + 0L, + 0L, + MotionEvent.ACTION_SCROLL, + 1, + arrayOf(properties), + arrayOf(coords), + 0, + 0, + 1f, + 1f, + 0, + 0, + InputDevice.SOURCE_MOUSE, + 0, + ) + } + + private companion object { + const val VIEWPORT_SIZE = 100 + const val CONTENT_SIZE = 1000 + } +} diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/view/ReactViewGroupTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/view/ReactViewGroupTest.kt index 96bb5a9eec8f..8964d5b5e3f3 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/view/ReactViewGroupTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/view/ReactViewGroupTest.kt @@ -12,12 +12,15 @@ import android.content.Context import android.graphics.BlendMode import android.graphics.Canvas import android.graphics.Paint +import android.view.InputDevice +import android.view.MotionEvent import android.view.View import android.view.ViewGroup import android.widget.FrameLayout import com.facebook.react.R import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests import com.facebook.react.uimanager.HasElevatedDescendantCache +import com.facebook.react.uimanager.PointerEvents import org.assertj.core.api.Assertions.assertThat import org.junit.Before import org.junit.Test @@ -266,6 +269,79 @@ class ReactViewGroupTest { assertThat(outer.hasOverlappingRendering()).isTrue() } + @Test + fun `pointer events - a box-only view receives hover and keeps it from its children`() { + val (rvg, child) = hoverTarget(PointerEvents.BOX_ONLY) + var viewHovered = false + var childHovered = false + rvg.setOnHoverListener { _, _ -> + viewHovered = true + true + } + child.setOnHoverListener { _, _ -> + childHovered = true + true + } + + dispatchHover(rvg, MotionEvent.ACTION_HOVER_ENTER) + + assertThat(viewHovered).isTrue() + assertThat(childHovered).isFalse() + } + + @Test + fun `pointer events - a none view and its children receive no hover`() { + val (rvg, child) = hoverTarget(PointerEvents.NONE) + var hovered = false + rvg.setOnHoverListener { _, _ -> + hovered = true + true + } + child.setOnHoverListener { _, _ -> + hovered = true + true + } + + assertThat(dispatchHover(rvg, MotionEvent.ACTION_HOVER_ENTER)).isFalse() + assertThat(hovered).isFalse() + } + + @Test + fun `pointer events - a box-only view keeps other generic motion events from its children`() { + val (rvg, child) = hoverTarget(PointerEvents.BOX_ONLY) + var childReceived = false + child.setOnGenericMotionListener { _, _ -> + childReceived = true + true + } + val event = MotionEvent.obtain(0L, 0L, MotionEvent.ACTION_SCROLL, 50f, 50f, 0) + event.source = InputDevice.SOURCE_MOUSE + + assertThat(rvg.dispatchGenericMotionEvent(event)).isFalse() + assertThat(childReceived).isFalse() + event.recycle() + } + + private fun hoverTarget(pointerEvents: PointerEvents): Pair { + val rvg = ReactViewGroup(context) + rvg.pointerEvents = pointerEvents + val child = View(context) + rvg.addView(child) + child.layout(0, 0, 100, 100) + rvg.layout(0, 0, 100, 100) + return rvg to child + } + + private fun dispatchHover(view: View, action: Int): Boolean { + val event = MotionEvent.obtain(0L, 0L, action, 50f, 50f, 0) + event.source = InputDevice.SOURCE_STYLUS + return try { + view.dispatchGenericMotionEvent(event) + } finally { + event.recycle() + } + } + // Mirrors the ViewManager mount/unmount hooks: attach/detach the child, then refresh the cache. private fun mount(parent: ReactViewGroup, child: View) { parent.addView(child)