2017-12-04 21:31:40 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2017-12-04 21:31:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTSurfaceHostingView.h"
|
|
|
|
|
|
|
|
#import "RCTDefines.h"
|
|
|
|
#import "RCTSurface.h"
|
|
|
|
#import "RCTSurfaceDelegate.h"
|
|
|
|
#import "RCTSurfaceView.h"
|
|
|
|
#import "RCTUtils.h"
|
|
|
|
|
2018-03-03 03:32:09 +00:00
|
|
|
@interface RCTSurfaceHostingView ()
|
2017-12-04 21:31:40 +00:00
|
|
|
|
|
|
|
@property (nonatomic, assign) BOOL isActivityIndicatorViewVisible;
|
|
|
|
@property (nonatomic, assign) BOOL isSurfaceViewVisible;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTSurfaceHostingView {
|
|
|
|
UIView *_Nullable _activityIndicatorView;
|
|
|
|
UIView *_Nullable _surfaceView;
|
|
|
|
RCTSurfaceStage _stage;
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)initWithFrame:(CGRect)frame)
|
|
|
|
RCT_NOT_IMPLEMENTED(- (nullable instancetype)initWithCoder:(NSCoder *)coder)
|
|
|
|
|
|
|
|
- (instancetype)initWithBridge:(RCTBridge *)bridge
|
|
|
|
moduleName:(NSString *)moduleName
|
|
|
|
initialProperties:(NSDictionary *)initialProperties
|
2018-03-20 00:33:04 +00:00
|
|
|
sizeMeasureMode:(RCTSurfaceSizeMeasureMode)sizeMeasureMode
|
2017-12-04 21:31:40 +00:00
|
|
|
{
|
2018-03-06 02:29:39 +00:00
|
|
|
RCTSurface *surface = [self createSurfaceWithBridge:bridge moduleName:moduleName initialProperties:initialProperties];
|
2018-03-20 00:33:04 +00:00
|
|
|
return [self initWithSurface:surface sizeMeasureMode:sizeMeasureMode];
|
2018-03-03 03:32:09 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 00:33:04 +00:00
|
|
|
- (instancetype)initWithSurface:(RCTSurface *)surface sizeMeasureMode:(RCTSurfaceSizeMeasureMode)sizeMeasureMode
|
2017-12-04 21:31:40 +00:00
|
|
|
{
|
|
|
|
if (self = [super initWithFrame:CGRectZero]) {
|
|
|
|
_surface = surface;
|
2018-03-20 00:33:04 +00:00
|
|
|
_sizeMeasureMode = sizeMeasureMode;
|
2017-12-04 21:31:40 +00:00
|
|
|
|
|
|
|
_surface.delegate = self;
|
|
|
|
_stage = surface.stage;
|
|
|
|
[self _updateViews];
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-03-06 02:29:39 +00:00
|
|
|
- (RCTSurface *)createSurfaceWithBridge:(RCTBridge *)bridge
|
|
|
|
moduleName:(NSString *)moduleName
|
|
|
|
initialProperties:(NSDictionary *)initialProperties
|
|
|
|
{
|
|
|
|
return [[RCTSurface alloc] initWithBridge:bridge moduleName:moduleName initialProperties:initialProperties];
|
|
|
|
}
|
|
|
|
|
2018-01-12 02:34:34 +00:00
|
|
|
- (void)setFrame:(CGRect)frame
|
|
|
|
{
|
|
|
|
[super setFrame:frame];
|
|
|
|
|
|
|
|
CGSize minimumSize;
|
|
|
|
CGSize maximumSize;
|
|
|
|
|
|
|
|
RCTSurfaceMinimumSizeAndMaximumSizeFromSizeAndSizeMeasureMode(
|
|
|
|
self.bounds.size,
|
|
|
|
_sizeMeasureMode,
|
2018-04-09 05:57:49 +00:00
|
|
|
&minimumSize,
|
|
|
|
&maximumSize
|
2018-01-12 02:34:34 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
[_surface setMinimumSize:minimumSize
|
|
|
|
maximumSize:maximumSize];
|
|
|
|
}
|
|
|
|
|
2017-12-04 21:31:40 +00:00
|
|
|
- (CGSize)intrinsicContentSize
|
|
|
|
{
|
|
|
|
if (RCTSurfaceStageIsPreparing(_stage)) {
|
|
|
|
if (_activityIndicatorView) {
|
|
|
|
return _activityIndicatorView.intrinsicContentSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CGSizeZero;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _surface.intrinsicSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (CGSize)sizeThatFits:(CGSize)size
|
|
|
|
{
|
|
|
|
if (RCTSurfaceStageIsPreparing(_stage)) {
|
|
|
|
if (_activityIndicatorView) {
|
|
|
|
return [_activityIndicatorView sizeThatFits:size];
|
|
|
|
}
|
|
|
|
|
|
|
|
return CGSizeZero;
|
|
|
|
}
|
|
|
|
|
2018-01-12 02:34:34 +00:00
|
|
|
CGSize minimumSize;
|
|
|
|
CGSize maximumSize;
|
2017-12-04 21:31:40 +00:00
|
|
|
|
2018-01-12 02:34:34 +00:00
|
|
|
RCTSurfaceMinimumSizeAndMaximumSizeFromSizeAndSizeMeasureMode(
|
|
|
|
size,
|
|
|
|
_sizeMeasureMode,
|
2018-04-09 05:57:49 +00:00
|
|
|
&minimumSize,
|
|
|
|
&maximumSize
|
2018-01-12 02:34:34 +00:00
|
|
|
);
|
2017-12-04 21:31:40 +00:00
|
|
|
|
2017-12-16 01:20:01 +00:00
|
|
|
return [_surface sizeThatFitsMinimumSize:minimumSize
|
2017-12-04 21:31:40 +00:00
|
|
|
maximumSize:maximumSize];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setStage:(RCTSurfaceStage)stage
|
|
|
|
{
|
|
|
|
if (stage == _stage) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL shouldInvalidateLayout =
|
|
|
|
RCTSurfaceStageIsRunning(stage) != RCTSurfaceStageIsRunning(_stage) ||
|
|
|
|
RCTSurfaceStageIsPreparing(stage) != RCTSurfaceStageIsPreparing(_stage);
|
|
|
|
|
|
|
|
_stage = stage;
|
|
|
|
|
|
|
|
if (shouldInvalidateLayout) {
|
|
|
|
[self _invalidateLayout];
|
|
|
|
[self _updateViews];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setSizeMeasureMode:(RCTSurfaceSizeMeasureMode)sizeMeasureMode
|
|
|
|
{
|
|
|
|
if (sizeMeasureMode == _sizeMeasureMode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_sizeMeasureMode = sizeMeasureMode;
|
|
|
|
[self _invalidateLayout];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - isActivityIndicatorViewVisible
|
|
|
|
|
|
|
|
- (void)setIsActivityIndicatorViewVisible:(BOOL)visible
|
|
|
|
{
|
|
|
|
if (_isActivityIndicatorViewVisible == visible) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-12 02:34:34 +00:00
|
|
|
_isActivityIndicatorViewVisible = visible;
|
|
|
|
|
2017-12-04 21:31:40 +00:00
|
|
|
if (visible) {
|
|
|
|
if (_activityIndicatorViewFactory) {
|
|
|
|
_activityIndicatorView = _activityIndicatorViewFactory();
|
|
|
|
_activityIndicatorView.frame = self.bounds;
|
|
|
|
_activityIndicatorView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
|
|
|
[self addSubview:_activityIndicatorView];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
[_activityIndicatorView removeFromSuperview];
|
|
|
|
_activityIndicatorView = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - isSurfaceViewVisible
|
|
|
|
|
|
|
|
- (void)setIsSurfaceViewVisible:(BOOL)visible
|
|
|
|
{
|
|
|
|
if (_isSurfaceViewVisible == visible) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-12 02:34:34 +00:00
|
|
|
_isSurfaceViewVisible = visible;
|
|
|
|
|
2017-12-04 21:31:40 +00:00
|
|
|
if (visible) {
|
|
|
|
_surfaceView = _surface.view;
|
|
|
|
_surfaceView.frame = self.bounds;
|
|
|
|
_surfaceView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
|
|
|
[self addSubview:_surfaceView];
|
|
|
|
} else {
|
|
|
|
[_surfaceView removeFromSuperview];
|
|
|
|
_surfaceView = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - activityIndicatorViewFactory
|
|
|
|
|
|
|
|
- (void)setActivityIndicatorViewFactory:(RCTSurfaceHostingViewActivityIndicatorViewFactory)activityIndicatorViewFactory
|
|
|
|
{
|
|
|
|
_activityIndicatorViewFactory = activityIndicatorViewFactory;
|
|
|
|
if (_isActivityIndicatorViewVisible) {
|
2018-01-12 02:34:34 +00:00
|
|
|
self.isActivityIndicatorViewVisible = NO;
|
2017-12-04 21:31:40 +00:00
|
|
|
self.isActivityIndicatorViewVisible = YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Private stuff
|
|
|
|
|
|
|
|
- (void)_invalidateLayout
|
|
|
|
{
|
2018-01-12 02:34:34 +00:00
|
|
|
[self invalidateIntrinsicContentSize];
|
2017-12-04 21:31:40 +00:00
|
|
|
[self.superview setNeedsLayout];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)_updateViews
|
|
|
|
{
|
|
|
|
self.isSurfaceViewVisible = RCTSurfaceStageIsRunning(_stage);
|
|
|
|
self.isActivityIndicatorViewVisible = RCTSurfaceStageIsPreparing(_stage);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)didMoveToWindow
|
|
|
|
{
|
|
|
|
[super didMoveToWindow];
|
|
|
|
[self _updateViews];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - RCTSurfaceDelegate
|
|
|
|
|
|
|
|
- (void)surface:(RCTSurface *)surface didChangeStage:(RCTSurfaceStage)stage
|
|
|
|
{
|
|
|
|
RCTExecuteOnMainQueue(^{
|
|
|
|
[self setStage:stage];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)surface:(RCTSurface *)surface didChangeIntrinsicSize:(CGSize)intrinsicSize
|
|
|
|
{
|
|
|
|
RCTExecuteOnMainQueue(^{
|
|
|
|
[self _invalidateLayout];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|