Skip to content

Commit

Permalink
Remove macOS only uses of scale factor
Browse files Browse the repository at this point in the history
  • Loading branch information
Saadnajmi committed Aug 7, 2024
1 parent 0c67608 commit 7ed80be
Show file tree
Hide file tree
Showing 12 changed files with 9 additions and 106 deletions.
8 changes: 4 additions & 4 deletions packages/react-native/Libraries/Image/RCTImageView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ static BOOL RCTShouldReloadImageForSizeChange(CGSize currentSize, CGSize idealSi
scale = scaleX;
}

NSSize newSize = NSMakeSize(RCTRoundPixelValue(originalImageSize.width * scale, windowScale),
RCTRoundPixelValue(originalImageSize.height * scale, windowScale));
NSSize newSize = NSMakeSize(RCTRoundPixelValue(originalImageSize.width * scale),
RCTRoundPixelValue(originalImageSize.height * scale));
NSImage *newImage = [[NSImage alloc] initWithSize:newSize];

for (NSImageRep *imageRep in [originalImage representations]) {
NSImageRep *newImageRep = [imageRep copy];
NSSize newImageRepSize = NSMakeSize(RCTRoundPixelValue(imageRep.size.width * scale, windowScale),
RCTRoundPixelValue(imageRep.size.height * scale, windowScale));
NSSize newImageRepSize = NSMakeSize(RCTRoundPixelValue(imageRep.size.width * scale),
RCTRoundPixelValue(imageRep.size.height * scale));

newImageRep.size = newImageRepSize;

Expand Down
14 changes: 0 additions & 14 deletions packages/react-native/Libraries/Text/Text/RCTTextShadowView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -310,19 +310,10 @@ - (void)layoutSubviewsWithContext:(RCTLayoutContext)layoutContext
UIFont *font = [textStorage attribute:NSFontAttributeName atIndex:range.location effectiveRange:nil];

CGRect frame = {
#if !TARGET_OS_OSX // [macOS]
{RCTRoundPixelValue(glyphRect.origin.x),
RCTRoundPixelValue(
glyphRect.origin.y + glyphRect.size.height - attachmentSize.height + font.descender)},
#else // [macOS
{RCTRoundPixelValue(glyphRect.origin.x, [self scale]),
RCTRoundPixelValue(glyphRect.origin.y + glyphRect.size.height - attachmentSize.height + font.descender, [self scale])},
#endif // macOS]
#if !TARGET_OS_OSX // [macOS]
{RCTRoundPixelValue(attachmentSize.width), RCTRoundPixelValue(attachmentSize.height)}};
#else // [macOS
{RCTRoundPixelValue(attachmentSize.width, [self scale]), RCTRoundPixelValue(attachmentSize.height, [self scale])}};
#endif // macOS]

NSRange truncatedGlyphRange =
[layoutManager truncatedGlyphRangeInLineFragmentForGlyphAtIndex:range.location];
Expand Down Expand Up @@ -427,12 +418,7 @@ static YGSize RCTTextShadowViewMeasure(
}

size = (CGSize){
#if !TARGET_OS_OSX // [macOS]
MIN(RCTCeilPixelValue(size.width), maximumSize.width), MIN(RCTCeilPixelValue(size.height), maximumSize.height)};
#else // [macOS
MIN(RCTCeilPixelValue(size.width, shadowTextView.scale), maximumSize.width),
MIN(RCTCeilPixelValue(size.height, shadowTextView.scale), maximumSize.height)};
#endif // macOS]

// Adding epsilon value illuminates problems with converting values from
// `double` to `float`, and then rounding them to pixel grid in Yoga.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,6 @@ - (CGSize)placeholderSize
UIEdgeInsets textContainerInset = self.textContainerInsets;
#endif // macOS]
NSString *placeholder = self.placeholder ?: @"";
#if !TARGET_OS_OSX // [macOS]
CGSize maxPlaceholderSize =
CGSizeMake(UIEdgeInsetsInsetRect(self.bounds, textContainerInset).size.width, CGFLOAT_MAX);
CGSize placeholderSize = [placeholder boundingRectWithSize:maxPlaceholderSize
Expand All @@ -469,11 +468,6 @@ - (CGSize)placeholderSize
context:nil]
.size;
placeholderSize = CGSizeMake(RCTCeilPixelValue(placeholderSize.width), RCTCeilPixelValue(placeholderSize.height));
#else // [macOS
CGFloat scale = _pointScaleFactor ?: self.window.backingScaleFactor;
CGSize placeholderSize = [placeholder sizeWithAttributes:[self _placeholderTextAttributes]];
placeholderSize = CGSizeMake(RCTCeilPixelValue(placeholderSize.width, scale), RCTCeilPixelValue(placeholderSize.height, scale));
#endif // macOS]
placeholderSize.width += textContainerInset.left + textContainerInset.right;
placeholderSize.height += textContainerInset.top + textContainerInset.bottom;
// Returning size DOES contain `textContainerInset` (aka `padding`; as `sizeThatFits:` does).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,8 @@ - (CGSize)sizeThatFitsMinimumSize:(CGSize)minimumSize maximumSize:(CGSize)maximu
CGSize size = [_layoutManager usedRectForTextContainer:_textContainer].size;

return (CGSize){
#if !TARGET_OS_OSX // [macOS]
MAX(minimumSize.width, MIN(RCTCeilPixelValue(size.width), maximumSize.width)),
MAX(minimumSize.height, MIN(RCTCeilPixelValue(size.height), maximumSize.height))};
#else // [macOS
MAX(minimumSize.width, MIN(RCTCeilPixelValue(size.width, [self scale]), maximumSize.width)),
MAX(minimumSize.height, MIN(RCTCeilPixelValue(size.height, [self scale]), maximumSize.height))};
#endif // macOS]
}

- (CGFloat)lastBaselineForSize:(CGSize)size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,20 +619,8 @@ - (CGSize)intrinsicContentSize
{
// Note: `placeholder` defines intrinsic size for `<TextInput>`.
NSString *text = self.placeholder ?: @"";
#if !TARGET_OS_OSX // [macOS]
CGSize size = [text sizeWithAttributes:[self _placeholderTextAttributes]];
size = CGSizeMake(RCTCeilPixelValue(size.width), RCTCeilPixelValue(size.height));
#else // [macOS
CGSize size = [text sizeWithAttributes:@{NSFontAttributeName: self.font}];
CGFloat scale = _pointScaleFactor ?: self.window.backingScaleFactor;
if (scale == 0.0 && RCTRunningInTestEnvironment()) {
// When running in the test environment the view is not on screen.
// Use a scaleFactor of 1 so that the test results are machine independent.
scale = 1;
}
RCTAssert(scale != 0.0, @"Layout occurs before the view is in a window?");
size = CGSizeMake(RCTCeilPixelValue(size.width, scale), RCTCeilPixelValue(size.height, scale));
#endif // macOS]
size.width += _textContainerInset.left + _textContainerInset.right;
size.height += _textContainerInset.top + _textContainerInset.bottom;
// Returning size DOES contain `textContainerInset` (aka `padding`).
Expand Down
6 changes: 0 additions & 6 deletions packages/react-native/React/Base/RCTUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,9 @@ RCT_EXTERN CGSize RCTScreenSize(void);
RCT_EXTERN CGSize RCTViewportSize(void);

// Round float coordinates to nearest whole screen pixel (not point)
#if !TARGET_OS_OSX // [macOS]
RCT_EXTERN CGFloat RCTRoundPixelValue(CGFloat value);
RCT_EXTERN CGFloat RCTCeilPixelValue(CGFloat value);
RCT_EXTERN CGFloat RCTFloorPixelValue(CGFloat value);
#else // [macOS
RCT_EXTERN CGFloat RCTRoundPixelValue(CGFloat value, CGFloat scale);
RCT_EXTERN CGFloat RCTCeilPixelValue(CGFloat value, CGFloat scale);
RCT_EXTERN CGFloat RCTFloorPixelValue(CGFloat value, CGFloat scale);
#endif // macOS]

// Convert a size in points to pixels, rounded up to the nearest integral size
RCT_EXTERN CGSize RCTSizeInPixels(CGSize pointSize, CGFloat scale);
Expand Down
19 changes: 1 addition & 18 deletions packages/react-native/React/Base/RCTUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ CGSize RCTScreenSize(void)
return size;
}
#else // [macOS
CGFloat RCTScreenScale()
CGFloat RCTScreenScale(void)
{
return [NSScreen mainScreen].backingScaleFactor;
}
Expand All @@ -402,7 +402,6 @@ CGSize RCTViewportSize(void)
#endif // macOS]
}

#if !TARGET_OS_OSX // [macOS]
CGFloat RCTRoundPixelValue(CGFloat value)
{
CGFloat scale = RCTScreenScale();
Expand All @@ -420,22 +419,6 @@ CGFloat RCTFloorPixelValue(CGFloat value)
CGFloat scale = RCTScreenScale();
return floor(value * scale) / scale;
}
#else // [macOS
CGFloat RCTRoundPixelValue(CGFloat value, CGFloat scale)
{
return round(value * scale) / scale;
}

CGFloat RCTCeilPixelValue(CGFloat value, CGFloat scale)
{
return ceil(value * scale) / scale;
}

CGFloat RCTFloorPixelValue(CGFloat value, CGFloat scale)
{
return floor(value * scale) / scale;
}
#endif // macOS]

CGSize RCTSizeInPixels(CGSize pointSize, CGFloat scale)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,10 @@ - (void)_updateStateIfNecessary

UIEdgeInsets insets = [self _safeAreaInsets];
CGFloat scale = _layoutMetrics.pointScaleFactor; // [macOS]
#if !TARGET_OS_OSX // [macOS]
insets.left = RCTRoundPixelValue(insets.left);
insets.top = RCTRoundPixelValue(insets.top);
insets.right = RCTRoundPixelValue(insets.right);
insets.bottom = RCTRoundPixelValue(insets.bottom);
#else // [macOS
insets.left = RCTRoundPixelValue(insets.left, scale);
insets.top = RCTRoundPixelValue(insets.top, scale);
insets.right = RCTRoundPixelValue(insets.right, scale);
insets.bottom = RCTRoundPixelValue(insets.bottom, scale);
#endif // macOS]

auto newPadding = RCTEdgeInsetsFromUIEdgeInsets(insets);
auto threshold = 1.0 / scale + 0.01; // Size of a pixel plus some small threshold. [macOS]
Expand Down
15 changes: 3 additions & 12 deletions packages/react-native/React/Views/RCTBorderDrawing.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,12 @@ RCTCornerInsets RCTGetCornerInsets(RCTCornerRadii cornerRadii, UIEdgeInsets edge
}};
}

static UIEdgeInsets RCTRoundInsetsToPixel(
UIEdgeInsets edgeInsets,
CGFloat scaleFactor // [macOS]
) {
#if !TARGET_OS_OSX // [macOS]
static UIEdgeInsets RCTRoundInsetsToPixel(UIEdgeInsets edgeInsets)
{
edgeInsets.top = RCTRoundPixelValue(edgeInsets.top);
edgeInsets.bottom = RCTRoundPixelValue(edgeInsets.bottom);
edgeInsets.left = RCTRoundPixelValue(edgeInsets.left);
edgeInsets.right = RCTRoundPixelValue(edgeInsets.right);
#else // [macOS
edgeInsets.top = RCTRoundPixelValue(edgeInsets.top, scaleFactor);
edgeInsets.bottom = RCTRoundPixelValue(edgeInsets.bottom, scaleFactor);
edgeInsets.left = RCTRoundPixelValue(edgeInsets.left, scaleFactor);
edgeInsets.right = RCTRoundPixelValue(edgeInsets.right, scaleFactor);
#endif // macOS]
return edgeInsets;
}

Expand Down Expand Up @@ -217,7 +208,7 @@ static CGPathRef RCTPathCreateOuterOutline(BOOL drawToEdge, CGRect rect, RCTCorn
// Incorrect render for borders that are not proportional to device pixel: borders get stretched and become
// significantly bigger than expected.
// Rdar: http://www.openradar.me/15959788
borderInsets = RCTRoundInsetsToPixel(borderInsets, scaleFactor); // [macOS]
borderInsets = RCTRoundInsetsToPixel(borderInsets);

const BOOL makeStretchable =
(borderInsets.left + cornerInsets.topLeft.width + borderInsets.right + cornerInsets.bottomRight.width <=
Expand Down
3 changes: 0 additions & 3 deletions packages/react-native/React/Views/RCTShadowView.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ typedef void (^RCTApplierBlock)(NSDictionary<NSNumber *, RCTPlatformView *> *vie
@property (nonatomic, assign, readonly) YGNodeRef yogaNode;
@property (nonatomic, copy) NSString *viewName;
@property (nonatomic, copy) RCTDirectEventBlock onLayout;
#if TARGET_OS_OSX // [macOS
@property (nonatomic) CGFloat scale;
#endif // macOS]

/**
* Computed layout of the view.
Expand Down
7 changes: 1 addition & 6 deletions packages/react-native/React/Views/RCTShadowView.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,7 @@ + (YGConfigRef)yogaConfig
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
yogaConfig = YGConfigNew();
#if !TARGET_OS_OSX // [macOS]
float pixelsInPoint = RCTScreenScale();
#else // [macOS
float pixelsInPoint = 1; // Use 1x alignment for macOS until we can use backing resolution
#endif // macOS]
YGConfigSetPointScaleFactor(yogaConfig, pixelsInPoint);
YGConfigSetPointScaleFactor(yogaConfig, RCTScreenScale());
YGConfigSetErrata(yogaConfig, YGErrataAll);
});
return yogaConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,12 @@ - (void)drawAttributedString:(AttributedString)attributedString

// `rect`'s width is stored in double precesion.
// `frame`'s width is also in double precesion but was stored as float in Yoga previously, precesion was lost.
#if !TARGET_OS_OSX // [macOS]
if (std::abs(RCTCeilPixelValue(rect.size.width) - frame.size.width) < threshold) {
#else // [macOS
if (std::abs(RCTCeilPixelValue(rect.size.width, RCTScreenScale()) - frame.size.width) < threshold) {
#endif // macOS]
// `textStorage` passed to this method was used to calculate size of frame. If that's the case, it's
// width is the same as frame's width. Origin must be adjusted, otherwise glyhps will be painted in wrong
// place.
// We could create new `NSTextStorage` for the specific frame, but that is expensive.
#if !TARGET_OS_OSX // [macOS]
origin.x -= RCTCeilPixelValue(rect.origin.x);
#else // [macOS
origin.x -= RCTCeilPixelValue(rect.origin.x, RCTScreenScale());
#endif // macOS]
}
}

Expand Down Expand Up @@ -298,12 +290,7 @@ - (TextMeasurement)_measureTextStorage:(NSTextStorage *)textStorage

CGSize size = [layoutManager usedRectForTextContainer:textContainer].size;

#if !TARGET_OS_OSX // [macOS]
size = (CGSize){RCTCeilPixelValue(size.width), RCTCeilPixelValue(size.height)};
#else // [macOS
CGFloat scale = [[NSScreen mainScreen] backingScaleFactor];
size = (CGSize){RCTCeilPixelValue(size.width, scale), RCTCeilPixelValue(size.height, scale)};
#endif // macOS]
__block auto attachments = TextMeasurement::Attachments{};

[textStorage
Expand Down

0 comments on commit 7ed80be

Please sign in to comment.