Skip to content

fix: make RCTScreenSize take horizontal orientation into account #51444

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions packages/react-native/React/Base/RCTUtils.mm
Original file line number Diff line number Diff line change
Expand Up @@ -383,21 +383,27 @@ CGFloat RCTFontSizeMultiplier(void)
return mapping[RCTSharedApplication().preferredContentSizeCategory].floatValue;
}

UIDeviceOrientation RCTDeviceOrientation(void) {
return [[UIDevice currentDevice] orientation];
}

CGSize RCTScreenSize(void)
{
// FIXME: this caches whatever the bounds were when it was first called, and then
// doesn't update when the device is rotated. We need to find another thread-
// safe way to get the screen size.
Comment on lines -388 to -390
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does screen size change if we have something insetting the window? Like, a keyboard open, or if a window on an iPad is resized?

I agree with the comment, that this is pretty sketchy 😀


static CGSize size;
static CGSize portraitSize;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
RCTUnsafeExecuteOnMainQueueSync(^{
size = [UIScreen mainScreen].bounds.size;
CGSize screenSize = [UIScreen mainScreen].bounds.size;
portraitSize = CGSizeMake(MIN(screenSize.width, screenSize.height),
MAX(screenSize.width, screenSize.height));
});
});

return size;

if (UIDeviceOrientationIsLandscape(RCTDeviceOrientation())) {
return CGSizeMake(portraitSize.height, portraitSize.width);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we were dispatching sync to avoid calling functions that required being on the UI thread? Is this safe?

} else {
return CGSizeMake(portraitSize.width, portraitSize.height);
}
}

CGSize RCTViewportSize(void)
Expand Down
Loading