From 7fba863ee71e081deedda2bcddd167f9b5ffa529 Mon Sep 17 00:00:00 2001 From: riteshshukla04 Date: Thu, 12 Jun 2025 21:18:10 +0530 Subject: [PATCH 1/3] Fix Height&Width not changing --- .../react-native/React/CoreModules/RCTDeviceInfo.mm | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/react-native/React/CoreModules/RCTDeviceInfo.mm b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm index 6b4fcef852252e..80424e2b518bac 100644 --- a/packages/react-native/React/CoreModules/RCTDeviceInfo.mm +++ b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm @@ -142,11 +142,12 @@ static BOOL RCTIsIPhoneNotched() static NSDictionary *RCTExportedDimensions(CGFloat fontScale) { - UIScreen *mainScreen = UIScreen.mainScreen; - CGSize screenSize = mainScreen.bounds.size; - - // We fallback to screen size if a key window is not found. - CGSize windowSize = [RCTKeyWindowValuesProxy sharedInstance].windowSize; + 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 = mainWindow ? mainWindow.bounds.size : screenSize; NSDictionary *dimsWindow = @{ @"width" : @(windowSize.width), From c7f52c14fc3aa2faad2bb4213b3720189776d489 Mon Sep 17 00:00:00 2001 From: riteshshukla04 Date: Thu, 12 Jun 2025 21:45:51 +0530 Subject: [PATCH 2/3] Fix Height&Width not changing --- .../React/CoreModules/RCTDeviceInfo.mm | 77 +++++++++++++------ 1 file changed, 53 insertions(+), 24 deletions(-) diff --git a/packages/react-native/React/CoreModules/RCTDeviceInfo.mm b/packages/react-native/React/CoreModules/RCTDeviceInfo.mm index 80424e2b518bac..d04bda2fe29494 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" @@ -31,8 +29,13 @@ @implementation RCTDeviceInfo { NSDictionary *_currentInterfaceDimensions; BOOL _isFullscreen; std::atomic _invalidated; + NSDictionary *_constants; + + __weak UIWindow *_applicationWindow; } +static NSString *const kFrameKeyPath = @"frame"; + @synthesize moduleRegistry = _moduleRegistry; RCT_EXPORT_MODULE() @@ -40,14 +43,26 @@ @implementation RCTDeviceInfo { - (instancetype)init { if (self = [super init]) { - [[RCTKeyWindowValuesProxy sharedInstance] startObservingWindowSizeIfNecessary]; + _applicationWindow = RCTKeyWindow(); + [_applicationWindow 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 +96,7 @@ - (void)initialize #if TARGET_OS_IOS - _currentInterfaceOrientation = [RCTKeyWindowValuesProxy sharedInstance].currentInterfaceOrientation; + _currentInterfaceOrientation = RCTKeyWindow().windowScene.interfaceOrientation; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(interfaceFrameDidChange) @@ -98,6 +113,15 @@ - (void)initialize selector:@selector(invalidate) name:RCTBridgeWillInvalidateModulesNotification object:nil]; + + _constants = @{ + @"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()), + }; } - (void)invalidate @@ -120,6 +144,8 @@ - (void)_cleanupObservers [[NSNotificationCenter defaultCenter] removeObserver:self name:RCTBridgeWillInvalidateModulesNotification object:nil]; + [_applicationWindow removeObserver:self forKeyPath:kFrameKeyPath]; + #if TARGET_OS_IOS [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil]; #endif @@ -132,8 +158,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 +173,12 @@ static BOOL RCTIsIPhoneNotched() static NSDictionary *RCTExportedDimensions(CGFloat fontScale) { - UIScreen *mainScreen = UIScreen.mainScreen; - CGSize screenSize = mainScreen.bounds.size; - UIView *mainWindow = RCTKeyWindow(); - - // We fallback to screen size if a key window is not found. + 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 = mainWindow ? mainWindow.bounds.size : screenSize; NSDictionary *dimsWindow = @{ @@ -171,7 +203,10 @@ - (NSDictionary *)_exportedDimensions RCTAssert(_moduleRegistry, @"Failed to get exported dimensions: RCTModuleRegistry is nil"); RCTAccessibilityManager *accessibilityManager = (RCTAccessibilityManager *)[_moduleRegistry moduleForName:"AccessibilityManager"]; - RCTAssert(accessibilityManager, @"Failed to get exported dimensions: AccessibilityManager is nil"); + // TOOD(T225745315): For some reason, accessibilityManager is nil in some cases. + // We default the fontScale to 1.0 in this case. This should be okay: if we assume + // that accessibilityManager will eventually become available, js will eventually + // be updated with the correct fontScale. CGFloat fontScale = accessibilityManager ? accessibilityManager.multiplier : 1.0; return RCTExportedDimensions(fontScale); } @@ -183,14 +218,7 @@ - (NSDictionary *)_exportedDimensions - (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()), - }; + return _constants; } - (void)didReceiveNewContentSizeMultiplier @@ -210,10 +238,11 @@ - (void)didReceiveNewContentSizeMultiplier - (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-> @@ -276,4 +305,4 @@ - (void)_interfaceFrameDidChange Class RCTDeviceInfoCls(void) { return RCTDeviceInfo.class; -} +} \ No newline at end of file From f888dfe6e69933ab7a0db8a7039f3cdbffd1a7d7 Mon Sep 17 00:00:00 2001 From: riteshshukla04 Date: Thu, 12 Jun 2025 21:49:52 +0530 Subject: [PATCH 3/3] Fix Height&Width not changing