diff --git a/packages/react-native/Libraries/SurfaceBackedComponent/RCTSurfaceBackedComponent.h b/packages/react-native/Libraries/SurfaceBackedComponent/RCTSurfaceBackedComponent.h deleted file mode 100644 index 63a9c960657165..00000000000000 --- a/packages/react-native/Libraries/SurfaceBackedComponent/RCTSurfaceBackedComponent.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * 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 -#import - -@class RCTBridge; - -/** - * ComponentKit component represents a React Native Surface created - * (and stored in the state) with given `bridge`, `moduleName`, - * and `properties`. - */ -@interface RCTSurfaceBackedComponent : CKCompositeComponent - -+ (instancetype)newWithBridge:(RCTBridge *)bridge - surfacePresenter:(RCTSurfacePresenter *)surfacePresenter - moduleName:(NSString *)moduleName - properties:(NSDictionary *)properties - options:(RCTSurfaceHostingComponentOptions)options; - -@end diff --git a/packages/react-native/Libraries/SurfaceBackedComponent/RCTSurfaceBackedComponent.mm b/packages/react-native/Libraries/SurfaceBackedComponent/RCTSurfaceBackedComponent.mm deleted file mode 100644 index 39517ac5e86e57..00000000000000 --- a/packages/react-native/Libraries/SurfaceBackedComponent/RCTSurfaceBackedComponent.mm +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import "RCTSurfaceBackedComponent.h" - -#import - -#import -#import -#import -#import -#import - -#import "RCTSurfaceBackedComponentState.h" - -@implementation RCTSurfaceBackedComponent - -+ (id)initialState -{ - return [RCTSurfaceBackedComponentState new]; -} - -+ (instancetype)newWithBridge:(RCTBridge *)bridge - surfacePresenter:(RCTSurfacePresenter *)surfacePresenter - moduleName:(NSString *)moduleName - properties:(NSDictionary *)properties - options:(RCTSurfaceHostingComponentOptions)options -{ - CKComponentScope scope(self, moduleName); - - RCTSurfaceBackedComponentState *state = scope.state(); - - // JavaScript entrypoints expects "fabric" key for Fabric surfaces - NSMutableDictionary *adjustedProperties = [[NSMutableDictionary alloc] initWithDictionary:properties]; - adjustedProperties[@"fabric"] = surfacePresenter ? @YES : nil; - - if (state.surface == nil || ![state.surface.moduleName isEqualToString:moduleName]) { - id surface; - if (surfacePresenter) { - surface = [[RCTFabricSurface alloc] initWithSurfacePresenter:surfacePresenter - moduleName:moduleName - initialProperties:adjustedProperties]; - } else { - surface = [[RCTSurface alloc] initWithBridge:bridge moduleName:moduleName initialProperties:adjustedProperties]; - } - [surface start]; - - state = [RCTSurfaceBackedComponentState newWithSurface:surface]; - - CKComponentScope::replaceState(scope, state); - } else { - if (![state.surface.properties isEqualToDictionary:adjustedProperties]) { - state.surface.properties = adjustedProperties; - } - } - - RCTSurfaceHostingComponent *surfaceHostingComponent = [RCTSurfaceHostingComponent newWithSurface:state.surface - options:options]; - - CKComponent *component; - if (options.activityIndicatorComponentFactory == nil || RCTSurfaceStageIsRunning(state.surface.stage)) { - component = surfaceHostingComponent; - } else { - component = [[CKOverlayLayoutComponent alloc] initWithComponent:surfaceHostingComponent - overlay:options.activityIndicatorComponentFactory() - accessibilityContext:{}]; - } - - return [super newWithComponent:component]; -} - -@end diff --git a/packages/react-native/Libraries/SurfaceBackedComponent/RCTSurfaceBackedComponentState.h b/packages/react-native/Libraries/SurfaceBackedComponent/RCTSurfaceBackedComponentState.h deleted file mode 100644 index 665fb802abc3fb..00000000000000 --- a/packages/react-native/Libraries/SurfaceBackedComponent/RCTSurfaceBackedComponentState.h +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import - -@protocol RCTSurfaceProtocol; - -@interface RCTSurfaceBackedComponentState : NSObject - -@property (atomic, readonly, strong) id surface; - -+ (instancetype)newWithSurface:(id)surface; - -@end diff --git a/packages/react-native/Libraries/SurfaceBackedComponent/RCTSurfaceBackedComponentState.mm b/packages/react-native/Libraries/SurfaceBackedComponent/RCTSurfaceBackedComponentState.mm deleted file mode 100644 index 224017da62b905..00000000000000 --- a/packages/react-native/Libraries/SurfaceBackedComponent/RCTSurfaceBackedComponentState.mm +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import "RCTSurfaceBackedComponentState.h" - -@implementation RCTSurfaceBackedComponentState - -+ (instancetype)newWithSurface:(id)surface -{ - return [[self alloc] initWithSurface:surface]; -} - -- (instancetype)initWithSurface:(id)surface -{ - if (self = [super init]) { - _surface = surface; - } - - return self; -} - -@end diff --git a/packages/react-native/Libraries/SurfaceHostingComponent/RCTSurfaceHostingComponent+Internal.h b/packages/react-native/Libraries/SurfaceHostingComponent/RCTSurfaceHostingComponent+Internal.h deleted file mode 100644 index b3d3d4616aee49..00000000000000 --- a/packages/react-native/Libraries/SurfaceHostingComponent/RCTSurfaceHostingComponent+Internal.h +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import -#import - -@class RCTSurfaceHostingComponentState; - -@interface RCTSurfaceHostingComponent () - -@property (nonatomic, strong, readonly) id surface; -@property (nonatomic, retain, readonly) RCTSurfaceHostingComponentState *state; -@property (nonatomic, assign, readonly) RCTSurfaceHostingComponentOptions options; - -@end diff --git a/packages/react-native/Libraries/SurfaceHostingComponent/RCTSurfaceHostingComponent.h b/packages/react-native/Libraries/SurfaceHostingComponent/RCTSurfaceHostingComponent.h deleted file mode 100644 index 1bd6f4667d325f..00000000000000 --- a/packages/react-native/Libraries/SurfaceHostingComponent/RCTSurfaceHostingComponent.h +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * 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 - -/** - * ComponentKit component represents given Surface instance. - */ -@interface RCTSurfaceHostingComponent : CKComponent - -+ (instancetype)newWithSurface:(id)surface options:(RCTSurfaceHostingComponentOptions)options; - -@end diff --git a/packages/react-native/Libraries/SurfaceHostingComponent/RCTSurfaceHostingComponent.mm b/packages/react-native/Libraries/SurfaceHostingComponent/RCTSurfaceHostingComponent.mm deleted file mode 100644 index 28bc33e5933e04..00000000000000 --- a/packages/react-native/Libraries/SurfaceHostingComponent/RCTSurfaceHostingComponent.mm +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import "RCTSurfaceHostingComponent.h" -#import "RCTSurfaceHostingComponent+Internal.h" - -#import - -#import -#import -#import -#import - -#import "RCTSurfaceHostingComponentController.h" -#import "RCTSurfaceHostingComponentState.h" - -@implementation RCTSurfaceHostingComponent - -+ (Class)controllerClass -{ - return [RCTSurfaceHostingComponentController class]; -} - -+ (id)initialState -{ - return [RCTSurfaceHostingComponentState new]; -} - -+ (instancetype)newWithSurface:(id)surface options:(RCTSurfaceHostingComponentOptions)options -{ - CKComponentScope scope(self, surface); - - RCTSurfaceHostingComponentState *const state = scope.state(); - - RCTSurfaceHostingComponentState *const newState = - [RCTSurfaceHostingComponentState newWithStage:surface.stage intrinsicSize:surface.intrinsicSize]; - - if (![state isEqual:newState]) { - CKComponentScope::replaceState(scope, newState); - } - - RCTSurfaceHostingComponent *const component = [super newWithView:{[UIView class]} size:{}]; - - if (component) { - component->_state = scope.state(); - component->_surface = surface; - component->_options = options; - } - - return component; -} - -- (RCLayout)computeLayoutThatFits:(CKSizeRange)constrainedSize -{ - // Optimistically communicating layout constraints to the `_surface`, - // just to provide layout constraints to React Native as early as possible. - // React Native *may* use this info later during applying the own state and - // related laying out in parallel with ComponentKit execution. - // This call will not interfere (or introduce any negative side effects) with - // following invocation of `sizeThatFitsMinimumSize:maximumSize:`. - // A weak point: We assume here that this particular layout will be - // mounted eventually, which is technically not guaranteed by ComponentKit. - // Therefore we also assume that the last layout calculated in a sequence - // will be mounted anyways, which is probably true for all *real* use cases. - // We plan to tackle this problem during the next big step in improving - // interop compatibilities of React Native which will enable us granularly - // control React Native mounting blocks and, as a result, implement - // truly synchronous mounting stage between React Native and ComponentKit. - [_surface setMinimumSize:constrainedSize.min maximumSize:constrainedSize.max]; - - // Just in case of the very first building pass, we give React Native a chance - // to prepare its internals for coming synchronous measuring. - if ([_surface isKindOfClass:[RCTSurface class]]) { - // Legacy Pre-Fabric Surface - [(RCTSurface *)_surface synchronouslyWaitForStage:RCTSurfaceStageSurfaceDidInitialLayout - timeout:_options.synchronousLayoutingTimeout]; - } else if ([_surface isKindOfClass:[RCTFabricSurface class]]) { - // Fabric Surface - // Hack: Increase timeout because RCTFabricSurface stage will be RCTSurfaceStageSurfaceDidInitialLayout - // before mounting has finished, which can cause sizeThatFitsMinimumSize to return the wrong value. - // Safe hack because timeout length can be increased without making the component seem slower. - // However if timeout length is less than the time to mount a surface, the size may be incorrect. - // TODO (T115399546) Allow RCTFabricSurface synchronouslyWaitFor to wait for mounting completion stage - NSTimeInterval timeout = 20; - [(RCTFabricSurface *)_surface synchronouslyWaitFor:timeout]; - } - - CGSize fittingSize = CGSizeZero; - if (_surface.stage & RCTSurfaceStageSurfaceDidInitialLayout) { - fittingSize = [_surface sizeThatFitsMinimumSize:constrainedSize.min maximumSize:constrainedSize.max]; - } else { - fittingSize = _options.activityIndicatorSize; - } - - fittingSize = constrainedSize.clamp(fittingSize); - return {self, fittingSize}; -} - -- (CKComponentBoundsAnimation)boundsAnimationFromPreviousComponent:(RCTSurfaceHostingComponent *)previousComponent -{ - if (_options.boundsAnimations && (previousComponent->_state.stage != _state.stage)) { - return { - .mode = CKComponentBoundsAnimationModeDefault, - .duration = 0.25, - .options = UIViewAnimationOptionCurveEaseInOut, - }; - } - - return {}; -} - -@end diff --git a/packages/react-native/Libraries/SurfaceHostingComponent/RCTSurfaceHostingComponentController.h b/packages/react-native/Libraries/SurfaceHostingComponent/RCTSurfaceHostingComponentController.h deleted file mode 100644 index 0627ecf3c2cdfe..00000000000000 --- a/packages/react-native/Libraries/SurfaceHostingComponent/RCTSurfaceHostingComponentController.h +++ /dev/null @@ -1,12 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import - -@interface RCTSurfaceHostingComponentController : CKComponentController - -@end diff --git a/packages/react-native/Libraries/SurfaceHostingComponent/RCTSurfaceHostingComponentController.mm b/packages/react-native/Libraries/SurfaceHostingComponent/RCTSurfaceHostingComponentController.mm deleted file mode 100644 index 7f82cd4158918d..00000000000000 --- a/packages/react-native/Libraries/SurfaceHostingComponent/RCTSurfaceHostingComponentController.mm +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import "RCTSurfaceHostingComponentController.h" - -#import -#import -#import -#import -#import - -#import "RCTSurfaceHostingComponent+Internal.h" -#import "RCTSurfaceHostingComponent.h" -#import "RCTSurfaceHostingComponentState.h" - -@interface RCTSurfaceHostingComponentController () -@end - -@implementation RCTSurfaceHostingComponentController { - id _surface; -} - -- (instancetype)initWithComponent:(RCTSurfaceHostingComponent *)component -{ - if (self = [super initWithComponent:component]) { - [self updateSurfaceWithComponent:component]; - } - - return self; -} - -#pragma mark - Lifecycle - -- (void)didMount -{ - [super didMount]; - [self mountSurfaceView]; -} - -- (void)didRemount -{ - [super didRemount]; - [self mountSurfaceView]; -} - -- (void)didUpdateComponent -{ - [super didUpdateComponent]; - [self updateSurfaceWithComponent:(RCTSurfaceHostingComponent *)self.component]; -} - -- (void)didUnmount -{ - [super didUnmount]; - [self unmountSurfaceView]; -} - -#pragma mark - Helpers - -- (void)updateSurfaceWithComponent:(RCTSurfaceHostingComponent *)component -{ - // Updating `surface` - id const surface = component.surface; - if (surface != _surface) { - if (_surface.delegate == self) { - _surface.delegate = nil; - } - - _surface = surface; - _surface.delegate = self; - } -} - -- (void)setIntrinsicSize:(CGSize)intrinsicSize -{ - __weak __typeof(self) weakSelf = self; - dispatch_async(dispatch_get_main_queue(), ^{ - __strong __typeof(self) strongSelf = weakSelf; - if (!strongSelf) { - return; - } - - [strongSelf.component - updateState:^(RCTSurfaceHostingComponentState *state) { - return [RCTSurfaceHostingComponentState newWithStage:state.stage intrinsicSize:intrinsicSize]; - } - mode:[strongSelf suitableStateUpdateMode]]; - }); -} - -- (void)setStage:(RCTSurfaceStage)stage -{ - __weak __typeof(self) weakSelf = self; - dispatch_async(dispatch_get_main_queue(), ^{ - __strong __typeof(self) strongSelf = weakSelf; - if (!strongSelf) { - return; - } - - [strongSelf.component - updateState:^(RCTSurfaceHostingComponentState *state) { - return [RCTSurfaceHostingComponentState newWithStage:stage intrinsicSize:state.intrinsicSize]; - } - mode:[strongSelf suitableStateUpdateMode]]; - }); -} - -- (CKUpdateMode)suitableStateUpdateMode -{ - return ((RCTSurfaceHostingComponent *)self.component).options.synchronousStateUpdates && RCTIsMainQueue() - ? CKUpdateModeSynchronous - : CKUpdateModeAsynchronous; -} - -- (void)mountSurfaceView -{ - UIView *const surfaceView = _surface.view; - - const CKComponentViewContext &context = [[self component] viewContext]; - - UIView *const superview = context.view; - superview.clipsToBounds = YES; - - RCTAssert([superview.subviews count] <= 1, @"Should never have more than a single stateful subview."); - - UIView *const existingSurfaceView = [superview.subviews lastObject]; - if (existingSurfaceView != surfaceView) { - [existingSurfaceView removeFromSuperview]; - surfaceView.frame = superview.bounds; - surfaceView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; - [superview addSubview:surfaceView]; - } -} - -- (void)unmountSurfaceView -{ - const CKComponentViewContext &context = [[self component] viewContext]; - - UIView *const superview = context.view; - RCTAssert([superview.subviews count] <= 1, @"Should never have more than a single stateful subview."); - UIView *const existingSurfaceView = [superview.subviews lastObject]; - [existingSurfaceView removeFromSuperview]; -} - -#pragma mark - RCTSurfaceDelegate - -- (void)surface:(RCTSurface *)surface didChangeIntrinsicSize:(CGSize)intrinsicSize -{ - [self setIntrinsicSize:intrinsicSize]; -} - -- (void)surface:(RCTSurface *)surface didChangeStage:(RCTSurfaceStage)stage -{ - [self setStage:stage]; -} - -@end diff --git a/packages/react-native/Libraries/SurfaceHostingComponent/RCTSurfaceHostingComponentOptions.h b/packages/react-native/Libraries/SurfaceHostingComponent/RCTSurfaceHostingComponentOptions.h deleted file mode 100644 index 06819d99c809f5..00000000000000 --- a/packages/react-native/Libraries/SurfaceHostingComponent/RCTSurfaceHostingComponentOptions.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import - -#import - -typedef CKComponent * (^RCTSurfaceHostingComponentOptionsActivityIndicatorComponentFactory)(); - -struct RCTSurfaceHostingComponentOptions { - NSTimeInterval synchronousLayoutingTimeout = 0.350; - BOOL synchronousStateUpdates = YES; - CGSize activityIndicatorSize = {44.0, 44.0}; - BOOL boundsAnimations = YES; - RCTSurfaceHostingComponentOptionsActivityIndicatorComponentFactory activityIndicatorComponentFactory = nil; -}; diff --git a/packages/react-native/Libraries/SurfaceHostingComponent/RCTSurfaceHostingComponentState.h b/packages/react-native/Libraries/SurfaceHostingComponent/RCTSurfaceHostingComponentState.h deleted file mode 100644 index 6036e74be774e0..00000000000000 --- a/packages/react-native/Libraries/SurfaceHostingComponent/RCTSurfaceHostingComponentState.h +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import - -#import - -@interface RCTSurfaceHostingComponentState : NSObject - -@property (nonatomic, readonly, assign) CGSize intrinsicSize; -@property (nonatomic, readonly, assign) RCTSurfaceStage stage; - -+ (instancetype)newWithStage:(RCTSurfaceStage)stage intrinsicSize:(CGSize)intrinsicSize; - -@end diff --git a/packages/react-native/Libraries/SurfaceHostingComponent/RCTSurfaceHostingComponentState.mm b/packages/react-native/Libraries/SurfaceHostingComponent/RCTSurfaceHostingComponentState.mm deleted file mode 100644 index f108bff23833d7..00000000000000 --- a/packages/react-native/Libraries/SurfaceHostingComponent/RCTSurfaceHostingComponentState.mm +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import "RCTSurfaceHostingComponentState.h" - -@implementation RCTSurfaceHostingComponentState - -+ (instancetype)newWithStage:(RCTSurfaceStage)stage intrinsicSize:(CGSize)intrinsicSize -{ - return [[self alloc] initWithStage:stage intrinsicSize:intrinsicSize]; -} - -- (instancetype)initWithStage:(RCTSurfaceStage)stage intrinsicSize:(CGSize)intrinsicSize -{ - if (self = [super init]) { - _stage = stage; - _intrinsicSize = intrinsicSize; - } - - return self; -} - -- (BOOL)isEqual:(RCTSurfaceHostingComponentState *)other -{ - if (other == self) { - return YES; - } - - return _stage == other->_stage && CGSizeEqualToSize(_intrinsicSize, other->_intrinsicSize); -} - -@end