From 5563f68326a86f15a375baeb55520cc8fa180333 Mon Sep 17 00:00:00 2001 From: Jason Morse Date: Fri, 13 Nov 2026 01:45:33 +0000 Subject: [PATCH 1/2] fix(view): bridge safe-area inset changes on macOS Supply the UIKit callback used by Fabric subclasses such as react-native-safe-area-context 5.11.1. AppKit has safeAreaInsets but no safeAreaInsetsDidChange selector. Notify subclasses after actual inset changes during attachment/layout, recording the value before dispatch to avoid duplicate notifications. Preserve UIKit behavior on iOS. Register and extend the existing macOS XCTest source; its baseline caller fails to compile, while both callback regressions pass with the shim. Keep this compatibility repair separate from native-props synchronization. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 33ea5c72-8bbd-448d-aa21-c9b99dd03c96 --- .../ReactApple/Libraries/RCTUIKit/RCTUIView.h | 2 + .../ReactApple/Libraries/RCTUIKit/RCTUIView.m | 18 ++++ .../RNTesterUnitTests_macOS.m | 86 +++++++++++++++---- .../RNTesterPods.xcodeproj/project.pbxproj | 4 + 4 files changed, 92 insertions(+), 18 deletions(-) diff --git a/packages/react-native/ReactApple/Libraries/RCTUIKit/RCTUIView.h b/packages/react-native/ReactApple/Libraries/RCTUIKit/RCTUIView.h index 76a97c0994da..d9b290958bf4 100644 --- a/packages/react-native/ReactApple/Libraries/RCTUIKit/RCTUIView.h +++ b/packages/react-native/ReactApple/Libraries/RCTUIKit/RCTUIView.h @@ -52,6 +52,8 @@ NS_ASSUME_NONNULL_BEGIN - (void)layoutSubviews; +- (void)safeAreaInsetsDidChange; + - (void)setNeedsDisplay; @property (nonatomic, copy) NSColor *backgroundColor; diff --git a/packages/react-native/ReactApple/Libraries/RCTUIKit/RCTUIView.m b/packages/react-native/ReactApple/Libraries/RCTUIKit/RCTUIView.m index 19528853dd6e..be9f8e3b85d8 100644 --- a/packages/react-native/ReactApple/Libraries/RCTUIKit/RCTUIView.m +++ b/packages/react-native/ReactApple/Libraries/RCTUIKit/RCTUIView.m @@ -24,6 +24,7 @@ @implementation RCTUIView BOOL _respondsToDisplayLayer; CATransform3D _transform3D; BOOL _hasCustomTransform3D; + NSEdgeInsets _lastSafeAreaInsets; } + (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key @@ -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]); @@ -99,6 +111,7 @@ - (BOOL)isFirstResponder { - (void)viewDidMoveToWindow { [self didMoveToWindow]; + RCTUIViewUpdateSafeAreaInsets(self); } - (BOOL)mouseDownCanMoveWindow{ @@ -209,6 +222,7 @@ - (void)layout { [super layout]; if (self.window != nil) { + RCTUIViewUpdateSafeAreaInsets(self); [self layoutSubviews]; } } @@ -281,6 +295,10 @@ - (void)layoutSubviews [super layout]; } +- (void)safeAreaInsetsDidChange +{ +} + - (void)setNeedsDisplay { self.needsDisplay = YES; diff --git a/packages/rn-tester/RNTester-macOSUnitTests/RNTesterUnitTests_macOS.m b/packages/rn-tester/RNTester-macOSUnitTests/RNTesterUnitTests_macOS.m index 7d7bfd4d574a..154bda5a3c29 100644 --- a/packages/rn-tester/RNTester-macOSUnitTests/RNTesterUnitTests_macOS.m +++ b/packages/rn-tester/RNTester-macOSUnitTests/RNTesterUnitTests_macOS.m @@ -6,36 +6,86 @@ // Copyright © 2017 Facebook. All rights reserved. // +// [macOS] + +#import #import +@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 diff --git a/packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj b/packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj index 90e6c295ce0e..5c8236d1cf33 100644 --- a/packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj +++ b/packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj @@ -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 */; }; @@ -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 = ""; }; /* [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 = ""; }; 0EA618022BE537D3001875EF /* RNTesterBundle.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; name = RNTesterBundle.bundle; path = RNTester/RNTesterBundle.bundle; sourceTree = ""; }; @@ -463,6 +465,7 @@ ACC52F56299ECB8A002A2B0B /* RNTester-macOSUnitTests */ = { isa = PBXGroup; children = ( + D5E7A8F02FF0010100000001 /* RNTesterUnitTests_macOS.m */, /* [macOS] */ ); path = "RNTester-macOSUnitTests"; sourceTree = ""; @@ -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 */, From eee401ea2f42eed7101b40c22c3b13dc2a712bac Mon Sep 17 00:00:00 2001 From: Jason Morse Date: Fri, 13 Nov 2026 02:41:12 +0000 Subject: [PATCH 2/2] fix(init): initialize Swift template properties before super The generated AppDelegate has immutable stored properties, so Swift definite initialization requires assigning them before NSObject.init. Keep them immutable and move only the superclass call. Actual compilation of the exact extracted class reproduces all three baseline errors and passes after the fix; typecheck alone does not run this analysis. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 33ea5c72-8bbd-448d-aa21-c9b99dd03c96 --- .../templates/macos/HelloWorld-macOS/AppDelegate.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/react-native/local-cli/generator-macos/templates/macos/HelloWorld-macOS/AppDelegate.swift b/packages/react-native/local-cli/generator-macos/templates/macos/HelloWorld-macOS/AppDelegate.swift index 1a9040193722..65c55c8b53b1 100644 --- a/packages/react-native/local-cli/generator-macos/templates/macos/HelloWorld-macOS/AppDelegate.swift +++ b/packages/react-native/local-cli/generator-macos/templates/macos/HelloWorld-macOS/AppDelegate.swift @@ -1,3 +1,5 @@ +// [macOS] + import SwiftUI import React import React_RCTAppDelegate @@ -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() } }