react-native-macos/React/Base/RCTRootView.m

237 строки
6.1 KiB
Mathematica
Исходник Обычный вид История

/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#import "RCTRootView.h"
2015-04-02 17:33:21 +03:00
#import <objc/runtime.h>
#import "RCTBridge.h"
#import "RCTContextExecutor.h"
#import "RCTDevMenu.h"
#import "RCTEventDispatcher.h"
#import "RCTKeyCommands.h"
#import "RCTLog.h"
#import "RCTSourceCode.h"
#import "RCTTouchHandler.h"
#import "RCTUIManager.h"
#import "RCTUtils.h"
#import "RCTWebViewExecutor.h"
2015-03-26 12:58:06 +03:00
#import "UIView+React.h"
2015-04-02 17:33:21 +03:00
NSString *const RCTJavaScriptDidLoadNotification = @"RCTJavaScriptDidLoadNotification";
NSString *const RCTReloadNotification = @"RCTReloadNotification";
2015-04-02 17:33:21 +03:00
NSString *const RCTReloadViewsNotification = @"RCTReloadViewsNotification";
2015-03-26 04:59:42 +03:00
/**
* HACK(t6568049) This should be removed soon, hiding to prevent people from
* relying on it
*/
@interface RCTBridge (RCTRootView)
- (void)setJavaScriptExecutor:(id<RCTJavaScriptExecutor>)executor;
@end
2015-04-02 17:33:21 +03:00
@interface RCTUIManager (RCTRootView)
- (NSNumber *)allocateRootTag;
@end
@implementation RCTRootView
{
RCTDevMenu *_devMenu;
RCTBridge *_bridge;
RCTTouchHandler *_touchHandler;
2015-04-02 17:33:21 +03:00
NSString *_moduleName;
2015-03-25 03:37:03 +03:00
BOOL _registered;
2015-03-26 04:59:42 +03:00
NSDictionary *_launchOptions;
2015-04-02 17:33:21 +03:00
UIView *_contentView;
}
2015-04-02 17:33:21 +03:00
- (instancetype)initWithBridge:(RCTBridge *)bridge
moduleName:(NSString *)moduleName
{
2015-04-02 17:33:21 +03:00
RCTAssert(bridge, @"A bridge instance is required to create an RCTRootView");
RCTAssert(moduleName, @"A moduleName is required to create an RCTRootView");
2015-04-02 17:33:21 +03:00
if ((self = [super init])) {
#ifdef DEBUG
_enableDevMenu = YES;
#endif
2015-04-02 17:33:21 +03:00
_bridge = bridge;
_moduleName = moduleName;
self.backgroundColor = [UIColor whiteColor];
[self setUp];
}
return self;
}
2015-03-26 04:59:42 +03:00
- (instancetype)initWithBundleURL:(NSURL *)bundleURL
moduleName:(NSString *)moduleName
launchOptions:(NSDictionary *)launchOptions
{
2015-04-02 17:33:21 +03:00
RCTBridge *bridge = [[RCTBridge alloc] initWithBundlePath:bundleURL.absoluteString
moduleProvider:nil
launchOptions:launchOptions];
return [self initWithBridge:bridge
moduleName:moduleName];
}
- (void)dealloc
{
[self tearDown];
}
- (void)setUp
{
if (!_registered) {
/**
* Every root view that is created must have a unique react tag.
* Numbering of these tags goes from 1, 11, 21, 31, etc
*
* NOTE: Since the bridge persists, the RootViews might be reused, so now
* the react tag is assigned every time we load new content.
*/
_contentView = [[UIView alloc] init];
_contentView.reactTag = [_bridge.uiManager allocateRootTag];
_touchHandler = [[RCTTouchHandler alloc] initWithBridge:_bridge];
[_contentView addGestureRecognizer:_touchHandler];
[self addSubview:_contentView];
2015-04-02 17:33:21 +03:00
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reload)
name:RCTReloadViewsNotification
object:_bridge];
if (_bridge.loaded) {
[self bundleFinishedLoading];
} else {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(bundleFinishedLoading)
name:RCTJavaScriptDidLoadNotification
object:_bridge];
}
}
}
2015-04-02 17:33:21 +03:00
- (void)tearDown
{
2015-04-02 17:33:21 +03:00
if (_registered) {
_registered = NO;
[[NSNotificationCenter defaultCenter] removeObserver:self];
2015-04-02 17:33:21 +03:00
[_contentView removeGestureRecognizer:_touchHandler];
[_contentView removeFromSuperview];
[_touchHandler invalidate];
[_bridge enqueueJSCall:@"ReactIOS.unmountComponentAtNodeAndRemoveContainer"
args:@[_contentView.reactTag]];
}
}
2015-04-02 17:33:21 +03:00
- (BOOL)isValid
{
2015-04-02 17:33:21 +03:00
return _registered;
}
- (void)invalidate
{
[self tearDown];
}
- (UIViewController *)backingViewController {
return _backingViewController ?: [super backingViewController];
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if (motion == UIEventSubtypeMotionShake && self.enableDevMenu) {
if (!_devMenu) {
2015-04-02 17:33:21 +03:00
_devMenu = [[RCTDevMenu alloc] initWithBridge:self.bridge];
}
[_devMenu show];
}
}
RCT_IMPORT_METHOD(AppRegistry, runApplication)
RCT_IMPORT_METHOD(ReactIOS, unmountComponentAtNodeAndRemoveContainer)
2015-04-02 17:33:21 +03:00
- (void)bundleFinishedLoading
{
2015-04-02 17:33:21 +03:00
dispatch_async(dispatch_get_main_queue(), ^{
2015-03-25 03:37:03 +03:00
_registered = YES;
NSString *moduleName = _moduleName ?: @"";
NSDictionary *appParameters = @{
@"rootTag": _contentView.reactTag,
@"initialProps": self.initialProperties ?: @{},
};
2015-04-02 17:33:21 +03:00
[_bridge.uiManager registerRootView:_contentView];
[_bridge enqueueJSCall:@"AppRegistry.runApplication"
args:@[moduleName, appParameters]];
2015-04-02 17:33:21 +03:00
});
}
2015-04-02 17:33:21 +03:00
- (void)layoutSubviews
{
2015-04-02 17:33:21 +03:00
[super layoutSubviews];
_contentView.frame = self.bounds;
if (_registered) {
[_bridge.uiManager setFrame:self.frame forRootView:_contentView];
}
}
2015-04-02 17:33:21 +03:00
- (void)setFrame:(CGRect)frame
{
2015-04-02 17:33:21 +03:00
[super setFrame:frame];
_contentView.frame = self.bounds;
}
2015-04-02 17:33:21 +03:00
- (void)reload
{
2015-04-02 17:33:21 +03:00
[self tearDown];
[self setUp];
}
2015-04-02 17:33:21 +03:00
+ (void)reloadAll
{
2015-04-02 17:33:21 +03:00
[[NSNotificationCenter defaultCenter] postNotificationName:RCTReloadNotification
object:self];
}
2015-04-02 17:33:21 +03:00
- (NSNumber *)reactTag
{
2015-04-02 17:33:21 +03:00
return _contentView.reactTag;
}
- (void)startOrResetInteractionTiming
{
[_touchHandler startOrResetInteractionTiming];
}
- (NSDictionary *)endAndResetInteractionTiming
{
return [_touchHandler endAndResetInteractionTiming];
}
@end
2015-04-02 17:33:21 +03:00
@implementation RCTUIManager (RCTRootView)
- (NSNumber *)allocateRootTag
{
NSNumber *rootTag = objc_getAssociatedObject(self, _cmd) ?: @1;
objc_setAssociatedObject(self, _cmd, @(rootTag.integerValue + 10), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
return rootTag;
}
@end