Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ NS_ASSUME_NONNULL_BEGIN

- (void)layoutSubviews;

- (void)safeAreaInsetsDidChange;

- (void)setNeedsDisplay;

@property (nonatomic, copy) NSColor *backgroundColor;
Expand Down
18 changes: 18 additions & 0 deletions packages/react-native/ReactApple/Libraries/RCTUIKit/RCTUIView.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ @implementation RCTUIView
BOOL _respondsToDisplayLayer;
CATransform3D _transform3D;
BOOL _hasCustomTransform3D;
NSEdgeInsets _lastSafeAreaInsets;
}

+ (NSSet<NSString *> *)keyPathsForValuesAffectingValueForKey:(NSString *)key
Expand Down Expand Up @@ -60,6 +61,17 @@ @implementation RCTUIView
return self;
}

static void RCTUIViewUpdateSafeAreaInsets(RCTUIView *self)
{
if (@available(macOS 11.0, *)) {
NSEdgeInsets insets = self.safeAreaInsets;
if (!NSEdgeInsetsEqual(insets, self->_lastSafeAreaInsets)) {
self->_lastSafeAreaInsets = insets;
[self safeAreaInsetsDidChange];
}
}
}

- (instancetype)initWithFrame:(NSRect)frameRect
{
return RCTUIViewCommonInit([super initWithFrame:frameRect]);
Expand Down Expand Up @@ -99,6 +111,7 @@ - (BOOL)isFirstResponder {
- (void)viewDidMoveToWindow
{
[self didMoveToWindow];
RCTUIViewUpdateSafeAreaInsets(self);
}

- (BOOL)mouseDownCanMoveWindow{
Expand Down Expand Up @@ -209,6 +222,7 @@ - (void)layout
{
[super layout];
if (self.window != nil) {
RCTUIViewUpdateSafeAreaInsets(self);
[self layoutSubviews];
}
}
Expand Down Expand Up @@ -281,6 +295,10 @@ - (void)layoutSubviews
[super layout];
}

- (void)safeAreaInsetsDidChange
{
}

- (void)setNeedsDisplay
{
self.needsDisplay = YES;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// [macOS]

import SwiftUI
import React
import React_RCTAppDelegate
Expand All @@ -22,14 +24,13 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let reactNativeFactory: RCTReactNativeFactory

override init() {
super.init()

let delegate = ReactNativeDelegate()
let factory = RCTReactNativeFactory(delegate: delegate)
delegate.dependencyProvider = RCTAppDependencyProvider()

reactNativeDelegate = delegate
reactNativeFactory = factory
super.init()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,86 @@
// Copyright © 2017 Facebook. All rights reserved.
//

// [macOS]

#import <React/RCTUIKit.h>
#import <XCTest/XCTest.h>

@interface RCTSafeAreaObservingView : RCTUIView
@property (nonatomic, assign) NSUInteger safeAreaChangeCount;
@property (nonatomic, assign) NSEdgeInsets observedSafeAreaInsets;
@end

@implementation RCTSafeAreaObservingView

- (void)safeAreaInsetsDidChange
{
[super safeAreaInsetsDidChange];
self.safeAreaChangeCount++;
self.observedSafeAreaInsets = self.safeAreaInsets;
}

@end

static void RCTPerformOnMainThread(dispatch_block_t block)
{
if (NSThread.isMainThread) {
block();
} else {
dispatch_sync(dispatch_get_main_queue(), block);
}
}

@interface RNTesterUnitTests_macOS : XCTestCase

@end

@implementation RNTesterUnitTests_macOS

- (void)setUp
- (void)testSafeAreaCallbackSupportsSuperclassCalls
{
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
RCTPerformOnMainThread(^{
[NSApplication sharedApplication];
RCTSafeAreaObservingView *view = [RCTSafeAreaObservingView new];
XCTAssertNoThrow([view safeAreaInsetsDidChange]);
XCTAssertEqual(view.safeAreaChangeCount, 1u);
});
}

- (void)tearDown
- (void)testLayoutReportsChangedSafeAreaInsetsOnce
{
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
RCTPerformOnMainThread(^{
[NSApplication sharedApplication];
NSWindow *window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 400, 300)
styleMask:NSWindowStyleMaskBorderless
backing:NSBackingStoreBuffered
defer:NO];
window.releasedWhenClosed = NO;
@try {
RCTSafeAreaObservingView *view = [[RCTSafeAreaObservingView alloc] initWithFrame:window.contentView.bounds];
window.contentView = view;
[view layoutSubtreeIfNeeded];
NSUInteger originalCount = view.safeAreaChangeCount;

//- (void)testFunctionalExample {
// // This is an example of a functional test case.
// // Use XCTAssert and related functions to verify your tests produce the correct results.
//}
//
//- (void)testPerformanceExample {
// // This is an example of a performance test case.
// [self measureBlock:^{
// // Put the code you want to measure the time of here.
// }];
//}
view.additionalSafeAreaInsets = NSEdgeInsetsMake(5, 7, 11, 13);
view.needsLayout = YES;
[view layoutSubtreeIfNeeded];
XCTAssertEqual(view.safeAreaChangeCount, originalCount + 1);
XCTAssertTrue(NSEdgeInsetsEqual(view.observedSafeAreaInsets, view.safeAreaInsets));

view.needsLayout = YES;
[view layoutSubtreeIfNeeded];
XCTAssertEqual(view.safeAreaChangeCount, originalCount + 1);

view.additionalSafeAreaInsets = NSEdgeInsetsMake(7, 11, 13, 17);
view.needsLayout = YES;
[view layoutSubtreeIfNeeded];
XCTAssertEqual(view.safeAreaChangeCount, originalCount + 2);
XCTAssertTrue(NSEdgeInsetsEqual(view.observedSafeAreaInsets, view.safeAreaInsets));
} @finally {
[window close];
}
});
}

@end
4 changes: 4 additions & 0 deletions packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
D5E7A8F02FF0010100000002 /* RNTesterUnitTests_macOS.m in Sources */ = {isa = PBXBuildFile; fileRef = D5E7A8F02FF0010100000001 /* RNTesterUnitTests_macOS.m */; }; /* [macOS] */
08E25EBE4A584CD7B70FBB1E /* libPods-RNTester.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D74056A5352F0925816E50E0 /* libPods-RNTester.a */; };
0CBC915D60C701A81731A5A4 /* libPods-RNTester-macOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 9C88AE81E635231C7F0F3BA4 /* libPods-RNTester-macOS.a */; };
0E3E8DF4F87D07566A9489D4 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = AC276E71CF27E40DFA22979F /* PrivacyInfo.xcprivacy */; };
Expand Down Expand Up @@ -142,6 +143,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
D5E7A8F02FF0010100000001 /* RNTesterUnitTests_macOS.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNTesterUnitTests_macOS.m; sourceTree = "<group>"; }; /* [macOS] */
07517909E4081E5D74D5DD60 /* libPods-RNTester-macOSUnitTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-RNTester-macOSUnitTests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
086F285685A842F5E52B6F9C /* Pods-RNTester-visionOS.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RNTester-visionOS.debug.xcconfig"; path = "Target Support Files/Pods-RNTester-visionOS/Pods-RNTester-visionOS.debug.xcconfig"; sourceTree = "<group>"; };
0EA618022BE537D3001875EF /* RNTesterBundle.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; name = RNTesterBundle.bundle; path = RNTester/RNTesterBundle.bundle; sourceTree = "<group>"; };
Expand Down Expand Up @@ -463,6 +465,7 @@
ACC52F56299ECB8A002A2B0B /* RNTester-macOSUnitTests */ = {
isa = PBXGroup;
children = (
D5E7A8F02FF0010100000001 /* RNTesterUnitTests_macOS.m */, /* [macOS] */
);
path = "RNTester-macOSUnitTests";
sourceTree = "<group>";
Expand Down Expand Up @@ -1290,6 +1293,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D5E7A8F02FF0010100000002 /* RNTesterUnitTests_macOS.m in Sources */, /* [macOS] */
AC73FCEF29B1318B0003586F /* RCTAnimationUtilsTests.m in Sources */,
AC73FD0729B131DA0003586F /* RCTUIManagerTests.m in Sources */,
AC73FCF729B131A20003586F /* RCTEventDispatcherTests.m in Sources */,
Expand Down
Loading