|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#import "RCTSurfaceHostingProxyRootView.h" |
| 9 | + |
| 10 | +#import <objc/runtime.h> |
| 11 | + |
| 12 | +#import "RCTAssert.h" |
| 13 | +#import "RCTBridge.h" |
| 14 | +#import "RCTLog.h" |
| 15 | +#import "RCTPerformanceLogger.h" |
| 16 | +#import "RCTProfile.h" |
| 17 | +#import "RCTRootContentView.h" |
| 18 | +#import "RCTRootViewDelegate.h" |
| 19 | +#import "RCTSurface.h" |
| 20 | +#import "UIView+React.h" |
| 21 | + |
| 22 | +static RCTSurfaceSizeMeasureMode convertToSurfaceSizeMeasureMode(RCTRootViewSizeFlexibility sizeFlexibility) { |
| 23 | + switch (sizeFlexibility) { |
| 24 | + case RCTRootViewSizeFlexibilityWidthAndHeight: |
| 25 | + return RCTSurfaceSizeMeasureModeWidthUndefined | RCTSurfaceSizeMeasureModeHeightUndefined; |
| 26 | + case RCTRootViewSizeFlexibilityWidth: |
| 27 | + return RCTSurfaceSizeMeasureModeWidthUndefined | RCTSurfaceSizeMeasureModeHeightExact; |
| 28 | + case RCTRootViewSizeFlexibilityHeight: |
| 29 | + return RCTSurfaceSizeMeasureModeWidthExact | RCTSurfaceSizeMeasureModeHeightUndefined; |
| 30 | + case RCTRootViewSizeFlexibilityNone: |
| 31 | + return RCTSurfaceSizeMeasureModeWidthExact | RCTSurfaceSizeMeasureModeHeightExact; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +static RCTRootViewSizeFlexibility convertToRootViewSizeFlexibility(RCTSurfaceSizeMeasureMode sizeMeasureMode) { |
| 36 | + switch (sizeMeasureMode) { |
| 37 | + case RCTSurfaceSizeMeasureModeWidthUndefined | RCTSurfaceSizeMeasureModeHeightUndefined: |
| 38 | + return RCTRootViewSizeFlexibilityWidthAndHeight; |
| 39 | + case RCTSurfaceSizeMeasureModeWidthUndefined | RCTSurfaceSizeMeasureModeHeightExact: |
| 40 | + return RCTRootViewSizeFlexibilityWidth; |
| 41 | + case RCTSurfaceSizeMeasureModeWidthExact | RCTSurfaceSizeMeasureModeHeightUndefined: |
| 42 | + return RCTRootViewSizeFlexibilityHeight; |
| 43 | + case RCTSurfaceSizeMeasureModeWidthExact | RCTSurfaceSizeMeasureModeHeightExact: |
| 44 | + default: |
| 45 | + return RCTRootViewSizeFlexibilityNone; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +@implementation RCTSurfaceHostingProxyRootView |
| 50 | + |
| 51 | +- (instancetype)initWithBridge:(RCTBridge *)bridge |
| 52 | + moduleName:(NSString *)moduleName |
| 53 | + initialProperties:(NSDictionary *)initialProperties |
| 54 | + fabric:(BOOL)fabric |
| 55 | +{ |
| 56 | + RCTAssertMainQueue(); |
| 57 | + RCTAssert(bridge, @"A bridge instance is required to create an RCTSurfaceHostingProxyRootView"); |
| 58 | + RCTAssert(moduleName, @"A moduleName is required to create an RCTSurfaceHostingProxyRootView"); |
| 59 | + |
| 60 | + RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"-[RCTSurfaceHostingProxyRootView init]", nil); |
| 61 | + if (!bridge.isLoading) { |
| 62 | + [bridge.performanceLogger markStartForTag:RCTPLTTI]; |
| 63 | + } |
| 64 | + |
| 65 | + if (self = [super initWithBridge:bridge moduleName:moduleName initialProperties:initialProperties fabric:fabric]) { |
| 66 | + self.backgroundColor = [UIColor whiteColor]; |
| 67 | + } |
| 68 | + |
| 69 | + RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @""); |
| 70 | + |
| 71 | + return self; |
| 72 | +} |
| 73 | + |
| 74 | +- (instancetype)initWithBridge:(RCTBridge *)bridge |
| 75 | + moduleName:(NSString *)moduleName |
| 76 | + initialProperties:(NSDictionary *)initialProperties |
| 77 | +{ |
| 78 | + return [self initWithBridge:bridge moduleName:moduleName initialProperties:initialProperties fabric:NO]; |
| 79 | +} |
| 80 | + |
| 81 | +- (instancetype)initWithBundleURL:(NSURL *)bundleURL |
| 82 | + moduleName:(NSString *)moduleName |
| 83 | + initialProperties:(NSDictionary *)initialProperties |
| 84 | + launchOptions:(NSDictionary *)launchOptions |
| 85 | + fabric:(BOOL)fabric |
| 86 | +{ |
| 87 | + RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:bundleURL |
| 88 | + moduleProvider:nil |
| 89 | + launchOptions:launchOptions]; |
| 90 | + |
| 91 | + return [self initWithBridge:bridge moduleName:moduleName initialProperties:initialProperties fabric:fabric]; |
| 92 | +} |
| 93 | + |
| 94 | +- (instancetype)initWithBundleURL:(NSURL *)bundleURL |
| 95 | + moduleName:(NSString *)moduleName |
| 96 | + initialProperties:(NSDictionary *)initialProperties |
| 97 | + launchOptions:(NSDictionary *)launchOptions |
| 98 | +{ |
| 99 | + return [self initWithBundleURL:bundleURL moduleName:moduleName initialProperties:initialProperties launchOptions:launchOptions fabric:NO]; |
| 100 | +} |
| 101 | + |
| 102 | +RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame) |
| 103 | +RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder) |
| 104 | + |
| 105 | +# pragma mark proxy methods to RCTSurfaceHostingView |
| 106 | + |
| 107 | +- (NSString *)moduleName |
| 108 | +{ |
| 109 | + return super.surface.moduleName; |
| 110 | +} |
| 111 | + |
| 112 | +- (RCTBridge *)bridge |
| 113 | +{ |
| 114 | + return super.surface.bridge; |
| 115 | +} |
| 116 | + |
| 117 | +- (BOOL)fabric |
| 118 | +{ |
| 119 | + return super.surface.fabric; |
| 120 | +} |
| 121 | + |
| 122 | +- (UIView *)contentView |
| 123 | +{ |
| 124 | + return self; |
| 125 | +} |
| 126 | + |
| 127 | +- (NSNumber *)reactTag |
| 128 | +{ |
| 129 | + return super.surface.rootViewTag; |
| 130 | +} |
| 131 | + |
| 132 | +- (RCTRootViewSizeFlexibility)sizeFlexibility |
| 133 | +{ |
| 134 | + return convertToRootViewSizeFlexibility(super.sizeMeasureMode); |
| 135 | +} |
| 136 | + |
| 137 | +- (void)setSizeFlexibility:(RCTRootViewSizeFlexibility)sizeFlexibility |
| 138 | +{ |
| 139 | + super.sizeMeasureMode = convertToSurfaceSizeMeasureMode(sizeFlexibility); |
| 140 | +} |
| 141 | + |
| 142 | +- (NSDictionary *)appProperties |
| 143 | +{ |
| 144 | + return super.surface.properties; |
| 145 | +} |
| 146 | + |
| 147 | +- (void)setAppProperties:(NSDictionary *)appProperties |
| 148 | +{ |
| 149 | + [super.surface setProperties:appProperties]; |
| 150 | +} |
| 151 | + |
| 152 | +- (CGSize)intrinsicContentSize |
| 153 | +{ |
| 154 | + return super.surface.intrinsicSize; |
| 155 | +} |
| 156 | + |
| 157 | +- (UIView *)loadingView |
| 158 | +{ |
| 159 | + return super.activityIndicatorViewFactory ? super.activityIndicatorViewFactory() : nil; |
| 160 | +} |
| 161 | + |
| 162 | +- (void)setLoadingView:(UIView *)loadingView |
| 163 | +{ |
| 164 | + super.activityIndicatorViewFactory = ^UIView *(void) { |
| 165 | + return loadingView; |
| 166 | + }; |
| 167 | +} |
| 168 | + |
| 169 | +#pragma mark RCTSurfaceDelegate proxying |
| 170 | + |
| 171 | +- (void)surface:(RCTSurface *)surface didChangeStage:(RCTSurfaceStage)stage |
| 172 | +{ |
| 173 | + [super surface:surface didChangeStage:stage]; |
| 174 | + if (RCTSurfaceStageIsRunning(stage)) { |
| 175 | + [super.surface.bridge.performanceLogger markStopForTag:RCTPLTTI]; |
| 176 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 177 | + [[NSNotificationCenter defaultCenter] postNotificationName:RCTContentDidAppearNotification |
| 178 | + object:self]; |
| 179 | + }); |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +- (void)surface:(RCTSurface *)surface didChangeIntrinsicSize:(CGSize)intrinsicSize |
| 184 | +{ |
| 185 | + [super surface:surface didChangeIntrinsicSize:intrinsicSize]; |
| 186 | + |
| 187 | + [_delegate rootViewDidChangeIntrinsicSize:(RCTRootView *)self]; |
| 188 | +} |
| 189 | + |
| 190 | +#pragma mark legacy |
| 191 | + |
| 192 | +- (UIViewController *)reactViewController |
| 193 | +{ |
| 194 | + return _reactViewController ?: [super reactViewController]; |
| 195 | +} |
| 196 | + |
| 197 | +#pragma mark unsupported |
| 198 | + |
| 199 | +- (void)cancelTouches |
| 200 | +{ |
| 201 | + // Not supported. |
| 202 | +} |
| 203 | + |
| 204 | +@end |
| 205 | + |
0 commit comments