Skip to content

Commit 024ac81

Browse files
cipolleschifacebook-github-bot
authored andcommitted
Send accessibility hover events from ReactViewGroup when pointer events are enabled (#59992)
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 Reviewed By: shwanton Differential Revision: D131164392
1 parent bbfd391 commit 024ac81

3 files changed

Lines changed: 290 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: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@ 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
1922
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests
2023
import com.facebook.react.uimanager.HasElevatedDescendantCache
24+
import com.facebook.react.uimanager.PointerEvents
2125
import org.assertj.core.api.Assertions.assertThat
26+
import org.junit.After
2227
import org.junit.Before
2328
import org.junit.Test
2429
import org.junit.runner.RunWith
@@ -36,6 +41,177 @@ class ReactViewGroupTest {
3641
context = Robolectric.buildActivity(Activity::class.java).create().get()
3742
}
3843

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

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

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

0 commit comments

Comments
 (0)