Sfoglia il codice sorgente

Default to prefixed auto-boxing macros, cleanup constraint implementations

Nikolay Tymchenko 11 anni fa
parent
commit
db4657d1de

+ 4 - 1
Examples/Masonry iOS Examples/Masonry iOS Examples-Prefix.pch

@@ -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

+ 5 - 74
Masonry/MASCompositeConstraint.m

@@ -37,36 +37,6 @@
     [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 {
@@ -87,7 +57,7 @@
     };
 }
 
-#pragma mark - MASLayoutPriority proxies
+#pragma mark - MASLayoutPriority proxy
 
 - (MASConstraint * (^)(MASLayoutPriority))priority {
     return ^id(MASLayoutPriority priority) {
@@ -98,27 +68,6 @@
     };
 }
 
-- (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, NSLayoutRelation))_equalToWithRelation {
@@ -130,12 +79,6 @@
     };
 }
 
-#pragma mark - Semantic properties
-
-- (MASConstraint *)with {
-    return self;
-}
-
 #pragma mark - Animator proxy
 
 #if TARGET_OS_MAC && !TARGET_OS_IPHONE
@@ -164,27 +107,15 @@
 
 #pragma mark - NSLayoutConstraint constant setters
 
-- (void)setInsets:(MASEdgeInsets)insets {
-    for (MASConstraint *constraint in self.childConstraints) {
-        constraint.insets = insets;
-    }
-}
-
-- (void)setOffset:(CGFloat)offset {
+- (void)setValueOffset:(id)offset {
     for (MASConstraint *constraint in self.childConstraints) {
-        constraint.offset = offset;
+        constraint.valueOffset = offset;
     }
 }
 
-- (void)setSizeOffset:(CGSize)sizeOffset {
-    for (MASConstraint *constraint in self.childConstraints) {
-        constraint.sizeOffset = sizeOffset;
-    }
-}
-
-- (void)setCenterOffset:(CGPoint)centerOffset {
+- (void)setInsets:(MASEdgeInsets)insets {
     for (MASConstraint *constraint in self.childConstraints) {
-        constraint.centerOffset = centerOffset;
+        constraint.insets = insets;
     }
 }
 

+ 49 - 34
Masonry/MASConstraint.h

@@ -19,13 +19,6 @@
 
 // Chaining Support
 
-/**
- *	Modifies the NSLayoutConstraint constant,
- *  only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 
- *  NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight
- */
-- (MASConstraint * (^)(MASEdgeInsets insets))insets;
-
 /**
  *	Modifies the NSLayoutConstraint constant,
  *  only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
@@ -45,6 +38,16 @@
  */
 - (MASConstraint * (^)(CGFloat offset))offset;
 
+// TODO: describe
+- (MASConstraint * (^)(id value))_valueOffset;
+
+/**
+ *	Modifies the NSLayoutConstraint constant,
+ *  only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following 
+ *  NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight
+ */
+- (MASConstraint * (^)(MASEdgeInsets insets))insets;
+
 /**
  *	Sets the NSLayoutConstraint multiplier property
  */
@@ -103,12 +106,6 @@
 // TODO: update docs for the methods above
 - (MASConstraint * (^)(id attr, NSLayoutRelation relation))_equalToWithRelation;
 
-#define equalTo(...)                 _equalToWithRelation(MASBoxValue((__VA_ARGS__)), NSLayoutRelationEqual)
-
-#define greaterThanOrEqualTo(...)    _equalToWithRelation(MASBoxValue((__VA_ARGS__)), NSLayoutRelationGreaterThanOrEqual)
-
-#define lessThanOrEqualTo(...)       _equalToWithRelation(MASBoxValue((__VA_ARGS__)), NSLayoutRelationLessThanOrEqual)
-
 /**
  *	optional semantic property which has no effect but improves the readability of constraint
  */
@@ -123,6 +120,9 @@
 // NSLayoutConstraint constant Setters
 // for use outside of mas_updateConstraints/mas_makeConstraints blocks
 
+// TODO: describe
+- (void)setValueOffset:(id)offset;
+
 /**
  *	Modifies the NSLayoutConstraint constant,
  *  only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
@@ -130,26 +130,6 @@
  */
 - (void)setInsets:(MASEdgeInsets)insets;
 
-/**
- *	Modifies the NSLayoutConstraint constant,
- *  only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
- *  NSLayoutAttributeWidth, NSLayoutAttributeHeight
- */
-- (void)setSizeOffset:(CGSize)sizeOffset;
-
-/**
- *	Modifies the NSLayoutConstraint constant,
- *  only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
- *  NSLayoutAttributeCenterX, NSLayoutAttributeCenterY
- */
-- (void)setCenterOffset:(CGPoint)centerOffset;
-
-/**
- *	Modifies the NSLayoutConstraint constant
- */
-- (void)setOffset:(CGFloat)offset;
-
-
 // NSLayoutConstraint Installation support
 
 #if TARGET_OS_MAC && !TARGET_OS_IPHONE
@@ -189,4 +169,39 @@
  */
 - (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(MASConstraint *)replacementConstraint;
 
-@end
+@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
+

+ 90 - 22
Masonry/MASConstraint.m

@@ -21,49 +21,117 @@
 	return [super init];
 }
 
-#pragma mark - Dummies
+#pragma mark - NSLayoutRelation proxies
 
-- (MASConstraint * (^)(id))equalTo { return nil; }
+- (MASConstraint * (^)(id))equalTo {
+    return ^id(id attribute) {
+        return self._equalToWithRelation(attribute, NSLayoutRelationEqual);
+    };
+}
 
-- (MASConstraint * (^)(id))greaterThanOrEqualTo { return nil; }
+- (MASConstraint * (^)(id))greaterThanOrEqualTo {
+    return ^id(id attribute) {
+        return self._equalToWithRelation(attribute, NSLayoutRelationGreaterThanOrEqual);
+    };
+}
 
-- (MASConstraint * (^)(id))lessThanOrEqualTo { return nil; }
+- (MASConstraint * (^)(id))lessThanOrEqualTo {
+    return ^id(id attribute) {
+        return self._equalToWithRelation(attribute, NSLayoutRelationLessThanOrEqual);
+    };
+}
 
-#pragma mark - Abstract
+#pragma mark - MASLayoutPriority proxies
 
-- (MASConstraint * (^)(MASEdgeInsets insets))insets { methodNotImplemented(); }
+- (MASConstraint * (^)())priorityLow {
+    return ^id{
+        self.priority(MASLayoutPriorityDefaultLow);
+        return self;
+    };
+}
 
-- (MASConstraint * (^)(CGSize offset))sizeOffset { methodNotImplemented(); }
+- (MASConstraint * (^)())priorityMedium {
+    return ^id{
+        self.priority(MASLayoutPriorityDefaultMedium);
+        return self;
+    };
+}
 
-- (MASConstraint * (^)(CGPoint offset))centerOffset { methodNotImplemented(); }
+- (MASConstraint * (^)())priorityHigh {
+    return ^id{
+        self.priority(MASLayoutPriorityDefaultHigh);
+        return self;
+    };
+}
 
-- (MASConstraint * (^)(CGFloat offset))offset { methodNotImplemented(); }
+#pragma mark - NSLayoutConstraint constant proxies
 
-- (MASConstraint * (^)(CGFloat multiplier))multipliedBy { methodNotImplemented(); }
+- (MASConstraint * (^)(CGSize))sizeOffset {
+    return ^id(CGSize offset) {
+        self.valueOffset = MASBoxValue(offset);
+        return self;
+    };
+}
 
-- (MASConstraint * (^)(CGFloat divider))dividedBy { methodNotImplemented(); }
+- (MASConstraint * (^)(CGPoint))centerOffset {
+    return ^id(CGPoint offset) {
+        self.valueOffset = MASBoxValue(offset);
+        return self;
+    };
+}
 
-- (MASConstraint * (^)(MASLayoutPriority priority))priority { methodNotImplemented(); }
+- (MASConstraint * (^)(CGFloat))offset {
+    return ^id(CGFloat offset){
+        self.valueOffset = MASBoxValue(offset);
+        return self;
+    };
+}
 
-- (MASConstraint * (^)())priorityLow { methodNotImplemented(); }
+- (MASConstraint * (^)(id))_valueOffset {
+    return ^id(id offset) {
+        self.valueOffset = offset;
+        return self;
+    };
+}
 
-- (MASConstraint * (^)())priorityMedium { methodNotImplemented(); }
+- (MASConstraint * (^)(MASEdgeInsets))insets {
+    return ^id(MASEdgeInsets insets){
+        self.insets = insets;
+        return self;
+    };
+}
 
-- (MASConstraint * (^)())priorityHigh { methodNotImplemented(); }
+#pragma mark - Semantic properties
 
-- (MASConstraint * (^)(id, NSLayoutRelation))_equalToWithRelation { methodNotImplemented(); }
+- (MASConstraint *)with {
+    return self;
+}
 
-- (MASConstraint *)with { methodNotImplemented(); }
+#pragma mark - Autocompletion dummies
 
-- (MASConstraint * (^)(id key))key { methodNotImplemented(); }
+- (MASConstraint * (^)(id attr))mas_equalTo { return nil; }
 
-- (void)setInsets:(MASEdgeInsets)insets { methodNotImplemented(); }
+- (MASConstraint * (^)(id attr))mas_greaterThanOrEqualTo { return nil; }
+
+- (MASConstraint * (^)(id attr))mas_lessThanOrEqualTo { return nil; }
+
+- (MASConstraint * (^)(id offset))mas_offset { return nil; }
+
+#pragma mark - Abstract
+
+- (MASConstraint * (^)(CGFloat multiplier))multipliedBy { methodNotImplemented(); }
 
-- (void)setSizeOffset:(CGSize)sizeOffset { methodNotImplemented(); }
+- (MASConstraint * (^)(CGFloat divider))dividedBy { methodNotImplemented(); }
+
+- (MASConstraint * (^)(MASLayoutPriority priority))priority { methodNotImplemented(); }
+
+- (MASConstraint * (^)(id, NSLayoutRelation))_equalToWithRelation { methodNotImplemented(); }
+
+- (MASConstraint * (^)(id key))key { methodNotImplemented(); }
 
-- (void)setCenterOffset:(CGPoint)centerOffset { methodNotImplemented(); }
+- (void)setValueOffset:(id)offset { methodNotImplemented(); }
 
-- (void)setOffset:(CGFloat)offset { methodNotImplemented(); }
+- (void)setInsets:(MASEdgeInsets)insets { methodNotImplemented(); }
 
 #if TARGET_OS_MAC && !TARGET_OS_IPHONE
 

+ 59 - 110
Masonry/MASViewConstraint.m

@@ -67,6 +67,58 @@
 #endif
 }
 
+- (void)setLayoutConstantWithValue:(NSValue *)value {
+    if ([value isKindOfClass:NSNumber.class]) {
+        self.offset = [(NSNumber *)value doubleValue];
+    } else if (strcmp(value.objCType, @encode(CGPoint)) == 0) {
+        CGPoint point;
+        [value getValue:&point];
+        self.centerOffset = point;
+    } else if (strcmp(value.objCType, @encode(CGSize)) == 0) {
+        CGSize size;
+        [value getValue:&size];
+        self.sizeOffset = size;
+    } else if (strcmp(value.objCType, @encode(MASEdgeInsets)) == 0) {
+        MASEdgeInsets insets;
+        [value getValue:&insets];
+        self.insets = insets;
+    } else {
+        NSAssert(NO, @"attempting to set layout constant with unsupported value: %@", value);
+    }
+}
+
+- (void)setOffset:(CGFloat)offset {
+    self.layoutConstant = offset;
+}
+
+- (void)setSizeOffset:(CGSize)sizeOffset {
+    NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute;
+    switch (layoutAttribute) {
+        case NSLayoutAttributeWidth:
+            self.layoutConstant = sizeOffset.width;
+            break;
+        case NSLayoutAttributeHeight:
+            self.layoutConstant = sizeOffset.height;
+            break;
+        default:
+            break;
+    }
+}
+
+- (void)setCenterOffset:(CGPoint)centerOffset {
+    NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute;
+    switch (layoutAttribute) {
+        case NSLayoutAttributeCenterX:
+            self.layoutConstant = centerOffset.x;
+            break;
+        case NSLayoutAttributeCenterY:
+            self.layoutConstant = centerOffset.y;
+            break;
+        default:
+            break;
+    }
+}
+
 - (void)setLayoutRelation:(NSLayoutRelation)layoutRelation {
     _layoutRelation = layoutRelation;
     self.hasLayoutRelation = YES;
@@ -77,26 +129,8 @@
 }
 
 - (void)setSecondViewAttribute:(id)secondViewAttribute {
-    if ([secondViewAttribute isKindOfClass:NSNumber.class]) {
-        self.layoutConstant = [secondViewAttribute doubleValue];
-    } else if ([secondViewAttribute isKindOfClass:NSValue.class]) {
-        NSValue *value = (NSValue *)secondViewAttribute;
-        if (strcmp(value.objCType, @encode(CGPoint)) == 0) {
-            CGPoint point;
-            [value getValue:&point];
-            self.centerOffset = point;
-        } else if (strcmp(value.objCType, @encode(CGSize)) == 0) {
-            CGSize size;
-            [value getValue:&size];
-            self.sizeOffset = size;
-        } else if (strcmp(value.objCType, @encode(MASEdgeInsets)) == 0) {
-            MASEdgeInsets insets;
-            [value getValue:&insets];
-            self.insets = insets;
-        } else {
-            // TODO: avoid duplication
-            NSAssert(NO, @"attempting to add unsupported attribute: %@", secondViewAttribute);
-        }
+    if ([secondViewAttribute isKindOfClass:NSValue.class]) {
+        [self setLayoutConstantWithValue:secondViewAttribute];
     } else if ([secondViewAttribute isKindOfClass:MAS_VIEW.class]) {
         _secondViewAttribute = [[MASViewAttribute alloc] initWithView:secondViewAttribute layoutAttribute:self.firstViewAttribute.layoutAttribute];
     } else if ([secondViewAttribute isKindOfClass:MASViewAttribute.class]) {
@@ -106,36 +140,6 @@
     }
 }
 
-#pragma mark - NSLayoutConstraint constant proxies
-
-- (MASConstraint * (^)(MASEdgeInsets))insets {
-    return ^id(MASEdgeInsets insets){
-        self.insets = insets;
-        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;
-    };
-}
-
-- (MASConstraint * (^)(CGFloat))offset {
-    return ^id(CGFloat offset){
-        self.offset = offset;
-        return self;
-    };
-}
-
 #pragma mark - NSLayoutConstraint multiplier proxies
 
 - (MASConstraint * (^)(CGFloat))multipliedBy {
@@ -159,7 +163,7 @@
     };
 }
 
-#pragma mark - MASLayoutPriority proxies
+#pragma mark - MASLayoutPriority proxy
 
 - (MASConstraint * (^)(MASLayoutPriority))priority {
     return ^id(MASLayoutPriority priority) {
@@ -171,27 +175,6 @@
     };
 }
 
-- (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 proxy
 
 - (MASConstraint * (^)(id, NSLayoutRelation))_equalToWithRelation {
@@ -217,12 +200,6 @@
     };
 }
 
-#pragma mark - Semantic properties
-
-- (MASConstraint *)with {
-    return self;
-}
-
 #pragma mark - Animator proxy
 
 #if TARGET_OS_MAC && !TARGET_OS_IPHONE
@@ -245,6 +222,10 @@
 
 #pragma mark - NSLayoutConstraint constant setters
 
+- (void)setValueOffset:(id)offset {
+    [self setLayoutConstantWithValue:offset];
+}
+
 - (void)setInsets:(MASEdgeInsets)insets {
     NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute;
     switch (layoutAttribute) {
@@ -265,38 +246,6 @@
     }
 }
 
-- (void)setOffset:(CGFloat)offset {
-    self.layoutConstant = offset;
-}
-
-- (void)setSizeOffset:(CGSize)sizeOffset {
-    NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute;
-    switch (layoutAttribute) {
-        case NSLayoutAttributeWidth:
-            self.layoutConstant = sizeOffset.width;
-            break;
-        case NSLayoutAttributeHeight:
-            self.layoutConstant = sizeOffset.height;
-            break;
-        default:
-            break;
-    }
-}
-
-- (void)setCenterOffset:(CGPoint)centerOffset {
-    NSLayoutAttribute layoutAttribute = self.firstViewAttribute.layoutAttribute;
-    switch (layoutAttribute) {
-        case NSLayoutAttributeCenterX:
-            self.layoutConstant = centerOffset.x;
-            break;
-        case NSLayoutAttributeCenterY:
-            self.layoutConstant = centerOffset.y;
-            break;
-        default:
-            break;
-    }
-}
-
 #pragma mark - MASConstraint
 
 - (void)install {