RCTWrapperShadowView.m
3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2004-present Facebook. All Rights Reserved.
#import "RCTWrapperShadowView.h"
#import <React/RCTBridge.h>
#import <React/RCTUIManager.h>
#import <React/RCTShadowView+Layout.h>
#import "RCTWrapperView.h"
@implementation RCTWrapperShadowView
{
__weak RCTBridge *_bridge;
RCTWrapperMeasureBlock _measureBlock;
CGSize _intrinsicContentSize;
}
- (instancetype)initWithBridge:(RCTBridge *)bridge
{
if (self = [super init]) {
_bridge = bridge;
YGNodeSetMeasureFunc(self.yogaNode, RCTWrapperShadowViewMeasure);
}
return self;
}
static YGSize RCTWrapperShadowViewMeasure(YGNodeRef node, float width, YGMeasureMode widthMode, float height, YGMeasureMode heightMode)
{
CGSize minimumSize = CGSizeMake(0, 0);
CGSize maximumSize = CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX);
switch (widthMode) {
case YGMeasureModeUndefined:
break;
case YGMeasureModeExactly:
minimumSize.width = width;
maximumSize.width = width;
break;
case YGMeasureModeAtMost:
maximumSize.width = width;
break;
}
switch (heightMode) {
case YGMeasureModeUndefined:
break;
case YGMeasureModeExactly:
minimumSize.height = height;
maximumSize.height = height;
break;
case YGMeasureModeAtMost:
maximumSize.height = height;
break;
}
RCTWrapperShadowView *shadowView = (__bridge RCTWrapperShadowView *)YGNodeGetContext(node);
CGSize size = [shadowView measureWithMinimumSize:minimumSize maximumSize:maximumSize];
return (YGSize){
RCTYogaFloatFromCoreGraphicsFloat(size.width),
RCTYogaFloatFromCoreGraphicsFloat(size.height)
};
}
- (CGSize)measureWithMinimumSize:(CGSize)minimumSize maximumSize:(CGSize)maximumSize
{
dispatch_time_t timeout = dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC);
if (!_measureBlock) {
RCTBridge *bridge = _bridge;
__block RCTWrapperMeasureBlock measureBlock;
NSNumber *reactTag = self.reactTag;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_main_queue(), ^{
RCTUIManager *uiManager = bridge.uiManager;
RCTWrapperView *view = (RCTWrapperView *)[uiManager viewForReactTag:reactTag];
measureBlock = view.measureBlock;
dispatch_semaphore_signal(semaphore);
});
if (dispatch_semaphore_wait(semaphore, timeout)) {
RCTLogError(@"Unable to retrieve `measureBlock` for view (%@) because the main thread is busy.", self);
}
_measureBlock = measureBlock;
}
if (!_measureBlock) {
return maximumSize;
}
__block CGSize size = maximumSize;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_main_queue(), ^{
size = self->_measureBlock(minimumSize, maximumSize);
dispatch_semaphore_signal(semaphore);
});
if (dispatch_semaphore_wait(semaphore, timeout)) {
RCTLogError(@"Unable to compute layout for view (%@) because the main thread is busy.", self);
}
return size;
}
- (BOOL)isYogaLeafNode
{
return YES;
}
- (CGSize)intrinsicContentSize
{
return _intrinsicContentSize;
}
- (void)setIntrinsicContentSize:(CGSize)size
{
_intrinsicContentSize = size;
YGNodeMarkDirty(self.yogaNode);
}
@end