Skip to content

Commit

Permalink
Calculate Correct Window Dimensions for iOS (#19932)
Browse files Browse the repository at this point in the history
Summary:
Fixes: #16152

[iOS] [Fixed] - Pass back correct dimensions for application window in Dimensions module
Pull Request resolved: #19932

Reviewed By: cpojer

Differential Revision: D14312906

Pulled By: PeteTheHeat

fbshipit-source-id: aacb729c89862267465eefc3534c48d9d4b5d746
  • Loading branch information
rdonnelly authored and facebook-github-bot committed Apr 10, 2019
1 parent 4059034 commit 33b55cc
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
48 changes: 44 additions & 4 deletions React/Modules/RCTDeviceInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
@implementation RCTDeviceInfo {
#if !TARGET_OS_TV
UIInterfaceOrientation _currentInterfaceOrientation;
NSDictionary *_currentInterfaceDimensions;
#endif
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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<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
};
}

Expand Down Expand Up @@ -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


Expand Down
19 changes: 16 additions & 3 deletions React/UIUtils/RCTUIUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 33b55cc

Please sign in to comment.