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
This commit is contained in:
Ryan Donnelly
2019-04-10 14:08:42 -07:00
committed by Facebook Github Bot
parent 40590348ea
commit 33b55cccca
2 changed files with 60 additions and 7 deletions

View File

@@ -16,6 +16,7 @@
@implementation RCTDeviceInfo {
#if !TARGET_OS_TV
UIInterfaceOrientation _currentInterfaceOrientation;
NSDictionary *_currentInterfaceDimensions;
#endif
}
@@ -48,6 +49,13 @@ RCT_EXPORT_MODULE()
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 NSDictionary *RCTExportedDimensions(RCTBridge *bridge)
RCTAssertMainQueue();
RCTDimensions dimensions = RCTGetDimensions(bridge.accessibilityManager.multiplier);
typeof (dimensions.window) window = dimensions.window; // Window and Screen are considered equal for iOS.
NSDictionary<NSString *, NSNumber *> *dims = @{
typeof (dimensions.window) window = dimensions.window;
NSDictionary<NSString *, NSNumber *> *dimsWindow = @{
@"width": @(window.width),
@"height": @(window.height),
@"scale": @(window.scale),
@"fontScale": @(window.fontScale)
};
typeof (dimensions.screen) screen = dimensions.screen;
NSDictionary<NSString *, NSNumber *> *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 @@ static NSDictionary *RCTExportedDimensions(RCTBridge *bridge)
_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

View File

@@ -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;
}