amis-rpc-design/node_modules/react-native-safe-area-context/ios/RNCSafeAreaContext.mm
2023-10-07 19:42:30 +08:00

73 lines
1.5 KiB
Plaintext

#import "RNCSafeAreaContext.h"
#import <React/RCTUtils.h>
#import <UIKit/UIKit.h>
#ifdef RCT_NEW_ARCH_ENABLED
#import <safeareacontext/safeareacontext.h>
#endif
#ifdef RCT_NEW_ARCH_ENABLED
using namespace facebook::react;
@interface RNCSafeAreaContext () <NativeSafeAreaContextSpec>
@end
#endif
@implementation RNCSafeAreaContext
RCT_EXPORT_MODULE()
+ (BOOL)requiresMainQueueSetup
{
return YES;
}
- (NSDictionary *)constantsToExport
{
return [self getConstants];
}
- (NSDictionary *)getConstants
{
__block NSDictionary *constants;
RCTUnsafeExecuteOnMainQueueSync(^{
UIWindow *window = RCTKeyWindow();
if (window == nil) {
constants = @{@"initialWindowMetrics" : [NSNull null]};
return;
}
UIEdgeInsets safeAreaInsets = window.safeAreaInsets;
constants = @{
@"initialWindowMetrics" : @{
@"insets" : @{
@"top" : @(safeAreaInsets.top),
@"right" : @(safeAreaInsets.right),
@"bottom" : @(safeAreaInsets.bottom),
@"left" : @(safeAreaInsets.left),
},
@"frame" : @{
@"x" : @(window.frame.origin.x),
@"y" : @(window.frame.origin.y),
@"width" : @(window.frame.size.width),
@"height" : @(window.frame.size.height),
},
}
};
});
return constants;
}
#ifdef RCT_NEW_ARCH_ENABLED
- (std::shared_ptr<TurboModule>)getTurboModule:(const ObjCTurboModule::InitParams &)params
{
return std::make_shared<NativeSafeAreaContextSpecJSI>(params);
}
#endif
@end