-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
RNSFullWindowOverlay.mm
199 lines (167 loc) · 4.73 KB
/
RNSFullWindowOverlay.mm
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#import <UIKit/UIKit.h>
#import "RNSFullWindowOverlay.h"
#ifdef RN_FABRIC_ENABLED
#import <React/RCTConversions.h>
#import <React/RCTFabricComponentsPlugins.h>
#import <React/RCTSurfaceTouchHandler.h>
#import <react/renderer/components/rnscreens/ComponentDescriptors.h>
#import <react/renderer/components/rnscreens/Props.h>
#import <react/renderer/components/rnscreens/RCTComponentViewHelpers.h>
#else
#import <React/RCTTouchHandler.h>
#endif // RN_FABRIC_ENABLED
@implementation RNSFullWindowOverlayContainer
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
for (UIView *view in [self subviews]) {
if (view.userInteractionEnabled && [view pointInside:[self convertPoint:point toView:view] withEvent:event]) {
return YES;
}
}
return NO;
}
@end
@implementation RNSFullWindowOverlay {
__weak RCTBridge *_bridge;
RNSFullWindowOverlayContainer *_container;
CGRect _reactFrame;
#ifdef RN_FABRIC_ENABLED
RCTSurfaceTouchHandler *_touchHandler;
#else
RCTTouchHandler *_touchHandler;
#endif // RN_FABRIC_ENABLED
}
#ifdef RN_FABRIC_ENABLED
- (instancetype)init
{
if (self = [super init]) {
static const auto defaultProps = std::make_shared<const facebook::react::RNSFullWindowOverlayProps>();
_props = defaultProps;
[self _initCommon];
}
return self;
}
#endif // RN_FABRIC_ENABLED
- (instancetype)initWithBridge:(RCTBridge *)bridge
{
if (self = [super init]) {
_bridge = bridge;
[self _initCommon];
}
return self;
}
- (void)_initCommon
{
_reactFrame = CGRectNull;
_container = self.container;
[self show];
}
- (void)addSubview:(UIView *)view
{
[_container addSubview:view];
}
- (RNSFullWindowOverlayContainer *)container
{
if (_container == nil) {
_container = [[RNSFullWindowOverlayContainer alloc] initWithFrame:_reactFrame];
}
return _container;
}
- (void)show
{
UIWindow *window = RCTSharedApplication().delegate.window;
[window addSubview:_container];
}
- (void)didMoveToWindow
{
if (self.window == nil) {
if (_container != nil) {
[_container removeFromSuperview];
[_touchHandler detachFromView:_container];
}
} else {
if (_touchHandler == nil) {
#ifdef RN_FABRIC_ENABLED
_touchHandler = [RCTSurfaceTouchHandler new];
#else
_touchHandler = [[RCTTouchHandler alloc] initWithBridge:_bridge];
#endif
}
[_touchHandler attachToView:_container];
}
}
#ifdef RN_FABRIC_ENABLED
#pragma mark - Fabric Specific
// When the component unmounts we remove it from window's children,
// so when the component gets recycled we need to add it back.
- (void)maybeShow
{
UIWindow *window = RCTSharedApplication().delegate.window;
if (![[window subviews] containsObject:self]) {
[window addSubview:_container];
}
}
+ (facebook::react::ComponentDescriptorProvider)componentDescriptorProvider
{
return facebook::react::concreteComponentDescriptorProvider<
facebook::react::RNSFullWindowOverlayComponentDescriptor>();
}
- (void)prepareForRecycle
{
[_container removeFromSuperview];
// Due to view recycling we don't really want to set _container = nil
// as it won't be instantiated when the component appears for the second time.
// We could consider nulling in here & using container (lazy getter) everywhere else.
// _container = nil;
[super prepareForRecycle];
}
- (void)mountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index
{
// When the component unmounts we remove it from window's children,
// so when the component gets recycled we need to add it back.
// As for now it is called here as we lack of method that is called
// just before component gets restored (from recycle pool).
[self maybeShow];
[self addSubview:childComponentView];
}
- (void)unmountChildComponentView:(UIView<RCTComponentViewProtocol> *)childComponentView index:(NSInteger)index
{
[childComponentView removeFromSuperview];
}
- (void)updateLayoutMetrics:(facebook::react::LayoutMetrics const &)layoutMetrics
oldLayoutMetrics:(facebook::react::LayoutMetrics const &)oldLayoutMetrics
{
CGRect frame = RCTCGRectFromRect(layoutMetrics.frame);
_reactFrame = frame;
[_container setFrame:frame];
}
#else
#pragma mark - Paper specific
- (void)reactSetFrame:(CGRect)frame
{
_reactFrame = frame;
[_container setFrame:frame];
}
- (void)invalidate
{
[_container removeFromSuperview];
_container = nil;
}
#endif // RN_FABRIC_ENABLED
@end
#ifdef RN_FABRIC_ENABLED
Class<RCTComponentViewProtocol> RNSFullWindowOverlayCls(void)
{
return RNSFullWindowOverlay.class;
}
#endif // RN_FABRIC_ENABLED
@implementation RNSFullWindowOverlayManager
RCT_EXPORT_MODULE()
#ifdef RN_FABRIC_ENABLED
#else
- (UIView *)view
{
return [[RNSFullWindowOverlay alloc] initWithBridge:self.bridge];
}
#endif // RN_FABRIC_ENABLED
@end