Skip to content

Commit 9d32bbf

Browse files
sammy-SCfacebook-github-bot
authored andcommitted
[skip ci] migrate RCTSafeArea away from RCTUnsafeExecuteOnMainQueueSync (facebook#49440)
Summary: changelog: [internal] move away from RCTUnsafeExecuteOnMainQueueSync in RCTSafeArea. Differential Revision: D69662510
1 parent 71bd096 commit 9d32bbf

File tree

5 files changed

+132
-0
lines changed

5 files changed

+132
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import <Foundation/Foundation.h>
9+
10+
void RCTInitializeUIKitProxies(void);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import "RCTInitializeUIKitProxies.h"
9+
#import "RCTWindowSafeAreaProxy.h"
10+
11+
void RCTInitializeUIKitProxies(void)
12+
{
13+
static dispatch_once_t onceToken;
14+
dispatch_once(&onceToken, ^{
15+
[[RCTWindowSafeAreaProxy sharedInstance] startObservingSafeArea];
16+
});
17+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import <UIKit/UIKit.h>
9+
10+
NS_ASSUME_NONNULL_BEGIN
11+
12+
@interface RCTWindowSafeAreaProxy : NSObject
13+
14+
+ (instancetype)sharedInstance;
15+
16+
/*
17+
* Property to access the current safe area insets of the window, read-only.
18+
* Thread safe.
19+
*/
20+
@property (nonatomic, readonly) UIEdgeInsets currentSafeAreaInsets;
21+
22+
- (void)startObservingSafeArea;
23+
24+
@end
25+
26+
NS_ASSUME_NONNULL_END
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import "RCTWindowSafeAreaProxy.h"
9+
#import <React/RCTAssert.h>
10+
#import <React/RCTUtils.h>
11+
#import <mutex>
12+
13+
#import <React/RCTConstants.h>
14+
15+
@implementation RCTWindowSafeAreaProxy {
16+
BOOL _isObserving;
17+
std::mutex _currentSafeAreaInsetsMutex;
18+
UIEdgeInsets _currentSafeAreaInsets;
19+
}
20+
21+
+ (instancetype)sharedInstance
22+
{
23+
static RCTWindowSafeAreaProxy *sharedInstance = nil;
24+
static dispatch_once_t onceToken;
25+
dispatch_once(&onceToken, ^{
26+
sharedInstance = [RCTWindowSafeAreaProxy new];
27+
});
28+
return sharedInstance;
29+
}
30+
31+
- (instancetype)init
32+
{
33+
self = [super init];
34+
if (self) {
35+
_isObserving = NO;
36+
_currentSafeAreaInsets = [UIApplication sharedApplication].delegate.window.safeAreaInsets;
37+
}
38+
return self;
39+
}
40+
41+
- (void)startObservingSafeArea
42+
{
43+
RCTAssertMainQueue();
44+
std::lock_guard<std::mutex> lock(_currentSafeAreaInsetsMutex);
45+
if (!_isObserving) {
46+
_isObserving = YES;
47+
[[NSNotificationCenter defaultCenter] addObserver:self
48+
selector:@selector(_interfaceFrameDidChange)
49+
name:RCTUserInterfaceStyleDidChangeNotification
50+
object:nil];
51+
}
52+
}
53+
54+
- (UIEdgeInsets)currentSafeAreaInsets
55+
{
56+
std::lock_guard<std::mutex> lock(_currentSafeAreaInsetsMutex);
57+
58+
if (!_isObserving) {
59+
// Fallback in case [startObservingSafeArea startObservingSafeArea] was not called.
60+
__block UIEdgeInsets insets;
61+
RCTUnsafeExecuteOnMainQueueSync(^{
62+
insets = [UIApplication sharedApplication].delegate.window.safeAreaInsets;
63+
});
64+
return insets;
65+
}
66+
67+
return _currentSafeAreaInsets;
68+
}
69+
70+
- (void)_interfaceFrameDidChange
71+
{
72+
std::lock_guard<std::mutex> lock(_currentSafeAreaInsetsMutex);
73+
_currentSafeAreaInsets = [UIApplication sharedApplication].delegate.window.safeAreaInsets;
74+
}
75+
76+
@end

packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.mm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#import <React/RCTBridgeModule.h>
1313
#import <React/RCTConvert.h>
1414
#import <React/RCTFabricSurface.h>
15+
#import <React/RCTInitializeUIKitProxies.h>
1516
#import <React/RCTInspectorDevServerHelper.h>
1617
#import <React/RCTInspectorNetworkHelper.h>
1718
#import <React/RCTInspectorUtils.h>
@@ -248,6 +249,8 @@ - (RCTFabricSurface *)createSurfaceWithModuleName:(NSString *)moduleName
248249
mode:(DisplayMode)displayMode
249250
initialProperties:(NSDictionary *)properties
250251
{
252+
RCTInitializeUIKitProxies();
253+
251254
RCTFabricSurface *surface = [[RCTFabricSurface alloc] initWithSurfacePresenter:self.surfacePresenter
252255
moduleName:moduleName
253256
initialProperties:properties];

0 commit comments

Comments
 (0)