Skip to content

Commit 9e94589

Browse files
Autopilot Botfacebook-github-bot
authored andcommitted
Send accessibility hover events from ReactViewGroup when pointer events are enabled
Summary: When `ReactFeatureFlags.dispatchPointerEvents` is enabled, `ReactViewGroup.onHoverEvent` returned `canBeTouchTarget(pointerEvents)` without calling `View.onHoverEvent`, so React Native views never sent `TYPE_VIEW_HOVER_ENTER`/`EXIT` accessibility events. Hover-driven accessibility services (for example hover text-to-speech) were silent over the body of a pressable view, while its plain text and image children, which don't override `onHoverEvent`, still spoke. Just calling `super.onHoverEvent` is not enough: a pressable often contains a full-size `pointerEvents="none"` overlay child, and `View.onHoverEvent` skips accessibility hover while any child is hovered. This change makes `ReactViewGroup` send hover enter/exit itself, from its nearest important-for-accessibility ancestor, when it can be a touch target. It stays silent while a child that can take the hover is under the pointer. `pointerEvents="none"` children are ignored and `box-none` children are checked one level deeper. The view remembers which view received HOVER_ENTER and sends the matching HOVER_EXIT to that same view, so enter/exit stay paired even if no ancestor was important for accessibility at enter time or the important ancestor changes mid-hover. A pending HOVER_EXIT is also sent when the view is recycled or detached from the window mid-hover. Behavior with pointer events disabled is unchanged. Note for reviewers: a plain hovered container now emits hover enter/exit the way stock Android does with the flag off. Changelog: [Android][Fixed] - Send accessibility hover events from `ReactViewGroup` when pointer events dispatch is enabled Differential Revision: D131164392
1 parent 3f54cc2 commit 9e94589

3 files changed

Lines changed: 291 additions & 1 deletion

File tree

‎packages/react-native/ReactAndroid/api/ReactAndroid.api‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6467,6 +6467,7 @@ public class com/facebook/react/views/view/ReactViewGroup : android/view/ViewGro
64676467
public fun invalidateElevatedDescendantCache ()Z
64686468
protected fun onAttachedToWindow ()V
64696469
protected fun onConfigurationChanged (Landroid/content/res/Configuration;)V
6470+
protected fun onDetachedFromWindow ()V
64706471
public fun onHoverEvent (Landroid/view/MotionEvent;)Z
64716472
public fun onInterceptTouchEvent (Landroid/view/MotionEvent;)Z
64726473
protected fun onLayout (ZIIII)V

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.kt‎

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import android.content.Context
1515
import android.content.res.Configuration
1616
import android.graphics.BlendMode
1717
import android.graphics.Canvas
18+
import android.graphics.Matrix
1819
import android.graphics.Paint
1920
import android.graphics.Rect
2021
import android.graphics.drawable.Drawable
@@ -23,6 +24,7 @@ import android.view.MotionEvent
2324
import android.view.View
2425
import android.view.ViewGroup
2526
import android.view.ViewStructure
27+
import android.view.accessibility.AccessibilityEvent
2628
import android.view.accessibility.AccessibilityManager
2729
import com.facebook.common.logging.FLog
2830
import com.facebook.react.R
@@ -159,6 +161,8 @@ public open class ReactViewGroup public constructor(context: Context?) :
159161
AccessibilityManager.AccessibilityStateChangeListener? =
160162
null
161163
private var focusOnAttach = false
164+
// Receives the HOVER_EXIT that pairs with the last accessibility HOVER_ENTER.
165+
private var accessibilityHoverSource: View? = null
162166

163167
internal var nativeBackgroundMap: ReadableMap? = null
164168
internal var nativeForegroundMap: ReadableMap? = null
@@ -200,6 +204,7 @@ public open class ReactViewGroup public constructor(context: Context?) :
200204
backfaceOpacity = 1f
201205
backfaceVisible = true
202206
childrenRemovedWhileTransitioning = null
207+
accessibilityHoverSource = null
203208
nativeBackgroundMap = null
204209
nativeForegroundMap = null
205210
}
@@ -216,6 +221,8 @@ public open class ReactViewGroup public constructor(context: Context?) :
216221
}
217222
}
218223

224+
sendPendingAccessibilityHoverExit()
225+
219226
// Set default field values
220227
initView()
221228
overflowInset.setEmpty()
@@ -312,11 +319,82 @@ public open class ReactViewGroup public constructor(context: Context?) :
312319
@Suppress("DEPRECATION")
313320
if (ReactFeatureFlags.dispatchPointerEvents) {
314321
// Match the logic from onTouchEvent if pointer events are enabled
315-
return canBeTouchTarget(pointerEvents)
322+
val canBeTarget = canBeTouchTarget(pointerEvents)
323+
if (canBeTarget || accessibilityHoverSource != null) {
324+
updateAccessibilityHoverState(event)
325+
}
326+
return canBeTarget
316327
}
317328
return super.onHoverEvent(event)
318329
}
319330

331+
/**
332+
* Sends the accessibility hover events that [View.onHoverEvent] would send, which hover-based
333+
* screen readers rely on. [View.onHoverEvent] can't be used here because it skips them while any
334+
* child is under the pointer, including children that pointer events exclude from hit testing.
335+
*/
336+
private fun updateAccessibilityHoverState(event: MotionEvent) {
337+
val x = event.x
338+
val y = event.y
339+
val isHovered =
340+
event.actionMasked != MotionEvent.ACTION_HOVER_EXIT &&
341+
x >= 0 &&
342+
y >= 0 &&
343+
x < width &&
344+
y < height &&
345+
!hasHoverTargetChildAt(this, x, y)
346+
if (accessibilityHoverSource != null) {
347+
if (!isHovered) {
348+
sendPendingAccessibilityHoverExit()
349+
}
350+
return
351+
}
352+
if (!isHovered) {
353+
return
354+
}
355+
var source: View? = this
356+
while (source != null && !source.isImportantForAccessibility) {
357+
source = source.parent as? View
358+
}
359+
if (source != null) {
360+
accessibilityHoverSource = source
361+
source.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER)
362+
}
363+
}
364+
365+
private fun sendPendingAccessibilityHoverExit() {
366+
val hoverSource = accessibilityHoverSource ?: return
367+
accessibilityHoverSource = null
368+
hoverSource.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT)
369+
}
370+
371+
private fun hasHoverTargetChildAt(parent: ViewGroup, x: Float, y: Float): Boolean {
372+
for (i in parent.childCount - 1 downTo 0) {
373+
val child = parent.getChildAt(i)
374+
if (child.visibility != VISIBLE) {
375+
continue
376+
}
377+
val point = floatArrayOf(x + parent.scrollX - child.left, y + parent.scrollY - child.top)
378+
if (!child.matrix.isIdentity) {
379+
val inverse = Matrix()
380+
child.matrix.invert(inverse)
381+
inverse.mapPoints(point)
382+
}
383+
if (point[0] < 0 || point[1] < 0 || point[0] >= child.width || point[1] >= child.height) {
384+
continue
385+
}
386+
when ((child as? ReactPointerEventsView)?.pointerEvents ?: PointerEvents.AUTO) {
387+
PointerEvents.NONE -> continue
388+
PointerEvents.BOX_NONE ->
389+
if (child is ViewGroup && hasHoverTargetChildAt(child, point[0], point[1])) {
390+
return true
391+
}
392+
else -> return true
393+
}
394+
}
395+
return false
396+
}
397+
320398
override fun dispatchGenericMotionEvent(ev: MotionEvent): Boolean {
321399
// We do not dispatch the motion event if its children are not supposed to receive it
322400
if (!canChildrenBeTouchTarget(pointerEvents)) {
@@ -675,6 +753,11 @@ public open class ReactViewGroup public constructor(context: Context?) :
675753
}
676754
}
677755

756+
override fun onDetachedFromWindow() {
757+
sendPendingAccessibilityHoverExit()
758+
super.onDetachedFromWindow()
759+
}
760+
678761
override fun onConfigurationChanged(newConfig: Configuration) {
679762
super.onConfigurationChanged(newConfig)
680763
if (nativeBackgroundMap != null) {

‎packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/view/ReactViewGroupTest.kt‎

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@ import android.content.Context
1212
import android.graphics.BlendMode
1313
import android.graphics.Canvas
1414
import android.graphics.Paint
15+
import android.view.InputDevice
16+
import android.view.MotionEvent
1517
import android.view.View
1618
import android.view.ViewGroup
19+
import android.view.accessibility.AccessibilityEvent
1720
import android.widget.FrameLayout
1821
import com.facebook.react.R
22+
import com.facebook.react.config.ReactFeatureFlags
1923
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests
2024
import com.facebook.react.uimanager.HasElevatedDescendantCache
25+
import com.facebook.react.uimanager.PointerEvents
2126
import org.assertj.core.api.Assertions.assertThat
27+
import org.junit.After
2228
import org.junit.Before
2329
import org.junit.Test
2430
import org.junit.runner.RunWith
@@ -36,6 +42,177 @@ class ReactViewGroupTest {
3642
context = Robolectric.buildActivity(Activity::class.java).create().get()
3743
}
3844

45+
@After
46+
fun tearDown() {
47+
@Suppress("DEPRECATION")
48+
ReactFeatureFlags.dispatchPointerEvents = false
49+
}
50+
51+
@Test
52+
fun `hover with pointer events - sends accessibility hover enter and exit`() {
53+
enablePointerEvents()
54+
val rvg = hoverableView()
55+
56+
assertThat(rvg.onHoverEvent(hoverEvent(MotionEvent.ACTION_HOVER_ENTER, 10f))).isTrue()
57+
assertThat(rvg.onHoverEvent(hoverEvent(MotionEvent.ACTION_HOVER_MOVE, 20f))).isTrue()
58+
assertThat(rvg.onHoverEvent(hoverEvent(MotionEvent.ACTION_HOVER_EXIT, 20f))).isTrue()
59+
60+
assertThat(rvg.sentAccessibilityEvents)
61+
.containsExactly(
62+
AccessibilityEvent.TYPE_VIEW_HOVER_ENTER,
63+
AccessibilityEvent.TYPE_VIEW_HOVER_EXIT,
64+
)
65+
}
66+
67+
@Test
68+
fun `hover with pointer events - sends hover exit when the pointer moves out of the view`() {
69+
enablePointerEvents()
70+
val rvg = hoverableView()
71+
72+
rvg.onHoverEvent(hoverEvent(MotionEvent.ACTION_HOVER_MOVE, 10f))
73+
rvg.onHoverEvent(hoverEvent(MotionEvent.ACTION_HOVER_MOVE, 150f))
74+
75+
assertThat(rvg.sentAccessibilityEvents)
76+
.containsExactly(
77+
AccessibilityEvent.TYPE_VIEW_HOVER_ENTER,
78+
AccessibilityEvent.TYPE_VIEW_HOVER_EXIT,
79+
)
80+
}
81+
82+
@Test
83+
fun `hover with pointer events - a hovered child attributes the hover to its accessible parent`() {
84+
enablePointerEvents()
85+
val parent = hoverableView()
86+
val overlay = ReactViewGroup(parent.context)
87+
parent.addView(overlay)
88+
overlay.layout(0, 0, 100, 100)
89+
90+
assertThat(overlay.onHoverEvent(hoverEvent(MotionEvent.ACTION_HOVER_ENTER, 10f))).isTrue()
91+
92+
assertThat(parent.sentAccessibilityEvents)
93+
.containsExactly(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER)
94+
}
95+
96+
@Test
97+
fun `hover with pointer events - a child with pointerEvents none does not block the parent hover`() {
98+
enablePointerEvents()
99+
val parent = hoverableView()
100+
val overlay = ReactViewGroup(parent.context).apply { pointerEvents = PointerEvents.NONE }
101+
parent.addView(overlay)
102+
overlay.layout(0, 0, 100, 100)
103+
104+
assertThat(
105+
parent.dispatchGenericMotionEvent(hoverEvent(MotionEvent.ACTION_HOVER_MOVE, 10f)),
106+
)
107+
.isTrue()
108+
109+
assertThat(parent.sentAccessibilityEvents)
110+
.containsExactly(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER)
111+
}
112+
113+
@Test
114+
fun `hover with pointer events - a child under the pointer keeps the parent hover silent`() {
115+
enablePointerEvents()
116+
val parent = hoverableView()
117+
val label = View(parent.context)
118+
parent.addView(label)
119+
label.layout(0, 0, 50, 50)
120+
121+
parent.onHoverEvent(hoverEvent(MotionEvent.ACTION_HOVER_MOVE, 10f))
122+
assertThat(parent.sentAccessibilityEvents).isEmpty()
123+
124+
parent.onHoverEvent(hoverEvent(MotionEvent.ACTION_HOVER_MOVE, 80f))
125+
assertThat(parent.sentAccessibilityEvents)
126+
.containsExactly(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER)
127+
}
128+
129+
@Test
130+
fun `hover with pointer events - a view that cannot be a touch target sends no hover events`() {
131+
enablePointerEvents()
132+
val rvg = hoverableView()
133+
rvg.pointerEvents = PointerEvents.BOX_NONE
134+
135+
assertThat(rvg.onHoverEvent(hoverEvent(MotionEvent.ACTION_HOVER_ENTER, 10f))).isFalse()
136+
137+
assertThat(rvg.sentAccessibilityEvents).isEmpty()
138+
}
139+
140+
@Test
141+
fun `hover with pointer events - a view with no accessible ancestor sends enter once it becomes accessible`() {
142+
enablePointerEvents()
143+
val rvg = AccessibilityEventRecordingViewGroup(context)
144+
rvg.layout(0, 0, 100, 100)
145+
146+
rvg.onHoverEvent(hoverEvent(MotionEvent.ACTION_HOVER_MOVE, 10f))
147+
assertThat(rvg.sentAccessibilityEvents).isEmpty()
148+
149+
rvg.contentDescription = "Button"
150+
rvg.onHoverEvent(hoverEvent(MotionEvent.ACTION_HOVER_MOVE, 20f))
151+
rvg.onHoverEvent(hoverEvent(MotionEvent.ACTION_HOVER_EXIT, 20f))
152+
153+
assertThat(rvg.sentAccessibilityEvents)
154+
.containsExactly(
155+
AccessibilityEvent.TYPE_VIEW_HOVER_ENTER,
156+
AccessibilityEvent.TYPE_VIEW_HOVER_EXIT,
157+
)
158+
}
159+
160+
@Test
161+
fun `hover with pointer events - hover exit goes to the view that got hover enter`() {
162+
enablePointerEvents()
163+
val parent = hoverableView()
164+
val overlay = AccessibilityEventRecordingViewGroup(parent.context)
165+
parent.addView(overlay)
166+
overlay.layout(0, 0, 100, 100)
167+
168+
overlay.onHoverEvent(hoverEvent(MotionEvent.ACTION_HOVER_MOVE, 10f))
169+
overlay.contentDescription = "Overlay"
170+
overlay.onHoverEvent(hoverEvent(MotionEvent.ACTION_HOVER_EXIT, 10f))
171+
172+
assertThat(parent.sentAccessibilityEvents)
173+
.containsExactly(
174+
AccessibilityEvent.TYPE_VIEW_HOVER_ENTER,
175+
AccessibilityEvent.TYPE_VIEW_HOVER_EXIT,
176+
)
177+
assertThat(overlay.sentAccessibilityEvents).isEmpty()
178+
}
179+
180+
@Test
181+
fun `hover with pointer events - recycling a hovered view sends the pending hover exit`() {
182+
enablePointerEvents()
183+
val parent = hoverableView()
184+
val overlay = ReactViewGroup(parent.context)
185+
parent.addView(overlay)
186+
overlay.layout(0, 0, 100, 100)
187+
188+
overlay.onHoverEvent(hoverEvent(MotionEvent.ACTION_HOVER_MOVE, 10f))
189+
overlay.recycleView()
190+
191+
assertThat(parent.sentAccessibilityEvents)
192+
.containsExactly(
193+
AccessibilityEvent.TYPE_VIEW_HOVER_ENTER,
194+
AccessibilityEvent.TYPE_VIEW_HOVER_EXIT,
195+
)
196+
}
197+
198+
@Test
199+
fun `hover with pointer events - detaching a hovered view sends the pending hover exit`() {
200+
enablePointerEvents()
201+
val parent = hoverableView()
202+
val overlay = ReactViewGroup(parent.context)
203+
parent.addView(overlay)
204+
overlay.layout(0, 0, 100, 100)
205+
206+
overlay.onHoverEvent(hoverEvent(MotionEvent.ACTION_HOVER_MOVE, 10f))
207+
parent.removeView(overlay)
208+
209+
assertThat(parent.sentAccessibilityEvents)
210+
.containsExactly(
211+
AccessibilityEvent.TYPE_VIEW_HOVER_ENTER,
212+
AccessibilityEvent.TYPE_VIEW_HOVER_EXIT,
213+
)
214+
}
215+
39216
@Test
40217
fun `View clipping - ensure allChildren properly resizes when adding views in sequence`() {
41218
val rvg = ReactViewGroup(context)
@@ -278,6 +455,35 @@ class ReactViewGroupTest {
278455
}
279456

280457
private fun elevatedChild(): View = View(context).apply { elevation = 10f }
458+
459+
private fun enablePointerEvents() {
460+
@Suppress("DEPRECATION")
461+
ReactFeatureFlags.dispatchPointerEvents = true
462+
}
463+
464+
// Accessibility hover events are only sent from views attached to a window.
465+
private fun hoverableView(): AccessibilityEventRecordingViewGroup {
466+
val activity = Robolectric.buildActivity(Activity::class.java).setup().get()
467+
val view =
468+
AccessibilityEventRecordingViewGroup(activity).apply { contentDescription = "Button" }
469+
activity.setContentView(view)
470+
view.layout(0, 0, 100, 100)
471+
return view
472+
}
473+
474+
private fun hoverEvent(action: Int, position: Float): MotionEvent =
475+
MotionEvent.obtain(0, 0, action, position, position, 0).apply {
476+
source = InputDevice.SOURCE_MOUSE
477+
}
478+
}
479+
480+
private class AccessibilityEventRecordingViewGroup(context: Context) : ReactViewGroup(context) {
481+
val sentAccessibilityEvents = mutableListOf<Int>()
482+
483+
override fun sendAccessibilityEvent(eventType: Int) {
484+
sentAccessibilityEvents.add(eventType)
485+
super.sendAccessibilityEvent(eventType)
486+
}
281487
}
282488

283489
class TestView(context: Context, yPos: Int) : View(context) {

0 commit comments

Comments
 (0)