Rename C api

Summary: This renames the core C api to use the new Yoga branding.

Differential Revision: D4259190

fbshipit-source-id: 26c8b356ca464d4304f5f9dc4192bff10cea2dc9
This commit is contained in:
Emil Sjolander
2016-12-03 04:40:18 -08:00
committed by Facebook Github Bot
parent f7cc614d67
commit dda24b1e23
49 changed files with 6459 additions and 6441 deletions

View File

@@ -11,21 +11,21 @@
#import <objc/runtime.h>
@interface CSSNodeBridge : NSObject
@property (nonatomic, assign, readonly) CSSNodeRef cnode;
@interface YGNodeBridge : NSObject
@property (nonatomic, assign, readonly) YGNodeRef cnode;
@end
@implementation CSSNodeBridge
@implementation YGNodeBridge
+ (void)initialize
{
CSSLayoutSetExperimentalFeatureEnabled(YGExperimentalFeatureWebFlexBasis, true);
YogaSetExperimentalFeatureEnabled(YGExperimentalFeatureWebFlexBasis, true);
}
- (instancetype)init
{
if ([super init]) {
_cnode = CSSNodeNew();
_cnode = YGNodeNew();
}
return self;
@@ -33,11 +33,11 @@
- (void)dealloc
{
CSSNodeFree(_cnode);
YGNodeFree(_cnode);
}
@end
@implementation UIView (CSSLayout)
@implementation UIView (Yoga)
- (BOOL)yg_usesYoga
{
@@ -53,7 +53,7 @@
- (NSUInteger)yg_numberOfChildren
{
return CSSNodeChildCount([self cssNode]);
return YGNodeChildCount([self ygNode]);
}
#pragma mark - Setters
@@ -78,120 +78,120 @@
- (void)yg_setDirection:(YGDirection)direction
{
CSSNodeStyleSetDirection([self cssNode], direction);
YGNodeStyleSetDirection([self ygNode], direction);
}
- (void)yg_setFlexDirection:(YGFlexDirection)flexDirection
{
CSSNodeStyleSetFlexDirection([self cssNode], flexDirection);
YGNodeStyleSetFlexDirection([self ygNode], flexDirection);
}
- (void)yg_setJustifyContent:(YGJustify)justifyContent
{
CSSNodeStyleSetJustifyContent([self cssNode], justifyContent);
YGNodeStyleSetJustifyContent([self ygNode], justifyContent);
}
- (void)yg_setAlignContent:(YGAlign)alignContent
{
CSSNodeStyleSetAlignContent([self cssNode], alignContent);
YGNodeStyleSetAlignContent([self ygNode], alignContent);
}
- (void)yg_setAlignItems:(YGAlign)alignItems
{
CSSNodeStyleSetAlignItems([self cssNode], alignItems);
YGNodeStyleSetAlignItems([self ygNode], alignItems);
}
- (void)yg_setAlignSelf:(YGAlign)alignSelf
{
CSSNodeStyleSetAlignSelf([self cssNode], alignSelf);
YGNodeStyleSetAlignSelf([self ygNode], alignSelf);
}
- (void)yg_setPositionType:(YGPositionType)positionType
{
CSSNodeStyleSetPositionType([self cssNode], positionType);
YGNodeStyleSetPositionType([self ygNode], positionType);
}
- (void)yg_setFlexWrap:(YGWrap)flexWrap
{
CSSNodeStyleSetFlexWrap([self cssNode], flexWrap);
YGNodeStyleSetFlexWrap([self ygNode], flexWrap);
}
- (void)yg_setFlexGrow:(CGFloat)flexGrow
{
CSSNodeStyleSetFlexGrow([self cssNode], flexGrow);
YGNodeStyleSetFlexGrow([self ygNode], flexGrow);
}
- (void)yg_setFlexShrink:(CGFloat)flexShrink
{
CSSNodeStyleSetFlexShrink([self cssNode], flexShrink);
YGNodeStyleSetFlexShrink([self ygNode], flexShrink);
}
- (void)yg_setFlexBasis:(CGFloat)flexBasis
{
CSSNodeStyleSetFlexBasis([self cssNode], flexBasis);
YGNodeStyleSetFlexBasis([self ygNode], flexBasis);
}
- (void)yg_setPosition:(CGFloat)position forEdge:(YGEdge)edge
{
CSSNodeStyleSetPosition([self cssNode], edge, position);
YGNodeStyleSetPosition([self ygNode], edge, position);
}
- (void)yg_setMargin:(CGFloat)margin forEdge:(YGEdge)edge
{
CSSNodeStyleSetMargin([self cssNode], edge, margin);
YGNodeStyleSetMargin([self ygNode], edge, margin);
}
- (void)yg_setPadding:(CGFloat)padding forEdge:(YGEdge)edge
{
CSSNodeStyleSetPadding([self cssNode], edge, padding);
YGNodeStyleSetPadding([self ygNode], edge, padding);
}
- (void)yg_setWidth:(CGFloat)width
{
CSSNodeStyleSetWidth([self cssNode], width);
YGNodeStyleSetWidth([self ygNode], width);
}
- (void)yg_setHeight:(CGFloat)height
{
CSSNodeStyleSetHeight([self cssNode], height);
YGNodeStyleSetHeight([self ygNode], height);
}
- (void)yg_setMinWidth:(CGFloat)minWidth
{
CSSNodeStyleSetMinWidth([self cssNode], minWidth);
YGNodeStyleSetMinWidth([self ygNode], minWidth);
}
- (void)yg_setMinHeight:(CGFloat)minHeight
{
CSSNodeStyleSetMinHeight([self cssNode], minHeight);
YGNodeStyleSetMinHeight([self ygNode], minHeight);
}
- (void)yg_setMaxWidth:(CGFloat)maxWidth
{
CSSNodeStyleSetMaxWidth([self cssNode], maxWidth);
YGNodeStyleSetMaxWidth([self ygNode], maxWidth);
}
- (void)yg_setMaxHeight:(CGFloat)maxHeight
{
CSSNodeStyleSetMaxHeight([self cssNode], maxHeight);
YGNodeStyleSetMaxHeight([self ygNode], maxHeight);
}
- (void)yg_setAspectRatio:(CGFloat)aspectRatio
{
CSSNodeStyleSetAspectRatio([self cssNode], aspectRatio);
YGNodeStyleSetAspectRatio([self ygNode], aspectRatio);
}
#pragma mark - Layout and Sizing
- (YGDirection)yg_resolvedDirection
{
return CSSNodeLayoutGetDirection([self cssNode]);
return YGNodeLayoutGetDirection([self ygNode]);
}
- (void)yg_applyLayout
{
[self calculateLayoutWithSize:self.bounds.size];
CSSApplyLayoutToViewHierarchy(self);
YGApplyLayoutToViewHierarchy(self);
}
- (CGSize)yg_intrinsicSize
@@ -205,13 +205,13 @@
#pragma mark - Private
- (CSSNodeRef)cssNode
- (YGNodeRef)ygNode
{
CSSNodeBridge *node = objc_getAssociatedObject(self, @selector(cssNode));
YGNodeBridge *node = objc_getAssociatedObject(self, @selector(ygNode));
if (!node) {
node = [CSSNodeBridge new];
CSSNodeSetContext(node.cnode, (__bridge void *) self);
objc_setAssociatedObject(self, @selector(cssNode), node, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
node = [YGNodeBridge new];
YGNodeSetContext(node.cnode, (__bridge void *) self);
objc_setAssociatedObject(self, @selector(ygNode), node, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return node.cnode;
@@ -219,26 +219,26 @@
- (CGSize)calculateLayoutWithSize:(CGSize)size
{
NSAssert([NSThread isMainThread], @"CSS Layout calculation must be done on main.");
NSAssert([self yg_usesYoga], @"CSS Layout is not enabled for this view.");
NSAssert([NSThread isMainThread], @"YG Layout calculation must be done on main.");
NSAssert([self yg_usesYoga], @"YG Layout is not enabled for this view.");
CSSAttachNodesFromViewHierachy(self);
YGAttachNodesFromViewHierachy(self);
const CSSNodeRef node = [self cssNode];
CSSNodeCalculateLayout(
const YGNodeRef node = [self ygNode];
YGNodeCalculateLayout(
node,
size.width,
size.height,
CSSNodeStyleGetDirection(node));
YGNodeStyleGetDirection(node));
return (CGSize) {
.width = CSSNodeLayoutGetWidth(node),
.height = CSSNodeLayoutGetHeight(node),
.width = YGNodeLayoutGetWidth(node),
.height = YGNodeLayoutGetHeight(node),
};
}
static CSSSize CSSMeasureView(
CSSNodeRef node,
static YGSize YGMeasureView(
YGNodeRef node,
float width,
YGMeasureMode widthMode,
float height,
@@ -247,19 +247,19 @@ static CSSSize CSSMeasureView(
const CGFloat constrainedWidth = (widthMode == YGMeasureModeUndefined) ? CGFLOAT_MAX : width;
const CGFloat constrainedHeight = (heightMode == YGMeasureModeUndefined) ? CGFLOAT_MAX: height;
UIView *view = (__bridge UIView*) CSSNodeGetContext(node);
UIView *view = (__bridge UIView*) YGNodeGetContext(node);
const CGSize sizeThatFits = [view sizeThatFits:(CGSize) {
.width = constrainedWidth,
.height = constrainedHeight,
}];
return (CSSSize) {
.width = CSSSanitizeMeasurement(constrainedWidth, sizeThatFits.width, widthMode),
.height = CSSSanitizeMeasurement(constrainedHeight, sizeThatFits.height, heightMode),
return (YGSize) {
.width = YGSanitizeMeasurement(constrainedWidth, sizeThatFits.width, widthMode),
.height = YGSanitizeMeasurement(constrainedHeight, sizeThatFits.height, heightMode),
};
}
static CGFloat CSSSanitizeMeasurement(
static CGFloat YGSanitizeMeasurement(
CGFloat constrainedSize,
CGFloat measuredSize,
YGMeasureMode measureMode)
@@ -276,15 +276,15 @@ static CGFloat CSSSanitizeMeasurement(
return result;
}
static void CSSAttachNodesFromViewHierachy(UIView *view) {
CSSNodeRef node = [view cssNode];
static void YGAttachNodesFromViewHierachy(UIView *view) {
YGNodeRef node = [view ygNode];
// Only leaf nodes should have a measure function
if (![view yg_usesYoga] || view.subviews.count == 0) {
CSSNodeSetMeasureFunc(node, CSSMeasureView);
CSSRemoveAllChildren(node);
YGNodeSetMeasureFunc(node, YGMeasureView);
YGRemoveAllChildren(node);
} else {
CSSNodeSetMeasureFunc(node, NULL);
YGNodeSetMeasureFunc(node, NULL);
// Create a list of all the subviews that we are going to use for layout.
NSMutableArray<UIView *> *subviewsToInclude = [[NSMutableArray alloc] initWithCapacity:view.subviews.count];
@@ -295,11 +295,11 @@ static void CSSAttachNodesFromViewHierachy(UIView *view) {
}
BOOL shouldReconstructChildList = NO;
if (CSSNodeChildCount(node) != subviewsToInclude.count) {
if (YGNodeChildCount(node) != subviewsToInclude.count) {
shouldReconstructChildList = YES;
} else {
for (int i = 0; i < subviewsToInclude.count; i++) {
if (CSSNodeGetChild(node, i) != [subviewsToInclude[i] cssNode]) {
if (YGNodeGetChild(node, i) != [subviewsToInclude[i] ygNode]) {
shouldReconstructChildList = YES;
break;
}
@@ -307,29 +307,29 @@ static void CSSAttachNodesFromViewHierachy(UIView *view) {
}
if (shouldReconstructChildList) {
CSSRemoveAllChildren(node);
YGRemoveAllChildren(node);
for (int i = 0 ; i < subviewsToInclude.count; i++) {
UIView *const subview = subviewsToInclude[i];
CSSNodeInsertChild(node, [subview cssNode], i);
CSSAttachNodesFromViewHierachy(subview);
YGNodeInsertChild(node, [subview ygNode], i);
YGAttachNodesFromViewHierachy(subview);
}
}
}
}
static void CSSRemoveAllChildren(const CSSNodeRef node)
static void YGRemoveAllChildren(const YGNodeRef node)
{
if (node == NULL) {
return;
}
while (CSSNodeChildCount(node) > 0) {
CSSNodeRemoveChild(node, CSSNodeGetChild(node, CSSNodeChildCount(node) - 1));
while (YGNodeChildCount(node) > 0) {
YGNodeRemoveChild(node, YGNodeGetChild(node, YGNodeChildCount(node) - 1));
}
}
static CGFloat CSSRoundPixelValue(CGFloat value)
static CGFloat YGRoundPixelValue(CGFloat value)
{
static CGFloat scale;
static dispatch_once_t onceToken;
@@ -340,38 +340,38 @@ static CGFloat CSSRoundPixelValue(CGFloat value)
return round(value * scale) / scale;
}
static void CSSApplyLayoutToViewHierarchy(UIView *view) {
static void YGApplyLayoutToViewHierarchy(UIView *view) {
NSCAssert([NSThread isMainThread], @"Framesetting should only be done on the main thread.");
if (![view yg_includeInLayout]) {
return;
}
CSSNodeRef node = [view cssNode];
YGNodeRef node = [view ygNode];
const CGPoint topLeft = {
CSSNodeLayoutGetLeft(node),
CSSNodeLayoutGetTop(node),
YGNodeLayoutGetLeft(node),
YGNodeLayoutGetTop(node),
};
const CGPoint bottomRight = {
topLeft.x + CSSNodeLayoutGetWidth(node),
topLeft.y + CSSNodeLayoutGetHeight(node),
topLeft.x + YGNodeLayoutGetWidth(node),
topLeft.y + YGNodeLayoutGetHeight(node),
};
view.frame = (CGRect) {
.origin = {
.x = CSSRoundPixelValue(topLeft.x),
.y = CSSRoundPixelValue(topLeft.y),
.x = YGRoundPixelValue(topLeft.x),
.y = YGRoundPixelValue(topLeft.y),
},
.size = {
.width = CSSRoundPixelValue(bottomRight.x) - CSSRoundPixelValue(topLeft.x),
.height = CSSRoundPixelValue(bottomRight.y) - CSSRoundPixelValue(topLeft.y),
.width = YGRoundPixelValue(bottomRight.x) - YGRoundPixelValue(topLeft.x),
.height = YGRoundPixelValue(bottomRight.y) - YGRoundPixelValue(topLeft.y),
},
};
const BOOL isLeaf = ![view yg_usesYoga] || view.subviews.count == 0;
if (!isLeaf) {
for (NSUInteger i = 0; i < view.subviews.count; i++) {
CSSApplyLayoutToViewHierarchy(view.subviews[i]);
YGApplyLayoutToViewHierarchy(view.subviews[i]);
}
}
}