mirror of
https://github.com/status-im/react-native.git
synced 2025-01-10 01:25:39 +00:00
3fd2e2da4f
Summary: The new interface of ComponentDescriptor makes ShadowNode creation/cloning process a bit more explicit: Now customers (UIManager) must prepare Props object explicitly before creation or cloning. Besides general clarity, we need this to prepare for a new virtual `ShadowNode::clone()` method which will serve "virtual constructor" role, redirecting execution to concrete ComponentDescriptor instance. Actually, the whole purpose of concrete ComponentDescriptor instance is serve "virtual constructor" role (and all this code should be "templated"). Reviewed By: mdvacca Differential Revision: D7591714 fbshipit-source-id: 8793b3ef70ed7ae113efb36ad1eee20573360dc8
172 lines
7.2 KiB
C++
172 lines
7.2 KiB
C++
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#include "FabricUIManager.h"
|
|
|
|
#include <glog/logging.h>
|
|
|
|
#include <fabric/core/componentDescriptor.h>
|
|
#include <fabric/core/LayoutContext.h>
|
|
#include <fabric/debug/DebugStringConvertible.h>
|
|
#include <fabric/debug/DebugStringConvertibleItem.h>
|
|
#include <fabric/view/ViewComponentDescriptor.h>
|
|
#include <fabric/view/ViewProps.h>
|
|
#include <fabric/view/ViewShadowNode.h>
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
static const RawProps rawPropsFromDynamic(const folly::dynamic object) {
|
|
// TODO: Convert this to something smarter, probably returning `std::iterator`.
|
|
RawProps result;
|
|
|
|
if (object.isNull()) {
|
|
return result;
|
|
}
|
|
|
|
assert(object.isObject());
|
|
|
|
for (const auto &pair : object.items()) {
|
|
assert(pair.first.isString());
|
|
result[pair.first.asString()] = pair.second;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
FabricUIManager::FabricUIManager(SharedComponentDescriptorRegistry componentDescriptorRegistry) {
|
|
componentDescriptorRegistry_ = componentDescriptorRegistry;
|
|
}
|
|
|
|
void FabricUIManager::setDelegate(UIManagerDelegate *delegate) {
|
|
delegate_ = delegate;
|
|
}
|
|
|
|
UIManagerDelegate *FabricUIManager::getDelegate() {
|
|
return delegate_;
|
|
}
|
|
|
|
SharedShadowNode FabricUIManager::createNode(int tag, std::string viewName, int rootTag, folly::dynamic props, void *instanceHandle) {
|
|
LOG(INFO) << "FabricUIManager::createNode(tag: " << tag << ", name: " << viewName << ", rootTag" << rootTag << ", props: " << props << ")";
|
|
const SharedComponentDescriptor &componentDescriptor = (*componentDescriptorRegistry_)["View"];
|
|
RawProps rawProps = rawPropsFromDynamic(props);
|
|
|
|
SharedShadowNode shadowNode =
|
|
componentDescriptor->createShadowNode(
|
|
tag,
|
|
rootTag,
|
|
instanceHandle,
|
|
componentDescriptor->cloneProps(nullptr, rawProps)
|
|
);
|
|
|
|
LOG(INFO) << "FabricUIManager::createNode() -> " << shadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false});
|
|
|
|
if (delegate_) {
|
|
delegate_->uiManagerDidCreateShadowNode(shadowNode);
|
|
}
|
|
|
|
return shadowNode;
|
|
}
|
|
|
|
SharedShadowNode FabricUIManager::cloneNode(const SharedShadowNode &shadowNode) {
|
|
LOG(INFO) << "FabricUIManager::cloneNode(shadowNode: " << shadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false}) << ")";
|
|
const SharedComponentDescriptor &componentDescriptor = (*componentDescriptorRegistry_)[shadowNode];
|
|
|
|
SharedShadowNode clonedShadowNode =
|
|
componentDescriptor->cloneShadowNode(shadowNode);
|
|
|
|
LOG(INFO) << "FabricUIManager::cloneNode() -> " << clonedShadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false});
|
|
return clonedShadowNode;
|
|
}
|
|
|
|
SharedShadowNode FabricUIManager::cloneNodeWithNewChildren(const SharedShadowNode &shadowNode) {
|
|
LOG(INFO) << "FabricUIManager::cloneNodeWithNewChildren(shadowNode: " << shadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false}) << ")";
|
|
// Assuming semantic: Cloning with same props but empty children.
|
|
const SharedComponentDescriptor &componentDescriptor = (*componentDescriptorRegistry_)[shadowNode];
|
|
|
|
SharedShadowNode clonedShadowNode =
|
|
componentDescriptor->cloneShadowNode(
|
|
shadowNode,
|
|
nullptr,
|
|
ShadowNode::emptySharedShadowNodeSharedList()
|
|
);
|
|
|
|
LOG(INFO) << "FabricUIManager::cloneNodeWithNewChildren() -> " << clonedShadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false});
|
|
return clonedShadowNode;
|
|
}
|
|
|
|
SharedShadowNode FabricUIManager::cloneNodeWithNewProps(const SharedShadowNode &shadowNode, folly::dynamic props) {
|
|
LOG(INFO) << "FabricUIManager::cloneNodeWithNewProps(shadowNode: " << shadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false}) << ", props: " << props << ")";
|
|
// Assuming semantic: Cloning with same children and specified props.
|
|
const SharedComponentDescriptor &componentDescriptor = (*componentDescriptorRegistry_)[shadowNode];
|
|
RawProps rawProps = rawPropsFromDynamic(props);
|
|
|
|
SharedShadowNode clonedShadowNode =
|
|
componentDescriptor->cloneShadowNode(
|
|
shadowNode,
|
|
componentDescriptor->cloneProps(shadowNode->getProps(), rawProps),
|
|
nullptr
|
|
);
|
|
|
|
LOG(INFO) << "FabricUIManager::cloneNodeWithNewProps() -> " << clonedShadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false});
|
|
return clonedShadowNode;
|
|
}
|
|
|
|
SharedShadowNode FabricUIManager::cloneNodeWithNewChildrenAndProps(const SharedShadowNode &shadowNode, folly::dynamic props) {
|
|
LOG(INFO) << "FabricUIManager::cloneNodeWithNewChildrenAndProps(shadowNode: " << shadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false}) << ", props: " << props << ")";
|
|
// Assuming semantic: Cloning with empty children and specified props.
|
|
const SharedComponentDescriptor &componentDescriptor = (*componentDescriptorRegistry_)[shadowNode];
|
|
RawProps rawProps = rawPropsFromDynamic(props);
|
|
|
|
SharedShadowNode clonedShadowNode =
|
|
componentDescriptor->cloneShadowNode(
|
|
shadowNode,
|
|
componentDescriptor->cloneProps(shadowNode->getProps(), rawProps),
|
|
ShadowNode::emptySharedShadowNodeSharedList()
|
|
);
|
|
|
|
LOG(INFO) << "FabricUIManager::cloneNodeWithNewChildrenAndProps() -> " << clonedShadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false});
|
|
return clonedShadowNode;
|
|
}
|
|
|
|
void FabricUIManager::appendChild(const SharedShadowNode &parentShadowNode, const SharedShadowNode &childShadowNode) {
|
|
LOG(INFO) << "FabricUIManager::appendChild(parentShadowNode: " << parentShadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false}) << ", childShadowNode: " << childShadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false}) << ")";
|
|
const SharedComponentDescriptor &componentDescriptor = (*componentDescriptorRegistry_)[parentShadowNode];
|
|
|
|
// TODO: Remove this after we move this to JS side.
|
|
if (childShadowNode->getSealed()) {
|
|
auto clonedChildShadowNode = componentDescriptor->cloneShadowNode(childShadowNode);
|
|
auto nonConstClonedChildShadowNode = std::const_pointer_cast<ShadowNode>(clonedChildShadowNode);
|
|
nonConstClonedChildShadowNode->shallowSourceNode();
|
|
componentDescriptor->appendChild(parentShadowNode, clonedChildShadowNode);
|
|
return;
|
|
}
|
|
|
|
componentDescriptor->appendChild(parentShadowNode, childShadowNode);
|
|
}
|
|
|
|
SharedShadowNodeUnsharedList FabricUIManager::createChildSet(int rootTag) {
|
|
LOG(INFO) << "FabricUIManager::createChildSet(rootTag: " << rootTag << ")";
|
|
return std::make_shared<SharedShadowNodeList>(SharedShadowNodeList({}));
|
|
}
|
|
|
|
void FabricUIManager::appendChildToSet(const SharedShadowNodeUnsharedList &shadowNodeList, const SharedShadowNode &shadowNode) {
|
|
LOG(INFO) << "FabricUIManager::appendChildToSet(shadowNodeList: " << shadowNodeList << ", shadowNode: " << shadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false}) << ")";
|
|
shadowNodeList->push_back(shadowNode);
|
|
}
|
|
|
|
void FabricUIManager::completeRoot(int rootTag, const SharedShadowNodeUnsharedList &children) {
|
|
LOG(INFO) << "FabricUIManager::completeRoot(rootTag: " << rootTag << ", shadowNodeList: " << children << ")";
|
|
|
|
if (delegate_) {
|
|
delegate_->uiManagerDidFinishTransaction(rootTag, children);
|
|
}
|
|
}
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|