Simplifying mess with RCTRootViewSizeFlexibility

Summary:
Now things look much more clear, I hope.
This diff:
 * Introduces new property of `RCTRootShadowView` `availableSize` which represents exactly what we transmit to layout engine;
 * Illuminates conflict between logical `availableSize` and explicitly specified size of DOM node (current `size`);
 * Splits overcomplicated `setSize:forView:` method into two unrelated ones;
 * Changes actual values of `RCTRootViewSizeFlexibility` enum constants for simpler usage;
 * Completely removes `sizeFlexibility` concept from `RCTRootShadowView` (in favor of special values of `availableSize`);
 * Makes the code clearer finally.

This is beginning of big effort to improve `RCTRootView` and co.

Reviewed By: mmmulani

Differential Revision: D4562834

fbshipit-source-id: f5baaf2859ea430d44645a6b5d35f222f15a668e
This commit is contained in:
Valentin Shergin
2017-02-19 23:05:42 -08:00
committed by Facebook Github Bot
parent 4a893ee071
commit ba170ec78c
6 changed files with 95 additions and 64 deletions

View File

@@ -58,7 +58,7 @@ RCT_EXTERN NSString *const RCTUIManagerRootViewKey;
/**
* Register a root view with the RCTUIManager.
*/
- (void)registerRootView:(UIView *)rootView withSizeFlexibility:(RCTRootViewSizeFlexibility)sizeFlexibility;
- (void)registerRootView:(UIView *)rootView;
/**
* Gets the view name associated with a reactTag.
@@ -70,6 +70,15 @@ RCT_EXTERN NSString *const RCTUIManagerRootViewKey;
*/
- (UIView *)viewForReactTag:(NSNumber *)reactTag;
/**
* Set the available size (`availableSize` property) for a root view.
* This might be used in response to changes in external layout constraints.
* This value will be directly trasmitted to layout engine and defines how big viewport is;
* this value does not affect root node size style properties.
* Can be considered as something similar to `setSize:forView:` but applicable only for root view.
*/
- (void)setAvailableSize:(CGSize)availableSize forRootView:(UIView *)rootView;
/**
* Set the size of a view. This might be in response to a screen rotation
* or some other layout event outside of the React-managed view hierarchy.
@@ -149,6 +158,14 @@ RCT_EXTERN NSString *const RCTUIManagerRootViewKey;
- (void)setFrame:(CGRect)frame forView:(UIView *)view
__deprecated_msg("Use `setSize:forView:` or `setIntrinsicContentSize:forView:` instead.");
/**
* This method is deprecated and will be removed in next releases.
* Use `registerRootView:` instead. There is no need to specify `sizeFlexibility` anymore.
*/
- (void)registerRootView:(UIView *)rootView withSizeFlexibility:(RCTRootViewSizeFlexibility)sizeFlexibility
__deprecated_msg("Use `registerRootView:` instead.");
@end
/**

View File

@@ -376,7 +376,7 @@ dispatch_queue_t RCTGetUIManagerQueue(void)
return RCTGetUIManagerQueue();
}
- (void)registerRootView:(UIView *)rootView withSizeFlexibility:(RCTRootViewSizeFlexibility)sizeFlexibility
- (void)registerRootView:(UIView *)rootView
{
RCTAssertMainQueue();
@@ -390,7 +390,6 @@ dispatch_queue_t RCTGetUIManagerQueue(void)
// Register view
_viewRegistry[reactTag] = rootView;
CGSize size = rootView.bounds.size;
// Register shadow view
dispatch_async(RCTGetUIManagerQueue(), ^{
@@ -400,10 +399,8 @@ dispatch_queue_t RCTGetUIManagerQueue(void)
RCTRootShadowView *shadowView = [RCTRootShadowView new];
shadowView.reactTag = reactTag;
shadowView.size = size;
shadowView.backgroundColor = rootView.backgroundColor;
shadowView.viewName = NSStringFromClass([rootView class]);
shadowView.sizeFlexibility = sizeFlexibility;
self->_shadowViewRegistry[shadowView.reactTag] = shadowView;
[self->_rootViewTags addObject:reactTag];
});
@@ -425,44 +422,39 @@ dispatch_queue_t RCTGetUIManagerQueue(void)
return _viewRegistry[reactTag];
}
- (void)setAvailableSize:(CGSize)availableSize forRootView:(UIView *)rootView
{
RCTAssertMainQueue();
NSNumber *reactTag = rootView.reactTag;
dispatch_async(RCTGetUIManagerQueue(), ^{
RCTRootShadowView *shadowView = (RCTRootShadowView *)self->_shadowViewRegistry[reactTag];
RCTAssert(shadowView != nil, @"Could not locate shadow view with tag #%@", reactTag);
RCTAssert([shadowView isKindOfClass:[RCTRootShadowView class]], @"Located shadow view (with tag #%@) is actually not root view.", reactTag);
if (CGSizeEqualToSize(availableSize, shadowView.availableSize)) {
return;
}
shadowView.availableSize = availableSize;
[self setNeedsLayout];
});
}
- (void)setSize:(CGSize)size forView:(UIView *)view
{
RCTAssertMainQueue();
// The following variable has no meaning if the view is not a react root view
RCTRootViewSizeFlexibility sizeFlexibility = RCTRootViewSizeFlexibilityNone;
if (RCTIsReactRootView(view.reactTag)) {
RCTRootView *rootView = (RCTRootView *)[view superview];
if (rootView != nil) {
sizeFlexibility = rootView.sizeFlexibility;
}
}
NSNumber *reactTag = view.reactTag;
dispatch_async(RCTGetUIManagerQueue(), ^{
RCTShadowView *shadowView = self->_shadowViewRegistry[reactTag];
RCTAssert(shadowView != nil, @"Could not locate shadow view with tag #%@", reactTag);
BOOL needsLayout = NO;
if (!CGSizeEqualToSize(size, shadowView.size)) {
shadowView.size = size;
needsLayout = YES;
if (CGSizeEqualToSize(size, shadowView.size)) {
return;
}
// Trigger re-layout when size flexibility changes, as the root view might grow or
// shrink in the flexible dimensions.
if (RCTIsReactRootView(reactTag)) {
RCTRootShadowView *rootShadowView = (RCTRootShadowView *)shadowView;
if (rootShadowView.sizeFlexibility != sizeFlexibility) {
rootShadowView.sizeFlexibility = sizeFlexibility;
needsLayout = YES;
}
}
if (needsLayout) {
[self setNeedsLayout];
}
shadowView.size = size;
[self setNeedsLayout];
});
}
@@ -1652,6 +1644,12 @@ static UIView *_jsResponder;
@implementation RCTUIManager (Deprecated)
- (void)registerRootView:(UIView *)rootView withSizeFlexibility:(RCTRootViewSizeFlexibility)sizeFlexibility
{
RCTLogWarn(@"Calling of `[-RCTUIManager registerRootView:withSizeFlexibility:]` which is deprecated.");
[self registerRootView:rootView];
}
- (void)setFrame:(CGRect)frame forView:(UIView *)view
{
RCTLogWarn(@"Calling of `[-RCTUIManager setFrame:forView:]` which is deprecated.");