[YogaKit] round-pixel view’s bounds

This commit is contained in:
vvveiii
2020-08-15 10:18:54 +08:00
parent 31fc173ee0
commit b498714d48

View File

@@ -488,9 +488,14 @@ static void YGRemoveAllChildren(const YGNodeRef node) {
YGNodeRemoveAllChildren(node); YGNodeRemoveAllChildren(node);
} }
static CGFloat YGAlignPixelValue(CGFloat value) { static inline CGPoint YGRoundPixelPosition(CGPoint p) {
CGFloat scale = YGScaleFactor(); CGFloat scale = YGScaleFactor();
return ceil(value * scale) / scale; return (CGPoint) { .x = round(p.x * scale) / scale, .y = round(p.y * scale) / scale };
}
static inline CGSize YGAlignPixelSize(CGSize s) {
CGFloat scale = YGScaleFactor();
return (CGSize) { .width = ceil(s.width * scale) / scale, .height = ceil(s.height * scale) / scale };
} }
static void YGApplyLayoutToViewHierarchy(UIView* view, BOOL preserveOrigin) { static void YGApplyLayoutToViewHierarchy(UIView* view, BOOL preserveOrigin) {
@@ -537,17 +542,9 @@ static void YGApplyLayoutToViewHierarchy(UIView* view, BOOL preserveOrigin) {
: CGPointZero; : CGPointZero;
#endif #endif
CGRect frame = (CGRect){ CGRect frame = (CGRect) {
.origin = .origin = (CGPoint) { .x = topLeft.x + origin.x, .y = topLeft.y + origin.y, },
{ .size = (CGSize) { .width = MAX(bottomRight.x - topLeft.x, 0), .height = MAX(bottomRight.y - topLeft.y, 0), }
.x = topLeft.x + origin.x,
.y = topLeft.y + origin.y,
},
.size =
{
.width = MAX(bottomRight.x - topLeft.x, 0),
.height = MAX(bottomRight.y - topLeft.y, 0),
},
}; };
#if TARGET_OS_OSX #if TARGET_OS_OSX
@@ -556,19 +553,19 @@ static void YGApplyLayoutToViewHierarchy(UIView* view, BOOL preserveOrigin) {
} }
view.frame = (CGRect) { view.frame = (CGRect) {
.origin = frame.origin, .origin = YGRoundPixelPosition(frame.origin),
.size = CGSizeMake(YGAlignPixelValue(frame.size.width), YGAlignPixelValue(frame.size.height)) .size = YGAlignPixelSize(frame.size)
}; };
#else #else
view.bounds = (CGRect) { view.bounds = (CGRect) {
.origin = view.bounds.origin, .origin = view.bounds.origin,
.size = CGSizeMake(YGAlignPixelValue(CGRectGetWidth(frame)), YGAlignPixelValue(CGRectGetHeight(frame))) .size = YGAlignPixelSize(frame.size)
}; };
view.center = (CGPoint) { view.center = YGRoundPixelPosition((CGPoint) {
.x = (CGFloat)(CGRectGetMinX(frame) + CGRectGetWidth(frame) * 0.5), .x = CGRectGetMinX(frame) + CGRectGetWidth(frame) * 0.5,
.y = (CGFloat)(CGRectGetMinY(frame) + CGRectGetHeight(frame) * 0.5) .y = CGRectGetMinY(frame) + CGRectGetHeight(frame) * 0.5
}; });
#endif #endif
if (!yoga.isLeaf) { if (!yoga.isLeaf) {