Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autoboxing for scalar/struct attribute values #62

Merged
merged 9 commits into from
Apr 30, 2014
8 changes: 4 additions & 4 deletions Examples/Masonry iOS Examples/MASExampleConstantsView.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ - (id)init {
make.right.equalTo(@-20);
}];

// auto-boxing macros allow you to simply use scalars and structs, they will be wrapped automatically

[view2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(@50);
make.centerX.equalTo(@0);
make.width.equalTo(@200);
make.height.equalTo(@100);
make.center.equalTo(CGPointMake(0, 50));
make.size.equalTo(CGSizeMake(200, 100));
}];

return self;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
//define this constant if you want to use Masonry without the 'mas_' prefix
#define MAS_SHORTHAND

#import "Masonry.h"
//define this constant if you want to enable auto-boxing for default syntax
#define MAS_SHORTHAND_GLOBALS

#import "Masonry.h"
#endif
83 changes: 4 additions & 79 deletions Masonry/MASCompositeConstraint.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,6 @@ - (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(M
[self.childConstraints replaceObjectAtIndex:index withObject:replacementConstraint];
}

#pragma mark - NSLayoutConstraint constant proxies

- (MASConstraint * (^)(MASEdgeInsets))insets {
return ^id(MASEdgeInsets insets) {
self.insets = insets;
return self;
};
}

- (MASConstraint * (^)(CGFloat))offset {
return ^id(CGFloat offset) {
self.offset = offset;
return self;
};
}

- (MASConstraint * (^)(CGSize))sizeOffset {
return ^id(CGSize offset) {
self.sizeOffset = offset;
return self;
};
}

- (MASConstraint * (^)(CGPoint))centerOffset {
return ^id(CGPoint offset) {
self.centerOffset = offset;
return self;
};
}

#pragma mark - NSLayoutConstraint multiplier proxies

- (MASConstraint * (^)(CGFloat))multipliedBy {
Expand All @@ -87,7 +57,7 @@ - (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(M
};
}

#pragma mark - MASLayoutPriority proxies
#pragma mark - MASLayoutPriority proxy

- (MASConstraint * (^)(MASLayoutPriority))priority {
return ^id(MASLayoutPriority priority) {
Expand All @@ -98,62 +68,17 @@ - (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(M
};
}

- (MASConstraint * (^)())priorityLow {
return ^id{
self.priority(MASLayoutPriorityDefaultLow);
return self;
};
}

- (MASConstraint * (^)())priorityMedium {
return ^id{
self.priority(MASLayoutPriorityDefaultMedium);
return self;
};
}

- (MASConstraint * (^)())priorityHigh {
return ^id{
self.priority(MASLayoutPriorityDefaultHigh);
return self;
};
}

#pragma mark - NSLayoutRelation proxies

- (MASConstraint * (^)(id))equalTo {
return ^id(id attr) {
for (MASConstraint *constraint in self.childConstraints.copy) {
constraint.equalTo(attr);
}
return self;
};
}

- (MASConstraint * (^)(id))greaterThanOrEqualTo {
return ^id(id attr) {
- (MASConstraint * (^)(id, NSLayoutRelation))_equalToWithRelation {
return ^id(id attr, NSLayoutRelation relation) {
for (MASConstraint *constraint in self.childConstraints.copy) {
constraint.greaterThanOrEqualTo(attr);
constraint._equalToWithRelation(attr, relation);
}
return self;
};
}

- (MASConstraint * (^)(id))lessThanOrEqualTo {
return ^id(id attr) {
for (MASConstraint *constraint in self.childConstraints.copy) {
constraint.lessThanOrEqualTo(attr);
}
return self;
};
}

#pragma mark - Semantic properties

- (MASConstraint *)with {
return self;
}

#pragma mark - Animator proxy

#if TARGET_OS_MAC && !TARGET_OS_IPHONE
Expand Down
74 changes: 69 additions & 5 deletions Masonry/MASConstraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

/**
* Modifies the NSLayoutConstraint constant,
* only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
* only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
* NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight
*/
- (MASConstraint * (^)(MASEdgeInsets insets))insets;
Expand Down Expand Up @@ -78,29 +78,29 @@
/**
* Sets the constraint relation to NSLayoutRelationEqual
* returns a block which accepts one of the following:
* MASViewAttribute, UIView, NSNumber, NSArray
* MASViewAttribute, UIView, NSValue, NSArray
* see readme for more details.
*/
- (MASConstraint * (^)(id attr))equalTo;

/**
* Sets the constraint relation to NSLayoutRelationGreaterThanOrEqual
* returns a block which accepts one of the following:
* MASViewAttribute, UIView, NSNumber, NSArray
* MASViewAttribute, UIView, NSValue, NSArray
* see readme for more details.
*/
- (MASConstraint * (^)(id attr))greaterThanOrEqualTo;

/**
* Sets the constraint relation to NSLayoutRelationLessThanOrEqual
* returns a block which accepts one of the following:
* MASViewAttribute, UIView, NSNumber, NSArray
* MASViewAttribute, UIView, NSValue, NSArray
* see readme for more details.
*/
- (MASConstraint * (^)(id attr))lessThanOrEqualTo;

/**
* optional semantic property which has no effect but improves the readability of constraint
* Optional semantic property which has no effect but improves the readability of constraint
*/
- (MASConstraint *)with;

Expand Down Expand Up @@ -171,6 +171,35 @@

@end


@interface MASConstraint (Private)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe move this into its own file? And then import it in the necessary .m files then it will be truly private as it wont be exposed by masonry.h

Also once its private we wont have to prefix with _


/**
* Modifies the NSLayoutConstraint constant based on a value type,
* see _setLayoutConstantWithValue: for details
*/
- (MASConstraint * (^)(id))_valueOffset;

/**
* Based on a provided value type, is equal to calling:
* NSNumber - setOffset:
* NSValue with CGPoint - setPointOffset:
* NSValue with CGSize - setSizeOffset:
* NSValue with MASEdgeInsets - setInsets:
*/
- (void)_setLayoutConstantWithValue:(NSValue *)value;

/**
* Sets the constraint relation to given NSLayoutRelation
* returns a block which accepts one of the following:
* MASViewAttribute, UIView, NSValue, NSArray
* see readme for more details.
*/
- (MASConstraint * (^)(id attr, NSLayoutRelation relation))_equalToWithRelation;

@end


@protocol MASConstraintDelegate <NSObject>

/**
Expand All @@ -180,3 +209,38 @@
- (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(MASConstraint *)replacementConstraint;

@end


/**
* Convenience auto-boxing macros for MASConstraint methods.
*
* Defining MAS_SHORTHAND_GLOBALS will turn on auto-boxing for default syntax.
* A potential drawback of this is that the unprefixed macros will appear in global scope.
*/
#define mas_equalTo(...) _equalToWithRelation(MASBoxValue((__VA_ARGS__)), NSLayoutRelationEqual)
#define mas_greaterThanOrEqualTo(...) _equalToWithRelation(MASBoxValue((__VA_ARGS__)), NSLayoutRelationGreaterThanOrEqual)
#define mas_lessThanOrEqualTo(...) _equalToWithRelation(MASBoxValue((__VA_ARGS__)), NSLayoutRelationLessThanOrEqual)

#define mas_offset(...) _valueOffset(MASBoxValue((__VA_ARGS__)))


#ifdef MAS_SHORTHAND_GLOBALS

#define equalTo(...) mas_equalTo(__VA_ARGS__)
#define greaterThanOrEqualTo(...) mas_greaterThanOrEqualTo(__VA_ARGS__)
#define lessThanOrEqualTo(...) mas_lessThanOrEqualTo(__VA_ARGS__)

#define offset(...) mas_offset(__VA_ARGS__)

#endif


@interface MASConstraint (AutocompletionSupport)

- (MASConstraint * (^)(id attr))mas_equalTo;
- (MASConstraint * (^)(id attr))mas_greaterThanOrEqualTo;
- (MASConstraint * (^)(id attr))mas_lessThanOrEqualTo;
- (MASConstraint * (^)(id offset))mas_offset;

@end

Loading