Consider local visible rect and translation when calcauating view bounds

Summary: In general we want a child to know its position without having be overriden by it parent via offset child. Therefore we need to consider translation and the result of getLocalVisibleRect. Despite its name it seems to be an offset from its normal bounds after scrolling in certain types of scroll views.

Reviewed By: lblasa

Differential Revision: D40021836

fbshipit-source-id: 4ae9a4b202d7ba201cdb8b6bc8f13e94baf2b37e
This commit is contained in:
Luke De Feo
2022-10-10 04:13:06 -07:00
committed by Facebook GitHub Bot
parent 9f6ba6cf8c
commit 9cbf35dea8

View File

@@ -10,6 +10,7 @@ package com.facebook.flipper.plugins.uidebugger.descriptors
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.os.Build import android.os.Build
@@ -28,8 +29,15 @@ object ViewDescriptor : ChainedDescriptor<View>() {
override fun onGetName(node: View): String = node.javaClass.simpleName override fun onGetName(node: View): String = node.javaClass.simpleName
override fun onGetBounds(node: View): Bounds = override fun onGetBounds(node: View): Bounds {
Bounds(node.left, node.top, node.width, node.height) val localVisible = Rect()
node.getLocalVisibleRect(localVisible)
return Bounds(
node.left - localVisible.left + node.translationX.toInt(),
node.top - localVisible.top + node.translationY.toInt(),
node.width,
node.height)
}
override fun onGetTags(node: View): Set<String> = BaseTags.NativeAndroid override fun onGetTags(node: View): Set<String> = BaseTags.NativeAndroid
@@ -37,8 +45,6 @@ object ViewDescriptor : ChainedDescriptor<View>() {
node: View, node: View,
attributeSections: MutableMap<SectionName, InspectableObject> attributeSections: MutableMap<SectionName, InspectableObject>
) { ) {
val positionOnScreen = IntArray(2)
node.getLocationOnScreen(positionOnScreen)
val props = mutableMapOf<String, Inspectable>() val props = mutableMapOf<String, Inspectable>()
props["height"] = InspectableValue.Number(node.height, mutable = true) props["height"] = InspectableValue.Number(node.height, mutable = true)
@@ -92,12 +98,27 @@ object ViewDescriptor : ChainedDescriptor<View>() {
"x" to InspectableValue.Number(node.pivotX, mutable = true), "x" to InspectableValue.Number(node.pivotX, mutable = true),
"y" to InspectableValue.Number(node.pivotY, mutable = true))) "y" to InspectableValue.Number(node.pivotY, mutable = true)))
val positionOnScreen = IntArray(2)
node.getLocationOnScreen(positionOnScreen)
props["globalPosition"] = props["globalPosition"] =
InspectableObject( InspectableObject(
mapOf( mapOf(
"x" to InspectableValue.Number(positionOnScreen[0], mutable = false), "x" to InspectableValue.Number(positionOnScreen[0], mutable = false),
"y" to InspectableValue.Number(positionOnScreen[1], mutable = false))) "y" to InspectableValue.Number(positionOnScreen[1], mutable = false)))
val localVisible = Rect()
node.getLocalVisibleRect(localVisible)
props["localVisible"] =
InspectableObject(
mapOf(
"x" to InspectableValue.Number(localVisible.left, mutable = true),
"y" to InspectableValue.Number(node.top, mutable = true),
"width" to InspectableValue.Number(node.width, mutable = true),
"height" to InspectableValue.Number(node.height, mutable = true)),
)
attributeSections["View"] = InspectableObject(props.toMap()) attributeSections["View"] = InspectableObject(props.toMap())
} }