forked from AppAndFlow/react-native-safe-area-context
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRNCSafeAreaViewComponentView.mm
More file actions
182 lines (152 loc) · 5.76 KB
/
Copy pathRNCSafeAreaViewComponentView.mm
File metadata and controls
182 lines (152 loc) · 5.76 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
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
#import "RNCSafeAreaViewComponentView.h"
#import <react/renderer/components/safeareacontext/EventEmitters.h>
#import <react/renderer/components/safeareacontext/Props.h>
#import <react/renderer/components/safeareacontext/RCTComponentViewHelpers.h>
#import <react/renderer/components/safeareacontext/RNCSafeAreaViewComponentDescriptor.h>
#import <react/renderer/components/safeareacontext/RNCSafeAreaViewShadowNode.h>
#import <React/RCTConversions.h>
#import <React/RCTFabricComponentsPlugins.h>
#import "RNCSafeAreaProviderComponentView.h"
#import "RNCSafeAreaUtils.h"
using namespace facebook::react;
@interface RNCSafeAreaViewComponentView () <RCTRNCSafeAreaViewViewProtocol>
@end
@implementation RNCSafeAreaViewComponentView {
RNCSafeAreaViewShadowNode::ConcreteState::Shared _state;
UIEdgeInsets _currentSafeAreaInsets;
__weak UIView *_Nullable _providerView;
}
// Needed because of this: https://github.com/facebook/react-native/pull/37274
+ (void)load
{
[super load];
}
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
static const auto defaultProps = std::make_shared<const RNCSafeAreaViewProps>();
_props = defaultProps;
}
return self;
}
- (NSString *)description
{
NSString *superDescription = [super description];
// Cutting the last `>` character.
if (superDescription.length > 0 && [superDescription characterAtIndex:superDescription.length - 1] == '>') {
superDescription = [superDescription substringToIndex:superDescription.length - 1];
}
#if TARGET_OS_IPHONE
NSString *providerViewSafeAreaInsetsString = NSStringFromUIEdgeInsets(_providerView.safeAreaInsets);
NSString *currentSafeAreaInsetsString = NSStringFromUIEdgeInsets(_currentSafeAreaInsets);
#elif TARGET_OS_OSX
NSString *providerViewSafeAreaInsetsString;
NSString *currentSafeAreaInsetsString;
if (@available(macOS 11.0, *)) {
providerViewSafeAreaInsetsString = [NSString stringWithFormat:@"{%f,%f,%f,%f}",
_providerView.safeAreaInsets.top,
_providerView.safeAreaInsets.left,
_providerView.safeAreaInsets.bottom,
_providerView.safeAreaInsets.right];
currentSafeAreaInsetsString = [NSString stringWithFormat:@"{%f,%f,%f,%f}",
_currentSafeAreaInsets.top,
_currentSafeAreaInsets.left,
_currentSafeAreaInsets.bottom,
_currentSafeAreaInsets.right];
} else {
providerViewSafeAreaInsetsString = @"{0.0,0.0,0.0,0.0}";
currentSafeAreaInsetsString = @"{0.0,0.0,0.0,0.0}";
}
#endif
return [NSString stringWithFormat:@"%@; RNCSafeAreaInsets = %@; appliedRNCSafeAreaInsets = %@>",
superDescription,
providerViewSafeAreaInsetsString,
currentSafeAreaInsetsString];
}
- (void)didMoveToWindow
{
UIView *previousProviderView = _providerView;
_providerView = [self findNearestProvider];
[self updateStateIfNecessary];
if (previousProviderView != _providerView) {
[NSNotificationCenter.defaultCenter removeObserver:self name:RNCSafeAreaDidChange object:previousProviderView];
[NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(safeAreaProviderInsetsDidChange:)
name:RNCSafeAreaDidChange
object:_providerView];
}
}
- (void)safeAreaProviderInsetsDidChange:(NSNotification *)notification
{
[self updateStateIfNecessary];
}
- (void)updateStateIfNecessary
{
if (_providerView == nil) {
return;
}
#if TARGET_OS_IPHONE
UIEdgeInsets safeAreaInsets = _providerView.safeAreaInsets;
if (UIEdgeInsetsEqualToEdgeInsetsWithThreshold(safeAreaInsets, _currentSafeAreaInsets, 1.0 / RCTScreenScale())) {
return;
}
#elif TARGET_OS_OSX
NSEdgeInsets safeAreaInsets = _providerView.safeAreaInsets;
if (NSEdgeInsetsEqualToEdgeInsetsWithThreshold(safeAreaInsets, _currentSafeAreaInsets, 1.0 / RCTScreenScale())) {
return;
}
#endif
_currentSafeAreaInsets = safeAreaInsets;
[self updateState];
}
- (UIView *)findNearestProvider
{
UIView *current = self.superview;
while (current != nil) {
if ([current isKindOfClass:RNCSafeAreaProviderComponentView.class]) {
return current;
}
current = current.superview;
}
return self;
}
- (void)updateState
{
if (!_state) {
return;
}
_state->updateState(
[=](RNCSafeAreaViewShadowNode::ConcreteState::Data const &oldData)
-> RNCSafeAreaViewShadowNode::ConcreteState::SharedData {
auto newData = oldData;
newData.insets = RCTEdgeInsetsFromUIEdgeInsets(_currentSafeAreaInsets);
return std::make_shared<RNCSafeAreaViewShadowNode::ConcreteState::Data const>(newData);
});
}
#pragma mark - RCTComponentViewProtocol
+ (ComponentDescriptorProvider)componentDescriptorProvider
{
return concreteComponentDescriptorProvider<RNCSafeAreaViewComponentDescriptor>();
}
- (void)updateState:(State::Shared const &)state oldState:(State::Shared const &)oldState
{
_state = std::static_pointer_cast<RNCSafeAreaViewShadowNode::ConcreteState const>(state);
}
- (void)finalizeUpdates:(RNComponentViewUpdateMask)updateMask
{
[super finalizeUpdates:updateMask];
[self updateStateIfNecessary];
}
- (void)prepareForRecycle
{
[super prepareForRecycle];
[NSNotificationCenter.defaultCenter removeObserver:self];
_state.reset();
_providerView = nil;
_currentSafeAreaInsets = UIEdgeInsetsZero;
}
@end
Class<RCTComponentViewProtocol> RNCSafeAreaViewCls(void)
{
return RNCSafeAreaViewComponentView.class;
}