From 33b55ccccad56e0b97af294749d728b67b03e658 Mon Sep 17 00:00:00 2001 From: Ryan Donnelly Date: Wed, 10 Apr 2019 14:08:42 -0700 Subject: [PATCH] Calculate Correct Window Dimensions for iOS (#19932) Summary: Fixes: #16152 [iOS] [Fixed] - Pass back correct dimensions for application window in Dimensions module Pull Request resolved: https://github.com/facebook/react-native/pull/19932 Reviewed By: cpojer Differential Revision: D14312906 Pulled By: PeteTheHeat fbshipit-source-id: aacb729c89862267465eefc3534c48d9d4b5d746 --- React/Modules/RCTDeviceInfo.m | 48 ++++++++++++++++++++++++++++++++--- React/UIUtils/RCTUIUtils.m | 19 +++++++++++--- 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/React/Modules/RCTDeviceInfo.m b/React/Modules/RCTDeviceInfo.m index 8feb87f26dec76..940a1076ac02b3 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 }; } @@ -163,6 +178,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; }