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
3 changes: 3 additions & 0 deletions Masonry.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 6 additions & 80 deletions Masonry/MASCompositeConstraint.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "MASCompositeConstraint.h"
#import "MASConstraint+Private.h"

@interface MASCompositeConstraint () <MASConstraintDelegate>

Expand Down Expand Up @@ -37,36 +38,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 +58,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 +69,17 @@ - (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(M
};
}

- (MASConstraint * (^)())priorityLow {
return ^id{
self.priority(MASLayoutPriorityDefaultLow);
return self;
};
}
#pragma mark - NSLayoutRelation proxy

- (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
59 changes: 59 additions & 0 deletions Masonry/MASConstraint+Private.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// MASConstraint+Private.h
// Masonry
//
// Created by Nick Tymchenko on 29/04/14.
// Copyright (c) 2014 cloudling. All rights reserved.
//

#import "MASConstraint.h"

@protocol MASConstraintDelegate;


@interface MASConstraint ()

/**
* Whether or not to check for an existing constraint instead of adding constraint
*/
@property (nonatomic, assign) BOOL updateExisting;

/**
* Usually MASConstraintMaker but could be a parent MASConstraint
*/
@property (nonatomic, weak) id<MASConstraintDelegate> delegate;

/**
* 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;

@end


@interface MASConstraint (Abstract)

/**
* 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, NSLayoutRelation))equalToWithRelation;

@end


@protocol MASConstraintDelegate <NSObject>

/**
* Notifies the delegate when the constraint needs to be replaced with another constraint. For example
* A MASViewConstraint may turn into a MASCompositeConstraint when an array is passed to one of the equality blocks
*/
- (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(MASConstraint *)replacementConstraint;

@end
69 changes: 47 additions & 22 deletions Masonry/MASConstraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

#import "MASUtilities.h"

@protocol MASConstraintDelegate;

/**
* Enables Constraints to be created with chainable syntax
* Constraint can represent single NSLayoutConstraint (MASViewConstraint)
Expand All @@ -21,7 +19,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 All @@ -45,6 +43,11 @@
*/
- (MASConstraint * (^)(CGFloat offset))offset;

/**
* Modifies the NSLayoutConstraint constant based on a value type
*/
- (MASConstraint * (^)(NSValue *value))valueOffset;

/**
* Sets the NSLayoutConstraint multiplier property
*/
Expand Down Expand Up @@ -78,29 +81,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 @@ -149,16 +152,6 @@
@property (nonatomic, copy, readonly) MASConstraint *animator;
#endif

/**
* Whether or not to check for an existing constraint instead of adding constraint
*/
@property (nonatomic, assign) BOOL updateExisting;

/**
* Usually MASConstraintMaker but could be a parent MASConstraint
*/
@property (nonatomic, weak) id<MASConstraintDelegate> delegate;

/**
* Creates a NSLayoutConstraint and adds it to the appropriate view.
*/
Expand All @@ -171,12 +164,44 @@

@end

@protocol MASConstraintDelegate <NSObject>

/**
* Notifies the delegate when the constraint needs to be replaced with another constraint. For example
* A MASViewConstraint may turn into a MASCompositeConstraint when an array is passed to one of the equality blocks
* 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.
*/
- (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(MASConstraint *)replacementConstraint;
#define mas_equalTo(...) equalTo(MASBoxValue((__VA_ARGS__)))
#define mas_greaterThanOrEqualTo(...) greaterThanOrEqualTo(MASBoxValue((__VA_ARGS__)))
#define mas_lessThanOrEqualTo(...) lessThanOrEqualTo(MASBoxValue((__VA_ARGS__)))

@end
#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 (AutoboxingSupport)

/**
* Aliases to corresponding relation methods (for shorthand macros)
* Also needed to aid autocompletion
*/
- (MASConstraint * (^)(id attr))mas_equalTo;
- (MASConstraint * (^)(id attr))mas_greaterThanOrEqualTo;
- (MASConstraint * (^)(id attr))mas_lessThanOrEqualTo;

/**
* A dummy method to aid autocompletion
*/
- (MASConstraint * (^)(id offset))mas_offset;

@end
Loading