Skip to content

Commit

Permalink
Force window manager to render correctly inside content for accessibi…
Browse files Browse the repository at this point in the history
…lity
  • Loading branch information
geoff-powell committed Oct 16, 2024
1 parent f54d9be commit aadaed0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package app.cash.paparazzi.accessibility

import android.annotation.SuppressLint
import android.graphics.Rect
import android.view.Gravity
import android.view.View
import android.view.View.VISIBLE
import android.view.ViewGroup
Expand All @@ -39,11 +41,22 @@ public class AccessibilityRenderExtension : RenderExtension {
override fun renderView(
contentView: View
): View {
// Window Manager needed to access accessibility elements for views that draw to other windows.
val windowManager = contentView.context.getSystemService(WindowManager::class.java)

return LinearLayout(contentView.context).apply {
orientation = LinearLayout.HORIZONTAL
weightSum = 2f
layoutParams = ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)

viewTreeObserver.addOnGlobalLayoutListener {
// Window manager is rendered at the root of the view hierarchy, rendering it full width.
// We need to restrict it when taking accessibility snapshots.
val windowManagerRootView = (windowManager as WindowManagerImpl).currentRootView
val renderedContentWidth = contentView.measuredWidth
windowManagerRootView.layoutParams = FrameLayout.LayoutParams(renderedContentWidth, MATCH_PARENT, Gravity.START)
}

val overlay = AccessibilityOverlayView(context).apply {
addView(contentView, FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT))
}
Expand All @@ -55,8 +68,6 @@ public class AccessibilityRenderExtension : RenderExtension {
addView(overlayDetailsView, LinearLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT, 1f))

OneShotPreDrawListener.add(this) {
// Window Manager needed to access accessibility elements for views that draw to other windows
val windowManager = context.getSystemService(WindowManager::class.java)
val windowManagerRootView = (windowManager as WindowManagerImpl).currentRootView

val elements = buildList {
Expand All @@ -70,6 +81,7 @@ public class AccessibilityRenderExtension : RenderExtension {
}
}

@SuppressLint("VisibleForTests")
private fun View.processAccessibleChildren(
processElement: (AccessibilityElement) -> Unit
) {
Expand Down

0 comments on commit aadaed0

Please sign in to comment.