下面列出了怎么用com.facebook.react.uimanager.ReactShadowNode的API类实例代码及写法,或者点击链接到github查看源代码。
private void enqueueUpdateProperties(ReactShadowNode newNode, ReactShadowNode prevNode) {
int reactTag = newNode.getReactTag();
if (DEBUG) {
FLog.d(
TAG,
"manageChildren.enqueueUpdateProperties " +
"\n\ttag: " + reactTag +
"\n\tviewClass: " + newNode.getViewClass() +
"\n\tinstanceHandle: " + newNode.getInstanceHandle() +
"\n\tnewProps: " + newNode.getNewProps());
}
if (prevNode != null) {
newNode.updateScreenLayout(prevNode);
}
if (newNode.getNewProps() != null) {
uiViewOperationQueue.enqueueUpdateProperties(
reactTag, newNode.getViewClass(), newNode.getNewProps());
}
uiViewOperationQueue.enqueueUpdateInstanceHandle(
reactTag, newNode.getInstanceHandle());
}
/**
* @return a clone of the {@link ReactShadowNode} received by parameter. The cloned
* ReactShadowNode will contain a copy of all the internal data of the original node,
* including its children set (note that the children nodes will not be cloned).
*/
@Nullable
@DoNotStrip
public ReactShadowNode cloneNode(ReactShadowNode node) {
if (DEBUG) {
FLog.d(TAG, "cloneNode \n\tnode: " + node);
}
SystraceMessage.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
"FabricUIManager.cloneNode")
.flush();
try {
ReactShadowNode clone = node.mutableCopy(node.getInstanceHandle());
assertReactShadowNodeCopy(node, clone);
return clone;
} catch (Throwable t) {
handleException(node, t);
return null;
} finally{
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}
}
/**
* @return a clone of the {@link ReactShadowNode} received by parameter. The cloned
* ReactShadowNode will contain a copy of all the internal data of the original node, but its
* children set will be empty.
*/
@Nullable
@DoNotStrip
public ReactShadowNode cloneNodeWithNewChildren(ReactShadowNode node) {
if (DEBUG) {
FLog.d(TAG, "cloneNodeWithNewChildren \n\tnode: " + node);
}
SystraceMessage.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
"FabricUIManager.cloneNodeWithNewChildren")
.flush();
try {
ReactShadowNode clone = node.mutableCopyWithNewChildren(node.getInstanceHandle());
assertReactShadowNodeCopy(node, clone);
return clone;
} catch (Throwable t) {
handleException(node, t);
return null;
} finally{
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}
}
/**
* @return a clone of the {@link ReactShadowNode} received by parameter. The cloned
* ReactShadowNode will contain a copy of all the internal data of the original node, but its
* props will be overridden with the {@link ReadableMap} received by parameter.
*/
@Nullable
@DoNotStrip
public ReactShadowNode cloneNodeWithNewProps(
ReactShadowNode node, @Nullable ReadableNativeMap newProps) {
if (DEBUG) {
FLog.d(TAG, "cloneNodeWithNewProps \n\tnode: " + node + "\n\tprops: " + newProps);
}
SystraceMessage.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
"FabricUIManager.cloneNodeWithNewProps")
.flush();
try {
ReactShadowNode clone = node.mutableCopyWithNewProps(node.getInstanceHandle(),
newProps == null ? null : new ReactStylesDiffMap(newProps));
assertReactShadowNodeCopy(node, clone);
return clone;
} catch (Throwable t) {
handleException(node, t);
return null;
} finally{
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}
}
/**
* @return a clone of the {@link ReactShadowNode} received by parameter. The cloned
* ReactShadowNode will contain a copy of all the internal data of the original node, but its
* props will be overridden with the {@link ReadableMap} received by parameter and its
* children set will be empty.
*/
@Nullable
@DoNotStrip
public ReactShadowNode cloneNodeWithNewChildrenAndProps(
ReactShadowNode node, ReadableNativeMap newProps) {
if (DEBUG) {
FLog.d(TAG, "cloneNodeWithNewChildrenAndProps \n\tnode: " + node + "\n\tnewProps: " + newProps);
}
SystraceMessage.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
"FabricUIManager.cloneNodeWithNewChildrenAndProps")
.flush();
try {
ReactShadowNode clone =
node.mutableCopyWithNewChildrenAndProps(node.getInstanceHandle(),
newProps == null ? null : new ReactStylesDiffMap(newProps));
assertReactShadowNodeCopy(node, clone);
return clone;
} catch (Throwable t) {
handleException(node, t);
return null;
} finally{
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}
}
/**
* Appends the child {@link ReactShadowNode} to the children set of the parent {@link
* ReactShadowNode}.
*/
@Nullable
@DoNotStrip
public void appendChild(ReactShadowNode parent, ReactShadowNode child) {
if (DEBUG) {
FLog.d(TAG, "appendChild \n\tparent: " + parent + "\n\tchild: " + child);
}
SystraceMessage.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
"FabricUIManager.appendChild")
.flush();
try {
// If the child to append was already committed (child.isSealed()),
// then we add a mutation of it. In the future this will be performed by FabricJS / Fiber.
//TODO: T27926878 avoid cloning shared child
if (child.isSealed()) {
child = child.mutableCopy(child.getInstanceHandle());
}
parent.addChildAt(child, parent.getChildCount());
} catch (Throwable t) {
handleException(parent, t);
} finally{
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}
}
/**
* Updates the root view size and re-render the RN surface.
*
* //TODO: change synchronization to integrate with new #render loop.
*/
private synchronized void updateRootSize(int rootTag, int newWidth, int newHeight) {
ReactShadowNode rootNode = getRootNode(rootTag);
if (rootNode == null) {
FLog.w(
ReactConstants.TAG,
"Tried to update size of non-existent tag: " + rootTag);
return;
}
ReactShadowNode newRootNode = rootNode.mutableCopy(rootNode.getInstanceHandle());
int newWidthSpec = View.MeasureSpec.makeMeasureSpec(newWidth, View.MeasureSpec.EXACTLY);
int newHeightSpec = View.MeasureSpec.makeMeasureSpec(newHeight, View.MeasureSpec.EXACTLY);
updateRootView(newRootNode, newWidthSpec, newHeightSpec);
completeRoot(rootTag, newRootNode.getChildrenList());
}
@Test
public void testSealReactShadowNode() {
ReactRootView rootView =
new ReactRootView(RuntimeEnvironment.application.getApplicationContext());
int rootTag = mFabricUIManager.addRootView(rootView);
String viewClass = ReactViewManager.REACT_CLASS;
ReactShadowNode container = mFabricUIManager.createNode(6, viewClass, rootTag, null, randomInstanceHandle());
List<ReactShadowNode> childSet = mFabricUIManager.createChildSet(rootTag);
mFabricUIManager.appendChildToSet(childSet, container);
assertThat(container.isSealed()).isFalse();
mFabricUIManager.completeRoot(rootTag, childSet);
assertThat(container.isSealed()).isTrue();
}
/**
* Tests that cloned text nodes will not share measure functions
*/
@Test
public void testTextMutableClone() {
ReactRootView rootView =
new ReactRootView(RuntimeEnvironment.application.getApplicationContext());
int rootTag = mFabricUIManager.addRootView(rootView);
ReactShadowNode text =
mFabricUIManager.createNode(0, ReactTextViewManager.REACT_CLASS, rootTag, null, randomInstanceHandle());
assertThat(text.isMeasureDefined()).isTrue();
ReactShadowNode textCopy = text.mutableCopy(randomInstanceHandle());
assertThat(textCopy.isMeasureDefined()).isTrue();
textCopy.setStyleWidth(200);
text.onBeforeLayout();
text.calculateLayout();
textCopy.onBeforeLayout();
textCopy.calculateLayout();
assertThat(text.getLayoutWidth()).isNotEqualTo(textCopy.getLayoutWidth());
}
private void assertSameFields(ReactShadowNode node1, ReactShadowNode node2) {
assertThat(node1.getReactTag()).isEqualTo(node2.getReactTag());
assertThat(node1.getViewClass()).isEqualTo(node2.getViewClass());
assertThat(node2.getParent()).isNull();
assertThat(node1.getThemedContext()).isEqualTo(node2.getThemedContext());
assertThat(node1.isVirtual()).isEqualTo(node2.isVirtual());
assertThat(node1.getLayoutDirection()).isEqualTo(node2.getLayoutDirection());
assertThat(node1.getLayoutHeight()).isEqualTo(node2.getLayoutHeight());
assertThat(node1.getLayoutWidth()).isEqualTo(node2.getLayoutWidth());
assertThat(node1.getLayoutX()).isEqualTo(node2.getLayoutX());
assertThat(node1.getLayoutY()).isEqualTo(node2.getLayoutY());
for (int spacingType = Spacing.LEFT; spacingType <= Spacing.ALL; spacingType++) {
assertThat(node1.getStylePadding(spacingType)).isEqualTo(node2.getStylePadding(spacingType));
}
assertThat(node1.getStyleWidth()).isEqualTo(node2.getStyleWidth());
assertThat(node1.getStyleHeight()).isEqualTo(node2.getStyleHeight());
}
@Test
public void testSimpleHierarchy() {
ReactShadowNode parent = createNode(0);
ReactShadowNode child1 = createNode(1);
ReactShadowNode child2 = createNode(2);
addChildren(parent, child1, child2);
ReactShadowNode parentCloned = createNode(0);
ReactShadowNode child3 = createNode(3);
addChildren(parentCloned, child3, child2);
mFabricReconciler.manageChildren(parent, parentCloned);
List<ManageChildrenOperation> expectedOperations = new ArrayList<>();
expectedOperations.add(
new ManageChildrenOperation(
0,
new int[] {0, 1},
new ViewAtIndex[] {new ViewAtIndex(3, 0), new ViewAtIndex(2, 1)},
new int[] {1}));
assertThat(mMockUIViewOperationQueue.getOperations()).isEqualTo(expectedOperations);
}
@Test
public void testVirtualNodes() {
ReactShadowNode parent = createNode(0);
ReactShadowNode child1 = createVirtualNode(1);
ReactShadowNode child2 = createVirtualNode(2);
ReactShadowNode child3 = createVirtualNode(3);
addChildren(parent, child1, child2, child3);
ReactShadowNode parentCloned = createNode(0);
ReactShadowNode child4 = createVirtualNode(4);
addChildren(parentCloned, child1, child4, child3);
mFabricReconciler.manageChildren(parent, parentCloned);
List<ManageChildrenOperation> expectedOperations = new ArrayList<>();
assertThat(mMockUIViewOperationQueue.getOperations()).isEqualTo(expectedOperations);
}
@Override
public ReactShadowNode createShadowNodeInstance() {
if (CLASS_GROUP.equals(mClassName)) {
return new ARTGroupShadowNode();
} else if (CLASS_SHAPE.equals(mClassName)) {
return new ARTShapeShadowNode();
} else if (CLASS_TEXT.equals(mClassName)) {
return new ARTTextShadowNode();
} else {
throw new IllegalStateException("Unexpected type " + mClassName);
}
}
@Override
public Class<? extends ReactShadowNode> getShadowNodeClass() {
if (CLASS_GROUP.equals(mClassName)) {
return ARTGroupShadowNode.class;
} else if (CLASS_SHAPE.equals(mClassName)) {
return ARTShapeShadowNode.class;
} else if (CLASS_TEXT.equals(mClassName)) {
return ARTTextShadowNode.class;
} else {
throw new IllegalStateException("Unexpected type " + mClassName);
}
}
private void markChildrenUpdatesSeen(ReactShadowNode shadowNode) {
for (int i = 0; i < shadowNode.getChildCount(); i++) {
ReactShadowNode child = shadowNode.getChildAt(i);
child.markUpdateSeen();
markChildrenUpdatesSeen(child);
}
}
@Override
public ReactShadowNode createShadowNodeInstance() {
if (CLASS_GROUP.equals(mClassName)) {
return new ARTGroupShadowNode();
} else if (CLASS_SHAPE.equals(mClassName)) {
return new ARTShapeShadowNode();
} else if (CLASS_TEXT.equals(mClassName)) {
return new ARTTextShadowNode();
} else {
throw new IllegalStateException("Unexpected type " + mClassName);
}
}
@Override
public Class<? extends ReactShadowNode> getShadowNodeClass() {
if (CLASS_GROUP.equals(mClassName)) {
return ARTGroupShadowNode.class;
} else if (CLASS_SHAPE.equals(mClassName)) {
return ARTShapeShadowNode.class;
} else if (CLASS_TEXT.equals(mClassName)) {
return ARTTextShadowNode.class;
} else {
throw new IllegalStateException("Unexpected type " + mClassName);
}
}
private void markChildrenUpdatesSeen(ReactShadowNode shadowNode) {
for (int i = 0; i < shadowNode.getChildCount(); i++) {
ReactShadowNode child = shadowNode.getChildAt(i);
child.markUpdateSeen();
markChildrenUpdatesSeen(child);
}
}
public void manageChildren(ReactShadowNode previousRootShadowNode, ReactShadowNode newRootShadowNode) {
SystraceMessage.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
"FabricReconciler.manageChildren")
.flush();
try {
List<ReactShadowNode> prevList =
previousRootShadowNode == null ? null : previousRootShadowNode.getChildrenList();
manageChildren(newRootShadowNode, prevList, newRootShadowNode.getChildrenList());
} finally{
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}
}
/** Creates a new {@link ReactShadowNode} */
@Nullable
@DoNotStrip
public ReactShadowNode createNode(
int reactTag, String viewName, int rootTag, ReadableNativeMap props, long eventTarget) {
if (DEBUG) {
FLog.d(TAG, "createNode \n\ttag: " + reactTag +
"\n\tviewName: " + viewName +
"\n\trootTag: " + rootTag +
"\n\tprops: " + props);
}
try {
ViewManager viewManager = mViewManagerRegistry.get(viewName);
ReactShadowNode node = viewManager.createShadowNodeInstance(mReactApplicationContext);
ReactShadowNode rootNode = getRootNode(rootTag);
node.setRootTag(rootNode.getReactTag());
node.setViewClassName(viewName);
node.setInstanceHandle(eventTarget);
node.setReactTag(reactTag);
node.setThemedContext(rootNode.getThemedContext());
ReactStylesDiffMap styles = updateProps(node, props);
if (!node.isVirtual()) {
mUIViewOperationQueue.enqueueCreateView(
rootNode.getThemedContext(), reactTag, viewName, styles);
}
return node;
} catch (Throwable t) {
handleException(getRootNode(rootTag), t);
return null;
}
}
private ReactStylesDiffMap updateProps(ReactShadowNode node, @Nullable ReadableNativeMap props) {
ReactStylesDiffMap styles = null;
if (props != null) {
styles = new ReactStylesDiffMap(props);
node.updateProperties(styles);
}
return styles;
}
private void assertReactShadowNodeCopy(ReactShadowNode source, ReactShadowNode target) {
Assertions.assertCondition(
source.getClass().equals(target.getClass()),
"Found "
+ target.getClass()
+ " class when expecting: "
+ source.getClass()
+ ". Check that "
+ source.getClass()
+ " implements the copy() method correctly.");
}
/**
* @return an empty {@link List<ReactShadowNode>} that will be used to append the {@link
* ReactShadowNode} elements of the root. Typically this List will contain one element.
*/
@DoNotStrip
public List<ReactShadowNode> createChildSet(int rootTag) {
if (DEBUG) {
FLog.d(TAG, "createChildSet rootTag: " + rootTag);
}
return new ArrayList<>(1);
}
@DoNotStrip
public synchronized void completeRoot(int rootTag, @Nullable List<ReactShadowNode> childList) {
SystraceMessage.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
"FabricUIManager.completeRoot")
.flush();
try {
long startTime = SystemClock.uptimeMillis();
childList = childList == null ? new LinkedList<ReactShadowNode>() : childList;
if (DEBUG) {
FLog.d(TAG, "completeRoot rootTag: " + rootTag + ", childList: " + childList);
}
ReactShadowNode currentRootShadowNode = getRootNode(rootTag);
Assertions.assertNotNull(
currentRootShadowNode,
"Root view with tag " + rootTag + " must be added before completeRoot is called");
currentRootShadowNode = calculateDiffingAndCreateNewRootNode(currentRootShadowNode, childList);
if (DEBUG) {
FLog.d(
TAG,
"ReactShadowNodeHierarchy after diffing: " + currentRootShadowNode.getHierarchyInfo());
}
applyUpdatesRecursive(currentRootShadowNode);
mUIViewOperationQueue.dispatchViewUpdates(
mCurrentBatch++, startTime, mLastCalculateLayoutTime);
mRootShadowNodeRegistry.replaceNode(currentRootShadowNode);
} catch (Exception e) {
handleException(getRootNode(rootTag), e);
} finally{
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}
}
private void notifyOnBeforeLayoutRecursive(ReactShadowNode node) {
if (!node.hasUpdates()) {
return;
}
for (int i = 0; i < node.getChildCount(); i++) {
notifyOnBeforeLayoutRecursive(node.getChildAt(i));
}
node.onBeforeLayout();
}
private ReactShadowNode calculateDiffingAndCreateNewRootNode(
ReactShadowNode currentRootShadowNode, List<ReactShadowNode> newChildList) {
SystraceMessage.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
"FabricUIManager.calculateDiffingAndCreateNewRootNode")
.flush();
try {
ReactShadowNode newRootShadowNode = currentRootShadowNode.mutableCopyWithNewChildren(currentRootShadowNode.getInstanceHandle());
for (ReactShadowNode child : newChildList) {
appendChild(newRootShadowNode, child);
}
if (DEBUG) {
FLog.d(
TAG,
"ReactShadowNodeHierarchy before calculateLayout: " + newRootShadowNode.getHierarchyInfo());
}
notifyOnBeforeLayoutRecursive(newRootShadowNode);
calculateLayout(newRootShadowNode);
if (DEBUG) {
FLog.d(
TAG,
"ReactShadowNodeHierarchy after calculateLayout: " + newRootShadowNode.getHierarchyInfo());
}
mFabricReconciler.manageChildren(currentRootShadowNode, newRootShadowNode);
return newRootShadowNode;
} finally{
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}
}
private void calculateLayout(ReactShadowNode newRootShadowNode) {
SystraceMessage.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
"FabricUIManager.calculateLayout")
.flush();
long startTime = SystemClock.uptimeMillis();
try {
newRootShadowNode.calculateLayout();
} finally{
mLastCalculateLayoutTime = SystemClock.uptimeMillis() - startTime;
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}
}
private void applyUpdatesRecursive(ReactShadowNode node) {
SystraceMessage.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "FabricUIManager.applyUpdatesRecursive")
.flush();
try {
applyUpdatesRecursive(node, 0, 0);
} finally{
SystraceMessage.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}
}
private void applyUpdatesRecursive(ReactShadowNode node, float absoluteX, float absoluteY) {
if (!node.hasUpdates()) {
return;
}
if (!node.isVirtualAnchor()) {
for (int i = 0; i < node.getChildCount(); i++) {
applyUpdatesRecursive(
node.getChildAt(i),
absoluteX + node.getLayoutX(),
absoluteY + node.getLayoutY());
}
}
int tag = node.getReactTag();
if (getRootNode(tag) == null) {
boolean frameDidChange =
node.dispatchUpdates(absoluteX, absoluteY, mUIViewOperationQueue, null);
// Notify JS about layout event if requested
// and if the position or dimensions actually changed
// (consistent with iOS and Android Default implementation).
if (frameDidChange && node.shouldNotifyOnLayout()) {
mUIViewOperationQueue.enqueueOnLayoutEvent(tag,
node.getScreenX(),
node.getScreenY(),
node.getScreenWidth(),
node.getScreenHeight());
}
}
// Set the reference to the OriginalReactShadowNode to NULL, as the tree is already committed
// and we do not need to hold references to the previous tree anymore
node.setOriginalReactShadowNode(null);
node.markUpdateSeen();
node.markAsSealed();
}
@Override
@DoNotStrip
public <T extends SizeMonitoringFrameLayout & MeasureSpecProvider> int addRootView(
final T rootView) {
SystraceMessage.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
"FabricUIManager.addRootView")
.flush();
try {
final int rootTag = ReactRootViewTagGenerator.getNextRootViewTag();
ThemedReactContext themedRootContext =
new ThemedReactContext(mReactApplicationContext, rootView.getContext());
ReactShadowNode rootShadowNode = createRootShadowNode(rootTag, themedRootContext);
int widthMeasureSpec = rootView.getWidthMeasureSpec();
int heightMeasureSpec = rootView.getHeightMeasureSpec();
updateRootView(rootShadowNode, widthMeasureSpec, heightMeasureSpec);
rootView.setOnSizeChangedListener(
new SizeMonitoringFrameLayout.OnSizeChangedListener() {
@Override
public void onSizeChanged(final int width, final int height, int oldW, int oldH) {
updateRootSize(rootTag, width, height);
}
});
mRootShadowNodeRegistry.registerNode(rootShadowNode);
mUIViewOperationQueue.addRootView(rootTag, rootView, themedRootContext);
return rootTag;
} finally{
Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE);
}
}