Skip to content

Commit

Permalink
feat(iOS): remove usages of UIScreen mainScreen for Trait collections (
Browse files Browse the repository at this point in the history
…#41214)

Summary:
The goal of this PR is to migrate from deprecated `[UIScreen mainScreen]` and get the `displayScale` from currentTraitCollection. Both of those return the same values.

## Changelog:

[IOS] [CHANGED] - retrieve screen scale from trait collection instead of UIScreen mainScreen

Pull Request resolved: #41214

Test Plan:
Go to Dimensions example and check that everything works as expected

### Before

<img src="https://github.com/facebook/react-native/assets/52801365/53141e67-63e2-4c3b-818e-6a232aae8a5b" height="500" />

### After

<img src="https://github.com/facebook/react-native/assets/52801365/33728fce-0298-459c-b63e-a0b8ea34bde1" height="500" />

Reviewed By: NickGerleman

Differential Revision: D50736794

Pulled By: javache

fbshipit-source-id: d512cba1120204be95caf43ac9916f6597e2ccc8
  • Loading branch information
okwasniewski authored and facebook-github-bot committed Nov 1, 2023
1 parent 817fedb commit ace81ff
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/react-native/React/Base/RCTUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,14 @@ static void RCTUnsafeExecuteOnMainQueueOnceSync(dispatch_once_t *onceToken, disp
void RCTComputeScreenScale(void)
{
dispatch_once(&onceTokenScreenScale, ^{
screenScale = [UIScreen mainScreen].scale;
screenScale = [UITraitCollection currentTraitCollection].displayScale;
});
}

CGFloat RCTScreenScale(void)
{
RCTUnsafeExecuteOnMainQueueOnceSync(&onceTokenScreenScale, ^{
screenScale = [UIScreen mainScreen].scale;
screenScale = [UITraitCollection currentTraitCollection].displayScale;
});

return screenScale;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
// `shouldRasterize`
if (oldViewProps.shouldRasterize != newViewProps.shouldRasterize) {
self.layer.shouldRasterize = newViewProps.shouldRasterize;
self.layer.rasterizationScale = newViewProps.shouldRasterize ? [UIScreen mainScreen].scale : 1.0;
self.layer.rasterizationScale = newViewProps.shouldRasterize ? self.traitCollection.displayScale : 1.0;
}

// `pointerEvents`
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/React/Views/RCTViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ - (RCTShadowView *)shadowView
{
view.layer.shouldRasterize = json ? [RCTConvert BOOL:json] : defaultView.layer.shouldRasterize;
view.layer.rasterizationScale =
view.layer.shouldRasterize ? [UIScreen mainScreen].scale : defaultView.layer.rasterizationScale;
view.layer.shouldRasterize ? view.traitCollection.displayScale : defaultView.layer.rasterizationScale;
}

RCT_REMAP_VIEW_PROPERTY(transform, reactTransform, CATransform3D)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ - (BOOL)compareWithImage:(UIImage *)image
CGImageGetColorSpace(image.CGImage),
(CGBitmapInfo)kCGImageAlphaPremultipliedLast);

CGFloat scaleFactor = [UIScreen mainScreen].scale;
CGFloat scaleFactor = [UITraitCollection currentTraitCollection].displayScale;
CGContextScaleCTM(referenceImageContext, scaleFactor, scaleFactor);
CGContextScaleCTM(imageContext, scaleFactor, scaleFactor);

Expand Down

0 comments on commit ace81ff

Please sign in to comment.