From 7b89a1dd4826487bd02a3e63835dfc684790e6a3 Mon Sep 17 00:00:00 2001 From: David Hart Date: Fri, 28 Apr 2017 10:32:02 -0700 Subject: [PATCH] Implemented percentage values in YogaKit Summary: We still need to wait for the `YGUnitPoint` PR to be merged :) But please let me know what you think. One caveat: because of a limitation of Swift, a literal value can be automatically understood as a point-based `YGValue`, but variables have to be explicitly cast. I haven't found a way around it yet: ``` view.yoga.width = 10 // value == 10, unit == YGUnitPixel let a: CGFloat = 100 view.yoga.height = a // Compiler error view.yoga.height = YGValue(a) // works, not great ``` Closes https://github.com/facebook/yoga/pull/390 Reviewed By: emilsjolander Differential Revision: D4954021 Pulled By: maxoll fbshipit-source-id: 5eff6aeb6dd969d0d5dc557b149bb5819b0e31de --- YogaKit.podspec | 2 +- YogaKit/Source/YGLayout.h | 66 ++-- YogaKit/Source/YGLayout.m | 96 +++--- YogaKit/Source/YGLayoutExtensions.swift | 46 +++ YogaKit/Tests/YogaKitTests.m | 312 ++++++++++-------- YogaKit/YogaKitSample/Podfile.lock | 2 +- .../YogaKitSample/SwiftViewController.swift | 5 + .../YogaKitSample/ViewController.m | 8 +- .../ViewControllers/BasicViewController.swift | 4 +- .../LayoutInclusionViewController.swift | 2 +- 10 files changed, 322 insertions(+), 221 deletions(-) create mode 100644 YogaKit/Source/YGLayoutExtensions.swift diff --git a/YogaKit.podspec b/YogaKit.podspec index 655c7e86..582dd1bb 100644 --- a/YogaKit.podspec +++ b/YogaKit.podspec @@ -19,7 +19,7 @@ podspec = Pod::Spec.new do |spec| spec.ios.frameworks = 'UIKit' spec.dependency 'Yoga', '~> 1.3' - spec.source_files = 'YogaKit/Source/*.{h,m}' + spec.source_files = 'YogaKit/Source/*.{h,m,swift}' spec.public_header_files = 'YogaKit/Source/{YGLayout,UIView+Yoga}.h' spec.private_header_files = 'YogaKit/Source/YGLayout+Private.h' end diff --git a/YogaKit/Source/YGLayout.h b/YogaKit/Source/YGLayout.h index fd7f44c1..fdd933b9 100644 --- a/YogaKit/Source/YGLayout.h +++ b/YogaKit/Source/YGLayout.h @@ -9,6 +9,10 @@ #import #import +#import + +extern YGValue YGPointValue(CGFloat value) NS_SWIFT_UNAVAILABLE("Use the swift Int and FloatingPoint extensions instead"); +extern YGValue YGPercentValue(CGFloat value) NS_SWIFT_UNAVAILABLE("Use the swift Int and FloatingPoint extensions instead"); typedef NS_OPTIONS(NSInteger, YGDimensionFlexibility) { YGDimensionFlexibilityFlexibleWidth = 1 << 0, @@ -41,34 +45,34 @@ typedef NS_OPTIONS(NSInteger, YGDimensionFlexibility) { @property (nonatomic, readwrite, assign) CGFloat flexGrow; @property (nonatomic, readwrite, assign) CGFloat flexShrink; -@property (nonatomic, readwrite, assign) CGFloat flexBasis; +@property (nonatomic, readwrite, assign) YGValue flexBasis; -@property (nonatomic, readwrite, assign) CGFloat left; -@property (nonatomic, readwrite, assign) CGFloat top; -@property (nonatomic, readwrite, assign) CGFloat right; -@property (nonatomic, readwrite, assign) CGFloat bottom; -@property (nonatomic, readwrite, assign) CGFloat start; -@property (nonatomic, readwrite, assign) CGFloat end; +@property (nonatomic, readwrite, assign) YGValue left; +@property (nonatomic, readwrite, assign) YGValue top; +@property (nonatomic, readwrite, assign) YGValue right; +@property (nonatomic, readwrite, assign) YGValue bottom; +@property (nonatomic, readwrite, assign) YGValue start; +@property (nonatomic, readwrite, assign) YGValue end; -@property (nonatomic, readwrite, assign) CGFloat marginLeft; -@property (nonatomic, readwrite, assign) CGFloat marginTop; -@property (nonatomic, readwrite, assign) CGFloat marginRight; -@property (nonatomic, readwrite, assign) CGFloat marginBottom; -@property (nonatomic, readwrite, assign) CGFloat marginStart; -@property (nonatomic, readwrite, assign) CGFloat marginEnd; -@property (nonatomic, readwrite, assign) CGFloat marginHorizontal; -@property (nonatomic, readwrite, assign) CGFloat marginVertical; -@property (nonatomic, readwrite, assign) CGFloat margin; +@property (nonatomic, readwrite, assign) YGValue marginLeft; +@property (nonatomic, readwrite, assign) YGValue marginTop; +@property (nonatomic, readwrite, assign) YGValue marginRight; +@property (nonatomic, readwrite, assign) YGValue marginBottom; +@property (nonatomic, readwrite, assign) YGValue marginStart; +@property (nonatomic, readwrite, assign) YGValue marginEnd; +@property (nonatomic, readwrite, assign) YGValue marginHorizontal; +@property (nonatomic, readwrite, assign) YGValue marginVertical; +@property (nonatomic, readwrite, assign) YGValue margin; -@property (nonatomic, readwrite, assign) CGFloat paddingLeft; -@property (nonatomic, readwrite, assign) CGFloat paddingTop; -@property (nonatomic, readwrite, assign) CGFloat paddingRight; -@property (nonatomic, readwrite, assign) CGFloat paddingBottom; -@property (nonatomic, readwrite, assign) CGFloat paddingStart; -@property (nonatomic, readwrite, assign) CGFloat paddingEnd; -@property (nonatomic, readwrite, assign) CGFloat paddingHorizontal; -@property (nonatomic, readwrite, assign) CGFloat paddingVertical; -@property (nonatomic, readwrite, assign) CGFloat padding; +@property (nonatomic, readwrite, assign) YGValue paddingLeft; +@property (nonatomic, readwrite, assign) YGValue paddingTop; +@property (nonatomic, readwrite, assign) YGValue paddingRight; +@property (nonatomic, readwrite, assign) YGValue paddingBottom; +@property (nonatomic, readwrite, assign) YGValue paddingStart; +@property (nonatomic, readwrite, assign) YGValue paddingEnd; +@property (nonatomic, readwrite, assign) YGValue paddingHorizontal; +@property (nonatomic, readwrite, assign) YGValue paddingVertical; +@property (nonatomic, readwrite, assign) YGValue padding; @property (nonatomic, readwrite, assign) CGFloat borderLeftWidth; @property (nonatomic, readwrite, assign) CGFloat borderTopWidth; @@ -78,12 +82,12 @@ typedef NS_OPTIONS(NSInteger, YGDimensionFlexibility) { @property (nonatomic, readwrite, assign) CGFloat borderEndWidth; @property (nonatomic, readwrite, assign) CGFloat borderWidth; -@property (nonatomic, readwrite, assign) CGFloat width; -@property (nonatomic, readwrite, assign) CGFloat height; -@property (nonatomic, readwrite, assign) CGFloat minWidth; -@property (nonatomic, readwrite, assign) CGFloat minHeight; -@property (nonatomic, readwrite, assign) CGFloat maxWidth; -@property (nonatomic, readwrite, assign) CGFloat maxHeight; +@property (nonatomic, readwrite, assign) YGValue width; +@property (nonatomic, readwrite, assign) YGValue height; +@property (nonatomic, readwrite, assign) YGValue minWidth; +@property (nonatomic, readwrite, assign) YGValue minHeight; +@property (nonatomic, readwrite, assign) YGValue maxWidth; +@property (nonatomic, readwrite, assign) YGValue maxHeight; // Yoga specific properties, not compatible with flexbox specification @property (nonatomic, readwrite, assign) CGFloat aspectRatio; diff --git a/YogaKit/Source/YGLayout.m b/YogaKit/Source/YGLayout.m index fbbb421b..b48d5e49 100644 --- a/YogaKit/Source/YGLayout.m +++ b/YogaKit/Source/YGLayout.m @@ -21,26 +21,30 @@ YGNodeStyleSet##capitalized_name(self.node, lowercased_name); \ } -#define YG_VALUE_PROPERTY(lowercased_name, capitalized_name) \ -- (CGFloat)lowercased_name \ -{ \ - YGValue value = YGNodeStyleGet##capitalized_name(self.node); \ - if (value.unit == YGUnitPoint) { \ - return value.value; \ - } else { \ - return YGUndefined; \ - } \ -} \ - \ -- (void)set##capitalized_name:(CGFloat)lowercased_name \ -{ \ - YGNodeStyleSet##capitalized_name(self.node, lowercased_name); \ +#define YG_VALUE_PROPERTY(lowercased_name, capitalized_name) \ +- (YGValue)lowercased_name \ +{ \ + return YGNodeStyleGet##capitalized_name(self.node); \ +} \ + \ +- (void)set##capitalized_name:(YGValue)lowercased_name \ +{ \ + switch (lowercased_name.unit) { \ + case YGUnitPoint: \ + YGNodeStyleSet##capitalized_name(self.node, lowercased_name.value); \ + break; \ + case YGUnitPercent: \ + YGNodeStyleSet##capitalized_name##Percent(self.node, lowercased_name.value); \ + break; \ + default: \ + NSAssert(NO, @"Not implemented"); \ + } \ } -#define YG_EDGE_PROPERTY_GETTER(lowercased_name, capitalized_name, property, edge) \ -- (CGFloat)lowercased_name \ -{ \ - return YGNodeStyleGet##property(self.node, edge); \ +#define YG_EDGE_PROPERTY_GETTER(type, lowercased_name, capitalized_name, property, edge) \ +- (type)lowercased_name \ +{ \ + return YGNodeStyleGet##property(self.node, edge); \ } #define YG_EDGE_PROPERTY_SETTER(lowercased_name, capitalized_name, property, edge) \ @@ -49,42 +53,50 @@ YGNodeStyleSet##property(self.node, edge, lowercased_name); \ } -#define YG_EDGE_PROPERTY(lowercased_name, capitalized_name, property, edge) \ -YG_EDGE_PROPERTY_GETTER(lowercased_name, capitalized_name, property, edge) \ +#define YG_EDGE_PROPERTY(lowercased_name, capitalized_name, property, edge) \ +YG_EDGE_PROPERTY_GETTER(CGFloat, lowercased_name, capitalized_name, property, edge) \ YG_EDGE_PROPERTY_SETTER(lowercased_name, capitalized_name, property, edge) -#define YG_VALUE_EDGE_PROPERTY_GETTER(objc_lowercased_name, objc_capitalized_name, c_name, edge) \ -- (CGFloat)objc_lowercased_name \ +#define YG_VALUE_EDGE_PROPERTY_SETTER(objc_lowercased_name, objc_capitalized_name, c_name, edge) \ +- (void)set##objc_capitalized_name:(YGValue)objc_lowercased_name \ { \ - YGValue value = YGNodeStyleGet##c_name(self.node, edge); \ - if (value.unit == YGUnitPoint) { \ - return value.value; \ - } else { \ - return YGUndefined; \ + switch (objc_lowercased_name.unit) { \ + case YGUnitPoint: \ + YGNodeStyleSet##c_name(self.node, edge, objc_lowercased_name.value); \ + break; \ + case YGUnitPercent: \ + YGNodeStyleSet##c_name##Percent(self.node, edge, objc_lowercased_name.value); \ + break; \ + default: \ + NSAssert(NO, @"Not implemented"); \ } \ } -#define YG_VALUE_EDGE_PROPERTY_SETTER(objc_lowercased_name, objc_capitalized_name, c_name, edge) \ -- (void)set##objc_capitalized_name:(CGFloat)objc_lowercased_name \ -{ \ - YGNodeStyleSet##c_name(self.node, edge, objc_lowercased_name); \ -} - -#define YG_VALUE_EDGE_PROPERTY(lowercased_name, capitalized_name, property, edge) \ -YG_VALUE_EDGE_PROPERTY_GETTER(lowercased_name, capitalized_name, property, edge) \ +#define YG_VALUE_EDGE_PROPERTY(lowercased_name, capitalized_name, property, edge) \ +YG_EDGE_PROPERTY_GETTER(YGValue, lowercased_name, capitalized_name, property, edge) \ YG_VALUE_EDGE_PROPERTY_SETTER(lowercased_name, capitalized_name, property, edge) -#define YG_VALUE_EDGES_PROPERTIES(lowercased_name, capitalized_name) \ -YG_VALUE_EDGE_PROPERTY(lowercased_name##Left, capitalized_name##Left, capitalized_name, YGEdgeLeft) \ -YG_VALUE_EDGE_PROPERTY(lowercased_name##Top, capitalized_name##Top, capitalized_name, YGEdgeTop) \ -YG_VALUE_EDGE_PROPERTY(lowercased_name##Right, capitalized_name##Right, capitalized_name, YGEdgeRight) \ -YG_VALUE_EDGE_PROPERTY(lowercased_name##Bottom, capitalized_name##Bottom, capitalized_name, YGEdgeBottom) \ -YG_VALUE_EDGE_PROPERTY(lowercased_name##Start, capitalized_name##Start, capitalized_name, YGEdgeStart) \ -YG_VALUE_EDGE_PROPERTY(lowercased_name##End, capitalized_name##End, capitalized_name, YGEdgeEnd) \ +#define YG_VALUE_EDGES_PROPERTIES(lowercased_name, capitalized_name) \ +YG_VALUE_EDGE_PROPERTY(lowercased_name##Left, capitalized_name##Left, capitalized_name, YGEdgeLeft) \ +YG_VALUE_EDGE_PROPERTY(lowercased_name##Top, capitalized_name##Top, capitalized_name, YGEdgeTop) \ +YG_VALUE_EDGE_PROPERTY(lowercased_name##Right, capitalized_name##Right, capitalized_name, YGEdgeRight) \ +YG_VALUE_EDGE_PROPERTY(lowercased_name##Bottom, capitalized_name##Bottom, capitalized_name, YGEdgeBottom) \ +YG_VALUE_EDGE_PROPERTY(lowercased_name##Start, capitalized_name##Start, capitalized_name, YGEdgeStart) \ +YG_VALUE_EDGE_PROPERTY(lowercased_name##End, capitalized_name##End, capitalized_name, YGEdgeEnd) \ YG_VALUE_EDGE_PROPERTY(lowercased_name##Horizontal, capitalized_name##Horizontal, capitalized_name, YGEdgeHorizontal) \ YG_VALUE_EDGE_PROPERTY(lowercased_name##Vertical, capitalized_name##Vertical, capitalized_name, YGEdgeVertical) \ YG_VALUE_EDGE_PROPERTY(lowercased_name, capitalized_name, capitalized_name, YGEdgeAll) +YGValue YGPointValue(CGFloat value) +{ + return (YGValue) { .value = value, .unit = YGUnitPoint }; +} + +YGValue YGPercentValue(CGFloat value) +{ + return (YGValue) { .value = value, .unit = YGUnitPercent }; +} + static YGConfigRef globalConfig; @interface YGLayout () diff --git a/YogaKit/Source/YGLayoutExtensions.swift b/YogaKit/Source/YGLayoutExtensions.swift new file mode 100644 index 00000000..842835b5 --- /dev/null +++ b/YogaKit/Source/YGLayoutExtensions.swift @@ -0,0 +1,46 @@ +/** + * Copyright (c) 2014-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +postfix operator % + +extension Int { + public static postfix func %(value: Int) -> YGValue { + return YGValue(value: Float(value), unit: .percent) + } +} + +extension Float { + public static postfix func %(value: Float) -> YGValue { + return YGValue(value: value, unit: .percent) + } +} + +extension CGFloat { + public static postfix func %(value: CGFloat) -> YGValue { + return YGValue(value: Float(value), unit: .percent) + } +} + +extension YGValue : ExpressibleByIntegerLiteral, ExpressibleByFloatLiteral { + public init(integerLiteral value: Int) { + self = YGValue(value: Float(value), unit: .point) + } + + public init(floatLiteral value: Float) { + self = YGValue(value: value, unit: .point) + } + + public init(_ value: Float) { + self = YGValue(value: value, unit: .point) + } + + public init(_ value: CGFloat) { + self = YGValue(value: Float(value), unit: .point) + } +} diff --git a/YogaKit/Tests/YogaKitTests.m b/YogaKit/Tests/YogaKitTests.m index 583da63f..ed4169c5 100644 --- a/YogaKit/Tests/YogaKitTests.m +++ b/YogaKit/Tests/YogaKitTests.m @@ -31,11 +31,11 @@ [view configureLayoutWithBlock:^(YGLayout *layout){ XCTAssertNotNil(layout); layout.isEnabled = YES; - layout.width = 25; + layout.width = YGPointValue(25); }]; XCTAssertTrue(view.yoga.isEnabled); - XCTAssertEqual(view.yoga.width, 25); + XCTAssertEqual(view.yoga.width.value, 25); } - (void)testNodesAreDeallocedWithSingleView @@ -44,7 +44,7 @@ @autoreleasepool { UIView *view = [[UIView alloc] initWithFrame:CGRectZero]; - view.yoga.flexBasis = 1; + view.yoga.flexBasis = YGPointValue(1); layoutRef = view.yoga; XCTAssertNotNil(layoutRef); @@ -63,11 +63,11 @@ @autoreleasepool { UIView *view = [[UIView alloc] initWithFrame:CGRectZero]; topLayout = view.yoga; - topLayout.flexBasis = 1; + topLayout.flexBasis = YGPointValue(1); UIView *subview = [[UIView alloc] initWithFrame:CGRectZero]; subviewLayout = subview.yoga; - subviewLayout.flexBasis = 1; + subviewLayout.flexBasis = YGPointValue(1); view = nil; } @@ -113,9 +113,9 @@ UIView *textBadgeView = [[UIView alloc] initWithFrame:CGRectZero]; textBadgeView.yoga.isEnabled = YES; - textBadgeView.yoga.margin = 0; - textBadgeView.yoga.width = 10; - textBadgeView.yoga.height = 10; + textBadgeView.yoga.margin = YGPointValue(0); + textBadgeView.yoga.width = YGPointValue(10); + textBadgeView.yoga.height = YGPointValue(10); [container addSubview:textBadgeView]; const CGSize textBadgeViewSize = textBadgeView.yoga.intrinsicSize; @@ -136,14 +136,14 @@ UIView *view = [[UIView alloc] initWithFrame:CGRectZero]; view.yoga.isEnabled = YES; - view.yoga.flexBasis = 0; + view.yoga.flexBasis = YGPointValue(0); view.yoga.flexGrow = 1; [container addSubview:view]; UIView *view2 = [[UIView alloc] initWithFrame:CGRectZero]; view2.yoga.isEnabled = YES; - view2.yoga.marginTop = 25; - view2.yoga.flexBasis = 0; + view2.yoga.marginTop = YGPointValue(25); + view2.yoga.flexBasis = YGPointValue(0); view2.yoga.flexGrow = 1; [container addSubview:view2]; @@ -161,8 +161,8 @@ UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; view.yoga.isEnabled = YES; - view.yoga.width = 100; - view.yoga.height = 100; + view.yoga.width = YGPointValue(100); + view.yoga.height = YGPointValue(100); [container addSubview:view]; [container.yoga applyLayoutPreservingOrigin:YES dimensionFlexibility:YGDimensionFlexibilityFlexibleWidth]; @@ -177,8 +177,8 @@ UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; view.yoga.isEnabled = YES; - view.yoga.width = 100; - view.yoga.height = 100; + view.yoga.width = YGPointValue(100); + view.yoga.height = YGPointValue(100); [container addSubview:view]; [container.yoga applyLayoutPreservingOrigin:YES dimensionFlexibility:YGDimensionFlexibilityFlexibleHeigth]; @@ -193,8 +193,8 @@ UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; view.yoga.isEnabled = YES; - view.yoga.width = 100; - view.yoga.height = 100; + view.yoga.width = YGPointValue(100); + view.yoga.height = YGPointValue(100); [container addSubview:view]; [container.yoga applyLayoutPreservingOrigin:YES dimensionFlexibility:YGDimensionFlexibilityFlexibleWidth | YGDimensionFlexibilityFlexibleHeigth]; @@ -437,12 +437,12 @@ XCTAssertTrue(view.yoga.isLeaf); view.yoga.isEnabled = YES; - view.yoga.width = 50.0; + view.yoga.width = YGPointValue(50); XCTAssertTrue(view.yoga.isLeaf); UIView *const subview = view.subviews[0]; subview.yoga.isEnabled = YES; - subview.yoga.width = 50.0; + subview.yoga.width = YGPointValue(50); XCTAssertFalse(view.yoga.isLeaf); } @@ -454,14 +454,14 @@ UIView *subview1 = [[UIView alloc] initWithFrame:CGRectZero]; subview1.yoga.isEnabled = YES; - subview1.yoga.width = 100; + subview1.yoga.width = YGPointValue(100); subview1.yoga.flexGrow = 1; subview1.yoga.flexDirection = YGFlexDirectionColumn; [container addSubview:subview1]; UIView *subview2 = [[UIView alloc] initWithFrame:CGRectZero]; subview2.yoga.isEnabled = YES; - subview2.yoga.width = 150; + subview2.yoga.width = YGPointValue(150); subview2.yoga.flexGrow = 1; subview2.yoga.flexDirection = YGFlexDirectionColumn; [container addSubview:subview2]; @@ -522,155 +522,195 @@ [container.yoga applyLayoutPreservingOrigin:YES]; } +- (void)testPointPercent +{ + XCTAssertEqual(YGPointValue(1).value, 1); + XCTAssertEqual(YGPointValue(1).unit, YGUnitPoint); + XCTAssertEqual(YGPercentValue(2).value, 2); + XCTAssertEqual(YGPercentValue(2).unit, YGUnitPercent); +} + - (void)testPositionalPropertiesWork { UIView *view = [[UIView alloc] initWithFrame:CGRectZero]; - view.yoga.left = 1; - XCTAssertEqual(YGNodeStyleGetPosition(view.yoga.node, YGEdgeLeft).value, 1); - XCTAssertEqual(YGNodeStyleGetPosition(view.yoga.node, YGEdgeLeft).unit, YGUnitPoint); - XCTAssertEqual(view.yoga.left, 1); + view.yoga.left = YGPointValue(1); + XCTAssertEqual(view.yoga.left.value, 1); + XCTAssertEqual(view.yoga.left.unit, YGUnitPoint); + view.yoga.left = YGPercentValue(2); + XCTAssertEqual(view.yoga.left.value, 2); + XCTAssertEqual(view.yoga.left.unit, YGUnitPercent); - view.yoga.right = 2; - XCTAssertEqual(YGNodeStyleGetPosition(view.yoga.node, YGEdgeRight).value, 2); - XCTAssertEqual(YGNodeStyleGetPosition(view.yoga.node, YGEdgeRight).unit, YGUnitPoint); - XCTAssertEqual(view.yoga.right, 2); + view.yoga.right = YGPointValue(3); + XCTAssertEqual(view.yoga.right.value, 3); + XCTAssertEqual(view.yoga.right.unit, YGUnitPoint); + view.yoga.right = YGPercentValue(4); + XCTAssertEqual(view.yoga.right.value, 4); + XCTAssertEqual(view.yoga.right.unit, YGUnitPercent); - view.yoga.top = 3; - XCTAssertEqual(YGNodeStyleGetPosition(view.yoga.node, YGEdgeTop).value, 3); - XCTAssertEqual(YGNodeStyleGetPosition(view.yoga.node, YGEdgeTop).unit, YGUnitPoint); - XCTAssertEqual(view.yoga.top, 3); + view.yoga.top = YGPointValue(5); + XCTAssertEqual(view.yoga.top.value, 5); + XCTAssertEqual(view.yoga.top.unit, YGUnitPoint); + view.yoga.top = YGPercentValue(6); + XCTAssertEqual(view.yoga.top.value, 6); + XCTAssertEqual(view.yoga.top.unit, YGUnitPercent); - view.yoga.bottom = 4; - XCTAssertEqual(YGNodeStyleGetPosition(view.yoga.node, YGEdgeBottom).value, 4); - XCTAssertEqual(YGNodeStyleGetPosition(view.yoga.node, YGEdgeBottom).unit, YGUnitPoint); - XCTAssertEqual(view.yoga.bottom, 4); + view.yoga.bottom = YGPointValue(7); + XCTAssertEqual(view.yoga.bottom.value, 7); + XCTAssertEqual(view.yoga.bottom.unit, YGUnitPoint); + view.yoga.bottom = YGPercentValue(8); + XCTAssertEqual(view.yoga.bottom.value, 8); + XCTAssertEqual(view.yoga.bottom.unit, YGUnitPercent); - view.yoga.start = 5; - XCTAssertEqual(YGNodeStyleGetPosition(view.yoga.node, YGEdgeStart).value, 5); - XCTAssertEqual(YGNodeStyleGetPosition(view.yoga.node, YGEdgeStart).unit, YGUnitPoint); - XCTAssertEqual(view.yoga.start, 5); + view.yoga.start = YGPointValue(9); + XCTAssertEqual(view.yoga.start.value, 9); + XCTAssertEqual(view.yoga.start.unit, YGUnitPoint); + view.yoga.start = YGPercentValue(10); + XCTAssertEqual(view.yoga.start.value, 10); + XCTAssertEqual(view.yoga.start.unit, YGUnitPercent); - view.yoga.end = 6; - XCTAssertEqual(YGNodeStyleGetPosition(view.yoga.node, YGEdgeEnd).value, 6); - XCTAssertEqual(YGNodeStyleGetPosition(view.yoga.node, YGEdgeEnd).unit, YGUnitPoint); - XCTAssertEqual(view.yoga.end, 6); + view.yoga.end = YGPointValue(11); + XCTAssertEqual(view.yoga.end.value, 11); + XCTAssertEqual(view.yoga.end.unit, YGUnitPoint); + view.yoga.end = YGPercentValue(12); + XCTAssertEqual(view.yoga.end.value, 12); + XCTAssertEqual(view.yoga.end.unit, YGUnitPercent); } - (void)testMarginPropertiesWork { UIView *view = [[UIView alloc] initWithFrame:CGRectZero]; - view.yoga.margin = 1; - XCTAssertEqual(view.yoga.margin, 1); - XCTAssertTrue(isnan(view.yoga.marginLeft)); - XCTAssertTrue(isnan(view.yoga.marginRight)); - XCTAssertTrue(isnan(view.yoga.marginStart)); - XCTAssertTrue(isnan(view.yoga.marginEnd)); - XCTAssertTrue(isnan(view.yoga.marginTop)); - XCTAssertTrue(isnan(view.yoga.marginBottom)); - XCTAssertTrue(isnan(view.yoga.marginHorizontal)); - XCTAssertTrue(isnan(view.yoga.marginVertical)); + view.yoga.margin = YGPointValue(1); + XCTAssertEqual(view.yoga.margin.value, 1); + XCTAssertEqual(view.yoga.margin.unit, YGUnitPoint); + view.yoga.margin = YGPercentValue(2); + XCTAssertEqual(view.yoga.margin.value, 2); + XCTAssertEqual(view.yoga.margin.unit, YGUnitPercent); - view.yoga.marginHorizontal = 2; - XCTAssertEqual(view.yoga.marginHorizontal, 2); - XCTAssertTrue(isnan(view.yoga.marginLeft)); - XCTAssertTrue(isnan(view.yoga.marginRight)); - XCTAssertTrue(isnan(view.yoga.marginStart)); - XCTAssertTrue(isnan(view.yoga.marginEnd)); + view.yoga.marginHorizontal = YGPointValue(3); + XCTAssertEqual(view.yoga.marginHorizontal.value, 3); + XCTAssertEqual(view.yoga.marginHorizontal.unit, YGUnitPoint); + view.yoga.marginHorizontal = YGPercentValue(4); + XCTAssertEqual(view.yoga.marginHorizontal.value, 4); + XCTAssertEqual(view.yoga.marginHorizontal.unit, YGUnitPercent); - view.yoga.marginVertical = 3; - XCTAssertEqual(view.yoga.marginVertical, 3); - XCTAssertTrue(isnan(view.yoga.marginTop)); - XCTAssertTrue(isnan(view.yoga.marginBottom)); + view.yoga.marginVertical = YGPointValue(5); + XCTAssertEqual(view.yoga.marginVertical.value, 5); + XCTAssertEqual(view.yoga.marginVertical.unit, YGUnitPoint); + view.yoga.marginVertical = YGPercentValue(6); + XCTAssertEqual(view.yoga.marginVertical.value, 6); + XCTAssertEqual(view.yoga.marginVertical.unit, YGUnitPercent); - view.yoga.marginLeft = 4; - XCTAssertEqual(YGNodeStyleGetMargin(view.yoga.node, YGEdgeLeft).value, 4); - XCTAssertEqual(YGNodeStyleGetMargin(view.yoga.node, YGEdgeLeft).unit, YGUnitPoint); - XCTAssertEqual(view.yoga.marginLeft, 4); + view.yoga.marginLeft = YGPointValue(7); + XCTAssertEqual(view.yoga.marginLeft.value, 7); + XCTAssertEqual(view.yoga.marginLeft.unit, YGUnitPoint); + view.yoga.marginLeft = YGPercentValue(8); + XCTAssertEqual(view.yoga.marginLeft.value, 8); + XCTAssertEqual(view.yoga.marginLeft.unit, YGUnitPercent); - view.yoga.marginRight = 5; - XCTAssertEqual(YGNodeStyleGetMargin(view.yoga.node, YGEdgeRight).value, 5); - XCTAssertEqual(YGNodeStyleGetMargin(view.yoga.node, YGEdgeRight).unit, YGUnitPoint); - XCTAssertEqual(view.yoga.marginRight, 5); + view.yoga.marginRight = YGPointValue(9); + XCTAssertEqual(view.yoga.marginRight.value, 9); + XCTAssertEqual(view.yoga.marginRight.unit, YGUnitPoint); + view.yoga.marginRight = YGPercentValue(10); + XCTAssertEqual(view.yoga.marginRight.value, 10); + XCTAssertEqual(view.yoga.marginRight.unit, YGUnitPercent); - view.yoga.marginTop = 6; - XCTAssertEqual(YGNodeStyleGetMargin(view.yoga.node, YGEdgeTop).value, 6); - XCTAssertEqual(YGNodeStyleGetMargin(view.yoga.node, YGEdgeTop).unit, YGUnitPoint); - XCTAssertEqual(view.yoga.marginTop, 6); + view.yoga.marginTop = YGPointValue(11); + XCTAssertEqual(view.yoga.marginTop.value, 11); + XCTAssertEqual(view.yoga.marginTop.unit, YGUnitPoint); + view.yoga.marginTop = YGPercentValue(12); + XCTAssertEqual(view.yoga.marginTop.value, 12); + XCTAssertEqual(view.yoga.marginTop.unit, YGUnitPercent); - view.yoga.marginBottom = 7; - XCTAssertEqual(YGNodeStyleGetMargin(view.yoga.node, YGEdgeBottom).value, 7); - XCTAssertEqual(YGNodeStyleGetMargin(view.yoga.node, YGEdgeBottom).unit, YGUnitPoint); - XCTAssertEqual(view.yoga.marginBottom, 7); + view.yoga.marginBottom = YGPointValue(13); + XCTAssertEqual(view.yoga.marginBottom.value, 13); + XCTAssertEqual(view.yoga.marginBottom.unit, YGUnitPoint); + view.yoga.marginBottom = YGPercentValue(14); + XCTAssertEqual(view.yoga.marginBottom.value, 14); + XCTAssertEqual(view.yoga.marginBottom.unit, YGUnitPercent); - view.yoga.marginStart = 8; - XCTAssertEqual(YGNodeStyleGetMargin(view.yoga.node, YGEdgeStart).value, 8); - XCTAssertEqual(YGNodeStyleGetMargin(view.yoga.node, YGEdgeStart).unit, YGUnitPoint); - XCTAssertEqual(view.yoga.marginStart, 8); + view.yoga.marginStart = YGPointValue(15); + XCTAssertEqual(view.yoga.marginStart.value, 15); + XCTAssertEqual(view.yoga.marginStart.unit, YGUnitPoint); + view.yoga.marginStart = YGPercentValue(16); + XCTAssertEqual(view.yoga.marginStart.value, 16); + XCTAssertEqual(view.yoga.marginStart.unit, YGUnitPercent); - view.yoga.marginEnd = 9; - XCTAssertEqual(YGNodeStyleGetMargin(view.yoga.node, YGEdgeEnd).value, 9); - XCTAssertEqual(YGNodeStyleGetMargin(view.yoga.node, YGEdgeEnd).unit, YGUnitPoint); - XCTAssertEqual(view.yoga.marginEnd, 9); + view.yoga.marginEnd = YGPointValue(17); + XCTAssertEqual(view.yoga.marginEnd.value, 17); + XCTAssertEqual(view.yoga.marginEnd.unit, YGUnitPoint); + view.yoga.marginEnd = YGPercentValue(18); + XCTAssertEqual(view.yoga.marginEnd.value, 18); + XCTAssertEqual(view.yoga.marginEnd.unit, YGUnitPercent); } - (void)testPaddingPropertiesWork { UIView *view = [[UIView alloc] initWithFrame:CGRectZero]; - view.yoga.padding = 1; - XCTAssertEqual(view.yoga.padding, 1); - XCTAssertTrue(isnan(view.yoga.paddingLeft)); - XCTAssertTrue(isnan(view.yoga.paddingRight)); - XCTAssertTrue(isnan(view.yoga.paddingStart)); - XCTAssertTrue(isnan(view.yoga.paddingEnd)); - XCTAssertTrue(isnan(view.yoga.paddingTop)); - XCTAssertTrue(isnan(view.yoga.paddingBottom)); - XCTAssertTrue(isnan(view.yoga.paddingHorizontal)); - XCTAssertTrue(isnan(view.yoga.paddingVertical)); + view.yoga.padding = YGPointValue(1); + XCTAssertEqual(view.yoga.padding.value, 1); + XCTAssertEqual(view.yoga.padding.unit, YGUnitPoint); + view.yoga.padding = YGPercentValue(2); + XCTAssertEqual(view.yoga.padding.value, 2); + XCTAssertEqual(view.yoga.padding.unit, YGUnitPercent); - view.yoga.paddingHorizontal = 2; - XCTAssertEqual(view.yoga.paddingHorizontal, 2); - XCTAssertTrue(isnan(view.yoga.paddingLeft)); - XCTAssertTrue(isnan(view.yoga.paddingRight)); - XCTAssertTrue(isnan(view.yoga.paddingStart)); - XCTAssertTrue(isnan(view.yoga.paddingEnd)); + view.yoga.paddingHorizontal = YGPointValue(3); + XCTAssertEqual(view.yoga.paddingHorizontal.value, 3); + XCTAssertEqual(view.yoga.paddingHorizontal.unit, YGUnitPoint); + view.yoga.paddingHorizontal = YGPercentValue(4); + XCTAssertEqual(view.yoga.paddingHorizontal.value, 4); + XCTAssertEqual(view.yoga.paddingHorizontal.unit, YGUnitPercent); - view.yoga.paddingVertical = 3; - XCTAssertEqual(view.yoga.paddingVertical, 3); - XCTAssertTrue(isnan(view.yoga.paddingTop)); - XCTAssertTrue(isnan(view.yoga.paddingBottom)); + view.yoga.paddingVertical = YGPointValue(5); + XCTAssertEqual(view.yoga.paddingVertical.value, 5); + XCTAssertEqual(view.yoga.paddingVertical.unit, YGUnitPoint); + view.yoga.paddingVertical = YGPercentValue(6); + XCTAssertEqual(view.yoga.paddingVertical.value, 6); + XCTAssertEqual(view.yoga.paddingVertical.unit, YGUnitPercent); - view.yoga.paddingLeft = 4; - XCTAssertEqual(YGNodeStyleGetPadding(view.yoga.node, YGEdgeLeft).value, 4); - XCTAssertEqual(YGNodeStyleGetPadding(view.yoga.node, YGEdgeLeft).unit, YGUnitPoint); - XCTAssertEqual(view.yoga.paddingLeft, 4); + view.yoga.paddingLeft = YGPointValue(7); + XCTAssertEqual(view.yoga.paddingLeft.value, 7); + XCTAssertEqual(view.yoga.paddingLeft.unit, YGUnitPoint); + view.yoga.paddingLeft = YGPercentValue(8); + XCTAssertEqual(view.yoga.paddingLeft.value, 8); + XCTAssertEqual(view.yoga.paddingLeft.unit, YGUnitPercent); - view.yoga.paddingRight = 5; - XCTAssertEqual(YGNodeStyleGetPadding(view.yoga.node, YGEdgeRight).value, 5); - XCTAssertEqual(YGNodeStyleGetPadding(view.yoga.node, YGEdgeRight).unit, YGUnitPoint); - XCTAssertEqual(view.yoga.paddingRight, 5); + view.yoga.paddingRight = YGPointValue(9); + XCTAssertEqual(view.yoga.paddingRight.value, 9); + XCTAssertEqual(view.yoga.paddingRight.unit, YGUnitPoint); + view.yoga.paddingRight = YGPercentValue(10); + XCTAssertEqual(view.yoga.paddingRight.value, 10); + XCTAssertEqual(view.yoga.paddingRight.unit, YGUnitPercent); - view.yoga.paddingTop = 6; - XCTAssertEqual(YGNodeStyleGetPadding(view.yoga.node, YGEdgeTop).value, 6); - XCTAssertEqual(YGNodeStyleGetPadding(view.yoga.node, YGEdgeTop).unit, YGUnitPoint); - XCTAssertEqual(view.yoga.paddingTop, 6); + view.yoga.paddingTop = YGPointValue(11); + XCTAssertEqual(view.yoga.paddingTop.value, 11); + XCTAssertEqual(view.yoga.paddingTop.unit, YGUnitPoint); + view.yoga.paddingTop = YGPercentValue(12); + XCTAssertEqual(view.yoga.paddingTop.value, 12); + XCTAssertEqual(view.yoga.paddingTop.unit, YGUnitPercent); - view.yoga.paddingBottom = 7; - XCTAssertEqual(YGNodeStyleGetPadding(view.yoga.node, YGEdgeBottom).value, 7); - XCTAssertEqual(YGNodeStyleGetPadding(view.yoga.node, YGEdgeBottom).unit, YGUnitPoint); - XCTAssertEqual(view.yoga.paddingBottom, 7); + view.yoga.paddingBottom = YGPointValue(13); + XCTAssertEqual(view.yoga.paddingBottom.value, 13); + XCTAssertEqual(view.yoga.paddingBottom.unit, YGUnitPoint); + view.yoga.paddingBottom = YGPercentValue(14); + XCTAssertEqual(view.yoga.paddingBottom.value, 14); + XCTAssertEqual(view.yoga.paddingBottom.unit, YGUnitPercent); - view.yoga.paddingStart = 8; - XCTAssertEqual(YGNodeStyleGetPadding(view.yoga.node, YGEdgeStart).value, 8); - XCTAssertEqual(YGNodeStyleGetPadding(view.yoga.node, YGEdgeStart).unit, YGUnitPoint); - XCTAssertEqual(view.yoga.paddingStart, 8); + view.yoga.paddingStart = YGPointValue(15); + XCTAssertEqual(view.yoga.paddingStart.value, 15); + XCTAssertEqual(view.yoga.paddingStart.unit, YGUnitPoint); + view.yoga.paddingStart = YGPercentValue(16); + XCTAssertEqual(view.yoga.paddingStart.value, 16); + XCTAssertEqual(view.yoga.paddingStart.unit, YGUnitPercent); - view.yoga.paddingEnd = 9; - XCTAssertEqual(YGNodeStyleGetPadding(view.yoga.node, YGEdgeEnd).value, 9); - XCTAssertEqual(YGNodeStyleGetPadding(view.yoga.node, YGEdgeEnd).unit, YGUnitPoint); - XCTAssertEqual(view.yoga.paddingEnd, 9); + view.yoga.paddingEnd = YGPointValue(17); + XCTAssertEqual(view.yoga.paddingEnd.value, 17); + XCTAssertEqual(view.yoga.paddingEnd.unit, YGUnitPoint); + view.yoga.paddingEnd = YGPercentValue(18); + XCTAssertEqual(view.yoga.paddingEnd.value, 18); + XCTAssertEqual(view.yoga.paddingEnd.unit, YGUnitPercent); } - (void)testBorderWidthPropertiesWork @@ -679,12 +719,6 @@ view.yoga.borderWidth = 1; XCTAssertEqual(view.yoga.borderWidth, 1); - XCTAssertTrue(isnan(view.yoga.borderLeftWidth)); - XCTAssertTrue(isnan(view.yoga.borderRightWidth)); - XCTAssertTrue(isnan(view.yoga.borderStartWidth)); - XCTAssertTrue(isnan(view.yoga.borderEndWidth)); - XCTAssertTrue(isnan(view.yoga.borderTopWidth)); - XCTAssertTrue(isnan(view.yoga.borderBottomWidth)); view.yoga.borderLeftWidth = 2; XCTAssertEqual(view.yoga.borderLeftWidth, 2); diff --git a/YogaKit/YogaKitSample/Podfile.lock b/YogaKit/YogaKitSample/Podfile.lock index baa0e955..2fd42243 100644 --- a/YogaKit/YogaKitSample/Podfile.lock +++ b/YogaKit/YogaKitSample/Podfile.lock @@ -19,7 +19,7 @@ EXTERNAL SOURCES: SPEC CHECKSUMS: IGListKit: b826c68ef7a4ae1626c09d4d3e1ea7a169e6c36e Yoga: 2ed1d7accfef3610a67f58c0cf101a0662137f2c - YogaKit: cddeccc6a8d2aff563e4c738d3bddb290a6de4cb + YogaKit: 6ffb39aec3ce792f1d14fb27cd98e7a73e85c71b PODFILE CHECKSUM: 216f8e7127767709e0e43f3711208d238fa5c404 diff --git a/YogaKit/YogaKitSample/YogaKitSample/SwiftViewController.swift b/YogaKit/YogaKitSample/YogaKitSample/SwiftViewController.swift index c0723d41..5a402af8 100644 --- a/YogaKit/YogaKitSample/YogaKitSample/SwiftViewController.swift +++ b/YogaKit/YogaKitSample/YogaKitSample/SwiftViewController.swift @@ -12,6 +12,11 @@ import YogaKit struct DemoItem { let name: String + root.backgroundColor = .red + root.yoga.isEnabled = true + root.yoga.width = YGValue(self.view.bounds.size.width) + root.yoga.height = YGValue(self.view.bounds.size.height) + root.yoga.alignItems = .center } final class SwiftViewController: UIViewController, IGListAdapterDataSource { diff --git a/YogaKit/YogaKitSample/YogaKitSample/ViewController.m b/YogaKit/YogaKitSample/YogaKitSample/ViewController.m index 102ef144..f95dab48 100644 --- a/YogaKit/YogaKitSample/YogaKitSample/ViewController.m +++ b/YogaKit/YogaKitSample/YogaKitSample/ViewController.m @@ -17,16 +17,16 @@ UIView *root = self.view; root.backgroundColor = [UIColor redColor]; root.yoga.isEnabled = YES; - root.yoga.width = self.view.bounds.size.width; - root.yoga.height = self.view.bounds.size.height; + root.yoga.width = YGPointValue(self.view.bounds.size.width); + root.yoga.height = YGPointValue(self.view.bounds.size.height); root.yoga.alignItems = YGAlignCenter; root.yoga.justifyContent = YGJustifyCenter; UIView *child1 = [UIView new]; child1.backgroundColor = [UIColor blueColor]; child1.yoga.isEnabled = YES; - child1.yoga.width = 100; - child1.yoga.height = 100; + child1.yoga.width = YGPointValue(100); + child1.yoga.height = YGPointValue(100); UIView *child2 = [UIView new]; child2.backgroundColor = [UIColor greenColor]; diff --git a/YogaKit/YogaKitSample/YogaKitSample/ViewControllers/BasicViewController.swift b/YogaKit/YogaKitSample/YogaKitSample/ViewControllers/BasicViewController.swift index cbf1d217..234cee31 100644 --- a/YogaKit/YogaKitSample/YogaKitSample/ViewControllers/BasicViewController.swift +++ b/YogaKit/YogaKitSample/YogaKitSample/ViewControllers/BasicViewController.swift @@ -17,8 +17,8 @@ final class BasicViewController: UIViewController { root.backgroundColor = .white root.configureLayout { (layout) in layout.isEnabled = true - layout.width = containerSize.width - layout.height = containerSize.height + layout.width = YGValue(containerSize.width) + layout.height = YGValue(containerSize.height) layout.alignItems = .center layout.justifyContent = .center } diff --git a/YogaKit/YogaKitSample/YogaKitSample/ViewControllers/LayoutInclusionViewController.swift b/YogaKit/YogaKitSample/YogaKitSample/ViewControllers/LayoutInclusionViewController.swift index 62129579..ce062402 100644 --- a/YogaKit/YogaKitSample/YogaKitSample/ViewControllers/LayoutInclusionViewController.swift +++ b/YogaKit/YogaKitSample/YogaKitSample/ViewControllers/LayoutInclusionViewController.swift @@ -29,7 +29,7 @@ final class LayoutInclusionViewController: UIViewController { contentView.configureLayout { (layout) in layout.isEnabled = true layout.height = 300 - layout.width = self.view.bounds.size.width + layout.width = YGValue(self.view.bounds.size.width) layout.flexDirection = .row layout.justifyContent = .center layout.paddingHorizontal = 25