Skip to content

Commit ceb1d1c

Browse files
sherginfacebook-github-bot
authored andcommitted
Removing our own implementation of round-to-pixel algorithm
Summary: > Okay, I don't remember where we first met > But hey, admitting is the first step This issue has a looong history. The original algorithm was introduced by Nick Lockwood (nicklockwood Hey Nick! We miss you!) a while ago and from the very beginning this has one small error that basically makes it useless (try to find it yourself, it's fun!) The problem was discovered and fixed twice (D4133643, D4983054), but every time we found that our <Text> infra was not ready for this, so we reverted and abandoned the change. As part of the last attempt to finally solve the issue, I ported the algorithm to Yoga where it lives today and works very well for Lytho and CK. For now, the vision is clear: * The basic algorithm should live in Yoga for unification and performance reasons. * We still have to have `absolutePostion` as part of this API because it might be useful for some components which implement its own custom/non-Yoga-based layout. * We have to enable it in RN eventually. So, this is the first step: Removing old, broken code which we don't plan to fix and use. Make React Native crisp again! Reviewed By: fkgozali Differential Revision: D6888662 fbshipit-source-id: 2e5098d9935dcbe05d66c777dad3a9ec8ac87ec3
1 parent c19bc79 commit ceb1d1c

File tree

1 file changed

+2
-49
lines changed

1 file changed

+2
-49
lines changed

React/Views/RCTShadowView.m

+2-49
Original file line numberDiff line numberDiff line change
@@ -154,35 +154,6 @@ static void RCTProcessMetaPropsBorder(const YGValue metaProps[META_PROP_COUNT],
154154
YGNodeStyleSetBorder(node, YGEdgeAll, metaProps[META_PROP_ALL].value);
155155
}
156156

157-
// The absolute stuff is so that we can take into account our absolute position when rounding in order to
158-
// snap to the pixel grid. For example, say you have the following structure:
159-
//
160-
// +--------+---------+--------+
161-
// | |+-------+| |
162-
// | || || |
163-
// | |+-------+| |
164-
// +--------+---------+--------+
165-
//
166-
// Say the screen width is 320 pts so the three big views will get the following x bounds from our layout system:
167-
// {0, 106.667}, {106.667, 213.333}, {213.333, 320}
168-
//
169-
// Assuming screen scale is 2, these numbers must be rounded to the nearest 0.5 to fit the pixel grid:
170-
// {0, 106.5}, {106.5, 213.5}, {213.5, 320}
171-
// You'll notice that the three widths are 106.5, 107, 106.5.
172-
//
173-
// This is great for the parent views but it gets trickier when we consider rounding for the subview.
174-
//
175-
// When we go to round the bounds for the subview in the middle, it's relative bounds are {0, 106.667}
176-
// which gets rounded to {0, 106.5}. This will cause the subview to be one pixel smaller than it should be.
177-
// this is why we need to pass in the absolute position in order to do the rounding relative to the screen's
178-
// grid rather than the view's grid.
179-
//
180-
// After passing in the absolutePosition of {106.667, y}, we do the following calculations:
181-
// absoluteLeft = round(absolutePosition.x + viewPosition.left) = round(106.667 + 0) = 106.5
182-
// absoluteRight = round(absolutePosition.x + viewPosition.left + viewSize.left) + round(106.667 + 0 + 106.667) = 213.5
183-
// width = 213.5 - 106.5 = 107
184-
// You'll notice that this is the same width we calculated for the parent view because we've taken its position into account.
185-
186157
- (void)applyLayoutNode:(YGNodeRef)node
187158
viewsWithNewFrame:(NSMutableSet<RCTShadowView *> *)viewsWithNewFrame
188159
absolutePosition:(CGPoint)absolutePosition
@@ -218,26 +189,8 @@ - (void)applyLayoutWithFrame:(CGRect)frame
218189
viewsWithUpdatedLayout:(NSMutableSet<RCTShadowView *> *)viewsWithUpdatedLayout
219190
absolutePosition:(CGPoint)absolutePosition
220191
{
221-
CGPoint absoluteTopLeft = {
222-
absolutePosition.x + frame.origin.x,
223-
absolutePosition.y + frame.origin.y
224-
};
225-
226-
CGPoint absoluteBottomRight = {
227-
absolutePosition.x + frame.origin.x + frame.size.width,
228-
absolutePosition.y + frame.origin.y + frame.size.height
229-
};
230-
231-
CGRect roundedFrame = {{
232-
RCTRoundPixelValue(frame.origin.x),
233-
RCTRoundPixelValue(frame.origin.y),
234-
}, {
235-
RCTRoundPixelValue(absoluteBottomRight.x - absoluteTopLeft.x),
236-
RCTRoundPixelValue(absoluteBottomRight.y - absoluteTopLeft.y)
237-
}};
238-
239-
if (!CGRectEqualToRect(_frame, roundedFrame) || _layoutDirection != layoutDirection) {
240-
_frame = roundedFrame;
192+
if (!CGRectEqualToRect(_frame, frame) || _layoutDirection != layoutDirection) {
193+
_frame = frame;
241194
_layoutDirection = layoutDirection;
242195
[viewsWithUpdatedLayout addObject:self];
243196
}

0 commit comments

Comments
 (0)