From d953acf345a33371f7e17a9b7ccd1fe4c50895d5 Mon Sep 17 00:00:00 2001 From: Swordsman-Inaction Date: Fri, 24 Mar 2017 16:45:00 +0800 Subject: [PATCH 1/2] Add viewIsDescendantOf for Android UIManager --- .../react/uimanager/ReactShadowNode.java | 17 +++++++++++++++++ .../react/uimanager/UIImplementation.java | 16 ++++++++++++++++ .../react/uimanager/UIManagerModule.java | 11 +++++++++++ 3 files changed, 44 insertions(+) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactShadowNode.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactShadowNode.java index d8c34346f261fd..41fc31fe018348 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactShadowNode.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactShadowNode.java @@ -445,6 +445,23 @@ public final int getTotalNativeChildren() { return mTotalNativeChildren; } + public boolean isDescendantOf(ReactShadowNode ancestorNode) { + ReactShadowNode parentNode = getParent(); + + boolean isDescendant = false; + + while (parentNode != null) { + if (parentNode == ancestorNode) { + isDescendant = true; + break; + } else { + parentNode = parentNode.getParent(); + } + } + + return isDescendant; + } + /** * Returns the offset within the native children owned by all layout-only nodes in the subtree * rooted at this node for the given child. Put another way, this returns the number of native diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java index ab6e6f2aa998e3..af444cc6e5211b 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java @@ -469,6 +469,22 @@ public void findSubviewIn(int reactTag, float targetX, float targetY, Callback c mOperationsQueue.enqueueFindTargetForTouch(reactTag, targetX, targetY, callback); } + /** + * Check if the first shadow node is the descendant of the second shadow node + */ + public void viewIsDescendantOf( + final int reactTag, + final int ancestorReactTag, + final Callback callback) { + ReactShadowNode node = mShadowNodeRegistry.getNode(reactTag); + ReactShadowNode ancestorNode = mShadowNodeRegistry.getNode(ancestorReactTag); + if (node == null || ancestorNode == null) { + callback.invoke(false); + return; + } + callback.invoke(node.isDescendantOf(ancestorNode)); + } + /** * Determines the location on screen, width, and height of the given view relative to the root * view and returns the values via an async callback. diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java index 702a6b539b8581..d4b4e782463644 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java @@ -408,6 +408,17 @@ public void findSubviewIn( callback); } + /** + * Check if the first shadow node is the descendant of the second shadow node + */ + @ReactMethod + public void viewIsDescendantOf( + final int reactTag, + final int ancestorReactTag, + final Callback callback) { + mUIImplementation.viewIsDescendantOf(reactTag, ancestorReactTag, callback); + } + /** * Registers a new Animation that can then be added to a View using {@link #addAnimation}. */ From c47ae32d8c325ce44884946e4041a433a68b182a Mon Sep 17 00:00:00 2001 From: Swordsman-Inaction Date: Wed, 5 Apr 2017 15:08:00 +0800 Subject: [PATCH 2/2] Fix code style --- .../java/com/facebook/react/uimanager/UIImplementation.java | 6 +++--- .../java/com/facebook/react/uimanager/UIManagerModule.java | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java index af444cc6e5211b..6fc29266feb477 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java @@ -473,9 +473,9 @@ public void findSubviewIn(int reactTag, float targetX, float targetY, Callback c * Check if the first shadow node is the descendant of the second shadow node */ public void viewIsDescendantOf( - final int reactTag, - final int ancestorReactTag, - final Callback callback) { + final int reactTag, + final int ancestorReactTag, + final Callback callback) { ReactShadowNode node = mShadowNodeRegistry.getNode(reactTag); ReactShadowNode ancestorNode = mShadowNodeRegistry.getNode(ancestorReactTag); if (node == null || ancestorNode == null) { diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java index d4b4e782463644..96f2c2966f3091 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java @@ -413,9 +413,9 @@ public void findSubviewIn( */ @ReactMethod public void viewIsDescendantOf( - final int reactTag, - final int ancestorReactTag, - final Callback callback) { + final int reactTag, + final int ancestorReactTag, + final Callback callback) { mUIImplementation.viewIsDescendantOf(reactTag, ancestorReactTag, callback); }