Add extended configuration method with self as argument #934

Closed
antonsergeev88 wants to merge 1 commits from extended-layout-configuration-block into main
3 changed files with 37 additions and 0 deletions

View File

@@ -10,6 +10,7 @@
NS_ASSUME_NONNULL_BEGIN
typedef void (^YGLayoutConfigurationBlock)(YGLayout *layout);
typedef void (^YGLayoutContainerConfigurationBlock)(YGLayout *layout, UIView *container);
@interface UIView (Yoga)
@@ -30,6 +31,14 @@ typedef void (^YGLayoutConfigurationBlock)(YGLayout *layout);
- (void)configureLayoutWithBlock:(YGLayoutConfigurationBlock)block
NS_SWIFT_NAME(configureLayout(block:));
/**
In ObjC land, every time you access `view.yoga.*` you are adding another `objc_msgSend`
to your code. If you plan on making multiple changes to YGLayout, it's more performant
to use this method, which uses a single objc_msgSend call.
*/
- (void)configureLayoutWithContainerBlock:(YGLayoutContainerConfigurationBlock)block
NS_SWIFT_NAME(configureLayout(containerBlock:));
@end
NS_ASSUME_NONNULL_END

View File

@@ -35,4 +35,11 @@ static const void *kYGYogaAssociatedKey = &kYGYogaAssociatedKey;
}
}
- (void)configureLayoutWithContainerBlock:(YGLayoutContainerConfigurationBlock)block
{
if (block != nil) {
block(self.yoga, self);
}
}
@end

View File

@@ -22,6 +22,13 @@
XCTAssertNoThrow([view configureLayoutWithBlock:block]);
}
- (void)testConfigureContainerLayoutIsNoOpWithNilBlock
{
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
id block = nil;
XCTAssertNoThrow([view configureLayoutWithContainerBlock:block]);
}
- (void)testConfigureLayoutBlockWorksWithValidBlock
{
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
@@ -35,6 +42,20 @@
XCTAssertEqual(view.yoga.width.value, 25);
}
- (void)testConfigureContainerLayoutBlockWorksWithValidBlock
{
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
[view configureLayoutWithContainerBlock:^(YGLayout *layout, UIView *container){
XCTAssertNotNil(layout);
XCTAssertTrue(view == container);
layout.isEnabled = YES;
layout.width = YGPointValue(25);
}];
XCTAssertTrue(view.yoga.isEnabled);
XCTAssertEqual(view.yoga.width.value, 25);
}
- (void)testNodesAreDeallocedWithSingleView
{
__weak YGLayout *layoutRef = nil;