From 7bf3b20837726ff43c2d7ed8a0d5012f3a1e1e7a Mon Sep 17 00:00:00 2001 From: Kevin Gozali Date: Wed, 7 Mar 2018 16:40:38 -0800 Subject: [PATCH] iOS: create C++ <-> ObjC++ connector for FabricUIManager to do UI operations Reviewed By: shergin Differential Revision: D7162911 fbshipit-source-id: 3e303020dafdccc51f52c3359a7054dc8a787978 --- .../RCTFabricPlatformUIOperationManager.h | 46 ++++++++++++++++++ .../RCTFabricPlatformUIOperationManager.mm | 48 +++++++++++++++++++ React/Fabric/RCTFabricUIManagerWrapper.h | 5 +- React/Fabric/RCTFabricUIManagerWrapper.mm | 8 +++- ReactCommon/fabric/FabricUIManager.cpp | 16 +++++-- ReactCommon/fabric/FabricUIManager.h | 17 +++++-- .../IFabricPlatformUIOperationManager.h | 23 +++++++++ ReactCommon/fabric/ShadowNode.cpp | 7 ++- ReactCommon/fabric/ShadowNode.h | 7 ++- 9 files changed, 163 insertions(+), 14 deletions(-) create mode 100644 React/Fabric/RCTFabricPlatformUIOperationManager.h create mode 100644 React/Fabric/RCTFabricPlatformUIOperationManager.mm create mode 100644 ReactCommon/fabric/IFabricPlatformUIOperationManager.h diff --git a/React/Fabric/RCTFabricPlatformUIOperationManager.h b/React/Fabric/RCTFabricPlatformUIOperationManager.h new file mode 100644 index 000000000..2e9df3c77 --- /dev/null +++ b/React/Fabric/RCTFabricPlatformUIOperationManager.h @@ -0,0 +1,46 @@ +/** + * 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. + */ + +#import +#import + +#import + +#include + +@class RCTFabricPlatformUIOperationManager; + +namespace facebook { +namespace react { + +/** + * Connector class (from C++ to ObjC++) to allow FabricUIManager to invoke native UI operations/updates. + * UIKit-related impl doesn't live here, but this class gets passed to the FabricUIManager C++ impl directly. + */ +class RCTFabricPlatformUIOperationManagerConnector : public IFabricPlatformUIOperationManager { +public: + RCTFabricPlatformUIOperationManagerConnector(); + ~RCTFabricPlatformUIOperationManagerConnector(); + + void performUIOperation(); + +private: + void *self_; + RCTFabricPlatformUIOperationManager *manager_; +}; + +} // namespace react +} // namespace facebook + +/** + * Actual ObjC++ implementation of the UI operations. + */ +@interface RCTFabricPlatformUIOperationManager : NSObject + +- (void)performUIOperation; + +@end diff --git a/React/Fabric/RCTFabricPlatformUIOperationManager.mm b/React/Fabric/RCTFabricPlatformUIOperationManager.mm new file mode 100644 index 000000000..3bccc58e0 --- /dev/null +++ b/React/Fabric/RCTFabricPlatformUIOperationManager.mm @@ -0,0 +1,48 @@ +/** + * 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. + */ + +#import "RCTFabricPlatformUIOperationManager.h" + +namespace facebook { +namespace react { + +RCTFabricPlatformUIOperationManagerConnector::RCTFabricPlatformUIOperationManagerConnector() { + self_ = (__bridge_retained void *)[RCTFabricPlatformUIOperationManager new]; + manager_ = (__bridge RCTFabricPlatformUIOperationManager *)self_; +} + +RCTFabricPlatformUIOperationManagerConnector::~RCTFabricPlatformUIOperationManagerConnector() { + CFRelease(self_); + self_ = NULL; + manager_ = NULL; +} + +void RCTFabricPlatformUIOperationManagerConnector::performUIOperation() { + [manager_ performUIOperation]; +} + +} // namespace react +} // namespace facebook + +// ----------------------------------------------------------------------------- +// Start of ObjC++ impl +// Access UIKit here. +// ----------------------------------------------------------------------------- +@implementation RCTFabricPlatformUIOperationManager + +- (void)dealloc +{ + NSLog(@"RCTFabricPlatformUIOperationManager: dealloc()"); +} + +- (void)performUIOperation +{ + // TODO + NSLog(@"RCTFabricPlatformUIOperationManager: performUIOperation()"); +} + +@end diff --git a/React/Fabric/RCTFabricUIManagerWrapper.h b/React/Fabric/RCTFabricUIManagerWrapper.h index 1db660fcf..d40ef78c3 100644 --- a/React/Fabric/RCTFabricUIManagerWrapper.h +++ b/React/Fabric/RCTFabricUIManagerWrapper.h @@ -18,8 +18,8 @@ namespace react { class FabricUIManager; -} -} +} // namespace react +} // namespace facebook using namespace facebook::react; @@ -28,7 +28,6 @@ using namespace facebook::react; */ @interface RCTFabricUIManagerWrapper : NSObject -- (instancetype)initWithManager:(std::shared_ptr)manager; - (std::shared_ptr)manager; @end diff --git a/React/Fabric/RCTFabricUIManagerWrapper.mm b/React/Fabric/RCTFabricUIManagerWrapper.mm index e1fc129d8..892669145 100644 --- a/React/Fabric/RCTFabricUIManagerWrapper.mm +++ b/React/Fabric/RCTFabricUIManagerWrapper.mm @@ -9,17 +9,21 @@ #include +#import "RCTFabricPlatformUIOperationManager.h" + // This file contains experimental placeholders, nothing is finalized. @implementation RCTFabricUIManagerWrapper { std::shared_ptr _manager; + std::shared_ptr _platformUIOperationManager; } -- (instancetype)initWithManager:(std::shared_ptr)manager +- (instancetype)init { self = [super init]; if (self) { - _manager = manager; + _platformUIOperationManager = std::make_shared(); + _manager = std::make_shared(_platformUIOperationManager); } return self; } diff --git a/ReactCommon/fabric/FabricUIManager.cpp b/ReactCommon/fabric/FabricUIManager.cpp index 1a383519a..54262fc9e 100644 --- a/ReactCommon/fabric/FabricUIManager.cpp +++ b/ReactCommon/fabric/FabricUIManager.cpp @@ -1,14 +1,23 @@ -// Copyright 2004-present Facebook. All Rights Reserved. +/** + * 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" #include "ShadowNode.h" namespace facebook { namespace react { -FabricUIManager::FabricUIManager() {} +FabricUIManager::FabricUIManager(const std::shared_ptr &platformUIOperationManager) : + platformUIOperationManager_(platformUIOperationManager) {}; ShadowNodeRef FabricUIManager::createNode(int reactTag, std::string viewName, int rootTag, folly::dynamic props, void *instanceHandle) { + platformUIOperationManager_->performUIOperation(); return std::make_shared(reactTag, viewName, rootTag, props, instanceHandle); } @@ -41,4 +50,5 @@ void FabricUIManager::appendChildToSet(const ShadowNodeSetRef &childSet, const S void FabricUIManager::completeRoot(int rootTag, const ShadowNodeSetRef &childSet) { } -}} +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/FabricUIManager.h b/ReactCommon/fabric/FabricUIManager.h index 7362c54df..06ee2a94f 100644 --- a/ReactCommon/fabric/FabricUIManager.h +++ b/ReactCommon/fabric/FabricUIManager.h @@ -1,4 +1,9 @@ -// Copyright 2004-present Facebook. All Rights Reserved. +/** + * 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. + */ #pragma once @@ -10,6 +15,7 @@ namespace facebook { namespace react { class ShadowNode; +class IFabricPlatformUIOperationManager; typedef std::shared_ptr ShadowNodeRef; typedef folly::fbvector ShadowNodeSet; @@ -17,7 +23,7 @@ typedef std::shared_ptr ShadowNodeSetRef; class FabricUIManager { public: - FabricUIManager(); + FabricUIManager(const std::shared_ptr &platformUIOperationManager); ShadowNodeRef createNode(int reactTag, std::string viewName, int rootTag, folly::dynamic props, void *instanceHandle); ShadowNodeRef cloneNode(const ShadowNodeRef &node); @@ -28,7 +34,10 @@ public: ShadowNodeSetRef createChildSet(int rootTag); void appendChildToSet(const ShadowNodeSetRef &childSet, const ShadowNodeRef &childNode); void completeRoot(int rootTag, const ShadowNodeSetRef &childSet); + +private: + std::shared_ptr platformUIOperationManager_; }; -}} - +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/IFabricPlatformUIOperationManager.h b/ReactCommon/fabric/IFabricPlatformUIOperationManager.h new file mode 100644 index 000000000..ff1434c75 --- /dev/null +++ b/ReactCommon/fabric/IFabricPlatformUIOperationManager.h @@ -0,0 +1,23 @@ +/** + * 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. + */ + +#pragma once + +namespace facebook { +namespace react { + +/** + * An interface for FabricUIManager to perform platform-specific UI operations, like updating native UIView's in iOS. + */ +class IFabricPlatformUIOperationManager { +public: + // TODO: add meaningful methods + virtual void performUIOperation() = 0; +}; + +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/ShadowNode.cpp b/ReactCommon/fabric/ShadowNode.cpp index 39b4d145a..9f675572b 100644 --- a/ReactCommon/fabric/ShadowNode.cpp +++ b/ReactCommon/fabric/ShadowNode.cpp @@ -1,4 +1,9 @@ -// Copyright 2004-present Facebook. All Rights Reserved. +/** + * 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 "ShadowNode.h" diff --git a/ReactCommon/fabric/ShadowNode.h b/ReactCommon/fabric/ShadowNode.h index a707f7160..257b7c19e 100644 --- a/ReactCommon/fabric/ShadowNode.h +++ b/ReactCommon/fabric/ShadowNode.h @@ -1,4 +1,9 @@ -// Copyright 2004-present Facebook. All Rights Reserved. +/** + * 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. + */ #pragma once