From 509d64881899eec2b574b5a74ecc3a243e8c963f Mon Sep 17 00:00:00 2001 From: Robert Payne Date: Sun, 12 Apr 2015 00:04:36 +1200 Subject: [PATCH] Updated API & Copyrights --- Snap.xcodeproj/project.pbxproj | 6 + Source/Constraint.swift | 859 +---------------------------- Source/ConstraintAttributes.swift | 2 +- Source/ConstraintBuilder.swift | 884 ++++++++++++++++++++++++++++++ Source/ConstraintItem.swift | 2 +- Source/ConstraintMaker.swift | 56 +- Source/ConstraintRelation.swift | 2 +- Source/Debugging.swift | 2 +- Source/EdgeInsets.swift | 2 +- Source/LayoutConstraint.swift | 4 +- Source/Snap.swift | 2 +- Source/View+Snap.swift | 2 +- 12 files changed, 930 insertions(+), 893 deletions(-) create mode 100644 Source/ConstraintBuilder.swift diff --git a/Snap.xcodeproj/project.pbxproj b/Snap.xcodeproj/project.pbxproj index 8b32da4..f6cb842 100644 --- a/Snap.xcodeproj/project.pbxproj +++ b/Snap.xcodeproj/project.pbxproj @@ -35,6 +35,8 @@ EEFCF32D1AD9250C00A425FA /* Debugging.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEFCF32B1AD910B900A425FA /* Debugging.swift */; }; EEFCF32F1AD926AE00A425FA /* Snap.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEFCF32E1AD926AE00A425FA /* Snap.swift */; }; EEFCF3301AD92C2200A425FA /* Snap.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEFCF32E1AD926AE00A425FA /* Snap.swift */; }; + EEFCF3321AD9432400A425FA /* ConstraintBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEFCF3311AD9432400A425FA /* ConstraintBuilder.swift */; }; + EEFCF3331AD9432400A425FA /* ConstraintBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEFCF3311AD9432400A425FA /* ConstraintBuilder.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -68,6 +70,7 @@ EECDB3841AC0C9D4006BBC11 /* Snap OSX Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Snap OSX Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; EEFCF32B1AD910B900A425FA /* Debugging.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Debugging.swift; sourceTree = ""; }; EEFCF32E1AD926AE00A425FA /* Snap.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Snap.swift; sourceTree = ""; }; + EEFCF3311AD9432400A425FA /* ConstraintBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConstraintBuilder.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -141,6 +144,7 @@ EECDB3661AC0C95C006BBC11 /* Snap.h */, EEFCF32E1AD926AE00A425FA /* Snap.swift */, EECDB35E1AC0C95C006BBC11 /* Constraint.swift */, + EEFCF3311AD9432400A425FA /* ConstraintBuilder.swift */, EECDB35F1AC0C95C006BBC11 /* ConstraintAttributes.swift */, EECDB3601AC0C95C006BBC11 /* ConstraintItem.swift */, EECDB3611AC0C95C006BBC11 /* ConstraintMaker.swift */, @@ -350,6 +354,7 @@ EEFCF32F1AD926AE00A425FA /* Snap.swift in Sources */, EEFCF32C1AD910B900A425FA /* Debugging.swift in Sources */, EECDB3701AC0C9A6006BBC11 /* ConstraintRelation.swift in Sources */, + EEFCF3321AD9432400A425FA /* ConstraintBuilder.swift in Sources */, EECDB3731AC0C9A6006BBC11 /* View+Snap.swift in Sources */, EECDB3711AC0C9A6006BBC11 /* EdgeInsets.swift in Sources */, EECDB36F1AC0C9A6006BBC11 /* ConstraintMaker.swift in Sources */, @@ -375,6 +380,7 @@ EEFCF3301AD92C2200A425FA /* Snap.swift in Sources */, EEFCF32D1AD9250C00A425FA /* Debugging.swift in Sources */, EECDB3991AC0CBFF006BBC11 /* ConstraintRelation.swift in Sources */, + EEFCF3331AD9432400A425FA /* ConstraintBuilder.swift in Sources */, EECDB39C1AC0CBFF006BBC11 /* View+Snap.swift in Sources */, EECDB39A1AC0CBFF006BBC11 /* EdgeInsets.swift in Sources */, EECDB3981AC0CBFF006BBC11 /* ConstraintMaker.swift in Sources */, diff --git a/Source/Constraint.swift b/Source/Constraint.swift index a0d0477..6da8ca8 100644 --- a/Source/Constraint.swift +++ b/Source/Constraint.swift @@ -1,7 +1,7 @@ // // Snap // -// Copyright (c) 2011-2014 Masonry Team - https://github.com/Masonry +// Copyright (c) 2011-2015 Masonry Team - https://github.com/Masonry // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -28,8 +28,8 @@ import AppKit #endif /** -* Constraint is a protocol that exposes the public methods on a Constraint -*/ + * Constraint is a protocol that exposes the public methods on a Constraint + */ public protocol Constraint: class { func install() -> [LayoutConstraint] @@ -38,856 +38,3 @@ public protocol Constraint: class { func deactivate() -> Void } - -public protocol ConstraintFinalizable: class { - - var constraint: Constraint { get } - -} - -/** - * ConstraintPriortizable is a protocol that allows a constraint to be prioritized - */ -public protocol ConstraintPriortizable: ConstraintFinalizable { - - func priority(priority: Float) -> ConstraintFinalizable - func priority(priority: Double) -> ConstraintFinalizable - func priority(priority: CGFloat) -> ConstraintFinalizable - func priority(priority: UInt) -> ConstraintFinalizable - func priority(priority: Int) -> ConstraintFinalizable - func priorityRequired() -> ConstraintFinalizable - func priorityHigh() -> ConstraintFinalizable - func priorityMedium() -> ConstraintFinalizable - func priorityLow() -> ConstraintFinalizable -} - -/** - * ConstraintMultipliable is a protocol that allows a constraint to be multiplied - */ -public protocol ConstraintMultipliable: ConstraintPriortizable { - - func multipliedBy(amount: Float) -> ConstraintPriortizable - func multipliedBy(amount: Double) -> ConstraintPriortizable - func multipliedBy(amount: CGFloat) -> ConstraintPriortizable - func multipliedBy(amount: Int) -> ConstraintPriortizable - func multipliedBy(amount: UInt) -> ConstraintPriortizable - - func dividedBy(amount: Float) -> ConstraintPriortizable - func dividedBy(amount: Double) -> ConstraintPriortizable - func dividedBy(amount: CGFloat) -> ConstraintPriortizable - func dividedBy(amount: Int) -> ConstraintPriortizable - func dividedBy(amount: UInt) -> ConstraintPriortizable -} - -/** - * ConstraintOffsetable is a protocol that allows a constraint to be offset - */ -public protocol ConstraintOffsetable: ConstraintMultipliable { - - func offset(amount: Float) -> ConstraintMultipliable - func offset(amount: Double) -> ConstraintMultipliable - func offset(amount: CGFloat) -> ConstraintMultipliable - func offset(amount: Int) -> ConstraintMultipliable - func offset(amount: UInt) -> ConstraintMultipliable - func offset(amount: CGPoint) -> ConstraintMultipliable - func offset(amount: CGSize) -> ConstraintMultipliable - func offset(amount: EdgeInsets) -> ConstraintMultipliable - - func insets(amount: EdgeInsets) -> ConstraintMultipliable -} - - - -/** - * ConstraintRelatable is a protocol that allows a constraint to be set related to another item/constant by equalTo, lessThanOrEqualTo or greaterThanOrEqualTo - */ -public protocol ConstraintRelatable: class { - - func equalTo(other: ConstraintItem) -> ConstraintOffsetable - func equalTo(other: View) -> ConstraintOffsetable - #if os(iOS) - func equalTo(other: UILayoutSupport) -> ConstraintOffsetable - #endif - func equalTo(other: Float) -> ConstraintMultipliable - func equalTo(other: Double) -> ConstraintMultipliable - func equalTo(other: CGFloat) -> ConstraintMultipliable - func equalTo(other: Int) -> ConstraintMultipliable - func equalTo(other: UInt) -> ConstraintMultipliable - func equalTo(other: CGSize) -> ConstraintMultipliable - func equalTo(other: CGPoint) -> ConstraintMultipliable - func equalTo(other: EdgeInsets) -> ConstraintMultipliable - - func lessThanOrEqualTo(other: ConstraintItem) -> ConstraintOffsetable - func lessThanOrEqualTo(other: View) -> ConstraintOffsetable - #if os(iOS) - func lessThanOrEqualTo(other: UILayoutSupport) -> ConstraintOffsetable - #endif - func lessThanOrEqualTo(other: Float) -> ConstraintMultipliable - func lessThanOrEqualTo(other: Double) -> ConstraintMultipliable - func lessThanOrEqualTo(other: CGFloat) -> ConstraintMultipliable - func lessThanOrEqualTo(other: Int) -> ConstraintMultipliable - func lessThanOrEqualTo(other: UInt) -> ConstraintMultipliable - func lessThanOrEqualTo(other: CGSize) -> ConstraintMultipliable - func lessThanOrEqualTo(other: CGPoint) -> ConstraintMultipliable - func lessThanOrEqualTo(other: EdgeInsets) -> ConstraintMultipliable - - func greaterThanOrEqualTo(other: ConstraintItem) -> ConstraintOffsetable - func greaterThanOrEqualTo(other: View) -> ConstraintOffsetable - #if os(iOS) - func greaterThanOrEqualTo(other: UILayoutSupport) -> ConstraintOffsetable - #endif - func greaterThanOrEqualTo(other: Float) -> ConstraintMultipliable - func greaterThanOrEqualTo(other: Double) -> ConstraintMultipliable - func greaterThanOrEqualTo(other: CGFloat) -> ConstraintMultipliable - func greaterThanOrEqualTo(other: Int) -> ConstraintMultipliable - func greaterThanOrEqualTo(other: UInt) -> ConstraintMultipliable - func greaterThanOrEqualTo(other: CGSize) -> ConstraintMultipliable - func greaterThanOrEqualTo(other: CGPoint) -> ConstraintMultipliable - func greaterThanOrEqualTo(other: EdgeInsets) -> ConstraintMultipliable - -} - -/** -* ConstraintExtendable is a protocol that allows a constraint to be extended -*/ -public protocol ConstraintExtendable: ConstraintRelatable { - - var left: ConstraintExtendable { get } - var top: ConstraintExtendable { get } - var bottom: ConstraintExtendable { get } - var leading: ConstraintExtendable { get } - var trailing: ConstraintExtendable { get } - var width: ConstraintExtendable { get } - var height: ConstraintExtendable { get } - var centerX: ConstraintExtendable { get } - var centerY: ConstraintExtendable { get } - var baseline: ConstraintExtendable { get } - - #if os(iOS) - var firstBaseline: ConstraintExtendable { get } - var leftMargin: ConstraintExtendable { get } - var rightMargin: ConstraintExtendable { get } - var topMargin: ConstraintExtendable { get } - var bottomMargin: ConstraintExtendable { get } - var leadingMargin: ConstraintExtendable { get } - var trailingMargin: ConstraintExtendable { get } - var centerXWithinMargins: ConstraintExtendable { get } - var centerYWithinMargins: ConstraintExtendable { get } - #endif -} - -/** - * MutableConstraint is a single item that defines all the properties for a single ConstraintMaker chain - */ -final internal class MutableConstraint: Constraint, ConstraintExtendable, ConstraintOffsetable, ConstraintFinalizable { - - var left: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Left) } - var top: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Top) } - var right: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Right) } - var bottom: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Bottom) } - var leading: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Leading) } - var trailing: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Trailing) } - var width: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Width) } - var height: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Height) } - var centerX: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.CenterX) } - var centerY: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.CenterY) } - var baseline: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Baseline) } - - #if os(iOS) - var firstBaseline: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.FirstBaseline) } - var leftMargin: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.LeftMargin) } - var rightMargin: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.RightMargin) } - var topMargin: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.TopMargin) } - var bottomMargin: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.BottomMargin) } - var leadingMargin: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.LeadingMargin) } - var trailingMargin: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.TrailingMargin) } - var centerXWithinMargins: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.CenterXWithinMargins) } - var centerYWithinMargins: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.CenterYWithinMargins) } - #endif - - // MARK: initializer - - init(fromItem: ConstraintItem) { - self.fromItem = fromItem - self.toItem = ConstraintItem(object: nil, attributes: ConstraintAttributes.None) - } - - // MARK: equalTo - - func equalTo(other: ConstraintItem) -> ConstraintOffsetable { - return self.constrainTo(other, relation: .Equal) - } - func equalTo(other: View) -> ConstraintOffsetable { - return self.constrainTo(other, relation: .Equal) - } - #if os(iOS) - func equalTo(other: UILayoutSupport) -> ConstraintOffsetable { - return self.constrainTo(other, relation: .Equal) - } - #endif - func equalTo(other: Float) -> ConstraintMultipliable { - return self.constrainTo(other, relation: .Equal) - } - func equalTo(other: Double) -> ConstraintMultipliable { - return self.constrainTo(Float(other), relation: .Equal) - } - func equalTo(other: CGFloat) -> ConstraintMultipliable { - return self.constrainTo(Float(other), relation: .Equal) - } - func equalTo(other: Int) -> ConstraintMultipliable { - return self.constrainTo(Float(other), relation: .Equal) - } - func equalTo(other: UInt) -> ConstraintMultipliable { - return self.constrainTo(Float(other), relation: .Equal) - } - func equalTo(other: CGSize) -> ConstraintMultipliable { - return self.constrainTo(other, relation: .Equal) - } - func equalTo(other: CGPoint) -> ConstraintMultipliable { - return self.constrainTo(other, relation: .Equal) - } - func equalTo(other: EdgeInsets) -> ConstraintMultipliable { - return self.constrainTo(other, relation: .Equal) - } - - // MARK: lessThanOrEqualTo - - func lessThanOrEqualTo(other: ConstraintItem) -> ConstraintOffsetable { - return self.constrainTo(other, relation: .LessThanOrEqualTo) - } - func lessThanOrEqualTo(other: View) -> ConstraintOffsetable { - return self.constrainTo(other, relation: .LessThanOrEqualTo) - } - #if os(iOS) - func lessThanOrEqualTo(other: UILayoutSupport) -> ConstraintOffsetable { - return self.constrainTo(other, relation: .LessThanOrEqualTo) - } - #endif - func lessThanOrEqualTo(other: Float) -> ConstraintMultipliable { - return self.constrainTo(other, relation: .LessThanOrEqualTo) - } - func lessThanOrEqualTo(other: Double) -> ConstraintMultipliable { - return self.constrainTo(Float(other), relation: .LessThanOrEqualTo) - } - func lessThanOrEqualTo(other: CGFloat) -> ConstraintMultipliable { - return self.constrainTo(Float(other), relation: .LessThanOrEqualTo) - } - func lessThanOrEqualTo(other: Int) -> ConstraintMultipliable { - return self.constrainTo(Float(other), relation: .LessThanOrEqualTo) - } - func lessThanOrEqualTo(other: UInt) -> ConstraintMultipliable { - return self.constrainTo(Float(other), relation: .LessThanOrEqualTo) - } - func lessThanOrEqualTo(other: CGSize) -> ConstraintMultipliable { - return self.constrainTo(other, relation: .LessThanOrEqualTo) - } - func lessThanOrEqualTo(other: CGPoint) -> ConstraintMultipliable { - return self.constrainTo(other, relation: .LessThanOrEqualTo) - } - func lessThanOrEqualTo(other: EdgeInsets) -> ConstraintMultipliable { - return self.constrainTo(other, relation: .LessThanOrEqualTo) - } - - // MARK: greaterThanOrEqualTo - - func greaterThanOrEqualTo(other: ConstraintItem) -> ConstraintOffsetable { - return self.constrainTo(other, relation: .GreaterThanOrEqualTo) - } - func greaterThanOrEqualTo(other: View) -> ConstraintOffsetable { - return self.constrainTo(other, relation: .GreaterThanOrEqualTo) - } - #if os(iOS) - func greaterThanOrEqualTo(other: UILayoutSupport) -> ConstraintOffsetable { - return self.constrainTo(other, relation: .GreaterThanOrEqualTo) - } - #endif - func greaterThanOrEqualTo(other: Float) -> ConstraintMultipliable { - return self.constrainTo(other, relation: .GreaterThanOrEqualTo) - } - func greaterThanOrEqualTo(other: Double) -> ConstraintMultipliable { - return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo) - } - func greaterThanOrEqualTo(other: CGFloat) -> ConstraintMultipliable { - return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo) - } - func greaterThanOrEqualTo(other: Int) -> ConstraintMultipliable { - return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo) - } - func greaterThanOrEqualTo(other: UInt) -> ConstraintMultipliable { - return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo) - } - func greaterThanOrEqualTo(other: CGSize) -> ConstraintMultipliable { - return self.constrainTo(other, relation: .GreaterThanOrEqualTo) - } - func greaterThanOrEqualTo(other: CGPoint) -> ConstraintMultipliable { - return self.constrainTo(other, relation: .GreaterThanOrEqualTo) - } - func greaterThanOrEqualTo(other: EdgeInsets) -> ConstraintMultipliable { - return self.constrainTo(other, relation: .GreaterThanOrEqualTo) - } - - // MARK: multiplier - - func multipliedBy(amount: Float) -> ConstraintPriortizable { - self.multiplier = amount - return self - } - func multipliedBy(amount: Double) -> ConstraintPriortizable { - return self.multipliedBy(Float(amount)) - } - func multipliedBy(amount: CGFloat) -> ConstraintPriortizable { - return self.multipliedBy(Float(amount)) - } - func multipliedBy(amount: Int) -> ConstraintPriortizable { - return self.multipliedBy(Float(amount)) - } - func multipliedBy(amount: UInt) -> ConstraintPriortizable { - return self.multipliedBy(Float(amount)) - } - - func dividedBy(amount: Float) -> ConstraintPriortizable { - self.multiplier = 1.0 / amount; - return self - } - func dividedBy(amount: Double) -> ConstraintPriortizable { - return self.dividedBy(Float(amount)) - } - func dividedBy(amount: CGFloat) -> ConstraintPriortizable { - return self.dividedBy(Float(amount)) - } - func dividedBy(amount: Int) -> ConstraintPriortizable { - return self.dividedBy(Float(amount)) - } - func dividedBy(amount: UInt) -> ConstraintPriortizable { - return self.dividedBy(Float(amount)) - } - - // MARK: priority - - func priority(priority: Float) -> ConstraintFinalizable { - self.priority = priority - return self - } - func priority(priority: Double) -> ConstraintFinalizable { - return self.priority(Float(priority)) - } - func priority(priority: CGFloat) -> ConstraintFinalizable { - return self.priority(Float(priority)) - } - func priority(priority: UInt) -> ConstraintFinalizable { - return self.priority(Float(priority)) - } - func priority(priority: Int) -> ConstraintFinalizable { - return self.priority(Float(priority)) - } - func priorityRequired() -> ConstraintFinalizable { - return self.priority(1000.0) - } - func priorityHigh() -> ConstraintFinalizable { - return self.priority(750.0) - } - func priorityMedium() -> ConstraintFinalizable { - #if os(iOS) - return self.priority(500.0) - #else - return self.priority(501.0) - #endif - } - func priorityLow() -> ConstraintFinalizable { - return self.priority(250.0) - } - - // MARK: offset - - func offset(amount: Float) -> ConstraintMultipliable { - self.offset = amount - return self - } - func offset(amount: Double) -> ConstraintMultipliable { - return self.offset(Float(amount)) - } - func offset(amount: CGFloat) -> ConstraintMultipliable { - return self.offset(Float(amount)) - } - func offset(amount: Int) -> ConstraintMultipliable { - return self.offset(Float(amount)) - } - func offset(amount: UInt) -> ConstraintMultipliable { - return self.offset(Float(amount)) - } - func offset(amount: CGPoint) -> ConstraintMultipliable { - self.offset = amount - return self - } - func offset(amount: CGSize) -> ConstraintMultipliable { - self.offset = amount - return self - } - func offset(amount: EdgeInsets) -> ConstraintMultipliable { - self.offset = amount - return self - } - - // MARK: insets - - func insets(amount: EdgeInsets) -> ConstraintMultipliable { - self.offset = amount - return self - } - - // MARK: Constraint - - var constraint: Constraint { - return self - } - - // MARK: install / uninstall - - func install() -> [LayoutConstraint] { - return self.installOnView(updateExisting: false) - } - - func uninstall() { - self.uninstallFromView() - } - - func activate() { - if NSLayoutConstraint.respondsToSelector("activateConstraints:") && self.installInfo != nil { - let layoutConstraints = self.installInfo!.layoutConstraints.allObjects as! [LayoutConstraint] - if layoutConstraints.count > 0 { - NSLayoutConstraint.activateConstraints(layoutConstraints) - } - } else { - self.install() - } - } - - func deactivate() { - if NSLayoutConstraint.respondsToSelector("deactivateConstraints:") && self.installInfo != nil { - let layoutConstraints = self.installInfo!.layoutConstraints.allObjects as! [LayoutConstraint] - if layoutConstraints.count > 0 { - NSLayoutConstraint.deactivateConstraints(layoutConstraints) - } - } else { - self.uninstall() - } - } - - func installOnView(updateExisting: Bool = false) -> [LayoutConstraint] { - var installOnView: View? = nil - if self.toItem.view != nil { - installOnView = MutableConstraint.closestCommonSuperviewFromView(self.fromItem.view, toView: self.toItem.view) - if installOnView == nil { - NSException(name: "Cannot Install Constraint", reason: "No common superview between views", userInfo: nil).raise() - return [] - } - } else { - installOnView = self.fromItem.view?.superview - if installOnView == nil { - if self.fromItem.attributes == ConstraintAttributes.Width || self.fromItem.attributes == ConstraintAttributes.Height { - installOnView = self.fromItem.view - } - - if installOnView == nil { - NSException(name: "Cannot Install Constraint", reason: "Missing superview", userInfo: nil).raise() - return [] - } - } - } - - if let installedOnView = self.installInfo?.view { - if installedOnView != installOnView { - NSException(name: "Cannot Install Constraint", reason: "Already installed on different view.", userInfo: nil).raise() - return [] - } - return self.installInfo?.layoutConstraints.allObjects as? [LayoutConstraint] ?? [] - } - - var newLayoutConstraints = [LayoutConstraint]() - let layoutFromAttributes = self.fromItem.attributes.layoutAttributes - let layoutToAttributes = self.toItem.attributes.layoutAttributes - - // get layout from - let layoutFrom: View? = self.fromItem.view - - // get layout relation - let layoutRelation: NSLayoutRelation = (self.relation != nil) ? self.relation!.layoutRelation : .Equal - - for layoutFromAttribute in layoutFromAttributes { - // get layout to attribute - let layoutToAttribute = (layoutToAttributes.count > 0) ? layoutToAttributes[0] : layoutFromAttribute - - // get layout constant - var layoutConstant: CGFloat = layoutToAttribute.snp_constantForValue(self.constant) - layoutConstant += layoutToAttribute.snp_offsetForValue(self.offset) - - // get layout to - var layoutTo: View? = self.toItem.view - if layoutTo == nil && layoutToAttribute != .Width && layoutToAttribute != .Height { - layoutTo = installOnView - } - - // create layout constraint - let layoutConstraint = LayoutConstraint( - item: layoutFrom!, - attribute: layoutFromAttribute, - relatedBy: layoutRelation, - toItem: layoutTo, - attribute: layoutToAttribute, - multiplier: CGFloat(self.multiplier), - constant: layoutConstant) - - // set priority - layoutConstraint.priority = self.priority - - // set constraint - layoutConstraint.snp_constraint = self - - newLayoutConstraints.append(layoutConstraint) - } - - // special logic for updating - if updateExisting { - // get existing constraints for this view - let existingLayoutConstraints = reverse(layoutFrom!.snp_installedLayoutConstraints) - - // array that will contain only new layout constraints to keep - var newLayoutConstraintsToKeep = [LayoutConstraint]() - - // begin looping - for layoutConstraint in newLayoutConstraints { - // layout constraint that should be updated - var updateLayoutConstraint: LayoutConstraint? = nil - - // loop through existing and check for match - for existingLayoutConstraint in existingLayoutConstraints { - if existingLayoutConstraint == layoutConstraint { - updateLayoutConstraint = existingLayoutConstraint - break - } - } - - // if we have existing one lets just update the constant - if updateLayoutConstraint != nil { - updateLayoutConstraint!.constant = layoutConstraint.constant - } - // otherwise add this layout constraint to new keep list - else { - newLayoutConstraintsToKeep.append(layoutConstraint) - } - } - - // set constraints to only new ones - newLayoutConstraints = newLayoutConstraintsToKeep - } - - // add constraints - installOnView!.addConstraints(newLayoutConstraints) - - // set install info - self.installInfo = ConstraintInstallInfo(view: installOnView, layoutConstraints: NSHashTable.weakObjectsHashTable()) - - // store which layout constraints are installed for this constraint - for layoutConstraint in newLayoutConstraints { - self.installInfo!.layoutConstraints.addObject(layoutConstraint) - } - - // store the layout constraints against the layout from view - layoutFrom!.snp_installedLayoutConstraints += newLayoutConstraints - - // return the new constraints - return newLayoutConstraints - } - - func uninstallFromView() { - if let installInfo = self.installInfo, - let installedLayoutConstraints = installInfo.layoutConstraints.allObjects as? [LayoutConstraint] { - - if installedLayoutConstraints.count > 0 { - - if let installedOnView = installInfo.view { - // remove the constraints from the UIView's storage - installedOnView.removeConstraints(installedLayoutConstraints) - } - - // remove the constraints from the from item view - if let fromView = self.fromItem.view { - fromView.snp_installedLayoutConstraints = fromView.snp_installedLayoutConstraints.filter { - return !contains(installedLayoutConstraints, $0) - } - } - } - - } - self.installInfo = nil - } - - // MARK: private - - private let fromItem: ConstraintItem - private var toItem: ConstraintItem - private var relation: ConstraintRelation? - private var constant: Any = Float(0.0) - private var multiplier: Float = 1.0 - private var priority: Float = 1000.0 - private var offset: Any = Float(0.0) - - private var installInfo: ConstraintInstallInfo? - - private func addConstraint(attributes: ConstraintAttributes) -> MutableConstraint { - if self.relation == nil { - self.fromItem.attributes += attributes - } - return self - } - - private func constrainTo(other: ConstraintItem, relation: ConstraintRelation) -> MutableConstraint { - if other.attributes != ConstraintAttributes.None { - let toLayoutAttributes = other.attributes.layoutAttributes - if toLayoutAttributes.count > 1 { - let fromLayoutAttributes = self.fromItem.attributes.layoutAttributes - if toLayoutAttributes != fromLayoutAttributes { - NSException(name: "Invalid Constraint", reason: "Cannot constrain to multiple non identical attributes", userInfo: nil).raise() - return self - } - other.attributes = ConstraintAttributes.None - } - } - self.toItem = other - self.relation = relation - return self - } - private func constrainTo(other: View, relation: ConstraintRelation) -> MutableConstraint { - return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation) - } - #if os(iOS) - private func constrainTo(other: UILayoutSupport, relation: ConstraintRelation) -> MutableConstraint { - return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation) - } - #endif - private func constrainTo(other: Float, relation: ConstraintRelation) -> MutableConstraint { - self.constant = other - return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation) - } - private func constrainTo(other: Double, relation: ConstraintRelation) -> MutableConstraint { - self.constant = other - return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation) - } - private func constrainTo(other: CGSize, relation: ConstraintRelation) -> MutableConstraint { - self.constant = other - return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation) - } - private func constrainTo(other: CGPoint, relation: ConstraintRelation) -> MutableConstraint { - self.constant = other - return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation) - } - private func constrainTo(other: EdgeInsets, relation: ConstraintRelation) -> MutableConstraint { - self.constant = other - return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation) - } - - private class func closestCommonSuperviewFromView(fromView: View?, toView: View?) -> View? { - var views = Set() - var fromView = fromView - var toView = toView - do { - if let view = toView { - if views.contains(view) { - return view - } - views.insert(view) - toView = view.superview - } - if let view = fromView { - if views.contains(view) { - return view - } - views.insert(view) - fromView = view.superview - } - } while (fromView != nil || toView != nil) - - return nil - } -} - -private extension NSLayoutAttribute { - - private func snp_offsetForValue(value: Any?) -> CGFloat { - // Float - if let float = value as? Float { - return CGFloat(float) - } - // Double - else if let double = value as? Double { - return CGFloat(double) - } - // UInt - else if let int = value as? Int { - return CGFloat(int) - } - // Int - else if let uint = value as? UInt { - return CGFloat(uint) - } - // CGFloat - else if let float = value as? CGFloat { - return float - } - // CGSize - else if let size = value as? CGSize { - if self == .Width { - return size.width - } else if self == .Height { - return size.height - } - } - // CGPoint - else if let point = value as? CGPoint { - #if os(iOS) - switch self { - case .Left, .CenterX, .LeftMargin, .CenterXWithinMargins: return point.x - case .Top, .CenterY, .TopMargin, .CenterYWithinMargins, .Baseline, .FirstBaseline: return point.y - case .Right, .RightMargin: return -point.x - case .Bottom, .BottomMargin: return -point.y - case .Leading, .LeadingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? point.x : -point.x - case .Trailing, .TrailingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? -point.x : point.x - case .Width, .Height, .NotAnAttribute: return CGFloat(0) - } - #else - switch self { - case .Left, .CenterX: return point.x - case .Top, .CenterY, .Baseline: return point.y - case .Right: return -point.x - case .Bottom: return -point.y - case .Leading: return (Config.interfaceLayoutDirection == .LeftToRight) ? point.x : -point.x - case .Trailing: return (Config.interfaceLayoutDirection == .LeftToRight) ? -point.x : point.x - case .Width, .Height, .NotAnAttribute: return CGFloat(0) - } - #endif - } - // EdgeInsets - else if let insets = value as? EdgeInsets { - #if os(iOS) - switch self { - case .Left, .CenterX, .LeftMargin, .CenterXWithinMargins: return insets.left - case .Top, .CenterY, .TopMargin, .CenterYWithinMargins, .Baseline, .FirstBaseline: return insets.top - case .Right, .RightMargin: return -insets.right - case .Bottom, .BottomMargin: return -insets.bottom - case .Leading, .LeadingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.left : -insets.right - case .Trailing, .TrailingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? -insets.right : insets.left - case .Width, .Height, .NotAnAttribute: return CGFloat(0) - } - #else - switch self { - case .Left, .CenterX: return insets.left - case .Top, .CenterY, .Baseline: return insets.top - case .Right: return -insets.right - case .Bottom: return -insets.bottom - case .Leading: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.left : -insets.right - case .Trailing: return (Config.interfaceLayoutDirection == .LeftToRight) ? -insets.right : insets.left - case .Width, .Height, .NotAnAttribute: return CGFloat(0) - } - #endif - } - - return CGFloat(0) - } - - private func snp_constantForValue(value: Any?) -> CGFloat { - // Float - if let float = value as? Float { - return CGFloat(float) - } - // Double - else if let double = value as? Double { - return CGFloat(double) - } - // UInt - else if let int = value as? Int { - return CGFloat(int) - } - // Int - else if let uint = value as? UInt { - return CGFloat(uint) - } - // CGFloat - else if let float = value as? CGFloat { - return float - } - // CGSize - else if let size = value as? CGSize { - if self == .Width { - return size.width - } else if self == .Height { - return size.height - } - } - // CGPoint - else if let point = value as? CGPoint { - #if os(iOS) - switch self { - case .Left, .CenterX, .LeftMargin, .CenterXWithinMargins: return point.x - case .Top, .CenterY, .TopMargin, .CenterYWithinMargins, .Baseline, .FirstBaseline: return point.y - case .Right, .RightMargin: return point.x - case .Bottom, .BottomMargin: return point.y - case .Leading, .LeadingMargin: return point.x - case .Trailing, .TrailingMargin: return point.x - case .Width, .Height, .NotAnAttribute: return CGFloat(0) - } - #else - switch self { - case .Left, .CenterX: return point.x - case .Top, .CenterY, .Baseline: return point.y - case .Right: return point.x - case .Bottom: return point.y - case .Leading: return point.x - case .Trailing: return point.x - case .Width, .Height, .NotAnAttribute: return CGFloat(0) - } - #endif - } - // EdgeInsets - else if let insets = value as? EdgeInsets { - #if os(iOS) - switch self { - case .Left, .CenterX, .LeftMargin, .CenterXWithinMargins: return insets.left - case .Top, .CenterY, .TopMargin, .CenterYWithinMargins, .Baseline, .FirstBaseline: return insets.top - case .Right, .RightMargin: return insets.right - case .Bottom, .BottomMargin: return insets.bottom - case .Leading, .LeadingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.left : insets.right - case .Trailing, .TrailingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.right : insets.left - case .Width, .Height, .NotAnAttribute: return CGFloat(0) - } - #else - switch self { - case .Left, .CenterX: return insets.left - case .Top, .CenterY, .Baseline: return insets.top - case .Right: return insets.right - case .Bottom: return insets.bottom - case .Leading: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.left : insets.right - case .Trailing: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.right : insets.left - case .Width, .Height, .NotAnAttribute: return CGFloat(0) - } - #endif - } - - return CGFloat(0); - } -} - -private struct ConstraintInstallInfo { - - weak var view: View? = nil - let layoutConstraints: NSHashTable - -} - - -internal func ==(left: MutableConstraint, right: MutableConstraint) -> Bool { - return (left.fromItem == right.fromItem && - left.toItem == right.toItem && - left.relation == right.relation && - left.multiplier == right.multiplier && - left.priority == right.priority) -} \ No newline at end of file diff --git a/Source/ConstraintAttributes.swift b/Source/ConstraintAttributes.swift index bc37c62..90e076c 100644 --- a/Source/ConstraintAttributes.swift +++ b/Source/ConstraintAttributes.swift @@ -1,7 +1,7 @@ // // Snap // -// Copyright (c) 2011-2014 Masonry Team - https://github.com/Masonry +// Copyright (c) 2011-2015 Masonry Team - https://github.com/Masonry // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/Source/ConstraintBuilder.swift b/Source/ConstraintBuilder.swift new file mode 100644 index 0000000..e70bd52 --- /dev/null +++ b/Source/ConstraintBuilder.swift @@ -0,0 +1,884 @@ +// +// Snap +// +// Copyright (c) 2011-2015 Masonry Team - https://github.com/Masonry +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#if os(iOS) +import UIKit +#else +import AppKit +#endif + +/** + * ConstraintBuilderFinalizable is a protocol that allows getting a finalized constraint + */ +public protocol ConstraintBuilderFinalizable: class { + + var constraint: Constraint { get } + +} + +/** + * ConstraintBuilderPriortizable is a protocol that allows a constraint to be prioritized + */ +public protocol ConstraintBuilderPriortizable: ConstraintBuilderFinalizable { + + func priority(priority: Float) -> ConstraintBuilderFinalizable + func priority(priority: Double) -> ConstraintBuilderFinalizable + func priority(priority: CGFloat) -> ConstraintBuilderFinalizable + func priority(priority: UInt) -> ConstraintBuilderFinalizable + func priority(priority: Int) -> ConstraintBuilderFinalizable + func priorityRequired() -> ConstraintBuilderFinalizable + func priorityHigh() -> ConstraintBuilderFinalizable + func priorityMedium() -> ConstraintBuilderFinalizable + func priorityLow() -> ConstraintBuilderFinalizable +} + +/** + * ConstraintBuilderMultipliable is a protocol that allows a constraint to be multiplied + */ +public protocol ConstraintBuilderMultipliable: ConstraintBuilderPriortizable { + + func multipliedBy(amount: Float) -> ConstraintBuilderPriortizable + func multipliedBy(amount: Double) -> ConstraintBuilderPriortizable + func multipliedBy(amount: CGFloat) -> ConstraintBuilderPriortizable + func multipliedBy(amount: Int) -> ConstraintBuilderPriortizable + func multipliedBy(amount: UInt) -> ConstraintBuilderPriortizable + + func dividedBy(amount: Float) -> ConstraintBuilderPriortizable + func dividedBy(amount: Double) -> ConstraintBuilderPriortizable + func dividedBy(amount: CGFloat) -> ConstraintBuilderPriortizable + func dividedBy(amount: Int) -> ConstraintBuilderPriortizable + func dividedBy(amount: UInt) -> ConstraintBuilderPriortizable +} + +/** + * ConstraintBuilderOffsetable is a protocol that allows a constraint to be offset + */ +public protocol ConstraintBuilderOffsetable: ConstraintBuilderMultipliable { + + func offset(amount: Float) -> ConstraintBuilderMultipliable + func offset(amount: Double) -> ConstraintBuilderMultipliable + func offset(amount: CGFloat) -> ConstraintBuilderMultipliable + func offset(amount: Int) -> ConstraintBuilderMultipliable + func offset(amount: UInt) -> ConstraintBuilderMultipliable + func offset(amount: CGPoint) -> ConstraintBuilderMultipliable + func offset(amount: CGSize) -> ConstraintBuilderMultipliable + func offset(amount: EdgeInsets) -> ConstraintBuilderMultipliable + + func insets(amount: EdgeInsets) -> ConstraintBuilderMultipliable +} + + + +/** + * ConstraintBuilderRelatable is a protocol that allows a constraint to be set related to another item/constant by equalTo, lessThanOrEqualTo or greaterThanOrEqualTo + */ +public protocol ConstraintBuilderRelatable: class { + + func equalTo(other: ConstraintItem) -> ConstraintBuilderOffsetable + func equalTo(other: View) -> ConstraintBuilderOffsetable + #if os(iOS) + func equalTo(other: UILayoutSupport) -> ConstraintBuilderOffsetable + #endif + func equalTo(other: Float) -> ConstraintBuilderMultipliable + func equalTo(other: Double) -> ConstraintBuilderMultipliable + func equalTo(other: CGFloat) -> ConstraintBuilderMultipliable + func equalTo(other: Int) -> ConstraintBuilderMultipliable + func equalTo(other: UInt) -> ConstraintBuilderMultipliable + func equalTo(other: CGSize) -> ConstraintBuilderMultipliable + func equalTo(other: CGPoint) -> ConstraintBuilderMultipliable + func equalTo(other: EdgeInsets) -> ConstraintBuilderMultipliable + + func lessThanOrEqualTo(other: ConstraintItem) -> ConstraintBuilderOffsetable + func lessThanOrEqualTo(other: View) -> ConstraintBuilderOffsetable + #if os(iOS) + func lessThanOrEqualTo(other: UILayoutSupport) -> ConstraintBuilderOffsetable + #endif + func lessThanOrEqualTo(other: Float) -> ConstraintBuilderMultipliable + func lessThanOrEqualTo(other: Double) -> ConstraintBuilderMultipliable + func lessThanOrEqualTo(other: CGFloat) -> ConstraintBuilderMultipliable + func lessThanOrEqualTo(other: Int) -> ConstraintBuilderMultipliable + func lessThanOrEqualTo(other: UInt) -> ConstraintBuilderMultipliable + func lessThanOrEqualTo(other: CGSize) -> ConstraintBuilderMultipliable + func lessThanOrEqualTo(other: CGPoint) -> ConstraintBuilderMultipliable + func lessThanOrEqualTo(other: EdgeInsets) -> ConstraintBuilderMultipliable + + func greaterThanOrEqualTo(other: ConstraintItem) -> ConstraintBuilderOffsetable + func greaterThanOrEqualTo(other: View) -> ConstraintBuilderOffsetable + #if os(iOS) + func greaterThanOrEqualTo(other: UILayoutSupport) -> ConstraintBuilderOffsetable + #endif + func greaterThanOrEqualTo(other: Float) -> ConstraintBuilderMultipliable + func greaterThanOrEqualTo(other: Double) -> ConstraintBuilderMultipliable + func greaterThanOrEqualTo(other: CGFloat) -> ConstraintBuilderMultipliable + func greaterThanOrEqualTo(other: Int) -> ConstraintBuilderMultipliable + func greaterThanOrEqualTo(other: UInt) -> ConstraintBuilderMultipliable + func greaterThanOrEqualTo(other: CGSize) -> ConstraintBuilderMultipliable + func greaterThanOrEqualTo(other: CGPoint) -> ConstraintBuilderMultipliable + func greaterThanOrEqualTo(other: EdgeInsets) -> ConstraintBuilderMultipliable + +} + +/** + * ConstraintBuilderExtendable is a protocol that allows a constraint to be extended + */ +public protocol ConstraintBuilderExtendable: ConstraintBuilderRelatable { + + var left: ConstraintBuilderExtendable { get } + var top: ConstraintBuilderExtendable { get } + var bottom: ConstraintBuilderExtendable { get } + var leading: ConstraintBuilderExtendable { get } + var trailing: ConstraintBuilderExtendable { get } + var width: ConstraintBuilderExtendable { get } + var height: ConstraintBuilderExtendable { get } + var centerX: ConstraintBuilderExtendable { get } + var centerY: ConstraintBuilderExtendable { get } + var baseline: ConstraintBuilderExtendable { get } + + #if os(iOS) + var firstBaseline: ConstraintBuilderExtendable { get } + var leftMargin: ConstraintBuilderExtendable { get } + var rightMargin: ConstraintBuilderExtendable { get } + var topMargin: ConstraintBuilderExtendable { get } + var bottomMargin: ConstraintBuilderExtendable { get } + var leadingMargin: ConstraintBuilderExtendable { get } + var trailingMargin: ConstraintBuilderExtendable { get } + var centerXWithinMargins: ConstraintBuilderExtendable { get } + var centerYWithinMargins: ConstraintBuilderExtendable { get } + #endif +} + +/** + * ConstraintBuilder is a single item that defines all the properties for a single ConstraintMaker building chain + */ +final internal class ConstraintBuilder: Constraint, ConstraintBuilderExtendable, ConstraintBuilderOffsetable, ConstraintBuilderFinalizable { + + var left: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.Left) } + var top: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.Top) } + var right: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.Right) } + var bottom: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.Bottom) } + var leading: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.Leading) } + var trailing: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.Trailing) } + var width: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.Width) } + var height: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.Height) } + var centerX: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.CenterX) } + var centerY: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.CenterY) } + var baseline: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.Baseline) } + + #if os(iOS) + var firstBaseline: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.FirstBaseline) } + var leftMargin: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.LeftMargin) } + var rightMargin: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.RightMargin) } + var topMargin: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.TopMargin) } + var bottomMargin: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.BottomMargin) } + var leadingMargin: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.LeadingMargin) } + var trailingMargin: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.TrailingMargin) } + var centerXWithinMargins: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.CenterXWithinMargins) } + var centerYWithinMargins: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.CenterYWithinMargins) } + #endif + + // MARK: initializer + + init(fromItem: ConstraintItem) { + self.fromItem = fromItem + self.toItem = ConstraintItem(object: nil, attributes: ConstraintAttributes.None) + } + + // MARK: equalTo + + func equalTo(other: ConstraintItem) -> ConstraintBuilderOffsetable { + return self.constrainTo(other, relation: .Equal) + } + func equalTo(other: View) -> ConstraintBuilderOffsetable { + return self.constrainTo(other, relation: .Equal) + } + #if os(iOS) + func equalTo(other: UILayoutSupport) -> ConstraintBuilderOffsetable { + return self.constrainTo(other, relation: .Equal) + } + #endif + func equalTo(other: Float) -> ConstraintBuilderMultipliable { + return self.constrainTo(other, relation: .Equal) + } + func equalTo(other: Double) -> ConstraintBuilderMultipliable { + return self.constrainTo(Float(other), relation: .Equal) + } + func equalTo(other: CGFloat) -> ConstraintBuilderMultipliable { + return self.constrainTo(Float(other), relation: .Equal) + } + func equalTo(other: Int) -> ConstraintBuilderMultipliable { + return self.constrainTo(Float(other), relation: .Equal) + } + func equalTo(other: UInt) -> ConstraintBuilderMultipliable { + return self.constrainTo(Float(other), relation: .Equal) + } + func equalTo(other: CGSize) -> ConstraintBuilderMultipliable { + return self.constrainTo(other, relation: .Equal) + } + func equalTo(other: CGPoint) -> ConstraintBuilderMultipliable { + return self.constrainTo(other, relation: .Equal) + } + func equalTo(other: EdgeInsets) -> ConstraintBuilderMultipliable { + return self.constrainTo(other, relation: .Equal) + } + + // MARK: lessThanOrEqualTo + + func lessThanOrEqualTo(other: ConstraintItem) -> ConstraintBuilderOffsetable { + return self.constrainTo(other, relation: .LessThanOrEqualTo) + } + func lessThanOrEqualTo(other: View) -> ConstraintBuilderOffsetable { + return self.constrainTo(other, relation: .LessThanOrEqualTo) + } + #if os(iOS) + func lessThanOrEqualTo(other: UILayoutSupport) -> ConstraintBuilderOffsetable { + return self.constrainTo(other, relation: .LessThanOrEqualTo) + } + #endif + func lessThanOrEqualTo(other: Float) -> ConstraintBuilderMultipliable { + return self.constrainTo(other, relation: .LessThanOrEqualTo) + } + func lessThanOrEqualTo(other: Double) -> ConstraintBuilderMultipliable { + return self.constrainTo(Float(other), relation: .LessThanOrEqualTo) + } + func lessThanOrEqualTo(other: CGFloat) -> ConstraintBuilderMultipliable { + return self.constrainTo(Float(other), relation: .LessThanOrEqualTo) + } + func lessThanOrEqualTo(other: Int) -> ConstraintBuilderMultipliable { + return self.constrainTo(Float(other), relation: .LessThanOrEqualTo) + } + func lessThanOrEqualTo(other: UInt) -> ConstraintBuilderMultipliable { + return self.constrainTo(Float(other), relation: .LessThanOrEqualTo) + } + func lessThanOrEqualTo(other: CGSize) -> ConstraintBuilderMultipliable { + return self.constrainTo(other, relation: .LessThanOrEqualTo) + } + func lessThanOrEqualTo(other: CGPoint) -> ConstraintBuilderMultipliable { + return self.constrainTo(other, relation: .LessThanOrEqualTo) + } + func lessThanOrEqualTo(other: EdgeInsets) -> ConstraintBuilderMultipliable { + return self.constrainTo(other, relation: .LessThanOrEqualTo) + } + + // MARK: greaterThanOrEqualTo + + func greaterThanOrEqualTo(other: ConstraintItem) -> ConstraintBuilderOffsetable { + return self.constrainTo(other, relation: .GreaterThanOrEqualTo) + } + func greaterThanOrEqualTo(other: View) -> ConstraintBuilderOffsetable { + return self.constrainTo(other, relation: .GreaterThanOrEqualTo) + } + #if os(iOS) + func greaterThanOrEqualTo(other: UILayoutSupport) -> ConstraintBuilderOffsetable { + return self.constrainTo(other, relation: .GreaterThanOrEqualTo) + } + #endif + func greaterThanOrEqualTo(other: Float) -> ConstraintBuilderMultipliable { + return self.constrainTo(other, relation: .GreaterThanOrEqualTo) + } + func greaterThanOrEqualTo(other: Double) -> ConstraintBuilderMultipliable { + return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo) + } + func greaterThanOrEqualTo(other: CGFloat) -> ConstraintBuilderMultipliable { + return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo) + } + func greaterThanOrEqualTo(other: Int) -> ConstraintBuilderMultipliable { + return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo) + } + func greaterThanOrEqualTo(other: UInt) -> ConstraintBuilderMultipliable { + return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo) + } + func greaterThanOrEqualTo(other: CGSize) -> ConstraintBuilderMultipliable { + return self.constrainTo(other, relation: .GreaterThanOrEqualTo) + } + func greaterThanOrEqualTo(other: CGPoint) -> ConstraintBuilderMultipliable { + return self.constrainTo(other, relation: .GreaterThanOrEqualTo) + } + func greaterThanOrEqualTo(other: EdgeInsets) -> ConstraintBuilderMultipliable { + return self.constrainTo(other, relation: .GreaterThanOrEqualTo) + } + + // MARK: multiplier + + func multipliedBy(amount: Float) -> ConstraintBuilderPriortizable { + self.multiplier = amount + return self + } + func multipliedBy(amount: Double) -> ConstraintBuilderPriortizable { + return self.multipliedBy(Float(amount)) + } + func multipliedBy(amount: CGFloat) -> ConstraintBuilderPriortizable { + return self.multipliedBy(Float(amount)) + } + func multipliedBy(amount: Int) -> ConstraintBuilderPriortizable { + return self.multipliedBy(Float(amount)) + } + func multipliedBy(amount: UInt) -> ConstraintBuilderPriortizable { + return self.multipliedBy(Float(amount)) + } + + func dividedBy(amount: Float) -> ConstraintBuilderPriortizable { + self.multiplier = 1.0 / amount; + return self + } + func dividedBy(amount: Double) -> ConstraintBuilderPriortizable { + return self.dividedBy(Float(amount)) + } + func dividedBy(amount: CGFloat) -> ConstraintBuilderPriortizable { + return self.dividedBy(Float(amount)) + } + func dividedBy(amount: Int) -> ConstraintBuilderPriortizable { + return self.dividedBy(Float(amount)) + } + func dividedBy(amount: UInt) -> ConstraintBuilderPriortizable { + return self.dividedBy(Float(amount)) + } + + // MARK: priority + + func priority(priority: Float) -> ConstraintBuilderFinalizable { + self.priority = priority + return self + } + func priority(priority: Double) -> ConstraintBuilderFinalizable { + return self.priority(Float(priority)) + } + func priority(priority: CGFloat) -> ConstraintBuilderFinalizable { + return self.priority(Float(priority)) + } + func priority(priority: UInt) -> ConstraintBuilderFinalizable { + return self.priority(Float(priority)) + } + func priority(priority: Int) -> ConstraintBuilderFinalizable { + return self.priority(Float(priority)) + } + func priorityRequired() -> ConstraintBuilderFinalizable { + return self.priority(1000.0) + } + func priorityHigh() -> ConstraintBuilderFinalizable { + return self.priority(750.0) + } + func priorityMedium() -> ConstraintBuilderFinalizable { + #if os(iOS) + return self.priority(500.0) + #else + return self.priority(501.0) + #endif + } + func priorityLow() -> ConstraintBuilderFinalizable { + return self.priority(250.0) + } + + // MARK: offset + + func offset(amount: Float) -> ConstraintBuilderMultipliable { + self.offset = amount + return self + } + func offset(amount: Double) -> ConstraintBuilderMultipliable { + return self.offset(Float(amount)) + } + func offset(amount: CGFloat) -> ConstraintBuilderMultipliable { + return self.offset(Float(amount)) + } + func offset(amount: Int) -> ConstraintBuilderMultipliable { + return self.offset(Float(amount)) + } + func offset(amount: UInt) -> ConstraintBuilderMultipliable { + return self.offset(Float(amount)) + } + func offset(amount: CGPoint) -> ConstraintBuilderMultipliable { + self.offset = amount + return self + } + func offset(amount: CGSize) -> ConstraintBuilderMultipliable { + self.offset = amount + return self + } + func offset(amount: EdgeInsets) -> ConstraintBuilderMultipliable { + self.offset = amount + return self + } + + // MARK: insets + + func insets(amount: EdgeInsets) -> ConstraintBuilderMultipliable { + self.offset = amount + return self + } + + // MARK: Constraint + + var constraint: Constraint { + return self + } + + // MARK: install / uninstall + + func install() -> [LayoutConstraint] { + return self.installOnView(updateExisting: false) + } + + func uninstall() { + self.uninstallFromView() + } + + func activate() { + if NSLayoutConstraint.respondsToSelector("activateConstraints:") && self.installInfo != nil { + let layoutConstraints = self.installInfo!.layoutConstraints.allObjects as! [LayoutConstraint] + if layoutConstraints.count > 0 { + NSLayoutConstraint.activateConstraints(layoutConstraints) + } + } else { + self.install() + } + } + + func deactivate() { + if NSLayoutConstraint.respondsToSelector("deactivateConstraints:") && self.installInfo != nil { + let layoutConstraints = self.installInfo!.layoutConstraints.allObjects as! [LayoutConstraint] + if layoutConstraints.count > 0 { + NSLayoutConstraint.deactivateConstraints(layoutConstraints) + } + } else { + self.uninstall() + } + } + + func installOnView(updateExisting: Bool = false) -> [LayoutConstraint] { + var installOnView: View? = nil + if self.toItem.view != nil { + installOnView = ConstraintBuilder.closestCommonSuperviewFromView(self.fromItem.view, toView: self.toItem.view) + if installOnView == nil { + NSException(name: "Cannot Install Constraint", reason: "No common superview between views", userInfo: nil).raise() + return [] + } + } else { + installOnView = self.fromItem.view?.superview + if installOnView == nil { + if self.fromItem.attributes == ConstraintAttributes.Width || self.fromItem.attributes == ConstraintAttributes.Height { + installOnView = self.fromItem.view + } + + if installOnView == nil { + NSException(name: "Cannot Install Constraint", reason: "Missing superview", userInfo: nil).raise() + return [] + } + } + } + + if let installedOnView = self.installInfo?.view { + if installedOnView != installOnView { + NSException(name: "Cannot Install Constraint", reason: "Already installed on different view.", userInfo: nil).raise() + return [] + } + return self.installInfo?.layoutConstraints.allObjects as? [LayoutConstraint] ?? [] + } + + var newLayoutConstraints = [LayoutConstraint]() + let layoutFromAttributes = self.fromItem.attributes.layoutAttributes + let layoutToAttributes = self.toItem.attributes.layoutAttributes + + // get layout from + let layoutFrom: View? = self.fromItem.view + + // get layout relation + let layoutRelation: NSLayoutRelation = (self.relation != nil) ? self.relation!.layoutRelation : .Equal + + for layoutFromAttribute in layoutFromAttributes { + // get layout to attribute + let layoutToAttribute = (layoutToAttributes.count > 0) ? layoutToAttributes[0] : layoutFromAttribute + + // get layout constant + var layoutConstant: CGFloat = layoutToAttribute.snp_constantForValue(self.constant) + layoutConstant += layoutToAttribute.snp_offsetForValue(self.offset) + + // get layout to + var layoutTo: View? = self.toItem.view + if layoutTo == nil && layoutToAttribute != .Width && layoutToAttribute != .Height { + layoutTo = installOnView + } + + // create layout constraint + let layoutConstraint = LayoutConstraint( + item: layoutFrom!, + attribute: layoutFromAttribute, + relatedBy: layoutRelation, + toItem: layoutTo, + attribute: layoutToAttribute, + multiplier: CGFloat(self.multiplier), + constant: layoutConstant) + + // set priority + layoutConstraint.priority = self.priority + + // set constraint + layoutConstraint.snp_constraint = self + + newLayoutConstraints.append(layoutConstraint) + } + + // special logic for updating + if updateExisting { + // get existing constraints for this view + let existingLayoutConstraints = reverse(layoutFrom!.snp_installedLayoutConstraints) + + // array that will contain only new layout constraints to keep + var newLayoutConstraintsToKeep = [LayoutConstraint]() + + // begin looping + for layoutConstraint in newLayoutConstraints { + // layout constraint that should be updated + var updateLayoutConstraint: LayoutConstraint? = nil + + // loop through existing and check for match + for existingLayoutConstraint in existingLayoutConstraints { + if existingLayoutConstraint == layoutConstraint { + updateLayoutConstraint = existingLayoutConstraint + break + } + } + + // if we have existing one lets just update the constant + if updateLayoutConstraint != nil { + updateLayoutConstraint!.constant = layoutConstraint.constant + } + // otherwise add this layout constraint to new keep list + else { + newLayoutConstraintsToKeep.append(layoutConstraint) + } + } + + // set constraints to only new ones + newLayoutConstraints = newLayoutConstraintsToKeep + } + + // add constraints + installOnView!.addConstraints(newLayoutConstraints) + + // set install info + self.installInfo = ConstraintInstallInfo(view: installOnView, layoutConstraints: NSHashTable.weakObjectsHashTable()) + + // store which layout constraints are installed for this constraint + for layoutConstraint in newLayoutConstraints { + self.installInfo!.layoutConstraints.addObject(layoutConstraint) + } + + // store the layout constraints against the layout from view + layoutFrom!.snp_installedLayoutConstraints += newLayoutConstraints + + // return the new constraints + return newLayoutConstraints + } + + func uninstallFromView() { + if let installInfo = self.installInfo, + let installedLayoutConstraints = installInfo.layoutConstraints.allObjects as? [LayoutConstraint] { + + if installedLayoutConstraints.count > 0 { + + if let installedOnView = installInfo.view { + // remove the constraints from the UIView's storage + installedOnView.removeConstraints(installedLayoutConstraints) + } + + // remove the constraints from the from item view + if let fromView = self.fromItem.view { + fromView.snp_installedLayoutConstraints = fromView.snp_installedLayoutConstraints.filter { + return !contains(installedLayoutConstraints, $0) + } + } + } + + } + self.installInfo = nil + } + + // MARK: private + + private let fromItem: ConstraintItem + private var toItem: ConstraintItem + private var relation: ConstraintRelation? + private var constant: Any = Float(0.0) + private var multiplier: Float = 1.0 + private var priority: Float = 1000.0 + private var offset: Any = Float(0.0) + + private var installInfo: ConstraintInstallInfo? + + private func addConstraint(attributes: ConstraintAttributes) -> ConstraintBuilder { + if self.relation == nil { + self.fromItem.attributes += attributes + } + return self + } + + private func constrainTo(other: ConstraintItem, relation: ConstraintRelation) -> ConstraintBuilder { + if other.attributes != ConstraintAttributes.None { + let toLayoutAttributes = other.attributes.layoutAttributes + if toLayoutAttributes.count > 1 { + let fromLayoutAttributes = self.fromItem.attributes.layoutAttributes + if toLayoutAttributes != fromLayoutAttributes { + NSException(name: "Invalid Constraint", reason: "Cannot constrain to multiple non identical attributes", userInfo: nil).raise() + return self + } + other.attributes = ConstraintAttributes.None + } + } + self.toItem = other + self.relation = relation + return self + } + private func constrainTo(other: View, relation: ConstraintRelation) -> ConstraintBuilder { + return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation) + } + #if os(iOS) + private func constrainTo(other: UILayoutSupport, relation: ConstraintRelation) -> ConstraintBuilder { + return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation) + } + #endif + private func constrainTo(other: Float, relation: ConstraintRelation) -> ConstraintBuilder { + self.constant = other + return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation) + } + private func constrainTo(other: Double, relation: ConstraintRelation) -> ConstraintBuilder { + self.constant = other + return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation) + } + private func constrainTo(other: CGSize, relation: ConstraintRelation) -> ConstraintBuilder { + self.constant = other + return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation) + } + private func constrainTo(other: CGPoint, relation: ConstraintRelation) -> ConstraintBuilder { + self.constant = other + return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation) + } + private func constrainTo(other: EdgeInsets, relation: ConstraintRelation) -> ConstraintBuilder { + self.constant = other + return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation) + } + + private class func closestCommonSuperviewFromView(fromView: View?, toView: View?) -> View? { + var views = Set() + var fromView = fromView + var toView = toView + do { + if let view = toView { + if views.contains(view) { + return view + } + views.insert(view) + toView = view.superview + } + if let view = fromView { + if views.contains(view) { + return view + } + views.insert(view) + fromView = view.superview + } + } while (fromView != nil || toView != nil) + + return nil + } +} + +private extension NSLayoutAttribute { + + private func snp_offsetForValue(value: Any?) -> CGFloat { + // Float + if let float = value as? Float { + return CGFloat(float) + } + // Double + else if let double = value as? Double { + return CGFloat(double) + } + // UInt + else if let int = value as? Int { + return CGFloat(int) + } + // Int + else if let uint = value as? UInt { + return CGFloat(uint) + } + // CGFloat + else if let float = value as? CGFloat { + return float + } + // CGSize + else if let size = value as? CGSize { + if self == .Width { + return size.width + } else if self == .Height { + return size.height + } + } + // CGPoint + else if let point = value as? CGPoint { + #if os(iOS) + switch self { + case .Left, .CenterX, .LeftMargin, .CenterXWithinMargins: return point.x + case .Top, .CenterY, .TopMargin, .CenterYWithinMargins, .Baseline, .FirstBaseline: return point.y + case .Right, .RightMargin: return -point.x + case .Bottom, .BottomMargin: return -point.y + case .Leading, .LeadingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? point.x : -point.x + case .Trailing, .TrailingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? -point.x : point.x + case .Width, .Height, .NotAnAttribute: return CGFloat(0) + } + #else + switch self { + case .Left, .CenterX: return point.x + case .Top, .CenterY, .Baseline: return point.y + case .Right: return -point.x + case .Bottom: return -point.y + case .Leading: return (Config.interfaceLayoutDirection == .LeftToRight) ? point.x : -point.x + case .Trailing: return (Config.interfaceLayoutDirection == .LeftToRight) ? -point.x : point.x + case .Width, .Height, .NotAnAttribute: return CGFloat(0) + } + #endif + } + // EdgeInsets + else if let insets = value as? EdgeInsets { + #if os(iOS) + switch self { + case .Left, .CenterX, .LeftMargin, .CenterXWithinMargins: return insets.left + case .Top, .CenterY, .TopMargin, .CenterYWithinMargins, .Baseline, .FirstBaseline: return insets.top + case .Right, .RightMargin: return -insets.right + case .Bottom, .BottomMargin: return -insets.bottom + case .Leading, .LeadingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.left : -insets.right + case .Trailing, .TrailingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? -insets.right : insets.left + case .Width, .Height, .NotAnAttribute: return CGFloat(0) + } + #else + switch self { + case .Left, .CenterX: return insets.left + case .Top, .CenterY, .Baseline: return insets.top + case .Right: return -insets.right + case .Bottom: return -insets.bottom + case .Leading: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.left : -insets.right + case .Trailing: return (Config.interfaceLayoutDirection == .LeftToRight) ? -insets.right : insets.left + case .Width, .Height, .NotAnAttribute: return CGFloat(0) + } + #endif + } + + return CGFloat(0) + } + + private func snp_constantForValue(value: Any?) -> CGFloat { + // Float + if let float = value as? Float { + return CGFloat(float) + } + // Double + else if let double = value as? Double { + return CGFloat(double) + } + // UInt + else if let int = value as? Int { + return CGFloat(int) + } + // Int + else if let uint = value as? UInt { + return CGFloat(uint) + } + // CGFloat + else if let float = value as? CGFloat { + return float + } + // CGSize + else if let size = value as? CGSize { + if self == .Width { + return size.width + } else if self == .Height { + return size.height + } + } + // CGPoint + else if let point = value as? CGPoint { + #if os(iOS) + switch self { + case .Left, .CenterX, .LeftMargin, .CenterXWithinMargins: return point.x + case .Top, .CenterY, .TopMargin, .CenterYWithinMargins, .Baseline, .FirstBaseline: return point.y + case .Right, .RightMargin: return point.x + case .Bottom, .BottomMargin: return point.y + case .Leading, .LeadingMargin: return point.x + case .Trailing, .TrailingMargin: return point.x + case .Width, .Height, .NotAnAttribute: return CGFloat(0) + } + #else + switch self { + case .Left, .CenterX: return point.x + case .Top, .CenterY, .Baseline: return point.y + case .Right: return point.x + case .Bottom: return point.y + case .Leading: return point.x + case .Trailing: return point.x + case .Width, .Height, .NotAnAttribute: return CGFloat(0) + } + #endif + } + // EdgeInsets + else if let insets = value as? EdgeInsets { + #if os(iOS) + switch self { + case .Left, .CenterX, .LeftMargin, .CenterXWithinMargins: return insets.left + case .Top, .CenterY, .TopMargin, .CenterYWithinMargins, .Baseline, .FirstBaseline: return insets.top + case .Right, .RightMargin: return insets.right + case .Bottom, .BottomMargin: return insets.bottom + case .Leading, .LeadingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.left : insets.right + case .Trailing, .TrailingMargin: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.right : insets.left + case .Width, .Height, .NotAnAttribute: return CGFloat(0) + } + #else + switch self { + case .Left, .CenterX: return insets.left + case .Top, .CenterY, .Baseline: return insets.top + case .Right: return insets.right + case .Bottom: return insets.bottom + case .Leading: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.left : insets.right + case .Trailing: return (Config.interfaceLayoutDirection == .LeftToRight) ? insets.right : insets.left + case .Width, .Height, .NotAnAttribute: return CGFloat(0) + } + #endif + } + + return CGFloat(0); + } +} + +private struct ConstraintInstallInfo { + + weak var view: View? = nil + let layoutConstraints: NSHashTable + +} + + +internal func ==(left: ConstraintBuilder, right: ConstraintBuilder) -> Bool { + return (left.fromItem == right.fromItem && + left.toItem == right.toItem && + left.relation == right.relation && + left.multiplier == right.multiplier && + left.priority == right.priority) +} \ No newline at end of file diff --git a/Source/ConstraintItem.swift b/Source/ConstraintItem.swift index f9f7c04..359fd32 100644 --- a/Source/ConstraintItem.swift +++ b/Source/ConstraintItem.swift @@ -1,7 +1,7 @@ // // Snap // -// Copyright (c) 2011-2014 Masonry Team - https://github.com/Masonry +// Copyright (c) 2011-2015 Masonry Team - https://github.com/Masonry // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/Source/ConstraintMaker.swift b/Source/ConstraintMaker.swift index fa6a5e1..7650227 100644 --- a/Source/ConstraintMaker.swift +++ b/Source/ConstraintMaker.swift @@ -1,7 +1,7 @@ // // Snap // -// Copyright (c) 2011-2014 Masonry Team - https://github.com/Masonry +// Copyright (c) 2011-2015 Masonry Team - https://github.com/Masonry // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -31,49 +31,49 @@ import AppKit * ConstraintMaker is the maker in snap that gets all constraints kickstarted */ final public class ConstraintMaker { - public var left: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Left) } - public var top: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Top) } - public var right: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Right) } - public var bottom: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Bottom) } - public var leading: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Leading) } - public var trailing: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Trailing) } - public var width: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Width) } - public var height: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Height) } - public var centerX: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.CenterX) } - public var centerY: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.CenterY) } - public var baseline: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Baseline) } + public var left: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.Left) } + public var top: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.Top) } + public var right: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.Right) } + public var bottom: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.Bottom) } + public var leading: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.Leading) } + public var trailing: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.Trailing) } + public var width: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.Width) } + public var height: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.Height) } + public var centerX: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.CenterX) } + public var centerY: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.CenterY) } + public var baseline: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.Baseline) } #if os(iOS) - public var firstBaseline: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.FirstBaseline) } - public var leftMargin: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.LeftMargin) } - public var rightMargin: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.RightMargin) } - public var topMargin: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.TopMargin) } - public var bottomMargin: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.BottomMargin) } - public var leadingMargin: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.LeadingMargin) } - public var trailingMargin: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.TrailingMargin) } - public var centerXWithinMargins: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.CenterXWithinMargins) } - public var centerYWithinMargins: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.CenterYWithinMargins) } + public var firstBaseline: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.FirstBaseline) } + public var leftMargin: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.LeftMargin) } + public var rightMargin: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.RightMargin) } + public var topMargin: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.TopMargin) } + public var bottomMargin: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.BottomMargin) } + public var leadingMargin: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.LeadingMargin) } + public var trailingMargin: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.TrailingMargin) } + public var centerXWithinMargins: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.CenterXWithinMargins) } + public var centerYWithinMargins: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.CenterYWithinMargins) } #endif - public var edges: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Edges) } - public var size: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Size) } - public var center: ConstraintExtendable { return self.addConstraint(ConstraintAttributes.Center) } + public var edges: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.Edges) } + public var size: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.Size) } + public var center: ConstraintBuilderExtendable { return self.addConstraint(ConstraintAttributes.Center) } init(view: View) { self.view = view } internal weak var view: View? - internal var constraints = [MutableConstraint]() + internal var constraints = [ConstraintBuilder]() - internal func addConstraint(attributes: ConstraintAttributes) -> MutableConstraint { + internal func addConstraint(attributes: ConstraintAttributes) -> ConstraintBuilder { let item = ConstraintItem(object: self.view, attributes: attributes) - let constraint = MutableConstraint(fromItem: item) + let constraint = ConstraintBuilder(fromItem: item) self.constraints.append(constraint) return constraint } - internal class func prepareConstraints(view: View, @noescape block: (make: ConstraintMaker) -> Void) -> [MutableConstraint] { + internal class func prepareConstraints(view: View, @noescape block: (make: ConstraintMaker) -> Void) -> [ConstraintBuilder] { let maker = ConstraintMaker(view: view) block(make: maker) return maker.constraints diff --git a/Source/ConstraintRelation.swift b/Source/ConstraintRelation.swift index a00c184..cc6b928 100644 --- a/Source/ConstraintRelation.swift +++ b/Source/ConstraintRelation.swift @@ -1,7 +1,7 @@ // // Snap // -// Copyright (c) 2011-2014 Masonry Team - https://github.com/Masonry +// Copyright (c) 2011-2015 Masonry Team - https://github.com/Masonry // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/Source/Debugging.swift b/Source/Debugging.swift index 581117f..97bc308 100644 --- a/Source/Debugging.swift +++ b/Source/Debugging.swift @@ -1,7 +1,7 @@ // // Snap // -// Copyright (c) 2011-2014 Masonry Team - https://github.com/Masonry +// Copyright (c) 2011-2015 Masonry Team - https://github.com/Masonry // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/Source/EdgeInsets.swift b/Source/EdgeInsets.swift index 7aa432d..d52c961 100644 --- a/Source/EdgeInsets.swift +++ b/Source/EdgeInsets.swift @@ -1,7 +1,7 @@ // // Snap // -// Copyright (c) 2011-2014 Masonry Team - https://github.com/Masonry +// Copyright (c) 2011-2015 Masonry Team - https://github.com/Masonry // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/Source/LayoutConstraint.swift b/Source/LayoutConstraint.swift index a05d1ba..b05d810 100644 --- a/Source/LayoutConstraint.swift +++ b/Source/LayoutConstraint.swift @@ -1,7 +1,7 @@ // // Snap // -// Copyright (c) 2011-2014 Masonry Team - https://github.com/Masonry +// Copyright (c) 2011-2015 Masonry Team - https://github.com/Masonry // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -31,7 +31,7 @@ import AppKit * LayoutConstraint is a subclass of NSLayoutConstraint to assist Snap and also provide better debugging */ final public class LayoutConstraint: NSLayoutConstraint { - internal var snp_constraint: MutableConstraint? = nil + internal var snp_constraint: ConstraintBuilder? = nil } public func ==(left: LayoutConstraint, right: LayoutConstraint) -> Bool { diff --git a/Source/Snap.swift b/Source/Snap.swift index 3358924..3203d80 100644 --- a/Source/Snap.swift +++ b/Source/Snap.swift @@ -1,7 +1,7 @@ // // Snap // -// Copyright (c) 2011-2014 Masonry Team - https://github.com/Masonry +// Copyright (c) 2011-2015 Masonry Team - https://github.com/Masonry // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/Source/View+Snap.swift b/Source/View+Snap.swift index 5279616..0ac24ed 100644 --- a/Source/View+Snap.swift +++ b/Source/View+Snap.swift @@ -1,7 +1,7 @@ // // Snap // -// Copyright (c) 2011-2014 Masonry Team - https://github.com/Masonry +// Copyright (c) 2011-2015 Masonry Team - https://github.com/Masonry // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal