diff --git a/packages/react-native/React/Base/RCTBridge.mm b/packages/react-native/React/Base/RCTBridge.mm index 7d26bf59bcd..32000177fcb 100644 --- a/packages/react-native/React/Base/RCTBridge.mm +++ b/packages/react-native/React/Base/RCTBridge.mm @@ -15,7 +15,6 @@ #if RCT_ENABLE_INSPECTOR #import "RCTInspectorDevServerHelper.h" #endif -#import #import #import #import @@ -512,7 +511,6 @@ RCT_NOT_IMPLEMENTED(-(instancetype)init) _bundleURL = [RCTConvert NSURL:_bundleURL.absoluteString]; RCTExecuteOnMainQueue(^{ - RCTInitializeUIKitProxies(); RCTRegisterReloadCommandListener(self); RCTReloadCommandSetBundleURL(self->_bundleURL); }); diff --git a/packages/react-native/React/Base/UIKitProxies/RCTInitialAccessibilityValuesProxy.h b/packages/react-native/React/Base/UIKitProxies/RCTInitialAccessibilityValuesProxy.h deleted file mode 100644 index ae369bf6873..00000000000 --- a/packages/react-native/React/Base/UIKitProxies/RCTInitialAccessibilityValuesProxy.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import - -NS_ASSUME_NONNULL_BEGIN - -@interface RCTInitialAccessibilityValuesProxy : NSObject - -+ (instancetype)sharedInstance; - -@property (readonly, nonatomic) BOOL isBoldTextEnabled; -@property (readonly, nonatomic) BOOL isGrayscaleEnabled; -@property (readonly, nonatomic) BOOL isInvertColorsEnabled; -@property (readonly, nonatomic) BOOL isReduceMotionEnabled; -@property (readonly, nonatomic) BOOL isDarkerSystemColorsEnabled; -@property (readonly, nonatomic) BOOL prefersCrossFadeTransitions; -@property (readonly, nonatomic) BOOL isReduceTransparencyEnabled; -@property (readonly, nonatomic) BOOL isVoiceOverEnabled; -@property (readonly, nonatomic) UIContentSizeCategory preferredContentSizeCategory; - -- (void)recordAccessibilityValues; - -@end - -NS_ASSUME_NONNULL_END diff --git a/packages/react-native/React/Base/UIKitProxies/RCTInitialAccessibilityValuesProxy.mm b/packages/react-native/React/Base/UIKitProxies/RCTInitialAccessibilityValuesProxy.mm deleted file mode 100644 index b13b049f5cf..00000000000 --- a/packages/react-native/React/Base/UIKitProxies/RCTInitialAccessibilityValuesProxy.mm +++ /dev/null @@ -1,185 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import "RCTInitialAccessibilityValuesProxy.h" -#import -#import - -@implementation RCTInitialAccessibilityValuesProxy { - BOOL _hasRecordedInitialAccessibilityValues; - BOOL _isBoldTextEnabled; - BOOL _isGrayscaleEnabled; - BOOL _isInvertColorsEnabled; - BOOL _isReduceMotionEnabled; - BOOL _isDarkerSystemColorsEnabled; - BOOL _isReduceTransparencyEnabled; - BOOL _isVoiceOverEnabled; - UIContentSizeCategory _preferredContentSizeCategory; - std::mutex _mutex; -} - -+ (instancetype)sharedInstance -{ - static RCTInitialAccessibilityValuesProxy *sharedInstance = nil; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - sharedInstance = [RCTInitialAccessibilityValuesProxy new]; - }); - return sharedInstance; -} - -- (BOOL)isBoldTextEnabled -{ - { - std::lock_guard lock(_mutex); - if (_hasRecordedInitialAccessibilityValues) { - return _isBoldTextEnabled; - } - } - - __block BOOL isBoldTextEnabled; - RCTUnsafeExecuteOnMainQueueSync(^{ - isBoldTextEnabled = UIAccessibilityIsBoldTextEnabled(); - }); - - return isBoldTextEnabled; -} - -- (BOOL)isGrayscaleEnabled -{ - { - std::lock_guard lock(_mutex); - if (_hasRecordedInitialAccessibilityValues) { - return _isGrayscaleEnabled; - } - } - - __block BOOL isGrayscaleEnabled; - RCTUnsafeExecuteOnMainQueueSync(^{ - isGrayscaleEnabled = UIAccessibilityIsGrayscaleEnabled(); - }); - - return isGrayscaleEnabled; -} - -- (BOOL)isInvertColorsEnabled -{ - { - std::lock_guard lock(_mutex); - if (_hasRecordedInitialAccessibilityValues) { - return _isInvertColorsEnabled; - } - } - - __block BOOL isInvertColorsEnabled; - RCTUnsafeExecuteOnMainQueueSync(^{ - isInvertColorsEnabled = UIAccessibilityIsInvertColorsEnabled(); - }); - - return isInvertColorsEnabled; -} - -- (BOOL)isReduceMotionEnabled -{ - { - std::lock_guard lock(_mutex); - if (_hasRecordedInitialAccessibilityValues) { - return _isReduceMotionEnabled; - } - } - - __block BOOL isReduceMotionEnabled; - RCTUnsafeExecuteOnMainQueueSync(^{ - isReduceMotionEnabled = UIAccessibilityIsReduceMotionEnabled(); - }); - - return isReduceMotionEnabled; -} - -- (BOOL)isDarkerSystemColorsEnabled -{ - { - std::lock_guard lock(_mutex); - if (_hasRecordedInitialAccessibilityValues) { - return _isDarkerSystemColorsEnabled; - } - } - - __block BOOL isDarkerSystemColorsEnabled; - RCTUnsafeExecuteOnMainQueueSync(^{ - isDarkerSystemColorsEnabled = UIAccessibilityDarkerSystemColorsEnabled(); - }); - - return isDarkerSystemColorsEnabled; -} - -- (BOOL)isReduceTransparencyEnabled -{ - { - std::lock_guard lock(_mutex); - if (_hasRecordedInitialAccessibilityValues) { - return _isReduceTransparencyEnabled; - } - } - - __block BOOL isReduceTransparencyEnabled; - RCTUnsafeExecuteOnMainQueueSync(^{ - isReduceTransparencyEnabled = UIAccessibilityIsReduceTransparencyEnabled(); - }); - - return isReduceTransparencyEnabled; -} - -- (BOOL)isVoiceOverEnabled -{ - { - std::lock_guard lock(_mutex); - if (_hasRecordedInitialAccessibilityValues) { - return _isVoiceOverEnabled; - } - } - - __block BOOL isVoiceOverEnabled; - RCTUnsafeExecuteOnMainQueueSync(^{ - isVoiceOverEnabled = UIAccessibilityIsVoiceOverRunning(); - }); - - return isVoiceOverEnabled; -} - -- (UIContentSizeCategory)preferredContentSizeCategory -{ - { - std::lock_guard lock(_mutex); - if (_hasRecordedInitialAccessibilityValues) { - return _preferredContentSizeCategory; - } - } - - __block UIContentSizeCategory preferredContentSizeCategory; - RCTUnsafeExecuteOnMainQueueSync(^{ - preferredContentSizeCategory = RCTSharedApplication().preferredContentSizeCategory; - }); - - return preferredContentSizeCategory; -} - -- (void)recordAccessibilityValues -{ - std::lock_guard lock(_mutex); - _hasRecordedInitialAccessibilityValues = YES; - _isBoldTextEnabled = UIAccessibilityIsBoldTextEnabled(); - _isGrayscaleEnabled = UIAccessibilityIsGrayscaleEnabled(); - _isInvertColorsEnabled = UIAccessibilityIsInvertColorsEnabled(); - _isReduceMotionEnabled = UIAccessibilityIsReduceMotionEnabled(); - _isDarkerSystemColorsEnabled = UIAccessibilityDarkerSystemColorsEnabled(); - _isReduceTransparencyEnabled = UIAccessibilityIsReduceTransparencyEnabled(); - _isVoiceOverEnabled = UIAccessibilityIsVoiceOverRunning(); - _preferredContentSizeCategory = RCTSharedApplication().preferredContentSizeCategory; -} - -@end diff --git a/packages/react-native/React/Base/UIKitProxies/RCTInitializeUIKitProxies.h b/packages/react-native/React/Base/UIKitProxies/RCTInitializeUIKitProxies.h deleted file mode 100644 index c9f0dbf3585..00000000000 --- a/packages/react-native/React/Base/UIKitProxies/RCTInitializeUIKitProxies.h +++ /dev/null @@ -1,10 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import - -void RCTInitializeUIKitProxies(void); diff --git a/packages/react-native/React/Base/UIKitProxies/RCTInitializeUIKitProxies.mm b/packages/react-native/React/Base/UIKitProxies/RCTInitializeUIKitProxies.mm deleted file mode 100644 index 6208f2051c0..00000000000 --- a/packages/react-native/React/Base/UIKitProxies/RCTInitializeUIKitProxies.mm +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import "RCTInitializeUIKitProxies.h" -#import -#import "RCTInitialAccessibilityValuesProxy.h" -#import "RCTKeyWindowValuesProxy.h" -#import "RCTTraitCollectionProxy.h" -#import "RCTWindowSafeAreaProxy.h" - -void RCTInitializeUIKitProxies(void) -{ - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - [[RCTWindowSafeAreaProxy sharedInstance] startObservingSafeArea]; - [[RCTTraitCollectionProxy sharedInstance] startObservingTraitCollection]; - [[RCTInitialAccessibilityValuesProxy sharedInstance] recordAccessibilityValues]; - [[RCTKeyWindowValuesProxy sharedInstance] startObservingWindowSizeIfNecessary]; - // Early cache size of screen to avoid main thread contention - RCTScreenSize(); - }); -} diff --git a/packages/react-native/React/Base/UIKitProxies/RCTKeyWindowValuesProxy.h b/packages/react-native/React/Base/UIKitProxies/RCTKeyWindowValuesProxy.h deleted file mode 100644 index 0723389c5b0..00000000000 --- a/packages/react-native/React/Base/UIKitProxies/RCTKeyWindowValuesProxy.h +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import - -NS_ASSUME_NONNULL_BEGIN - -@interface RCTKeyWindowValuesProxy : NSObject - -+ (instancetype)sharedInstance; - -@property (assign, readonly) CGSize windowSize; -@property (assign, readonly) UIInterfaceOrientation currentInterfaceOrientation; - -- (void)startObservingWindowSizeIfNecessary; - -@end - -NS_ASSUME_NONNULL_END diff --git a/packages/react-native/React/Base/UIKitProxies/RCTKeyWindowValuesProxy.mm b/packages/react-native/React/Base/UIKitProxies/RCTKeyWindowValuesProxy.mm deleted file mode 100644 index bbf118c8a1a..00000000000 --- a/packages/react-native/React/Base/UIKitProxies/RCTKeyWindowValuesProxy.mm +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import "RCTKeyWindowValuesProxy.h" -#import -#import -#import - -#import - -static NSString *const kFrameKeyPath = @"frame"; - -@implementation RCTKeyWindowValuesProxy { - BOOL _isObserving; - std::mutex _mutex; - CGSize _currentWindowSize; - UIInterfaceOrientation _currentInterfaceOrientation; -} - -+ (instancetype)sharedInstance -{ - static RCTKeyWindowValuesProxy *sharedInstance = nil; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - sharedInstance = [RCTKeyWindowValuesProxy new]; - }); - return sharedInstance; -} - -- (instancetype)init -{ - self = [super init]; - if (self) { - _isObserving = NO; - UIView *mainWindow = RCTKeyWindow(); - _currentWindowSize = mainWindow ? mainWindow.bounds.size : UIScreen.mainScreen.bounds.size; - } - return self; -} - -- (void)startObservingWindowSizeIfNecessary -{ - // Accesing _isObserving must be done under the lock to avoid a race condition. - // We can't hold the lock while calling RCTUnsafeExecuteOnMainQueueSync. - // Therefore, reading/writing _isObserving is kept separate from calling RCTUnsafeExecuteOnMainQueueSync. - { - std::lock_guard lock(_mutex); - if (_isObserving) { - return; - } - _isObserving = YES; - } - - // For backwards compatibility, we register for notifications from the main thread only. - // On the new architecture, we are already on the main thread and RCTUnsafeExecuteOnMainQueueSync will simply call - // the block. - RCTUnsafeExecuteOnMainQueueSync(^{ - [RCTKeyWindow() addObserver:self forKeyPath:kFrameKeyPath options:NSKeyValueObservingOptionNew context:nil]; - }); - - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(_interfaceOrientationDidChange) - name:UIApplicationDidBecomeActiveNotification - object:nil]; -} - -- (void)observeValueForKeyPath:(NSString *)keyPath - ofObject:(id)object - change:(NSDictionary *)change - context:(void *)context -{ - if ([keyPath isEqualToString:kFrameKeyPath]) { - [[NSNotificationCenter defaultCenter] postNotificationName:RCTWindowFrameDidChangeNotification object:self]; - { - std::lock_guard lock(_mutex); - _currentWindowSize = RCTKeyWindow().bounds.size; - } - } -} - -- (CGSize)windowSize -{ - { - std::lock_guard lock(_mutex); - if (_isObserving) { - return _currentWindowSize; - } - } - - __block CGSize size; - RCTUnsafeExecuteOnMainQueueSync(^{ - size = RCTKeyWindow().bounds.size; - }); - return size; -} - -- (UIInterfaceOrientation)currentInterfaceOrientation -{ - { - std::lock_guard lock(_mutex); - if (_isObserving) { - return _currentInterfaceOrientation; - } - } - - __block UIInterfaceOrientation interfaceOrientation; - RCTUnsafeExecuteOnMainQueueSync(^{ - interfaceOrientation = RCTKeyWindow().windowScene.interfaceOrientation; - }); - return interfaceOrientation; -} - -- (void)_interfaceOrientationDidChange -{ - std::lock_guard lock(_mutex); - _currentInterfaceOrientation = RCTKeyWindow().windowScene.interfaceOrientation; -} - -@end diff --git a/packages/react-native/React/Base/UIKitProxies/RCTTraitCollectionProxy.h b/packages/react-native/React/Base/UIKitProxies/RCTTraitCollectionProxy.h deleted file mode 100644 index a92d9c5eb50..00000000000 --- a/packages/react-native/React/Base/UIKitProxies/RCTTraitCollectionProxy.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import - -NS_ASSUME_NONNULL_BEGIN - -@interface RCTTraitCollectionProxy : NSObject - -+ (instancetype)sharedInstance; - -/* - * Property to access the current trait collection. - * Thread safe. - */ -@property (nonatomic, readonly) UITraitCollection *currentTraitCollection; - -- (void)startObservingTraitCollection; - -@end - -NS_ASSUME_NONNULL_END diff --git a/packages/react-native/React/Base/UIKitProxies/RCTTraitCollectionProxy.mm b/packages/react-native/React/Base/UIKitProxies/RCTTraitCollectionProxy.mm deleted file mode 100644 index 69cf8aef11b..00000000000 --- a/packages/react-native/React/Base/UIKitProxies/RCTTraitCollectionProxy.mm +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import "RCTTraitCollectionProxy.h" -#import -#import - -#import - -@implementation RCTTraitCollectionProxy { - BOOL _isObserving; - std::mutex _mutex; - UITraitCollection *_currentTraitCollection; -} - -+ (instancetype)sharedInstance -{ - static RCTTraitCollectionProxy *sharedInstance = nil; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - sharedInstance = [RCTTraitCollectionProxy new]; - }); - return sharedInstance; -} - -- (instancetype)init -{ - self = [super init]; - if (self) { - _isObserving = NO; - _currentTraitCollection = [RCTKeyWindow().traitCollection copy]; - } - return self; -} - -- (void)startObservingTraitCollection -{ - RCTAssertMainQueue(); - std::lock_guard lock(_mutex); - if (!_isObserving) { - _isObserving = YES; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(_appearanceDidChange:) - name:RCTUserInterfaceStyleDidChangeNotification - object:nil]; - } -} - -- (UITraitCollection *)currentTraitCollection -{ - { - std::lock_guard lock(_mutex); - if (_isObserving) { - return _currentTraitCollection; - } - } - - // Fallback in case [RCTTraitCollectionProxy startObservingTraitCollection] was not called. - __block UITraitCollection *traitCollection = nil; - RCTUnsafeExecuteOnMainQueueSync(^{ - traitCollection = [RCTKeyWindow().traitCollection copy]; - }); - return traitCollection; -} - -- (void)_appearanceDidChange:(NSNotification *)notification -{ - std::lock_guard lock(_mutex); - - NSDictionary *userInfo = [notification userInfo]; - if (userInfo) { - _currentTraitCollection = userInfo[RCTUserInterfaceStyleDidChangeNotificationTraitCollectionKey]; - } -} - -@end diff --git a/packages/react-native/React/Base/UIKitProxies/RCTWindowSafeAreaProxy.h b/packages/react-native/React/Base/UIKitProxies/RCTWindowSafeAreaProxy.h deleted file mode 100644 index 043ef4271ff..00000000000 --- a/packages/react-native/React/Base/UIKitProxies/RCTWindowSafeAreaProxy.h +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import - -NS_ASSUME_NONNULL_BEGIN - -@interface RCTWindowSafeAreaProxy : NSObject - -+ (instancetype)sharedInstance; - -/* - * Property to access the current safe area insets of the window, read-only. - * Thread safe. - */ -@property (nonatomic, readonly) UIEdgeInsets currentSafeAreaInsets; - -- (void)startObservingSafeArea; - -@end - -NS_ASSUME_NONNULL_END diff --git a/packages/react-native/React/Base/UIKitProxies/RCTWindowSafeAreaProxy.mm b/packages/react-native/React/Base/UIKitProxies/RCTWindowSafeAreaProxy.mm deleted file mode 100644 index 440514a5088..00000000000 --- a/packages/react-native/React/Base/UIKitProxies/RCTWindowSafeAreaProxy.mm +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import "RCTWindowSafeAreaProxy.h" -#import -#import -#import - -#import - -@implementation RCTWindowSafeAreaProxy { - BOOL _isObserving; - std::mutex _mutex; - UIEdgeInsets _currentSafeAreaInsets; -} - -+ (instancetype)sharedInstance -{ - static RCTWindowSafeAreaProxy *sharedInstance = nil; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - sharedInstance = [RCTWindowSafeAreaProxy new]; - }); - return sharedInstance; -} - -- (void)startObservingSafeArea -{ - RCTAssertMainQueue(); - std::lock_guard lock(_mutex); - if (!_isObserving) { - _isObserving = YES; - _currentSafeAreaInsets = RCTKeyWindow().safeAreaInsets; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(_interfaceFrameDidChange) - name:RCTUserInterfaceStyleDidChangeNotification - object:nil]; - } -} - -- (UIEdgeInsets)currentSafeAreaInsets -{ - { - std::lock_guard lock(_mutex); - if (_isObserving) { - return _currentSafeAreaInsets; - } - } - - // Fallback in case [startObservingSafeArea startObservingSafeArea] was not called. - __block UIEdgeInsets insets; -#if !TARGET_OS_MACCATALYST - RCTUnsafeExecuteOnMainQueueSync(^{ - insets = [UIApplication sharedApplication].delegate.window.safeAreaInsets; - }); -#endif - return insets; -} - -- (void)_interfaceFrameDidChange -{ - std::lock_guard lock(_mutex); - _currentSafeAreaInsets = RCTKeyWindow().safeAreaInsets; -} - -@end diff --git a/packages/react-native/React/CoreModules/RCTAccessibilityManager.mm b/packages/react-native/React/CoreModules/RCTAccessibilityManager.mm index 6158f4a2426..c32a12d57a7 100644 --- a/packages/react-native/React/CoreModules/RCTAccessibilityManager.mm +++ b/packages/react-native/React/CoreModules/RCTAccessibilityManager.mm @@ -12,7 +12,6 @@ #import #import #import -#import #import #import @@ -38,7 +37,7 @@ RCT_EXPORT_MODULE() + (BOOL)requiresMainQueueSetup { - return NO; + return YES; } - (instancetype)init @@ -92,15 +91,14 @@ RCT_EXPORT_MODULE() name:UIAccessibilityVoiceOverStatusDidChangeNotification object:nil]; - RCTInitialAccessibilityValuesProxy *initialValuesProxy = [RCTInitialAccessibilityValuesProxy sharedInstance]; - self.contentSizeCategory = initialValuesProxy.preferredContentSizeCategory; - _isBoldTextEnabled = initialValuesProxy.isBoldTextEnabled; - _isGrayscaleEnabled = initialValuesProxy.isGrayscaleEnabled; - _isInvertColorsEnabled = initialValuesProxy.isInvertColorsEnabled; - _isReduceMotionEnabled = initialValuesProxy.isReduceMotionEnabled; - _isDarkerSystemColorsEnabled = initialValuesProxy.isDarkerSystemColorsEnabled; - _isReduceTransparencyEnabled = initialValuesProxy.isReduceTransparencyEnabled; - _isVoiceOverEnabled = initialValuesProxy.isVoiceOverEnabled; + self.contentSizeCategory = RCTSharedApplication().preferredContentSizeCategory; + _isBoldTextEnabled = UIAccessibilityIsBoldTextEnabled(); + _isGrayscaleEnabled = UIAccessibilityIsGrayscaleEnabled(); + _isInvertColorsEnabled = UIAccessibilityIsInvertColorsEnabled(); + _isReduceMotionEnabled = UIAccessibilityIsReduceMotionEnabled(); + _isDarkerSystemColorsEnabled = UIAccessibilityDarkerSystemColorsEnabled(); + _isReduceTransparencyEnabled = UIAccessibilityIsReduceTransparencyEnabled(); + _isVoiceOverEnabled = UIAccessibilityIsVoiceOverRunning(); } return self; } diff --git a/packages/react-native/React/CoreModules/RCTAppearance.mm b/packages/react-native/React/CoreModules/RCTAppearance.mm index e9419d89315..67f3eec8c70 100644 --- a/packages/react-native/React/CoreModules/RCTAppearance.mm +++ b/packages/react-native/React/CoreModules/RCTAppearance.mm @@ -10,7 +10,6 @@ #import #import #import -#import #import "CoreModulesPlugins.h" @@ -90,7 +89,7 @@ NSString *RCTColorSchemePreference(UITraitCollection *traitCollection) - (instancetype)init { if ((self = [super init])) { - UITraitCollection *traitCollection = [RCTTraitCollectionProxy sharedInstance].currentTraitCollection; + UITraitCollection *traitCollection = RCTKeyWindow().traitCollection; _currentColorScheme = RCTColorSchemePreference(traitCollection); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appearanceChanged:) @@ -104,7 +103,7 @@ RCT_EXPORT_MODULE(Appearance) + (BOOL)requiresMainQueueSetup { - return NO; + return YES; } - (dispatch_queue_t)methodQueue @@ -133,7 +132,10 @@ RCT_EXPORT_METHOD(setColorScheme : (NSString *)style) RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSString *, getColorScheme) { if (!sIsAppearancePreferenceSet) { - UITraitCollection *traitCollection = [RCTTraitCollectionProxy sharedInstance].currentTraitCollection; + __block UITraitCollection *traitCollection = nil; + RCTUnsafeExecuteOnMainQueueSync(^{ + traitCollection = RCTKeyWindow().traitCollection; + }); _currentColorScheme = RCTColorSchemePreference(traitCollection); } return _currentColorScheme; diff --git a/packages/react-native/React/CoreModules/RCTDeviceInfo.mm b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm index 6b4fcef8522..193e97448de 100644 --- a/packages/react-native/React/CoreModules/RCTDeviceInfo.mm +++ b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm @@ -14,9 +14,7 @@ #import #import #import -#import #import -#import #import #import "CoreModulesPlugins.h" @@ -33,6 +31,8 @@ using namespace facebook::react; std::atomic _invalidated; } +static NSString *const kFrameKeyPath = @"frame"; + @synthesize moduleRegistry = _moduleRegistry; RCT_EXPORT_MODULE() @@ -40,14 +40,25 @@ RCT_EXPORT_MODULE() - (instancetype)init { if (self = [super init]) { - [[RCTKeyWindowValuesProxy sharedInstance] startObservingWindowSizeIfNecessary]; + [RCTKeyWindow() addObserver:self forKeyPath:kFrameKeyPath options:NSKeyValueObservingOptionNew context:nil]; } return self; } +- (void)observeValueForKeyPath:(NSString *)keyPath + ofObject:(id)object + change:(NSDictionary *)change + context:(void *)context +{ + if ([keyPath isEqualToString:kFrameKeyPath]) { + [self interfaceFrameDidChange]; + [[NSNotificationCenter defaultCenter] postNotificationName:RCTWindowFrameDidChangeNotification object:self]; + } +} + + (BOOL)requiresMainQueueSetup { - return NO; + return YES; } - (dispatch_queue_t)methodQueue @@ -81,7 +92,7 @@ RCT_EXPORT_MODULE() #if TARGET_OS_IOS - _currentInterfaceOrientation = [RCTKeyWindowValuesProxy sharedInstance].currentInterfaceOrientation; + _currentInterfaceOrientation = RCTKeyWindow().windowScene.interfaceOrientation; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(interfaceFrameDidChange) @@ -120,6 +131,8 @@ RCT_EXPORT_MODULE() [[NSNotificationCenter defaultCenter] removeObserver:self name:RCTBridgeWillInvalidateModulesNotification object:nil]; + [RCTKeyWindow() removeObserver:self forKeyPath:kFrameKeyPath]; + #if TARGET_OS_IOS [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil]; #endif @@ -132,8 +145,13 @@ static BOOL RCTIsIPhoneNotched() #if TARGET_OS_IOS dispatch_once(&onceToken, ^{ + RCTAssertMainQueue(); + // 20pt is the top safeArea value in non-notched devices - isIPhoneNotched = [RCTWindowSafeAreaProxy sharedInstance].currentSafeAreaInsets.top > 20; + UIWindow *keyWindow = RCTKeyWindow(); + if (keyWindow) { + isIPhoneNotched = keyWindow.safeAreaInsets.top > 20; + } }); #endif @@ -142,11 +160,13 @@ static BOOL RCTIsIPhoneNotched() static NSDictionary *RCTExportedDimensions(CGFloat fontScale) { + RCTAssertMainQueue(); UIScreen *mainScreen = UIScreen.mainScreen; CGSize screenSize = mainScreen.bounds.size; + UIView *mainWindow = RCTKeyWindow(); // We fallback to screen size if a key window is not found. - CGSize windowSize = [RCTKeyWindowValuesProxy sharedInstance].windowSize; + CGSize windowSize = mainWindow ? mainWindow.bounds.size : screenSize; NSDictionary *dimsWindow = @{ @"width" : @(windowSize.width), @@ -182,14 +202,20 @@ static NSDictionary *RCTExportedDimensions(CGFloat fontScale) - (NSDictionary *)getConstants { - return @{ - @"Dimensions" : [self _exportedDimensions], - // Note: - // This prop is deprecated and will be removed in a future release. - // Please use this only for a quick and temporary solution. - // Use instead. - @"isIPhoneX_deprecated" : @(RCTIsIPhoneNotched()), - }; + __block NSDictionary *constants; + __weak __typeof(self) weakSelf = self; + RCTUnsafeExecuteOnMainQueueSync(^{ + constants = @{ + @"Dimensions" : [weakSelf _exportedDimensions], + // Note: + // This prop is deprecated and will be removed in a future release. + // Please use this only for a quick and temporary solution. + // Use instead. + @"isIPhoneX_deprecated" : @(RCTIsIPhoneNotched()), + }; + }); + + return constants; } - (void)didReceiveNewContentSizeMultiplier @@ -209,10 +235,11 @@ static NSDictionary *RCTExportedDimensions(CGFloat fontScale) - (void)interfaceOrientationDidChange { #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST - UIWindow *keyWindow = RCTKeyWindow(); - UIInterfaceOrientation nextOrientation = keyWindow.windowScene.interfaceOrientation; + UIApplication *application = RCTSharedApplication(); + UIInterfaceOrientation nextOrientation = RCTKeyWindow().windowScene.interfaceOrientation; - BOOL isRunningInFullScreen = CGRectEqualToRect(keyWindow.frame, keyWindow.screen.bounds); + BOOL isRunningInFullScreen = + CGRectEqualToRect(application.delegate.window.frame, application.delegate.window.screen.bounds); // We are catching here two situations for multitasking view: // a) The app is in Split View and the container gets resized -> !isRunningInFullScreen // b) The app changes to/from fullscreen example: App runs in slide over mode and goes into fullscreen-> diff --git a/packages/react-native/React/CoreModules/RCTPlatform.mm b/packages/react-native/React/CoreModules/RCTPlatform.mm index 8ae71b13007..ec8a5f3220e 100644 --- a/packages/react-native/React/CoreModules/RCTPlatform.mm +++ b/packages/react-native/React/CoreModules/RCTPlatform.mm @@ -10,7 +10,6 @@ #import #import -#import #import #import @@ -49,7 +48,7 @@ RCT_EXPORT_MODULE(PlatformConstants) + (BOOL)requiresMainQueueSetup { - return NO; + return YES; } - (dispatch_queue_t)methodQueue @@ -65,27 +64,30 @@ RCT_EXPORT_MODULE(PlatformConstants) - (ModuleConstants)getConstants { - UIDevice *device = [UIDevice currentDevice]; - bool isForceTouchAvailable = [RCTTraitCollectionProxy sharedInstance].currentTraitCollection.forceTouchCapability == - UIForceTouchCapabilityAvailable; - auto versions = RCTGetReactNativeVersion(); - return typedConstants({ - .forceTouchAvailable = isForceTouchAvailable, - .osVersion = [device systemVersion], - .systemName = [device systemName], - .interfaceIdiom = interfaceIdiom([device userInterfaceIdiom]), - .isTesting = RCTRunningInTestEnvironment() ? true : false, - .reactNativeVersion = JS::NativePlatformConstantsIOS::ConstantsReactNativeVersion::Builder( - {.minor = [versions[@"minor"] doubleValue], - .major = [versions[@"major"] doubleValue], - .patch = [versions[@"patch"] doubleValue], - .prerelease = [versions[@"prerelease"] isKindOfClass:[NSNull class]] ? nullptr : versions[@"prerelease"]}), + __block ModuleConstants constants; + RCTUnsafeExecuteOnMainQueueSync(^{ + UIDevice *device = [UIDevice currentDevice]; + auto versions = RCTGetReactNativeVersion(); + constants = typedConstants({ + .forceTouchAvailable = RCTForceTouchAvailable() ? true : false, + .osVersion = [device systemVersion], + .systemName = [device systemName], + .interfaceIdiom = interfaceIdiom([device userInterfaceIdiom]), + .isTesting = RCTRunningInTestEnvironment() ? true : false, + .reactNativeVersion = JS::NativePlatformConstantsIOS::ConstantsReactNativeVersion::Builder( + {.minor = [versions[@"minor"] doubleValue], + .major = [versions[@"major"] doubleValue], + .patch = [versions[@"patch"] doubleValue], + .prerelease = [versions[@"prerelease"] isKindOfClass:[NSNull class]] ? nullptr : versions[@"prerelease"]}), #if TARGET_OS_MACCATALYST - .isMacCatalyst = true, + .isMacCatalyst = true, #else - .isMacCatalyst = false, + .isMacCatalyst = false, #endif + }); }); + + return constants; } - (std::shared_ptr)getTurboModule:(const ObjCTurboModule::InitParams &)params diff --git a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.mm b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.mm index d0a4e7c5ec8..e9ca6925ca4 100644 --- a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.mm +++ b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.mm @@ -12,7 +12,6 @@ #import #import #import -#import #import #import #import @@ -194,7 +193,6 @@ class RCTHostHostTargetDelegate : public facebook::react::jsinspector_modern::Ho RCTExecuteOnMainQueue(^{ // Listen to reload commands RCTRegisterReloadCommandListener(self); - RCTInitializeUIKitProxies(); }); _inspectorHostDelegate = std::make_unique(self);