diff --git a/.github/workflows/microsoft-pr.yml b/.github/workflows/microsoft-pr.yml index fc077dda043ecf..ef918ae5c07ac1 100644 --- a/.github/workflows/microsoft-pr.yml +++ b/.github/workflows/microsoft-pr.yml @@ -24,6 +24,7 @@ jobs: uses: ./.github/actions/microsoft-setup-toolchain with: node-version: '22' + cache-npm-dependencies: '' # We lint the PR title instead of the commit message to avoid script injection attacks. # Using environment variables prevents potential security vulnerabilities as described in: # https://docs.github.com/en/actions/security-for-github-actions/security-guides/security-hardening-for-github-actions#example-of-a-script-injection-attack diff --git a/.nx/version-plans/version-plan-1763131586011.md b/.nx/version-plans/version-plan-1763131586011.md new file mode 100644 index 00000000000000..c1c2c8dc0515a6 --- /dev/null +++ b/.nx/version-plans/version-plan-1763131586011.md @@ -0,0 +1,5 @@ +--- +__default__: patch +--- + +Pick a bunch of bug fixes diff --git a/packages/react-native/Libraries/LinkingIOS/RCTLinkingManager.mm b/packages/react-native/Libraries/LinkingIOS/RCTLinkingManager.mm index ad75ed482c49f1..f0c943f6aa300a 100644 --- a/packages/react-native/Libraries/LinkingIOS/RCTLinkingManager.mm +++ b/packages/react-native/Libraries/LinkingIOS/RCTLinkingManager.mm @@ -155,6 +155,8 @@ - (void)handleOpenURLNotification:(NSNotification *)notification RCT_EXPORT_METHOD(getInitialURL : (RCTPromiseResolveBlock)resolve reject : (__unused RCTPromiseRejectBlock)reject) { NSURL *initialURL = nil; +#pragma clang diagnostic push // [macOS] +#pragma clang diagnostic ignored "-Wdeprecated-declarations" // [macOS] if (self.bridge.launchOptions[UIApplicationLaunchOptionsURLKey]) { initialURL = self.bridge.launchOptions[UIApplicationLaunchOptionsURLKey]; } else { @@ -164,6 +166,7 @@ - (void)handleOpenURLNotification:(NSNotification *)notification initialURL = ((NSUserActivity *)userActivityDictionary[@"UIApplicationLaunchOptionsUserActivityKey"]).webpageURL; } } +#pragma clang diagnostic pop // [macOS] resolve(RCTNullIfNil(initialURL.absoluteString)); } diff --git a/packages/react-native/React/Base/UIKitProxies/RCTAppearanceProxy.h b/packages/react-native/React/Base/UIKitProxies/RCTAppearanceProxy.h new file mode 100644 index 00000000000000..c3886a109ac5ee --- /dev/null +++ b/packages/react-native/React/Base/UIKitProxies/RCTAppearanceProxy.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) Microsoft Corporation. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#if TARGET_OS_OSX // [macOS +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface RCTAppearanceProxy : NSObject + ++ (instancetype)sharedInstance; + +/* + * Property to access the current appearance. + * Thread safe. + */ +@property (nonatomic, readonly) NSAppearance *currentAppearance; + +- (void)startObservingAppearance; + +@end + +NS_ASSUME_NONNULL_END +#endif // macOS] diff --git a/packages/react-native/React/Base/UIKitProxies/RCTAppearanceProxy.mm b/packages/react-native/React/Base/UIKitProxies/RCTAppearanceProxy.mm new file mode 100644 index 00000000000000..9e25b3604f996f --- /dev/null +++ b/packages/react-native/React/Base/UIKitProxies/RCTAppearanceProxy.mm @@ -0,0 +1,92 @@ +/* + * Copyright (c) Microsoft Corporation. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#if TARGET_OS_OSX // [macOS +#import "RCTAppearanceProxy.h" + +#import +#import + +#import + +@implementation RCTAppearanceProxy { + BOOL _isObserving; + std::mutex _mutex; + NSAppearance *_currentAppearance; +} + ++ (instancetype)sharedInstance +{ + static RCTAppearanceProxy *sharedInstance = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + sharedInstance = [RCTAppearanceProxy new]; + }); + return sharedInstance; +} + +- (instancetype)init +{ + self = [super init]; + if (self) { + _isObserving = NO; + _currentAppearance = [NSApp effectiveAppearance]; + } + return self; +} + +- (void)startObservingAppearance +{ + RCTAssertMainQueue(); + std::lock_guard lock(_mutex); + if (!_isObserving) { + _isObserving = YES; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(_appearanceDidChange:) + name:RCTUserInterfaceStyleDidChangeNotification + object:nil]; + } +} + +- (NSAppearance *)currentAppearance +{ + { + std::lock_guard lock(_mutex); + if (_isObserving) { + return _currentAppearance; + } + } + + __block NSAppearance *appearance = nil; + if (RCTIsMainQueue()) { + appearance = [NSApp effectiveAppearance]; + } else { + dispatch_sync(dispatch_get_main_queue(), ^{ + appearance = [NSApp effectiveAppearance]; + }); + } + return appearance; +} + +- (void)_appearanceDidChange:(NSNotification *)notification +{ + std::lock_guard lock(_mutex); + + NSDictionary *userInfo = [notification userInfo]; + if (userInfo) { + NSAppearance *appearance = userInfo[RCTUserInterfaceStyleDidChangeNotificationAppearanceKey]; + if (appearance != nil) { + _currentAppearance = appearance; + return; + } + } + + _currentAppearance = [NSApp effectiveAppearance]; +} + +@end +#endif // macOS] diff --git a/packages/react-native/React/Base/UIKitProxies/RCTInitializeUIKitProxies.mm b/packages/react-native/React/Base/UIKitProxies/RCTInitializeUIKitProxies.mm index 09dfb43d4ab2da..4e059fab360340 100644 --- a/packages/react-native/React/Base/UIKitProxies/RCTInitializeUIKitProxies.mm +++ b/packages/react-native/React/Base/UIKitProxies/RCTInitializeUIKitProxies.mm @@ -10,6 +10,9 @@ #import "RCTKeyWindowValuesProxy.h" #import "RCTTraitCollectionProxy.h" #import "RCTWindowSafeAreaProxy.h" +#if TARGET_OS_OSX // [macOS +#import "RCTAppearanceProxy.h" +#endif // macOS] void RCTInitializeUIKitProxies(void) { @@ -19,7 +22,9 @@ void RCTInitializeUIKitProxies(void) #if !TARGET_OS_OSX // [macOS] [[RCTTraitCollectionProxy sharedInstance] startObservingTraitCollection]; [[RCTInitialAccessibilityValuesProxy sharedInstance] recordAccessibilityValues]; -#endif // [macOS] +#else // [macOS + [[RCTAppearanceProxy sharedInstance] startObservingAppearance]; +#endif // macOS] [[RCTKeyWindowValuesProxy sharedInstance] startObservingWindowSizeIfNecessary]; }); } diff --git a/packages/react-native/React/CoreModules/RCTAppearance.mm b/packages/react-native/React/CoreModules/RCTAppearance.mm index 4053d54a9e1a12..857c18d6ce5650 100644 --- a/packages/react-native/React/CoreModules/RCTAppearance.mm +++ b/packages/react-native/React/CoreModules/RCTAppearance.mm @@ -14,6 +14,12 @@ #import "CoreModulesPlugins.h" +#if TARGET_OS_OSX // [macOS +#import + +#import "RCTAppearanceProxy.h" +#endif // macOS] + using namespace facebook::react; NSString *const RCTAppearanceColorSchemeLight = @"light"; @@ -119,7 +125,7 @@ - (instancetype)init UITraitCollection *traitCollection = [RCTTraitCollectionProxy sharedInstance].currentTraitCollection; _currentColorScheme = RCTColorSchemePreference(traitCollection); #else // [macOS - NSAppearance *appearance = RCTSharedApplication().appearance; + NSAppearance *appearance = [RCTAppearanceProxy sharedInstance].currentAppearance; _currentColorScheme = RCTColorSchemePreference(appearance); #endif // macOS] [[NSNotificationCenter defaultCenter] addObserver:self @@ -134,7 +140,11 @@ - (instancetype)init + (BOOL)requiresMainQueueSetup { +#if !TARGET_OS_OSX // [macOS] return NO; +#else // [macOS + return YES; +#endif // macOS] } - (dispatch_queue_t)methodQueue @@ -160,13 +170,15 @@ - (dispatch_queue_t)methodQueue window.overrideUserInterfaceStyle = userInterfaceStyle; } #else // [macOS - NSAppearance *appearance = nil; - if ([style isEqualToString:@"light"]) { - appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua]; - } else if ([style isEqualToString:@"dark"]) { - appearance = [NSAppearance appearanceNamed:NSAppearanceNameDarkAqua]; - } - RCTSharedApplication().appearance = appearance; + RCTExecuteOnMainQueue(^{ + NSAppearance *appearance = nil; + if ([style isEqualToString:@"light"]) { + appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua]; + } else if ([style isEqualToString:@"dark"]) { + appearance = [NSAppearance appearanceNamed:NSAppearanceNameDarkAqua]; + } + RCTSharedApplication().appearance = appearance; + }); #endif // macOS] } @@ -177,10 +189,7 @@ - (dispatch_queue_t)methodQueue UITraitCollection *traitCollection = [RCTTraitCollectionProxy sharedInstance].currentTraitCollection; _currentColorScheme = RCTColorSchemePreference(traitCollection); #else // [macOS - __block NSAppearance *appearance = nil; - RCTUnsafeExecuteOnMainQueueSync(^{ - appearance = RCTKeyWindow().appearance; - }); + NSAppearance *appearance = [RCTAppearanceProxy sharedInstance].currentAppearance; _currentColorScheme = RCTColorSchemePreference(appearance); #endif // macOS] } @@ -190,23 +199,19 @@ - (dispatch_queue_t)methodQueue - (void)appearanceChanged:(NSNotification *)notification { +#if !TARGET_OS_OSX // [macOS NSDictionary *userInfo = [notification userInfo]; -#if !TARGET_OS_OSX // [macOS] UITraitCollection *traitCollection = nil; if (userInfo) { traitCollection = userInfo[RCTUserInterfaceStyleDidChangeNotificationTraitCollectionKey]; } NSString *newColorScheme = RCTColorSchemePreference(traitCollection); #else // [macOS - NSAppearance *appearance = nil; - if (userInfo) { - appearance = userInfo[RCTUserInterfaceStyleDidChangeNotificationAppearanceKey]; - } - NSString *newColorScheme = RCTColorSchemePreference(appearance); + NSString *newColorScheme = RCTColorSchemePreference([RCTAppearanceProxy sharedInstance].currentAppearance); #endif // macOS] if (![_currentColorScheme isEqualToString:newColorScheme]) { _currentColorScheme = newColorScheme; - [self sendEventWithName:@"appearanceChanged" body:@{@"colorScheme" : newColorScheme}]; + [self sendEventWithName:@"appearanceChanged" body:@{ @"colorScheme" : newColorScheme }]; } } diff --git a/packages/react-native/React/CoreModules/RCTDevLoadingView.mm b/packages/react-native/React/CoreModules/RCTDevLoadingView.mm index 3298232b47e981..64276ea56676ad 100644 --- a/packages/react-native/React/CoreModules/RCTDevLoadingView.mm +++ b/packages/react-native/React/CoreModules/RCTDevLoadingView.mm @@ -150,7 +150,7 @@ - (void)showMessage:(NSString *)message color:(RCTUIColor *)color backgroundColo #if !TARGET_OS_OSX // [macOS] [self->_window.rootViewController.view addSubview:self->_container]; #else // [macOS - [self->_window.contentViewController.view addSubview:self->_container]; + [self->_window.contentView addSubview:self->_container]; #endif // macOS] [self->_container addSubview:self->_label]; @@ -175,6 +175,20 @@ - (void)showMessage:(NSString *)message color:(RCTUIColor *)color backgroundColo [self->_label.bottomAnchor constraintEqualToAnchor:self->_container.bottomAnchor constant:-5], ]]; #else // [macOS + // Container constraints + [NSLayoutConstraint activateConstraints:@[ + [self->_container.topAnchor constraintEqualToAnchor:self->_window.contentView.topAnchor], + [self->_container.leadingAnchor constraintEqualToAnchor:self->_window.contentView.leadingAnchor], + [self->_container.trailingAnchor constraintEqualToAnchor:self->_window.contentView.trailingAnchor], + [self->_container.bottomAnchor constraintEqualToAnchor:self->_window.contentView.bottomAnchor], + + // Label constraints + [self->_label.centerXAnchor constraintEqualToAnchor:self->_container.centerXAnchor], + [self->_label.centerYAnchor constraintEqualToAnchor:self->_container.centerYAnchor], + [self->_label.leadingAnchor constraintGreaterThanOrEqualToAnchor:self->_container.leadingAnchor constant:10], + [self->_label.trailingAnchor constraintLessThanOrEqualToAnchor:self->_container.trailingAnchor constant:-10], + ]]; + if (![[RCTKeyWindow() sheets] doesContain:self->_window]) { [RCTKeyWindow() beginSheet:self->_window completionHandler:^(NSModalResponse returnCode) { [self->_window orderOut:self]; diff --git a/packages/react-native/React/CoreModules/RCTDeviceInfo.mm b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm index 3e1bd9504930e5..ec175bd0f0ea83 100644 --- a/packages/react-native/React/CoreModules/RCTDeviceInfo.mm +++ b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm @@ -64,7 +64,11 @@ - (void)observeValueForKeyPath:(NSString *)keyPath + (BOOL)requiresMainQueueSetup { +#if !TARGET_OS_OSX // [macOS] + return NO; +#else // [macOS return YES; +#endif // macOS] } - (dispatch_queue_t)methodQueue diff --git a/packages/react-native/local-cli/generator-macos/templates/macos/Podfile b/packages/react-native/local-cli/generator-macos/templates/macos/Podfile index cc3684c055c0a8..cb8277f75a963c 100644 --- a/packages/react-native/local-cli/generator-macos/templates/macos/Podfile +++ b/packages/react-native/local-cli/generator-macos/templates/macos/Podfile @@ -1,10 +1,15 @@ -require_relative '../node_modules/react-native-macos/scripts/react_native_pods' -require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules' +require 'pathname' + +ws_dir = Pathname.new(__dir__) +ws_dir = ws_dir.parent until + File.exist?("#{ws_dir}/node_modules/react-native-macos/scripts/react_native_pods.rb") || + ws_dir.expand_path.to_s == '/' +require "#{ws_dir}/node_modules/react-native-macos/scripts/react_native_pods.rb" prepare_react_native_project! target 'HelloWorld-macOS' do - platform :macos, '11.0' + platform :macos, '14.0' use_native_modules! # Flags change depending on the env values.