diff --git a/ReactCommon/fabric/BUCK b/ReactCommon/fabric/BUCK index 162ae5ec4..62a6c5cca 100644 --- a/ReactCommon/fabric/BUCK +++ b/ReactCommon/fabric/BUCK @@ -40,6 +40,7 @@ rn_xplat_cxx_library( "xplat//folly:molly", "xplat//third-party/glog:glog", react_native_xplat_target("fabric/core:core"), + react_native_xplat_target("fabric/uimanager:uimanager"), react_native_xplat_target("fabric/view:view"), ], ) diff --git a/ReactCommon/fabric/FabricUIManager.cpp b/ReactCommon/fabric/FabricUIManager.cpp deleted file mode 100644 index 9b6ec3216..000000000 --- a/ReactCommon/fabric/FabricUIManager.cpp +++ /dev/null @@ -1,53 +0,0 @@ -/** - * 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 "IFabricPlatformUIOperationManager.h" - -namespace facebook { -namespace react { - -FabricUIManager::FabricUIManager(const std::shared_ptr &platformUIOperationManager) : - platformUIOperationManager_(platformUIOperationManager) {}; - -SharedShadowNode FabricUIManager::createNode(int reactTag, std::string viewName, int rootTag, folly::dynamic props, void *instanceHandle) { - platformUIOperationManager_->performUIOperation(); - return nullptr; -} - -SharedShadowNode FabricUIManager::cloneNode(const SharedShadowNode &node) { - return nullptr; -} - -SharedShadowNode FabricUIManager::cloneNodeWithNewChildren(const SharedShadowNode &node) { - return nullptr; -} - -SharedShadowNode FabricUIManager::cloneNodeWithNewProps(const SharedShadowNode &node, folly::dynamic props) { - return nullptr; -} - -SharedShadowNode FabricUIManager::cloneNodeWithNewChildrenAndProps(const SharedShadowNode &node, folly::dynamic newProps) { - return nullptr; -} - -void FabricUIManager::appendChild(const SharedShadowNode &parentNode, const SharedShadowNode &childNode) { -} - -SharedShadowNodeUnsharedList FabricUIManager::createChildSet(int rootTag) { - return nullptr; -} - -void FabricUIManager::appendChildToSet(const SharedShadowNodeUnsharedList &childSet, const SharedShadowNode &childNode) { -} - -void FabricUIManager::completeRoot(int rootTag, const SharedShadowNodeUnsharedList &childSet) { -} - -} // namespace react -} // namespace facebook diff --git a/ReactCommon/fabric/uimanager/BUCK b/ReactCommon/fabric/uimanager/BUCK new file mode 100644 index 000000000..8b3e3ad6b --- /dev/null +++ b/ReactCommon/fabric/uimanager/BUCK @@ -0,0 +1,52 @@ +load("//configurations/buck/apple:flag_defs.bzl", "get_debug_preprocessor_flags") +load("//ReactNative:DEFS.bzl", "IS_OSS_BUILD", "react_native_xplat_target", "rn_xplat_cxx_library", "APPLE_INSPECTOR_FLAGS") + +APPLE_COMPILER_FLAGS = [] + +if not IS_OSS_BUILD: + load("@xplat//configurations/buck/apple:flag_defs.bzl", "get_static_library_ios_flags", "flags") + APPLE_COMPILER_FLAGS = flags.get_flag_value(get_static_library_ios_flags(), 'compiler_flags') + +rn_xplat_cxx_library( + name = "uimanager", + srcs = glob( + [ + "**/*.cpp", + ], + ), + headers = glob( + [ + "**/*.h", + ], + ), + header_namespace = "", + exported_headers = subdir_glob( + [ + ("", "*.h"), + ], + prefix = "fabric/uimanager", + ), + compiler_flags = [ + "-fexceptions", + "-frtti", + "-std=c++14", + "-Wall", + ], + fbobjc_compiler_flags = APPLE_COMPILER_FLAGS, + fbobjc_preprocessor_flags = get_debug_preprocessor_flags() + APPLE_INSPECTOR_FLAGS, + force_static = True, + preprocessor_flags = [ + "-DLOG_TAG=\"ReactNative\"", + "-DWITH_FBSYSTRACE=1", + ], + visibility = ["PUBLIC"], + deps = [ + "xplat//fbsystrace:fbsystrace", + "xplat//folly:headers_only", + "xplat//folly:memory", + "xplat//folly:molly", + "xplat//third-party/glog:glog", + react_native_xplat_target("fabric/debug:debug"), + react_native_xplat_target("fabric/core:core"), + ], +) diff --git a/ReactCommon/fabric/uimanager/ComponentDescriptorRegistry.cpp b/ReactCommon/fabric/uimanager/ComponentDescriptorRegistry.cpp new file mode 100644 index 000000000..d209f590c --- /dev/null +++ b/ReactCommon/fabric/uimanager/ComponentDescriptorRegistry.cpp @@ -0,0 +1,26 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +#include "ComponentDescriptorRegistry.h" + +namespace facebook { +namespace react { + +void ComponentDescriptorRegistry::registerComponentDescriptor(SharedComponentDescriptor componentDescriptor) { + ComponentHandle componentHandle = componentDescriptor->getComponentHandle(); + _registryByHandle[componentHandle] = componentDescriptor; + + ComponentName componentName = componentDescriptor->getComponentName(); + _registryByName[componentName] = componentDescriptor; +} + +const SharedComponentDescriptor ComponentDescriptorRegistry::operator[](const SharedShadowNode &shadowNode) { + ComponentHandle componentHandle = shadowNode->getComponentHandle(); + return _registryByHandle[componentHandle]; +} + +const SharedComponentDescriptor ComponentDescriptorRegistry::operator[](const ComponentName &componentName) { + return _registryByName[componentName]; +} + +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/uimanager/ComponentDescriptorRegistry.h b/ReactCommon/fabric/uimanager/ComponentDescriptorRegistry.h new file mode 100644 index 000000000..462ab9f7f --- /dev/null +++ b/ReactCommon/fabric/uimanager/ComponentDescriptorRegistry.h @@ -0,0 +1,29 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +#pragma once + +#include + +#include + +namespace facebook { +namespace react { + +/* + * Registry of particular `ComponentDescriptor`s. + */ +class ComponentDescriptorRegistry { + +public: + void registerComponentDescriptor(SharedComponentDescriptor componentDescriptor); + + const SharedComponentDescriptor operator[](const SharedShadowNode &shadowNode); + const SharedComponentDescriptor operator[](const ComponentName &componentName); + +private: + std::unordered_map _registryByHandle; + std::unordered_map _registryByName; +}; + +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/uimanager/FabricUIManager.cpp b/ReactCommon/fabric/uimanager/FabricUIManager.cpp new file mode 100644 index 000000000..d35762482 --- /dev/null +++ b/ReactCommon/fabric/uimanager/FabricUIManager.cpp @@ -0,0 +1,109 @@ +/** + * 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 +#include +#include +#include + +#include "IFabricPlatformUIOperationManager.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(const std::shared_ptr &platformUIOperationManager): + platformUIOperationManager_(platformUIOperationManager) { + + SharedComponentDescriptor viewComponentDescriptor = std::make_shared(); + _registry.registerComponentDescriptor(viewComponentDescriptor); +} + +SharedShadowNode FabricUIManager::createNode(int tag, std::string viewName, int rootTag, folly::dynamic props, void *instanceHandle) { + ComponentDescriptor &componentDescriptor = *_registry["View"]; + RawProps rawProps = rawPropsFromDynamic(props); + SharedShadowNode shadowNode = componentDescriptor.createShadowNode(tag, rootTag, instanceHandle, rawProps); + return shadowNode; +} + +SharedShadowNode FabricUIManager::cloneNode(const SharedShadowNode &shadowNode) { + ComponentDescriptor &componentDescriptor = *_registry[shadowNode]; + return componentDescriptor.cloneShadowNode(shadowNode); +} + +SharedShadowNode FabricUIManager::cloneNodeWithNewChildren(const SharedShadowNode &shadowNode) { + // Assuming semantic: Cloning with same props but empty children. + ComponentDescriptor &componentDescriptor = *_registry[shadowNode]; + return componentDescriptor.cloneShadowNode(shadowNode, nullptr, {}); +} + +SharedShadowNode FabricUIManager::cloneNodeWithNewProps(const SharedShadowNode &shadowNode, folly::dynamic props) { + // Assuming semantic: Cloning with same children and specified props. + ComponentDescriptor &componentDescriptor = *_registry[shadowNode]; + RawProps rawProps = rawPropsFromDynamic(props); + return componentDescriptor.cloneShadowNode(shadowNode, std::make_shared(rawProps), nullptr); +} + +SharedShadowNode FabricUIManager::cloneNodeWithNewChildrenAndProps(const SharedShadowNode &shadowNode, folly::dynamic props) { + // Assuming semantic: Cloning with empty children and specified props. + ComponentDescriptor &componentDescriptor = *_registry[shadowNode]; + RawProps rawProps = rawPropsFromDynamic(props); + return componentDescriptor.cloneShadowNode(shadowNode, std::make_shared(rawProps), {}); +} + +void FabricUIManager::appendChild(const SharedShadowNode &parentShadowNode, const SharedShadowNode &childShadowNode) { + ComponentDescriptor &componentDescriptor = *_registry[parentShadowNode]; + componentDescriptor.appendChild(parentShadowNode, childShadowNode); +} + +SharedShadowNodeUnsharedList FabricUIManager::createChildSet(int rootTag) { + return std::make_shared(SharedShadowNodeList({})); +} + +void FabricUIManager::appendChildToSet(const SharedShadowNodeUnsharedList &shadowNodeList, const SharedShadowNode &shadowNode) { + shadowNodeList->push_back(shadowNode); +} + +void FabricUIManager::completeRoot(int rootTag, const SharedShadowNodeUnsharedList &children) { + ComponentDescriptor &componentDescriptor = *_registry["View"]; + SharedShadowNode previousRootShadowNode = componentDescriptor.createShadowNode(rootTag, rootTag, nullptr, {}); + auto childrenCopy = std::make_shared(SharedShadowNodeList(*children)); + SharedShadowNode rootShadowNode = componentDescriptor.cloneShadowNode(previousRootShadowNode, nullptr, childrenCopy); + + SharedViewShadowNode viewShadowNode = std::dynamic_pointer_cast(rootShadowNode); + LayoutContext layoutContext = LayoutContext(); + layoutContext.affectedShadowNodes = std::make_shared>(); + + auto nonConstViewShadowNode = std::const_pointer_cast(viewShadowNode); + nonConstViewShadowNode->layout(layoutContext); + + rootShadowNode->sealRecursive(); + + printf("## layouted rootShadowNode: %s \n", nonConstViewShadowNode->getDebugDescription(0).c_str()); +} + +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/FabricUIManager.h b/ReactCommon/fabric/uimanager/FabricUIManager.h similarity index 88% rename from ReactCommon/fabric/FabricUIManager.h rename to ReactCommon/fabric/uimanager/FabricUIManager.h index 4857a4291..1db615b37 100644 --- a/ReactCommon/fabric/FabricUIManager.h +++ b/ReactCommon/fabric/uimanager/FabricUIManager.h @@ -7,18 +7,22 @@ #pragma once -#include -#include #include +#include + +#include #include +#include namespace facebook { namespace react { class IFabricPlatformUIOperationManager; +class ComponentDescriptorRegistry; class FabricUIManager { + public: FabricUIManager(const std::shared_ptr &platformUIOperationManager); @@ -33,6 +37,7 @@ public: void completeRoot(int rootTag, const SharedShadowNodeUnsharedList &childSet); private: + ComponentDescriptorRegistry _registry; std::shared_ptr platformUIOperationManager_; }; diff --git a/ReactCommon/fabric/IFabricPlatformUIOperationManager.h b/ReactCommon/fabric/uimanager/IFabricPlatformUIOperationManager.h similarity index 100% rename from ReactCommon/fabric/IFabricPlatformUIOperationManager.h rename to ReactCommon/fabric/uimanager/IFabricPlatformUIOperationManager.h