forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[skip ci] migrate RCTSafeArea away from RCTUnsafeExecuteOnMainQueueSy…
…nc (facebook#49440) Summary: changelog: [internal] move away from RCTUnsafeExecuteOnMainQueueSync in RCTSafeArea. Differential Revision: D69662510
- Loading branch information
1 parent
71bd096
commit ca3b8e8
Showing
5 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
packages/react-native/React/Base/UIKitProxies/RCTInitializeUIKitProxies.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
void RCTInitializeUIKitProxies(void); |
17 changes: 17 additions & 0 deletions
17
packages/react-native/React/Base/UIKitProxies/RCTInitializeUIKitProxies.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#import "RCTInitializeUIKitProxies.h" | ||
#import "RCTWindowSafeAreaProxy.h" | ||
|
||
void RCTInitializeUIKitProxies(void) | ||
{ | ||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
[[RCTWindowSafeAreaProxy sharedInstance] startObservingSafeArea]; | ||
}); | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/react-native/React/Base/UIKitProxies/RCTWindowSafeAreaProxy.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface RCTWindowSafeAreaProxy : NSObject | ||
|
||
+ (instancetype)sharedInstance; | ||
|
||
/* | ||
* Property to access the current safe area insets of the window, read-only. | ||
* Thread safe. | ||
*/ | ||
@property (nonatomic, readonly) UIEdgeInsets currentSafeAreaInsets; | ||
|
||
- (void)startObservingSafeArea; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
76 changes: 76 additions & 0 deletions
76
packages/react-native/React/Base/UIKitProxies/RCTWindowSafeAreaProxy.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#import "RCTWindowSafeAreaProxy.h" | ||
#import <React/RCTAssert.h> | ||
#import <React/RCTUtils.h> | ||
#import <mutex> | ||
|
||
#import <React/RCTConstants.h> | ||
|
||
@implementation RCTWindowSafeAreaProxy { | ||
BOOL _isObserving; | ||
std::mutex _currentSafeAreaInsetsMutex; | ||
UIEdgeInsets _currentSafeAreaInsets; | ||
} | ||
|
||
+ (instancetype)sharedInstance | ||
{ | ||
static RCTWindowSafeAreaProxy *sharedInstance = nil; | ||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
sharedInstance = [RCTWindowSafeAreaProxy new]; | ||
}); | ||
return sharedInstance; | ||
} | ||
|
||
- (instancetype)init | ||
{ | ||
self = [super init]; | ||
if (self) { | ||
_isObserving = NO; | ||
_currentSafeAreaInsets = [UIApplication sharedApplication].delegate.window.safeAreaInsets; | ||
} | ||
return self; | ||
} | ||
|
||
- (void)startObservingSafeArea | ||
{ | ||
RCTAssertMainQueue(); | ||
std::lock_guard<std::mutex> lock(_currentSafeAreaInsetsMutex); | ||
if (!_isObserving) { | ||
_isObserving = YES; | ||
[[NSNotificationCenter defaultCenter] addObserver:self | ||
selector:@selector(_interfaceFrameDidChange) | ||
name:RCTUserInterfaceStyleDidChangeNotification | ||
object:nil]; | ||
} | ||
} | ||
|
||
- (UIEdgeInsets)currentSafeAreaInsets | ||
{ | ||
std::lock_guard<std::mutex> lock(_currentSafeAreaInsetsMutex); | ||
|
||
if (!_isObserving) { | ||
// Fallback in case [startObservingSafeArea startObservingSafeArea] was not called. | ||
__block UIEdgeInsets insets; | ||
RCTUnsafeExecuteOnMainQueueSync(^{ | ||
insets = [UIApplication sharedApplication].delegate.window.safeAreaInsets; | ||
}); | ||
return insets; | ||
} | ||
|
||
return _currentSafeAreaInsets; | ||
} | ||
|
||
- (void)_interfaceFrameDidChange | ||
{ | ||
std::lock_guard<std::mutex> lock(_currentSafeAreaInsetsMutex); | ||
_currentSafeAreaInsets = [UIApplication sharedApplication].delegate.window.safeAreaInsets; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters