first commit

This commit is contained in:
2026-03-10 16:18:05 +00:00
commit 11f9c069b5
31635 changed files with 3187747 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
#pragma once
#import <UIKit/UIKit.h>
/**
* @protocol RNSSafeAreaProviding
* @brief Allows containers that obscure some part of its subviews to provide safe area.
*/
@protocol RNSSafeAreaProviding
/**
@brief Responsible for providing current safe area insets.
In most cases, this method should return `self.safeAreaInsets` unless it's necessary to modify the insets.
@returns `UIEdgeInsets` describing the safe area.
*/
- (UIEdgeInsets)providerSafeAreaInsets;
/**
@brief Responsible for notifying about a change in safe area.
This method should be called in `UIView`'s `safeAreaInsetsDidChange`.
Implementation of this method should use `NSNotificationCenter` to post notification with `RNSSafeAreaDidChange` name
defined in `RNSSafeAreaViewNotifications.h`.
*/
- (void)dispatchSafeAreaDidChangeNotification;
@end

View File

@@ -0,0 +1,32 @@
#pragma once
// Implementation adapted from `react-native-safe-area-context`:
// https://github.com/AppAndFlow/react-native-safe-area-context/tree/v5.6.1
#import <UIKit/UIKit.h>
#import "RNSReactBaseView.h"
#import "RNSSafeAreaViewEdges.h"
#if !RCT_NEW_ARCH_ENABLED
#import <React/RCTBridge.h>
#endif // !RCT_NEW_ARCH_ENABLED
NS_ASSUME_NONNULL_BEGIN
@interface RNSSafeAreaViewComponentView : RNSReactBaseView
@end
#pragma mark - Paper specific
#if !RCT_NEW_ARCH_ENABLED
@interface RNSSafeAreaViewComponentView ()
- (instancetype)initWithBridge:(RCTBridge *)bridge;
@property (nonatomic, assign) RNSSafeAreaViewEdges edges;
@end
#endif // !RCT_NEW_ARCH_ENABLED
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,228 @@
// Implementation adapted from `react-native-safe-area-context`:
// https://github.com/AppAndFlow/react-native-safe-area-context/tree/v5.6.1
#import "RNSSafeAreaViewComponentView.h"
#import <React/RCTConversions.h>
#import <React/RCTUtils.h>
#import "RNSSafeAreaProviding.h"
#import "RNSSafeAreaViewNotifications.h"
#if RCT_NEW_ARCH_ENABLED
#import <cxxreact/ReactNativeVersion.h>
#import <react/renderer/components/rnscreens/ComponentDescriptors.h>
#import <react/renderer/components/rnscreens/Props.h>
#import <rnscreens/RNSSafeAreaViewComponentDescriptor.h>
#import <rnscreens/RNSSafeAreaViewState.h>
#else
#import <React/RCTUIManager.h>
#import "RNSSafeAreaViewEdges.h"
#import "RNSSafeAreaViewLocalData.h"
#endif // RCT_NEW_ARCH_ENABLED
#if RCT_NEW_ARCH_ENABLED
namespace react = facebook::react;
#endif // RCT_NEW_ARCH_ENABLED
static BOOL UIEdgeInsetsEqualToEdgeInsetsWithThreshold(UIEdgeInsets insets1, UIEdgeInsets insets2, CGFloat threshold);
#pragma mark - View implementation
@implementation RNSSafeAreaViewComponentView {
#if RCT_NEW_ARCH_ENABLED
facebook::react::RNSSafeAreaViewShadowNode::ConcreteState::Shared _state;
#else
__weak RCTBridge *_bridge;
RNSSafeAreaViewEdges _edges;
#endif // RCT_NEW_ARCH_ENABLED
UIEdgeInsets _currentSafeAreaInsets;
__weak UIView<RNSSafeAreaProviding> *_Nullable _providerView;
}
#if RCT_NEW_ARCH_ENABLED
// 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]) {
[self initState];
}
return self;
}
#else
- (instancetype)initWithBridge:(RCTBridge *)bridge
{
if (self = [super initWithFrame:CGRectZero]) {
_bridge = bridge;
[self initState];
}
return self;
}
RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : (NSCoder *)decoder)
RCT_NOT_IMPLEMENTED(-(instancetype)initWithFrame : (CGRect)frame)
#endif // RCT_NEW_ARCH_ENABLED
- (void)initState
{
#if RCT_NEW_ARCH_ENABLED
static const auto defaultProps = std::make_shared<const react::RNSSafeAreaViewProps>();
_props = defaultProps;
#else
_edges = RNSSafeAreaViewEdgesMake(false, false, false, false);
#endif // RCT_NEW_ARCH_ENABLED
}
- (void)didMoveToWindow
{
UIView *previousProviderView = _providerView;
if (self.window != nil) {
_providerView = [self findNearestAncestorProvider];
[self updateStateIfNeeded];
} else {
_providerView = nil;
}
if (previousProviderView != _providerView) {
if (previousProviderView != nil) {
[NSNotificationCenter.defaultCenter removeObserver:self name:RNSSafeAreaDidChange object:previousProviderView];
}
if (_providerView != nil) {
[NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(safeAreaProviderInsetsDidChange:)
name:RNSSafeAreaDidChange
object:_providerView];
}
}
}
- (UIView<RNSSafeAreaProviding> *_Nullable)findNearestAncestorProvider
{
UIView *current = self.superview;
while (current != nil) {
if ([current respondsToSelector:@selector(providerSafeAreaInsets)]) {
return static_cast<UIView<RNSSafeAreaProviding> *>(current);
}
current = current.superview;
}
return nil;
}
- (void)safeAreaProviderInsetsDidChange:(NSNotification *)notification
{
[self updateStateIfNeeded];
}
- (void)updateStateIfNeeded
{
if (_providerView == nil) {
return;
}
UIEdgeInsets safeAreaInsets = _providerView.providerSafeAreaInsets;
if (UIEdgeInsetsEqualToEdgeInsetsWithThreshold(safeAreaInsets, _currentSafeAreaInsets, 1.0 / RCTScreenScale())) {
return;
}
_currentSafeAreaInsets = safeAreaInsets;
#if RCT_NEW_ARCH_ENABLED
[self updateState];
#else
[self updateLocalData];
#endif // RCT_NEW_ARCH_ENABLED
}
#if RCT_NEW_ARCH_ENABLED
- (void)updateState
{
using facebook::react::RNSSafeAreaViewShadowNode;
if (!_state) {
return;
}
auto newData = facebook::react::RNSSafeAreaViewState{RCTEdgeInsetsFromUIEdgeInsets(_currentSafeAreaInsets)};
_state->updateState(
std::move(newData)
#if REACT_NATIVE_VERSION_MINOR >= 82
,
facebook::react::EventQueue::UpdateMode::unstable_Immediate
#endif // REACT_NATIVE_VERSION_MINOR >= 82
);
}
#else
- (void)updateLocalData
{
RNSSafeAreaViewLocalData *localData = [[RNSSafeAreaViewLocalData alloc] initWithInsets:_currentSafeAreaInsets
edges:_edges];
[_bridge.uiManager setLocalData:localData forView:self];
}
#endif // RCT_NEW_ARCH_ENABLED
#if RCT_NEW_ARCH_ENABLED
#pragma mark - RCTComponentViewProtocol
+ (react::ComponentDescriptorProvider)componentDescriptorProvider
{
return react::concreteComponentDescriptorProvider<react::RNSSafeAreaViewComponentDescriptor>();
}
- (void)updateState:(facebook::react::State::Shared const &)state
oldState:(facebook::react::State::Shared const &)oldState
{
using facebook::react::RNSSafeAreaViewShadowNode;
_state = std::static_pointer_cast<RNSSafeAreaViewShadowNode::ConcreteState const>(state);
}
- (void)finalizeUpdates:(RNComponentViewUpdateMask)updateMask
{
[super finalizeUpdates:updateMask];
if (updateMask & RNComponentViewUpdateMaskProps) {
[self updateStateIfNeeded];
}
}
- (void)prepareForRecycle
{
[super prepareForRecycle];
[NSNotificationCenter.defaultCenter removeObserver:self];
_state.reset();
_providerView = nil;
_currentSafeAreaInsets = UIEdgeInsetsZero;
}
#else
#pragma mark - Paper props / LEGACY RCTComponent protocol
- (void)setEdges:(RNSSafeAreaViewEdges)edges
{
_edges = edges;
[self updateLocalData];
}
#endif
@end
#pragma mark - Utility functions
static BOOL UIEdgeInsetsEqualToEdgeInsetsWithThreshold(UIEdgeInsets insets1, UIEdgeInsets insets2, CGFloat threshold)
{
return ABS(insets1.left - insets2.left) <= threshold && ABS(insets1.right - insets2.right) <= threshold &&
ABS(insets1.top - insets2.top) <= threshold && ABS(insets1.bottom - insets2.bottom) <= threshold;
}
#if RCT_NEW_ARCH_ENABLED
#pragma mark - View class exposure
Class<RCTComponentViewProtocol> RNSSafeAreaView(void)
{
return RNSSafeAreaViewComponentView.class;
}
#endif // RCT_NEW_ARCH_ENABLED

View File

@@ -0,0 +1,5 @@
#pragma once
#pragma mark - NSNotificationCenter notification names
#define RNSSafeAreaDidChange @"RNSSafeAreaDidChange"

View File

@@ -0,0 +1,16 @@
#pragma once
// Implementation adapted from `react-native-safe-area-context`:
// https://github.com/AppAndFlow/react-native-safe-area-context/tree/v5.6.1
#if !RCT_NEW_ARCH_ENABLED
typedef struct RNSSafeAreaViewEdges {
BOOL top;
BOOL right;
BOOL bottom;
BOOL left;
} RNSSafeAreaViewEdges;
RNSSafeAreaViewEdges
RNSSafeAreaViewEdgesMake(BOOL top, BOOL right, BOOL bottom, BOOL left);
#endif // !RCT_NEW_ARCH_ENABLED

View File

@@ -0,0 +1,37 @@
// Implementation adapted from `react-native-safe-area-context`:
// https://github.com/AppAndFlow/react-native-safe-area-context/tree/v5.6.1
#import "RNSSafeAreaViewEdges.h"
#if !RCT_NEW_ARCH_ENABLED
#import <React/RCTConvert.h>
RNSSafeAreaViewEdges RNSSafeAreaViewEdgesMake(BOOL top, BOOL right, BOOL bottom, BOOL left)
{
RNSSafeAreaViewEdges edges;
edges.top = top;
edges.left = left;
edges.bottom = bottom;
edges.right = right;
return edges;
}
RNSSafeAreaViewEdges RNSSafeAreaViewEdgesMakeString(NSString *top, NSString *right, NSString *bottom, NSString *left)
{
RNSSafeAreaViewEdges edges;
edges.top = [RCTConvert BOOL:top];
edges.right = [RCTConvert BOOL:right];
edges.bottom = [RCTConvert BOOL:bottom];
edges.left = [RCTConvert BOOL:left];
return edges;
}
@implementation RCTConvert (RNSSafeAreaViewEdges)
RCT_CUSTOM_CONVERTER(
RNSSafeAreaViewEdges,
RNSSafeAreaViewEdges,
RNSSafeAreaViewEdgesMakeString(json[@"top"], json[@"right"], json[@"bottom"], json[@"left"]))
@end
#endif // !RCT_NEW_ARCH_ENABLED

View File

@@ -0,0 +1,22 @@
#pragma once
// Implementation adapted from `react-native-safe-area-context`:
// https://github.com/AppAndFlow/react-native-safe-area-context/tree/v5.6.1
#if !RCT_NEW_ARCH_ENABLED
#import <UIKit/UIKit.h>
#import "RNSSafeAreaViewEdges.h"
NS_ASSUME_NONNULL_BEGIN
@interface RNSSafeAreaViewLocalData : NSObject
- (instancetype)initWithInsets:(UIEdgeInsets)insets edges:(RNSSafeAreaViewEdges)edges;
@property (atomic, readonly) UIEdgeInsets insets;
@property (atomic, readonly) RNSSafeAreaViewEdges edges;
@end
NS_ASSUME_NONNULL_END
#endif // !RCT_NEW_ARCH_ENABLED

View File

@@ -0,0 +1,20 @@
// Implementation adapted from `react-native-safe-area-context`:
// https://github.com/AppAndFlow/react-native-safe-area-context/tree/v5.6.1
#if !RCT_NEW_ARCH_ENABLED
#import "RNSSafeAreaViewLocalData.h"
@implementation RNSSafeAreaViewLocalData
- (instancetype)initWithInsets:(UIEdgeInsets)insets edges:(RNSSafeAreaViewEdges)edges
{
if (self = [super init]) {
_insets = insets;
_edges = edges;
}
return self;
}
@end
#endif // !RCT_NEW_ARCH_ENABLED

View File

@@ -0,0 +1,16 @@
#pragma once
// Implementation adapted from `react-native-safe-area-context`:
// https://github.com/AppAndFlow/react-native-safe-area-context/tree/v5.6.1
#if !RCT_NEW_ARCH_ENABLED
#import <React/RCTViewManager.h>
NS_ASSUME_NONNULL_BEGIN
@interface RNSSafeAreaViewManager : RCTViewManager
@end
NS_ASSUME_NONNULL_END
#endif // !RCT_NEW_ARCH_ENABLED

View File

@@ -0,0 +1,28 @@
// Implementation adapted from `react-native-safe-area-context`:
// https://github.com/AppAndFlow/react-native-safe-area-context/tree/v5.6.1
#import "RNSSafeAreaViewManager.h"
#if !RCT_NEW_ARCH_ENABLED
#import "RNSSafeAreaViewComponentView.h"
#import "RNSSafeAreaViewEdges.h"
#import "RNSSafeAreaViewShadowView.h"
@implementation RNSSafeAreaViewManager
RCT_EXPORT_MODULE(RNSSafeAreaView)
- (UIView *)view
{
return [[RNSSafeAreaViewComponentView alloc] initWithBridge:self.bridge];
}
- (RNSSafeAreaViewShadowView *)shadowView
{
return [RNSSafeAreaViewShadowView new];
}
RCT_EXPORT_VIEW_PROPERTY(edges, RNSSafeAreaViewEdges)
@end
#endif // !RCT_NEW_ARCH_ENABLED

View File

@@ -0,0 +1,16 @@
#pragma once
// Implementation adapted from `react-native-safe-area-context`:
// https://github.com/AppAndFlow/react-native-safe-area-context/tree/v5.6.1
#if !RCT_NEW_ARCH_ENABLED
#import <React/RCTShadowView.h>
NS_ASSUME_NONNULL_BEGIN
@interface RNSSafeAreaViewShadowView : RCTShadowView
@end
NS_ASSUME_NONNULL_END
#endif // !RCT_NEW_ARCH_ENABLED

View File

@@ -0,0 +1,152 @@
// Implementation adapted from `react-native-safe-area-context`:
// https://github.com/AppAndFlow/react-native-safe-area-context/tree/v5.6.1
#import "RNSSafeAreaViewShadowView.h"
#if !RCT_NEW_ARCH_ENABLED
#import <React/RCTAssert.h>
#include <math.h>
#import "RNSSafeAreaViewEdges.h"
#import "RNSSafeAreaViewLocalData.h"
// From RCTShadowView.m
typedef NS_ENUM(unsigned int, meta_prop_t) {
META_PROP_LEFT,
META_PROP_TOP,
META_PROP_RIGHT,
META_PROP_BOTTOM,
META_PROP_HORIZONTAL,
META_PROP_VERTICAL,
META_PROP_ALL,
META_PROP_COUNT,
};
@implementation RNSSafeAreaViewShadowView {
RNSSafeAreaViewLocalData *_localData;
bool _needsUpdate;
YGValue _marginMetaProps[META_PROP_COUNT];
}
- (instancetype)init
{
self = [super init];
if (self) {
_needsUpdate = false;
for (unsigned int ii = 0; ii < META_PROP_COUNT; ii++) {
_marginMetaProps[ii] = YGValueUndefined;
}
}
return self;
}
- (void)extractEdges:(YGValue[])_metaProps
top:(CGFloat *)top
right:(CGFloat *)right
bottom:(CGFloat *)bottom
left:(CGFloat *)left
{
if (_metaProps[META_PROP_ALL].unit == YGUnitPoint) {
*top = _metaProps[META_PROP_ALL].value;
*right = _metaProps[META_PROP_ALL].value;
*bottom = _metaProps[META_PROP_ALL].value;
*left = _metaProps[META_PROP_ALL].value;
}
if (_metaProps[META_PROP_HORIZONTAL].unit == YGUnitPoint) {
*right = _metaProps[META_PROP_HORIZONTAL].value;
*left = _metaProps[META_PROP_HORIZONTAL].value;
}
if (_metaProps[META_PROP_VERTICAL].unit == YGUnitPoint) {
*top = _metaProps[META_PROP_VERTICAL].value;
*bottom = _metaProps[META_PROP_VERTICAL].value;
}
if (_metaProps[META_PROP_TOP].unit == YGUnitPoint) {
*top = _metaProps[META_PROP_TOP].value;
}
if (_metaProps[META_PROP_RIGHT].unit == YGUnitPoint) {
*right = _metaProps[META_PROP_RIGHT].value;
}
if (_metaProps[META_PROP_BOTTOM].unit == YGUnitPoint) {
*bottom = _metaProps[META_PROP_BOTTOM].value;
}
if (_metaProps[META_PROP_LEFT].unit == YGUnitPoint) {
*left = _metaProps[META_PROP_LEFT].value;
}
}
- (void)updateInsets
{
if (_localData == nil) {
return;
}
UIEdgeInsets insets = _localData.insets;
RNSSafeAreaViewEdges edges = _localData.edges;
CGFloat top = 0;
CGFloat right = 0;
CGFloat bottom = 0;
CGFloat left = 0;
[self extractEdges:_marginMetaProps top:&top right:&right bottom:&bottom left:&left];
super.marginTop =
(YGValue){static_cast<float>([self getEdgeValue:edges.top insetValue:insets.top edgeValue:top]), YGUnitPoint};
super.marginRight = (YGValue){
static_cast<float>([self getEdgeValue:edges.right insetValue:insets.right edgeValue:right]), YGUnitPoint};
super.marginBottom = (YGValue){
static_cast<float>([self getEdgeValue:edges.bottom insetValue:insets.bottom edgeValue:bottom]), YGUnitPoint};
super.marginLeft =
(YGValue){static_cast<float>([self getEdgeValue:edges.left insetValue:insets.left edgeValue:left]), YGUnitPoint};
}
- (CGFloat)getEdgeValue:(BOOL)edgeMode insetValue:(CGFloat)insetValue edgeValue:(CGFloat)edgeValue
{
return edgeMode ? insetValue + edgeValue : edgeValue;
}
- (void)didSetProps:(NSArray<NSString *> *)changedProps
{
if (_needsUpdate) {
_needsUpdate = false;
[self updateInsets];
}
[super didSetProps:changedProps];
}
- (void)setLocalData:(RNSSafeAreaViewLocalData *)localData
{
RCTAssert(
[localData isKindOfClass:[RNSSafeAreaViewLocalData class]],
@"Local data object for `RNSSafeAreaViewShadowView` must be `RNSSafeAreaViewLocalData` instance.");
_localData = localData;
_needsUpdate = false;
[self updateInsets];
[super didSetProps:@[ @"marginTop", @"marginRight", @"marginBottom", @"marginLeft" ]];
}
#define SHADOW_VIEW_MARGIN_PROP(edge, metaProp) \
-(void)setMargin##edge : (YGValue)value \
{ \
[super setMargin##edge:value]; \
_needsUpdate = true; \
_marginMetaProps[META_PROP_##metaProp] = value; \
}
SHADOW_VIEW_MARGIN_PROP(, ALL);
SHADOW_VIEW_MARGIN_PROP(Vertical, VERTICAL);
SHADOW_VIEW_MARGIN_PROP(Horizontal, HORIZONTAL);
SHADOW_VIEW_MARGIN_PROP(Top, TOP);
SHADOW_VIEW_MARGIN_PROP(Right, RIGHT);
SHADOW_VIEW_MARGIN_PROP(Bottom, BOTTOM);
SHADOW_VIEW_MARGIN_PROP(Left, LEFT);
@end
#endif // !RCT_NEW_ARCH_ENABLED