Skip to content

Commit

Permalink
[skip ci] migrate RCTSafeArea away from RCTUnsafeExecuteOnMainQueueSy…
Browse files Browse the repository at this point in the history
…nc (facebook#49440)

Summary:

changelog: [internal]

move away from RCTUnsafeExecuteOnMainQueueSync in RCTSafeArea.

Differential Revision: D69662510
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Feb 18, 2025
1 parent 71bd096 commit ca3b8e8
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 0 deletions.
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);
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];
});
}
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import <React/RCTBridgeModule.h>
#import <React/RCTConvert.h>
#import <React/RCTFabricSurface.h>
#import <React/RCTInitializeUIKitProxies.h>
#import <React/RCTInspectorDevServerHelper.h>
#import <React/RCTInspectorNetworkHelper.h>
#import <React/RCTInspectorUtils.h>
Expand Down Expand Up @@ -248,6 +249,8 @@ - (RCTFabricSurface *)createSurfaceWithModuleName:(NSString *)moduleName
mode:(DisplayMode)displayMode
initialProperties:(NSDictionary *)properties
{
RCTInitializeUIKitProxies();

RCTFabricSurface *surface = [[RCTFabricSurface alloc] initWithSurfacePresenter:self.surfacePresenter
moduleName:moduleName
initialProperties:properties];
Expand Down

0 comments on commit ca3b8e8

Please sign in to comment.
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy