Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate Correct Window Dimensions for iOS #19932

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 44 additions & 4 deletions React/Modules/RCTDeviceInfo.m
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 @@ -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


Expand Down
19 changes: 16 additions & 3 deletions React/UIUtils/RCTUIUtils.m
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