mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-05-01 06:22:39 +08:00
Fix Nodes not having correct view managers externally
Reviewed By: ahmedre Differential Revision: D4927963 fbshipit-source-id: f4993aa45f6313e814d03141f828d37eafade059
This commit is contained in:
committed by
Facebook Github Bot
parent
6272ef87bc
commit
c8bb422882
@@ -9,11 +9,6 @@
|
||||
|
||||
package com.facebook.react.flat;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.facebook.infer.annotation.Assertions;
|
||||
import com.facebook.react.bridge.Callback;
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
@@ -28,6 +23,12 @@ import com.facebook.react.uimanager.ViewManagerRegistry;
|
||||
import com.facebook.react.uimanager.events.EventDispatcher;
|
||||
import com.facebook.yoga.YogaDirection;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* FlatUIImplementation builds on top of UIImplementation and allows pre-creating everything
|
||||
* required for drawing (DrawCommands) and touching (NodeRegions) views in background thread
|
||||
@@ -35,15 +36,66 @@ import com.facebook.yoga.YogaDirection;
|
||||
*/
|
||||
public class FlatUIImplementation extends UIImplementation {
|
||||
|
||||
private static final Map<String, Class<? extends ViewManager>> flatManagerClassMap;
|
||||
|
||||
static {
|
||||
flatManagerClassMap = new HashMap<>();
|
||||
flatManagerClassMap.put(RCTViewManager.REACT_CLASS, RCTViewManager.class);
|
||||
flatManagerClassMap.put(RCTTextManager.REACT_CLASS, RCTTextManager.class);
|
||||
flatManagerClassMap.put(RCTRawTextManager.REACT_CLASS, RCTRawTextManager.class);
|
||||
flatManagerClassMap.put(RCTVirtualTextManager.REACT_CLASS, RCTVirtualTextManager.class);
|
||||
flatManagerClassMap.put(RCTTextInlineImageManager.REACT_CLASS, RCTTextInlineImageManager.class);
|
||||
flatManagerClassMap.put(RCTImageViewManager.REACT_CLASS, RCTImageViewManager.class);
|
||||
flatManagerClassMap.put(RCTTextInputManager.REACT_CLASS, RCTTextInputManager.class);
|
||||
flatManagerClassMap.put(RCTViewPagerManager.REACT_CLASS, RCTViewPagerManager.class);
|
||||
flatManagerClassMap.put(FlatARTSurfaceViewManager.REACT_CLASS, FlatARTSurfaceViewManager.class);
|
||||
flatManagerClassMap.put(RCTModalHostManager.REACT_CLASS, RCTModalHostManager.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the map of view managers, checking that the managers FlatUI requires are correctly
|
||||
* overriden.
|
||||
*/
|
||||
private static Map<String, ViewManager> buildViewManagerMap(List<ViewManager> viewManagers) {
|
||||
Map<String, ViewManager> viewManagerMap = new HashMap<>();
|
||||
for (ViewManager viewManager : viewManagers) {
|
||||
viewManagerMap.put(viewManager.getName(), viewManager);
|
||||
}
|
||||
for (Map.Entry<String, Class<? extends ViewManager>> entry : flatManagerClassMap.entrySet()) {
|
||||
String name = entry.getKey();
|
||||
ViewManager maybeFlatViewManager = viewManagerMap.get(name);
|
||||
if (maybeFlatViewManager == null) {
|
||||
// We don't have a view manager for this name in the package, no need to add one.
|
||||
continue;
|
||||
}
|
||||
|
||||
Class<? extends ViewManager> flatClazz = entry.getValue();
|
||||
if (maybeFlatViewManager.getClass() != flatClazz) {
|
||||
// If we have instances that have flat equivalents, override them.
|
||||
try {
|
||||
viewManagerMap.put(name, flatClazz.newInstance());
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException("Unable to access flat class for " + name, e);
|
||||
} catch (InstantiationException e) {
|
||||
throw new RuntimeException("Unable to instantiate flat class for " + name, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
return viewManagerMap;
|
||||
}
|
||||
|
||||
public static FlatUIImplementation createInstance(
|
||||
ReactApplicationContext reactContext,
|
||||
List<ViewManager> viewManagers,
|
||||
EventDispatcher eventDispatcher,
|
||||
boolean memoryImprovementEnabled) {
|
||||
|
||||
RCTImageViewManager rctImageViewManager = findRCTImageManager(viewManagers);
|
||||
if (rctImageViewManager != null) {
|
||||
Object callerContext = rctImageViewManager.getCallerContext();
|
||||
Map<String, ViewManager> viewManagerMap = buildViewManagerMap(viewManagers);
|
||||
|
||||
RCTImageViewManager imageViewManager =
|
||||
(RCTImageViewManager) viewManagerMap.get(RCTImageViewManager.REACT_CLASS);
|
||||
if (imageViewManager != null) {
|
||||
Object callerContext = imageViewManager.getCallerContext();
|
||||
if (callerContext != null) {
|
||||
RCTImageView.setCallerContext(callerContext);
|
||||
}
|
||||
@@ -52,15 +104,15 @@ public class FlatUIImplementation extends UIImplementation {
|
||||
|
||||
TypefaceCache.setAssetManager(reactContext.getAssets());
|
||||
|
||||
ViewManagerRegistry viewManagerRegistry = new ViewManagerRegistry(viewManagers);
|
||||
ViewManagerRegistry viewManagerRegistry = new ViewManagerRegistry(viewManagerMap);
|
||||
FlatNativeViewHierarchyManager nativeViewHierarchyManager = new FlatNativeViewHierarchyManager(
|
||||
viewManagerRegistry);
|
||||
viewManagerRegistry);
|
||||
FlatUIViewOperationQueue operationsQueue = new FlatUIViewOperationQueue(
|
||||
reactContext,
|
||||
nativeViewHierarchyManager);
|
||||
reactContext,
|
||||
nativeViewHierarchyManager);
|
||||
return new FlatUIImplementation(
|
||||
reactContext,
|
||||
rctImageViewManager,
|
||||
imageViewManager,
|
||||
viewManagerRegistry,
|
||||
operationsQueue,
|
||||
eventDispatcher,
|
||||
@@ -126,9 +178,9 @@ public class FlatUIImplementation extends UIImplementation {
|
||||
|
||||
@Override
|
||||
protected void handleCreateView(
|
||||
ReactShadowNode cssNode,
|
||||
int rootViewTag,
|
||||
@Nullable ReactStylesDiffMap styles) {
|
||||
ReactShadowNode cssNode,
|
||||
int rootViewTag,
|
||||
@Nullable ReactStylesDiffMap styles) {
|
||||
if (cssNode instanceof FlatShadowNode) {
|
||||
FlatShadowNode node = (FlatShadowNode) cssNode;
|
||||
|
||||
@@ -146,9 +198,9 @@ public class FlatUIImplementation extends UIImplementation {
|
||||
|
||||
@Override
|
||||
protected void handleUpdateView(
|
||||
ReactShadowNode cssNode,
|
||||
String className,
|
||||
ReactStylesDiffMap styles) {
|
||||
ReactShadowNode cssNode,
|
||||
String className,
|
||||
ReactStylesDiffMap styles) {
|
||||
if (cssNode instanceof FlatShadowNode) {
|
||||
FlatShadowNode node = (FlatShadowNode) cssNode;
|
||||
|
||||
@@ -164,12 +216,12 @@ public class FlatUIImplementation extends UIImplementation {
|
||||
|
||||
@Override
|
||||
public void manageChildren(
|
||||
int viewTag,
|
||||
@Nullable ReadableArray moveFrom,
|
||||
@Nullable ReadableArray moveTo,
|
||||
@Nullable ReadableArray addChildTags,
|
||||
@Nullable ReadableArray addAtIndices,
|
||||
@Nullable ReadableArray removeFrom) {
|
||||
int viewTag,
|
||||
@Nullable ReadableArray moveFrom,
|
||||
@Nullable ReadableArray moveTo,
|
||||
@Nullable ReadableArray addChildTags,
|
||||
@Nullable ReadableArray addAtIndices,
|
||||
@Nullable ReadableArray removeFrom) {
|
||||
|
||||
ReactShadowNode parentNode = resolveShadowNode(viewTag);
|
||||
|
||||
@@ -182,8 +234,8 @@ public class FlatUIImplementation extends UIImplementation {
|
||||
|
||||
@Override
|
||||
public void setChildren(
|
||||
int viewTag,
|
||||
ReadableArray children) {
|
||||
int viewTag,
|
||||
ReadableArray children) {
|
||||
|
||||
ReactShadowNode parentNode = resolveShadowNode(viewTag);
|
||||
|
||||
@@ -244,13 +296,13 @@ public class FlatUIImplementation extends UIImplementation {
|
||||
|
||||
FlatUIViewOperationQueue operationsQueue = mStateBuilder.getOperationsQueue();
|
||||
operationsQueue.enqueueMeasureVirtualView(
|
||||
node.getReactTag(),
|
||||
xInParent / parentWidth,
|
||||
yInParent / parentHeight,
|
||||
width / parentWidth,
|
||||
height / parentHeight,
|
||||
relativeToWindow,
|
||||
callback);
|
||||
node.getReactTag(),
|
||||
xInParent / parentWidth,
|
||||
yInParent / parentHeight,
|
||||
width / parentWidth,
|
||||
height / parentHeight,
|
||||
relativeToWindow,
|
||||
callback);
|
||||
}
|
||||
|
||||
private void ensureMountsToViewAndBackingViewIsCreated(int reactTag) {
|
||||
@@ -304,10 +356,10 @@ public class FlatUIImplementation extends UIImplementation {
|
||||
* preparing elements in moveFrom to be re-added at proper index.
|
||||
*/
|
||||
private void removeChildren(
|
||||
ReactShadowNode parentNode,
|
||||
@Nullable ReadableArray moveFrom,
|
||||
@Nullable ReadableArray moveTo,
|
||||
@Nullable ReadableArray removeFrom) {
|
||||
ReactShadowNode parentNode,
|
||||
@Nullable ReadableArray moveFrom,
|
||||
@Nullable ReadableArray moveTo,
|
||||
@Nullable ReadableArray removeFrom) {
|
||||
|
||||
int prevIndex = Integer.MAX_VALUE;
|
||||
|
||||
@@ -388,7 +440,7 @@ public class FlatUIImplementation extends UIImplementation {
|
||||
if (tmpNode instanceof FlatShadowNode) {
|
||||
FlatShadowNode flatTmpNode = (FlatShadowNode) tmpNode;
|
||||
if (flatTmpNode.mountsToView() && flatTmpNode.isBackingViewCreated() &&
|
||||
flatTmpNode.getParent() != null) {
|
||||
flatTmpNode.getParent() != null) {
|
||||
tag = flatTmpNode.getReactTag();
|
||||
break;
|
||||
}
|
||||
@@ -418,9 +470,9 @@ public class FlatUIImplementation extends UIImplementation {
|
||||
* Adds all children from addChildTags and moveFrom/moveTo.
|
||||
*/
|
||||
private void addChildren(
|
||||
ReactShadowNode parentNode,
|
||||
@Nullable ReadableArray addChildTags,
|
||||
@Nullable ReadableArray addAtIndices) {
|
||||
ReactShadowNode parentNode,
|
||||
@Nullable ReadableArray addChildTags,
|
||||
@Nullable ReadableArray addAtIndices) {
|
||||
|
||||
int prevIndex = -1;
|
||||
|
||||
@@ -485,12 +537,12 @@ public class FlatUIImplementation extends UIImplementation {
|
||||
* Removes a child from parent, verifying that we are removing in descending order.
|
||||
*/
|
||||
private static ReactShadowNode removeChildAt(
|
||||
ReactShadowNode parentNode,
|
||||
int index,
|
||||
int prevIndex) {
|
||||
ReactShadowNode parentNode,
|
||||
int index,
|
||||
int prevIndex) {
|
||||
if (index >= prevIndex) {
|
||||
throw new RuntimeException(
|
||||
"Invariant failure, needs sorting! " + index + " >= " + prevIndex);
|
||||
"Invariant failure, needs sorting! " + index + " >= " + prevIndex);
|
||||
}
|
||||
|
||||
return parentNode.removeChildAt(index);
|
||||
@@ -500,13 +552,13 @@ public class FlatUIImplementation extends UIImplementation {
|
||||
* Adds a child to parent, verifying that we are adding in ascending order.
|
||||
*/
|
||||
private static void addChildAt(
|
||||
ReactShadowNode parentNode,
|
||||
ReactShadowNode childNode,
|
||||
int index,
|
||||
int prevIndex) {
|
||||
ReactShadowNode parentNode,
|
||||
ReactShadowNode childNode,
|
||||
int index,
|
||||
int prevIndex) {
|
||||
if (index <= prevIndex) {
|
||||
throw new RuntimeException(
|
||||
"Invariant failure, needs sorting! " + index + " <= " + prevIndex);
|
||||
"Invariant failure, needs sorting! " + index + " <= " + prevIndex);
|
||||
}
|
||||
|
||||
parentNode.addChildAt(childNode, index);
|
||||
@@ -520,9 +572,9 @@ public class FlatUIImplementation extends UIImplementation {
|
||||
|
||||
@Override
|
||||
protected void applyUpdatesRecursive(
|
||||
ReactShadowNode cssNode,
|
||||
float absoluteX,
|
||||
float absoluteY) {
|
||||
ReactShadowNode cssNode,
|
||||
float absoluteX,
|
||||
float absoluteY) {
|
||||
mStateBuilder.applyUpdates((FlatRootShadowNode) cssNode);
|
||||
}
|
||||
|
||||
@@ -551,18 +603,8 @@ public class FlatUIImplementation extends UIImplementation {
|
||||
|
||||
FlatUIViewOperationQueue operationsQueue = mStateBuilder.getOperationsQueue();
|
||||
operationsQueue.enqueueSetJSResponder(
|
||||
node == null ? tag : node.getReactTag(),
|
||||
possiblyVirtualReactTag,
|
||||
blockNativeResponder);
|
||||
}
|
||||
|
||||
private static @Nullable RCTImageViewManager findRCTImageManager(List<ViewManager> viewManagers) {
|
||||
for (int i = 0, size = viewManagers.size(); i != size; ++i) {
|
||||
if (viewManagers.get(i) instanceof RCTImageViewManager) {
|
||||
return (RCTImageViewManager) viewManagers.get(i);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
node == null ? tag : node.getReactTag(),
|
||||
possiblyVirtualReactTag,
|
||||
blockNativeResponder);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user