From a0ee7741591445d5590034c74043e5ece3dcbd48 Mon Sep 17 00:00:00 2001 From: Luke De Feo Date: Tue, 13 Sep 2022 11:05:42 -0700 Subject: [PATCH] Fix active child Summary: The active child was only being passed for the native scan full traversal, Now we have it for the partial traversal also Reviewed By: lblasa Differential Revision: D39466933 fbshipit-source-id: a0e281e4f9a21bf2edd12f18ecdb68a29ead3476 --- .../uidebugger/observers/TreeObserver.kt | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/android/src/main/java/com/facebook/flipper/plugins/uidebugger/observers/TreeObserver.kt b/android/src/main/java/com/facebook/flipper/plugins/uidebugger/observers/TreeObserver.kt index e451e0861..c7931bbe5 100644 --- a/android/src/main/java/com/facebook/flipper/plugins/uidebugger/observers/TreeObserver.kt +++ b/android/src/main/java/com/facebook/flipper/plugins/uidebugger/observers/TreeObserver.kt @@ -132,6 +132,11 @@ fun Any.identityHashCode(): HashCode { return System.identityHashCode(this) } +/** + * This will traverse the layout hierarchy untill it sees a node that has an observer registered for + * it. The first item in the pair is the visited nodes The second item are any observable roots + * discovered + */ class PartialLayoutTraversal( private val descriptorRegister: DescriptorRegister, private val treeObserverfactory: TreeObserverFactory, @@ -142,7 +147,7 @@ class PartialLayoutTraversal( fun traverse(root: Any): Pair, List> { val visited = mutableListOf() - val skipped = mutableListOf() + val observableRoots = mutableListOf() val stack = mutableListOf() stack.add(root) @@ -154,7 +159,7 @@ class PartialLayoutTraversal( // if we encounter a node that has it own observer, dont traverse if (node != root && treeObserverfactory.hasObserverFor(node)) { - skipped.add(node) + observableRoots.add(node) continue } @@ -164,13 +169,24 @@ class PartialLayoutTraversal( descriptor.getChildren(node, children) val childrenIds = mutableListOf() + val activeChild = descriptor.getActiveChild(node) + for (child in children) { // it might make sense one day to remove id from the descriptor since its always the // hash code val childDescriptor = descriptorRegister.descriptorForClassUnsafe(child::class.java).asAny() childrenIds.add(childDescriptor.getId(child)) - stack.add(child) + // if there is an active child then dont traverse it + if (activeChild == null) { + stack.add(child) + } + } + var activeChildId: String? = null + if (activeChild != null) { + stack.add(activeChild) + activeChildId = + descriptorRegister.descriptorForClassUnsafe(activeChild.javaClass).getId(activeChild) } val attributes = mutableMapOf() @@ -178,12 +194,17 @@ class PartialLayoutTraversal( // NOTE active child null here visited.add( - Node(descriptor.getId(node), descriptor.getName(node), attributes, childrenIds, null)) + Node( + descriptor.getId(node), + descriptor.getName(node), + attributes, + childrenIds, + activeChildId)) } catch (exception: Exception) { Log.e(LogTag, "Error while processing node ${node.javaClass.name} ${node} ", exception) } } - return Pair(visited, skipped) + return Pair(visited, observableRoots) } }