Skip to content

Commit

Permalink
Introducing [RCTUIManager _executeBlockWithShadowView:forTag:]
Browse files Browse the repository at this point in the history
Summary:
New super simple abstraction in RCTUIManager.
Nothing really changed and RCTUIManager became shorter.

Reviewed By: rsnara

Differential Revision: D5990342

fbshipit-source-id: b38397b789a999168ac14625585065eda73d328f
  • Loading branch information
shergin authored and facebook-github-bot committed Nov 30, 2017
1 parent 343c5a9 commit 71b498b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 42 deletions.
2 changes: 1 addition & 1 deletion React/Modules/RCTUIManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ RCT_EXTERN NSString *const RCTUIManagerWillUpdateViewsDueToContentSizeMultiplier
* Use `UIViewNoIntrinsicMetric` to ignore a dimension.
* The `size` must NOT include padding and border.
*/
- (void)setIntrinsicContentSize:(CGSize)size forView:(UIView *)view;
- (void)setIntrinsicContentSize:(CGSize)intrinsicContentSize forView:(UIView *)view;

/**
* Update the background color of a view. The source of truth for
Expand Down
73 changes: 32 additions & 41 deletions React/Modules/RCTUIManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -327,39 +327,46 @@ - (RCTShadowView *)shadowViewForReactTag:(NSNumber *)reactTag
return _shadowViewRegistry[reactTag];
}

- (void)setAvailableSize:(CGSize)availableSize forRootView:(UIView *)rootView
- (void)_executeBlockWithShadowView:(void (^)(RCTShadowView *shadowView))block forTag:(NSNumber *)tag
{
RCTAssertMainQueue();
NSNumber *reactTag = rootView.reactTag;

RCTExecuteOnUIManagerQueue(^{
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);
RCTShadowView *shadowView = self->_shadowViewRegistry[tag];

if (CGSizeEqualToSize(availableSize, shadowView.availableSize)) {
if (shadowView == nil) {
RCTLogInfo(@"Could not locate shadow view with tag #%@, this is probably caused by a temporary inconsistency between native views and shadow views.", tag);
return;
}

shadowView.availableSize = availableSize;
[self setNeedsLayout];
block(shadowView);
});
}

- (void)setLocalData:(NSObject *)localData forView:(UIView *)view
- (void)setAvailableSize:(CGSize)availableSize forRootView:(UIView *)rootView
{
RCTAssertMainQueue();
NSNumber *tag = view.reactTag;
[self _executeBlockWithShadowView:^(RCTShadowView *shadowView) {
RCTAssert([shadowView isKindOfClass:[RCTRootShadowView class]], @"Located shadow view is actually not root view.");

RCTExecuteOnUIManagerQueue(^{
RCTShadowView *shadowView = self->_shadowViewRegistry[tag];
if (shadowView == nil) {
RCTLogWarn(@"Could not locate shadow view with tag #%@, this is probably caused by a temporary inconsistency between native views and shadow views.", tag);
RCTRootShadowView *rootShadowView = (RCTRootShadowView *)shadowView;

if (CGSizeEqualToSize(availableSize, rootShadowView.availableSize)) {
return;
}

rootShadowView.availableSize = availableSize;
[self setNeedsLayout];
} forTag:rootView.reactTag];
}

- (void)setLocalData:(NSObject *)localData forView:(UIView *)view
{
RCTAssertMainQueue();
[self _executeBlockWithShadowView:^(RCTShadowView *shadowView) {
shadowView.localData = localData;
[self setNeedsLayout];
});
} forTag:view.reactTag];
}

/**
Expand Down Expand Up @@ -392,56 +399,40 @@ - (UIView *)_lookupViewForNativeID:(NSString *)nativeID inView:(UIView *)view
- (void)setSize:(CGSize)size forView:(UIView *)view
{
RCTAssertMainQueue();

NSNumber *reactTag = view.reactTag;
RCTExecuteOnUIManagerQueue(^{
RCTShadowView *shadowView = self->_shadowViewRegistry[reactTag];
RCTAssert(shadowView != nil, @"Could not locate shadow view with tag #%@", reactTag);

[self _executeBlockWithShadowView:^(RCTShadowView *shadowView) {
if (CGSizeEqualToSize(size, shadowView.size)) {
return;
}

shadowView.size = size;
[self setNeedsLayout];
});
} forTag:view.reactTag];
}

- (void)setIntrinsicContentSize:(CGSize)size forView:(UIView *)view
- (void)setIntrinsicContentSize:(CGSize)intrinsicContentSize forView:(UIView *)view
{
RCTAssertMainQueue();

NSNumber *reactTag = view.reactTag;
RCTExecuteOnUIManagerQueue(^{
RCTShadowView *shadowView = self->_shadowViewRegistry[reactTag];
if (shadowView == nil) {
RCTLogWarn(@"Could not locate shadow view with tag #%@, this is probably caused by a temporary inconsistency between native views and shadow views.", reactTag);
[self _executeBlockWithShadowView:^(RCTShadowView *shadowView) {
if (CGSizeEqualToSize(shadowView.intrinsicContentSize, intrinsicContentSize)) {
return;
}

if (!CGSizeEqualToSize(shadowView.intrinsicContentSize, size)) {
shadowView.intrinsicContentSize = size;
[self setNeedsLayout];
}
});

shadowView.intrinsicContentSize = intrinsicContentSize;
} forTag:view.reactTag];
}

- (void)setBackgroundColor:(UIColor *)color forView:(UIView *)view
{
RCTAssertMainQueue();

NSNumber *reactTag = view.reactTag;
RCTExecuteOnUIManagerQueue(^{
[self _executeBlockWithShadowView:^(RCTShadowView *shadowView) {
if (!self->_viewRegistry) {
return;
}

RCTShadowView *shadowView = self->_shadowViewRegistry[reactTag];
RCTAssert(shadowView != nil, @"Could not locate root view with tag #%@", reactTag);
shadowView.backgroundColor = color;
[self _amendPendingUIBlocksWithStylePropagationUpdateForShadowView:shadowView];
[self flushUIBlocks];
});
} forTag:view.reactTag];
}

/**
Expand Down

0 comments on commit 71b498b

Please sign in to comment.