diff --git a/React/Modules/RCTDeviceInfo.m b/React/Modules/RCTDeviceInfo.m index 1be4fcd873689c..e619fe020518e3 100644 --- a/React/Modules/RCTDeviceInfo.m +++ b/React/Modules/RCTDeviceInfo.m @@ -16,6 +16,7 @@ @implementation RCTDeviceInfo { #if !TARGET_OS_TV UIInterfaceOrientation _currentInterfaceOrientation; + NSDictionary *_currentInterfaceDimensions; #endif } @@ -48,6 +49,13 @@ - (void)setBridge:(RCTBridge *)bridge selector:@selector(interfaceOrientationDidChange) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil]; + + _currentInterfaceDimensions = RCTExportedDimensions(_bridge); + + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(interfaceFrameDidChange) + name:UIApplicationDidBecomeActiveNotification + object:nil]; #endif } @@ -77,16 +85,23 @@ static BOOL RCTIsIPhoneX() { RCTAssertMainQueue(); RCTDimensions dimensions = RCTGetDimensions(bridge.accessibilityManager.multiplier); - typeof (dimensions.window) window = dimensions.window; // Window and Screen are considered equal for iOS. - NSDictionary *dims = @{ + typeof (dimensions.window) window = dimensions.window; + NSDictionary *dimsWindow = @{ @"width": @(window.width), @"height": @(window.height), @"scale": @(window.scale), @"fontScale": @(window.fontScale) }; + typeof (dimensions.screen) screen = dimensions.screen; + NSDictionary *dimsScreen = @{ + @"width": @(screen.width), + @"height": @(screen.height), + @"scale": @(screen.scale), + @"fontScale": @(screen.fontScale) + }; return @{ - @"window": dims, - @"screen": dims + @"window": dimsWindow, + @"screen": dimsScreen }; } @@ -158,6 +173,31 @@ - (void)_interfaceOrientationDidChange _currentInterfaceOrientation = nextOrientation; } + +- (void)interfaceFrameDidChange +{ + __weak typeof(self) weakSelf = self; + RCTExecuteOnMainQueue(^{ + [weakSelf _interfaceFrameDidChange]; + }); +} + + +- (void)_interfaceFrameDidChange +{ + NSDictionary *nextInterfaceDimensions = RCTExportedDimensions(_bridge); + + if (!([nextInterfaceDimensions isEqual:_currentInterfaceDimensions])) { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" + [_bridge.eventDispatcher sendDeviceEventWithName:@"didUpdateDimensions" + body:nextInterfaceDimensions]; +#pragma clang diagnostic pop + } + + _currentInterfaceDimensions = nextInterfaceDimensions; +} + #endif // TARGET_OS_TV diff --git a/React/UIUtils/RCTUIUtils.m b/React/UIUtils/RCTUIUtils.m index dcc3353ccf6f60..961c9426e3564c 100644 --- a/React/UIUtils/RCTUIUtils.m +++ b/React/UIUtils/RCTUIUtils.m @@ -7,19 +7,32 @@ #import "RCTUIUtils.h" +#import "RCTUtils.h" + RCTDimensions RCTGetDimensions(CGFloat fontScale) { UIScreen *mainScreen = UIScreen.mainScreen; CGSize screenSize = mainScreen.bounds.size; + + UIView *mainWindow; + mainWindow = RCTKeyWindow(); + CGSize windowSize = mainWindow.bounds.size; + RCTDimensions result; - typeof (result.window) dims = { + typeof (result.screen) dimsScreen = { .width = screenSize.width, .height = screenSize.height, .scale = mainScreen.scale, .fontScale = fontScale }; - result.window = dims; - result.screen = dims; + typeof (result.window) dimsWindow = { + .width = windowSize.width, + .height = windowSize.height, + .scale = mainScreen.scale, + .fontScale = fontScale + }; + result.screen = dimsScreen; + result.window = dimsWindow; return result; }