Skip to content

Commit 993958c

Browse files
committed
Write macOS equivalents for all iOS code
1 parent 328d587 commit 993958c

11 files changed

+132
-5
lines changed

ios/RNCSafeAreaContext.mm

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#import "RNCSafeAreaContext.h"
22

33
#import <React/RCTUtils.h>
4+
#if TARGET_OS_IPHONE
45
#import <UIKit/UIKit.h>
6+
#elif TARGET_OS_OSX
7+
#import <AppKit/AppKit.h>
8+
#endif
59
#ifdef RCT_NEW_ARCH_ENABLED
610
#import <safeareacontext/safeareacontext.h>
711
#endif
@@ -32,13 +36,27 @@ - (NSDictionary *)getConstants
3236
__block NSDictionary *constants;
3337

3438
RCTUnsafeExecuteOnMainQueueSync(^{
39+
#if TARGET_OS_IPHONE
3540
UIWindow *window = RCTKeyWindow();
41+
#elif TARGET_OS_OSX
42+
NSWindow *window = RCTKeyWindow();
43+
#endif
3644
if (window == nil) {
3745
constants = @{@"initialWindowMetrics" : [NSNull null]};
3846
return;
3947
}
40-
48+
49+
#if TARGET_OS_IPHONE
4150
UIEdgeInsets safeAreaInsets = window.safeAreaInsets;
51+
#elif TARGET_OS_OSX
52+
NSEdgeInsets safeAreaInsets;
53+
if (@available(macOS 10.10, *)) {
54+
safeAreaInsets = NSEdgeInsetsZero;
55+
} else {
56+
safeAreaInsets = NSEdgeInsetsMake(0, 0, 0, 0);
57+
}
58+
#endif
59+
4260
constants = @{
4361
@"initialWindowMetrics" : @{
4462
@"insets" : @{

ios/RNCSafeAreaProvider.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
#if TARGET_OS_IPHONE
12
#import <UIKit/UIKit.h>
3+
#elif TARGET_OS_OSX
4+
#import <AppKit/AppKit.h>
5+
#endif
26

37
#import <React/RCTView.h>
48

ios/RNCSafeAreaProvider.m

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ @implementation RNCSafeAreaProvider {
1313
- (instancetype)init
1414
{
1515
if ((self = [super init])) {
16-
#if !TARGET_OS_TV
16+
#if !TARGET_OS_TV && !TARGET_OS_OSX
1717
[NSNotificationCenter.defaultCenter addObserver:self
1818
selector:@selector(invalidateSafeAreaInsets)
1919
name:UIKeyboardDidShowNotification
@@ -48,11 +48,26 @@ - (void)invalidateSafeAreaInsets
4848
return;
4949
}
5050

51+
#if TARGET_OS_IPHONE
5152
UIEdgeInsets safeAreaInsets = self.safeAreaInsets;
53+
#elif TARGET_OS_OSX
54+
NSEdgeInsets safeAreaInsets;
55+
if (@available(macOS 11.0, *)) {
56+
safeAreaInsets = self.safeAreaInsets;
57+
} else if (@available(macOS 10.10, *)) {
58+
safeAreaInsets = NSEdgeInsetsZero;
59+
} else {
60+
safeAreaInsets = NSEdgeInsetsMake(0, 0, 0, 0);
61+
}
62+
#endif
5263
CGRect frame = [self convertRect:self.bounds toView:RNCParentViewController(self).view];
5364

5465
if (_initialInsetsSent &&
66+
#if TARGET_OS_IPHONE
5567
UIEdgeInsetsEqualToEdgeInsetsWithThreshold(safeAreaInsets, _currentSafeAreaInsets, 1.0 / RCTScreenScale()) &&
68+
#elif TARGET_OS_OSX
69+
NSEdgeInsetsEqualToEdgeInsetsWithThreshold(safeAreaInsets, _currentSafeAreaInsets, 1.0 / RCTScreenScale()) &&
70+
#endif
5671
CGRectEqualToRect(frame, _currentFrame)) {
5772
return;
5873
}

ios/RNCSafeAreaProviderManager.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ @implementation RNCSafeAreaProviderManager
88

99
RCT_EXPORT_VIEW_PROPERTY(onInsetsChange, RCTDirectEventBlock)
1010

11+
#if TARGET_OS_IPHONE
1112
- (UIView *)view
13+
#elif TARGET_OS_OSX
14+
- (NSView *)view
15+
#endif
1216
{
1317
return [RNCSafeAreaProvider new];
1418
}

ios/RNCSafeAreaUtils.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
#import <React/RCTDefines.h>
22
#import <React/RCTView.h>
3+
#if TARGET_OS_IPHONE
34
#import <UIKit/UIKit.h>
5+
#elif TARGET_OS_OSX
6+
#import <AppKit/AppKit.h>
7+
#endif
48

59
extern NSString *const RNCSafeAreaDidChange;
610

711
RCT_EXTERN BOOL
12+
#if TARGET_OS_IPHONE
813
UIEdgeInsetsEqualToEdgeInsetsWithThreshold(UIEdgeInsets insets1, UIEdgeInsets insets2, CGFloat threshold);
14+
#elif TARGET_OS_OSX
15+
NSEdgeInsetsEqualToEdgeInsetsWithThreshold(NSEdgeInsets insets1, NSEdgeInsets insets2, CGFloat threshold);
16+
#endif
917

18+
#if TARGET_OS_IPHONE
1019
RCT_EXTERN UIViewController *RNCParentViewController(UIView *view);
20+
#elif TARGET_OS_OSX
21+
RCT_EXTERN NSViewController *RNCParentViewController(NSView *view);
22+
#endif

ios/RNCSafeAreaUtils.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44

55
NSString *const RNCSafeAreaDidChange = @"RNCSafeAreaDidChange";
66

7+
#if TARGET_OS_IPHONE
78
BOOL UIEdgeInsetsEqualToEdgeInsetsWithThreshold(UIEdgeInsets insets1, UIEdgeInsets insets2, CGFloat threshold)
9+
#elif TARGET_OS_OSX
10+
BOOL NSEdgeInsetsEqualToEdgeInsetsWithThreshold(NSEdgeInsets insets1, NSEdgeInsets insets2, CGFloat threshold)
11+
#endif
812
{
913
return ABS(insets1.left - insets2.left) <= threshold && ABS(insets1.right - insets2.right) <= threshold &&
1014
ABS(insets1.top - insets2.top) <= threshold && ABS(insets1.bottom - insets2.bottom) <= threshold;
1115
}
1216

17+
#if TARGET_OS_IPHONE
1318
UIViewController *RNCParentViewController(UIView *view)
1419
{
1520
UIResponder *responder = view.nextResponder;
@@ -21,3 +26,16 @@ BOOL UIEdgeInsetsEqualToEdgeInsetsWithThreshold(UIEdgeInsets insets1, UIEdgeInse
2126
}
2227
return nil;
2328
}
29+
#elif TARGET_OS_OSX
30+
NSViewController *RNCParentViewController(NSView *view)
31+
{
32+
NSResponder *responder = view.nextResponder;
33+
while (responder != nil) {
34+
if ([responder isKindOfClass:[NSViewController class]]) {
35+
return (NSViewController *)responder;
36+
}
37+
responder = responder.nextResponder;
38+
}
39+
return nil;
40+
}
41+
#endif

ios/RNCSafeAreaView.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#import <React/RCTBridge.h>
22
#import <React/RCTView.h>
3+
#if TARGET_OS_IPHONE
34
#import <UIKit/UIKit.h>
5+
#elif TARGET_OS_OSX
6+
#import <AppKit/AppKit.h>
7+
#endif
48

59
#import "RNCSafeAreaViewEdges.h"
610
#import "RNCSafeAreaViewMode.h"

ios/RNCSafeAreaView.m

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111

1212
@implementation RNCSafeAreaView {
1313
__weak RCTBridge *_bridge;
14+
#if TARGET_OS_IPHONE
1415
UIEdgeInsets _currentSafeAreaInsets;
16+
#elif TARGET_OS_OSX
17+
NSEdgeInsets _currentSafeAreaInsets;
18+
#endif
1519
RNCSafeAreaViewMode _mode;
1620
RNCSafeAreaViewEdges _edges;
1721
__weak RNCSafeAreaProvider *_Nullable _providerView;
@@ -41,16 +45,35 @@ - (NSString *)description
4145
if (superDescription.length > 0 && [superDescription characterAtIndex:superDescription.length - 1] == '>') {
4246
superDescription = [superDescription substringToIndex:superDescription.length - 1];
4347
}
48+
49+
#if TARGET_OS_IPHONE
50+
NSString *providerViewSafeAreaInsetsString = NSStringFromUIEdgeInsets(_providerView.safeAreaInsets);
51+
NSString *currentSafeAreaInsetsString = NSStringFromUIEdgeInsets(_currentSafeAreaInsets);
52+
#elif TARGET_OS_OSX
53+
NSString *providerViewSafeAreaInsetsString;
54+
NSString *currentSafeAreaInsetsString;
55+
if (@available(macOS 11.0, *)) {
56+
providerViewSafeAreaInsetsString = [NSString stringWithFormat:@"{%f,%f,%f,%f}", _providerView.safeAreaInsets.top, _providerView.safeAreaInsets.left, _providerView.safeAreaInsets.bottom, _providerView.safeAreaInsets.right];
57+
currentSafeAreaInsetsString = [NSString stringWithFormat:@"{%f,%f,%f,%f}", _currentSafeAreaInsets.top, _currentSafeAreaInsets.left, _currentSafeAreaInsets.bottom, _currentSafeAreaInsets.right];
58+
} else {
59+
providerViewSafeAreaInsetsString = @"{0.0,0.0,0.0,0.0}";
60+
currentSafeAreaInsetsString = @"{0.0,0.0,0.0,0.0}";
61+
}
62+
#endif
4463

4564
return [NSString stringWithFormat:@"%@; RNCSafeAreaInsets = %@; appliedRNCSafeAreaInsets = %@>",
4665
superDescription,
47-
NSStringFromUIEdgeInsets(_providerView.safeAreaInsets),
48-
NSStringFromUIEdgeInsets(_currentSafeAreaInsets)];
66+
providerViewSafeAreaInsetsString,
67+
currentSafeAreaInsetsString];
4968
}
5069

5170
- (void)didMoveToWindow
5271
{
72+
#if TARGET_OS_IPHONE
5373
UIView *previousProviderView = _providerView;
74+
#elif TARGET_OS_OSX
75+
NSView *previousProviderView = _providerView;
76+
#endif
5477
_providerView = [self findNearestProvider];
5578

5679
[self invalidateSafeAreaInsets];
@@ -79,19 +102,29 @@ - (void)invalidateSafeAreaInsets
79102
if (_providerView == nil) {
80103
return;
81104
}
105+
#if TARGET_OS_IPHONE
82106
UIEdgeInsets safeAreaInsets = _providerView.safeAreaInsets;
83-
84107
if (UIEdgeInsetsEqualToEdgeInsetsWithThreshold(safeAreaInsets, _currentSafeAreaInsets, 1.0 / RCTScreenScale())) {
85108
return;
86109
}
110+
#elif TARGET_OS_OSX
111+
NSEdgeInsets safeAreaInsets = _providerView.safeAreaInsets;
112+
if (NSEdgeInsetsEqualToEdgeInsetsWithThreshold(safeAreaInsets, _currentSafeAreaInsets, 1.0 / RCTScreenScale())) {
113+
return;
114+
}
115+
#endif
87116

88117
_currentSafeAreaInsets = safeAreaInsets;
89118
[self updateLocalData];
90119
}
91120

92121
- (nullable RNCSafeAreaProvider *)findNearestProvider
93122
{
123+
#if TARGET_OS_IPHONE
94124
UIView *current = self.reactSuperview;
125+
#elif TARGET_OS_OSX
126+
NSView *current = self.reactSuperview;
127+
#endif
95128
while (current != nil) {
96129
if ([current isKindOfClass:RNCSafeAreaProvider.class]) {
97130
return (RNCSafeAreaProvider *)current;

ios/RNCSafeAreaViewLocalData.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
#if TARGET_OS_IPHONE
12
#import <UIKit/UIKit.h>
3+
#elif TARGET_OS_OSX
4+
#import <AppKit/AppKit.h>
5+
#endif
26

37
#import "RNCSafeAreaViewEdges.h"
48
#import "RNCSafeAreaViewMode.h"
@@ -7,9 +11,16 @@ NS_ASSUME_NONNULL_BEGIN
711

812
@interface RNCSafeAreaViewLocalData : NSObject
913

14+
#if TARGET_OS_IPHONE
1015
- (instancetype)initWithInsets:(UIEdgeInsets)insets mode:(RNCSafeAreaViewMode)mode edges:(RNCSafeAreaViewEdges)edges;
1116

1217
@property (atomic, readonly) UIEdgeInsets insets;
18+
#elif TARGET_OS_OSX
19+
- (instancetype)initWithInsets:(NSEdgeInsets)insets mode:(RNCSafeAreaViewMode)mode edges:(RNCSafeAreaViewEdges)edges;
20+
21+
@property (atomic, readonly) NSEdgeInsets insets;
22+
#endif
23+
1324
@property (atomic, readonly) RNCSafeAreaViewMode mode;
1425
@property (atomic, readonly) RNCSafeAreaViewEdges edges;
1526

ios/RNCSafeAreaViewLocalData.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
@implementation RNCSafeAreaViewLocalData
44

5+
#if TARGET_OS_IPHONE
56
- (instancetype)initWithInsets:(UIEdgeInsets)insets mode:(RNCSafeAreaViewMode)mode edges:(RNCSafeAreaViewEdges)edges
7+
#elif TARGET_OS_OSX
8+
- (instancetype)initWithInsets:(NSEdgeInsets)insets mode:(RNCSafeAreaViewMode)mode edges:(RNCSafeAreaViewEdges)edges
9+
#endif
610
{
711
if (self = [super init]) {
812
_insets = insets;

0 commit comments

Comments
 (0)