Cleaned up some APIs and added support for prepare constraints.

This commit is contained in:
Robert Payne 2015-02-17 11:11:58 +13:00
parent 4158cf0d99
commit b7eb62fba0
3 changed files with 103 additions and 91 deletions

View File

@ -31,19 +31,24 @@ import AppKit
* Constraint is a single item that defines all the properties for a single ConstraintMaker chain * Constraint is a single item that defines all the properties for a single ConstraintMaker chain
*/ */
public class Constraint { public class Constraint {
public var left: Constraint { return addConstraint(ConstraintAttributes.Left) } public var left: Constraint { return self.addConstraint(ConstraintAttributes.Left) }
public var top: Constraint { return addConstraint(ConstraintAttributes.Top) } public var top: Constraint { return self.addConstraint(ConstraintAttributes.Top) }
public var right: Constraint { return addConstraint(ConstraintAttributes.Right) } public var right: Constraint { return self.addConstraint(ConstraintAttributes.Right) }
public var bottom: Constraint { return addConstraint(ConstraintAttributes.Bottom) } public var bottom: Constraint { return self.addConstraint(ConstraintAttributes.Bottom) }
public var leading: Constraint { return addConstraint(ConstraintAttributes.Leading) } public var leading: Constraint { return self.addConstraint(ConstraintAttributes.Leading) }
public var trailing: Constraint { return addConstraint(ConstraintAttributes.Trailing) } public var trailing: Constraint { return self.addConstraint(ConstraintAttributes.Trailing) }
public var width: Constraint { return addConstraint(ConstraintAttributes.Width) } public var width: Constraint { return self.addConstraint(ConstraintAttributes.Width) }
public var height: Constraint { return addConstraint(ConstraintAttributes.Height) } public var height: Constraint { return self.addConstraint(ConstraintAttributes.Height) }
public var centerX: Constraint { return addConstraint(ConstraintAttributes.CenterX) } public var centerX: Constraint { return self.addConstraint(ConstraintAttributes.CenterX) }
public var centerY: Constraint { return addConstraint(ConstraintAttributes.CenterY) } public var centerY: Constraint { return self.addConstraint(ConstraintAttributes.CenterY) }
public var baseline: Constraint { return addConstraint(ConstraintAttributes.Baseline) } public var baseline: Constraint { return self.addConstraint(ConstraintAttributes.Baseline) }
public var and: Constraint { return self } public var and: Constraint {
if self.relation != nil {
fatalError("And is semantic only and can only be used before a relation is set.")
}
return self
}
public var with: Constraint { return self } public var with: Constraint { return self }
// MARK: initializer // MARK: initializer
@ -56,115 +61,115 @@ public class Constraint {
// MARK: equalTo // MARK: equalTo
public func equalTo(other: ConstraintItem) -> Constraint { public func equalTo(other: ConstraintItem) -> Constraint {
return constrainTo(other, relation: .Equal) return self.constrainTo(other, relation: .Equal)
} }
public func equalTo(other: View) -> Constraint { public func equalTo(other: View) -> Constraint {
return constrainTo(other, relation: .Equal) return self.constrainTo(other, relation: .Equal)
} }
#if os(iOS) #if os(iOS)
public func equalTo(other: UILayoutSupport) -> Constraint { public func equalTo(other: UILayoutSupport) -> Constraint {
return constrainTo(other, relation: .Equal) return self.constrainTo(other, relation: .Equal)
} }
#endif #endif
public func equalTo(other: Float) -> Constraint { public func equalTo(other: Float) -> Constraint {
return constrainTo(other, relation: .Equal) return self.constrainTo(other, relation: .Equal)
} }
public func equalTo(other: Double) -> Constraint { public func equalTo(other: Double) -> Constraint {
return constrainTo(Float(other), relation: .Equal) return self.constrainTo(Float(other), relation: .Equal)
} }
public func equalTo(other: CGFloat) -> Constraint { public func equalTo(other: CGFloat) -> Constraint {
return constrainTo(Float(other), relation: .Equal) return self.constrainTo(Float(other), relation: .Equal)
} }
public func equalTo(other: Int) -> Constraint { public func equalTo(other: Int) -> Constraint {
return constrainTo(Float(other), relation: .Equal) return self.constrainTo(Float(other), relation: .Equal)
} }
public func equalTo(other: UInt) -> Constraint { public func equalTo(other: UInt) -> Constraint {
return constrainTo(Float(other), relation: .Equal) return self.constrainTo(Float(other), relation: .Equal)
} }
public func equalTo(other: CGSize) -> Constraint { public func equalTo(other: CGSize) -> Constraint {
return constrainTo(other, relation: .Equal) return self.constrainTo(other, relation: .Equal)
} }
public func equalTo(other: CGPoint) -> Constraint { public func equalTo(other: CGPoint) -> Constraint {
return constrainTo(other, relation: .Equal) return self.constrainTo(other, relation: .Equal)
} }
public func equalTo(other: EdgeInsets) -> Constraint { public func equalTo(other: EdgeInsets) -> Constraint {
return constrainTo(other, relation: .Equal) return self.constrainTo(other, relation: .Equal)
} }
// MARK: lessThanOrEqualTo // MARK: lessThanOrEqualTo
public func lessThanOrEqualTo(other: ConstraintItem) -> Constraint { public func lessThanOrEqualTo(other: ConstraintItem) -> Constraint {
return constrainTo(other, relation: .LessThanOrEqualTo) return self.constrainTo(other, relation: .LessThanOrEqualTo)
} }
public func lessThanOrEqualTo(other: View) -> Constraint { public func lessThanOrEqualTo(other: View) -> Constraint {
return constrainTo(other, relation: .LessThanOrEqualTo) return self.constrainTo(other, relation: .LessThanOrEqualTo)
} }
#if os(iOS) #if os(iOS)
public func lessThanOrEqualTo(other: UILayoutSupport) -> Constraint { public func lessThanOrEqualTo(other: UILayoutSupport) -> Constraint {
return constrainTo(other, relation: .LessThanOrEqualTo) return self.constrainTo(other, relation: .LessThanOrEqualTo)
} }
#endif #endif
public func lessThanOrEqualTo(other: Float) -> Constraint { public func lessThanOrEqualTo(other: Float) -> Constraint {
return constrainTo(other, relation: .LessThanOrEqualTo) return self.constrainTo(other, relation: .LessThanOrEqualTo)
} }
public func lessThanOrEqualTo(other: Double) -> Constraint { public func lessThanOrEqualTo(other: Double) -> Constraint {
return constrainTo(Float(other), relation: .LessThanOrEqualTo) return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
} }
public func lessThanOrEqualTo(other: CGFloat) -> Constraint { public func lessThanOrEqualTo(other: CGFloat) -> Constraint {
return constrainTo(Float(other), relation: .LessThanOrEqualTo) return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
} }
public func lessThanOrEqualTo(other: Int) -> Constraint { public func lessThanOrEqualTo(other: Int) -> Constraint {
return constrainTo(Float(other), relation: .LessThanOrEqualTo) return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
} }
public func lessThanOrEqualTo(other: UInt) -> Constraint { public func lessThanOrEqualTo(other: UInt) -> Constraint {
return constrainTo(Float(other), relation: .LessThanOrEqualTo) return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
} }
public func lessThanOrEqualTo(other: CGSize) -> Constraint { public func lessThanOrEqualTo(other: CGSize) -> Constraint {
return constrainTo(other, relation: .LessThanOrEqualTo) return self.constrainTo(other, relation: .LessThanOrEqualTo)
} }
public func lessThanOrEqualTo(other: CGPoint) -> Constraint { public func lessThanOrEqualTo(other: CGPoint) -> Constraint {
return constrainTo(other, relation: .LessThanOrEqualTo) return self.constrainTo(other, relation: .LessThanOrEqualTo)
} }
public func lessThanOrEqualTo(other: EdgeInsets) -> Constraint { public func lessThanOrEqualTo(other: EdgeInsets) -> Constraint {
return constrainTo(other, relation: .LessThanOrEqualTo) return self.constrainTo(other, relation: .LessThanOrEqualTo)
} }
// MARK: greaterThanOrEqualTo // MARK: greaterThanOrEqualTo
public func greaterThanOrEqualTo(other: ConstraintItem) -> Constraint { public func greaterThanOrEqualTo(other: ConstraintItem) -> Constraint {
return constrainTo(other, relation: .GreaterThanOrEqualTo) return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
} }
public func greaterThanOrEqualTo(other: View) -> Constraint { public func greaterThanOrEqualTo(other: View) -> Constraint {
return constrainTo(other, relation: .GreaterThanOrEqualTo) return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
} }
#if os(iOS) #if os(iOS)
public func greaterThanOrEqualTo(other: UILayoutSupport) -> Constraint { public func greaterThanOrEqualTo(other: UILayoutSupport) -> Constraint {
return constrainTo(other, relation: .GreaterThanOrEqualTo) return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
} }
#endif #endif
public func greaterThanOrEqualTo(other: Float) -> Constraint { public func greaterThanOrEqualTo(other: Float) -> Constraint {
return constrainTo(other, relation: .GreaterThanOrEqualTo) return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
} }
public func greaterThanOrEqualTo(other: Double) -> Constraint { public func greaterThanOrEqualTo(other: Double) -> Constraint {
return constrainTo(Float(other), relation: .GreaterThanOrEqualTo) return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
} }
public func greaterThanOrEqualTo(other: CGFloat) -> Constraint { public func greaterThanOrEqualTo(other: CGFloat) -> Constraint {
return constrainTo(Float(other), relation: .GreaterThanOrEqualTo) return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
} }
public func greaterThanOrEqualTo(other: Int) -> Constraint { public func greaterThanOrEqualTo(other: Int) -> Constraint {
return constrainTo(Float(other), relation: .GreaterThanOrEqualTo) return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
} }
public func greaterThanOrEqualTo(other: UInt) -> Constraint { public func greaterThanOrEqualTo(other: UInt) -> Constraint {
return constrainTo(Float(other), relation: .GreaterThanOrEqualTo) return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
} }
public func greaterThanOrEqualTo(other: CGSize) -> Constraint { public func greaterThanOrEqualTo(other: CGSize) -> Constraint {
return constrainTo(other, relation: .GreaterThanOrEqualTo) return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
} }
public func greaterThanOrEqualTo(other: CGPoint) -> Constraint { public func greaterThanOrEqualTo(other: CGPoint) -> Constraint {
return constrainTo(other, relation: .GreaterThanOrEqualTo) return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
} }
public func greaterThanOrEqualTo(other: EdgeInsets) -> Constraint { public func greaterThanOrEqualTo(other: EdgeInsets) -> Constraint {
return constrainTo(other, relation: .GreaterThanOrEqualTo) return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
} }
// MARK: multiplier // MARK: multiplier
@ -308,7 +313,7 @@ public class Constraint {
} }
} }
var layoutConstraints: Array<LayoutConstraint> = [] var newLayoutConstraints = Array<LayoutConstraint>()
let layoutFromAttributes = self.fromItem.attributes.layoutAttributes let layoutFromAttributes = self.fromItem.attributes.layoutAttributes
let layoutToAttributes = self.toItem.attributes.layoutAttributes let layoutToAttributes = self.toItem.attributes.layoutAttributes
@ -348,7 +353,7 @@ public class Constraint {
// set constraint // set constraint
layoutConstraint.constraint = self layoutConstraint.constraint = self
layoutConstraints.append(layoutConstraint) newLayoutConstraints.append(layoutConstraint)
} }
// special logic for updating // special logic for updating
@ -356,11 +361,11 @@ public class Constraint {
// get existing constraints for this view // get existing constraints for this view
let existingLayoutConstraints = reverse(layoutFrom!.snp_installedLayoutConstraints) let existingLayoutConstraints = reverse(layoutFrom!.snp_installedLayoutConstraints)
// array that will contain only new layout constraints // array that will contain only new layout constraints to keep
var newLayoutConstraints = Array<LayoutConstraint>() var newLayoutConstraintsToKeep = Array<LayoutConstraint>()
// begin looping // begin looping
for layoutConstraint in layoutConstraints { for layoutConstraint in newLayoutConstraints {
// layout constraint that should be updated // layout constraint that should be updated
var updateLayoutConstraint: LayoutConstraint? = nil var updateLayoutConstraint: LayoutConstraint? = nil
@ -376,26 +381,35 @@ public class Constraint {
if updateLayoutConstraint != nil { if updateLayoutConstraint != nil {
updateLayoutConstraint!.constant = layoutConstraint.constant updateLayoutConstraint!.constant = layoutConstraint.constant
} }
// otherwise add this layout constraint to new list // otherwise add this layout constraint to new keep list
else { else {
newLayoutConstraints.append(layoutConstraint) newLayoutConstraintsToKeep.append(layoutConstraint)
} }
} }
// set constraints to only new ones // set constraints to only new ones
layoutConstraints = newLayoutConstraints newLayoutConstraints = newLayoutConstraintsToKeep
} }
// add constraints // add constraints
installOnView!.addConstraints(layoutConstraints) installOnView!.addConstraints(newLayoutConstraints)
// store which view this constraint was installed on
self.installedOnView = installOnView self.installedOnView = installOnView
// store which layout constraints are installed for this constraint
self.installedLayoutConstraints = NSHashTable.weakObjectsHashTable() self.installedLayoutConstraints = NSHashTable.weakObjectsHashTable()
for layoutConstraint in layoutConstraints { for layoutConstraint in newLayoutConstraints {
self.installedLayoutConstraints!.addObject(layoutConstraint) self.installedLayoutConstraints!.addObject(layoutConstraint)
} }
return layoutConstraints // store the layout constraints against the installed on view
var layoutConstraints = Array<LayoutConstraint>(layoutFrom!.snp_installedLayoutConstraints)
layoutConstraints += newLayoutConstraints
layoutFrom!.snp_installedLayoutConstraints = layoutConstraints
// return the new constraints
return newLayoutConstraints
} }
internal func uninstallFromView() { internal func uninstallFromView() {

View File

@ -31,21 +31,21 @@ import AppKit
* ConstraintMaker is the maker in snap that gets all constraints kickstarted * ConstraintMaker is the maker in snap that gets all constraints kickstarted
*/ */
public class ConstraintMaker { public class ConstraintMaker {
public var left: Constraint { return addConstraint(ConstraintAttributes.Left) } public var left: Constraint { return self.addConstraint(ConstraintAttributes.Left) }
public var top: Constraint { return addConstraint(ConstraintAttributes.Top) } public var top: Constraint { return self.addConstraint(ConstraintAttributes.Top) }
public var right: Constraint { return addConstraint(ConstraintAttributes.Right) } public var right: Constraint { return self.addConstraint(ConstraintAttributes.Right) }
public var bottom: Constraint { return addConstraint(ConstraintAttributes.Bottom) } public var bottom: Constraint { return self.addConstraint(ConstraintAttributes.Bottom) }
public var leading: Constraint { return addConstraint(ConstraintAttributes.Leading) } public var leading: Constraint { return self.addConstraint(ConstraintAttributes.Leading) }
public var trailing: Constraint { return addConstraint(ConstraintAttributes.Trailing) } public var trailing: Constraint { return self.addConstraint(ConstraintAttributes.Trailing) }
public var width: Constraint { return addConstraint(ConstraintAttributes.Width) } public var width: Constraint { return self.addConstraint(ConstraintAttributes.Width) }
public var height: Constraint { return addConstraint(ConstraintAttributes.Height) } public var height: Constraint { return self.addConstraint(ConstraintAttributes.Height) }
public var centerX: Constraint { return addConstraint(ConstraintAttributes.CenterX) } public var centerX: Constraint { return self.addConstraint(ConstraintAttributes.CenterX) }
public var centerY: Constraint { return addConstraint(ConstraintAttributes.CenterY) } public var centerY: Constraint { return self.addConstraint(ConstraintAttributes.CenterY) }
public var baseline: Constraint { return addConstraint(ConstraintAttributes.Baseline) } public var baseline: Constraint { return self.addConstraint(ConstraintAttributes.Baseline) }
public var edges: Constraint { return addConstraint(ConstraintAttributes.Edges) } public var edges: Constraint { return self.addConstraint(ConstraintAttributes.Edges) }
public var size: Constraint { return addConstraint(ConstraintAttributes.Size) } public var size: Constraint { return self.addConstraint(ConstraintAttributes.Size) }
public var center: Constraint { return addConstraint(ConstraintAttributes.Center) } public var center: Constraint { return self.addConstraint(ConstraintAttributes.Center) }
init(view: View) { init(view: View) {
self.view = view self.view = view
@ -61,6 +61,12 @@ public class ConstraintMaker {
return constraint return constraint
} }
internal class func prepareConstraints(view: View, block: (make: ConstraintMaker) -> Void) -> Array<Constraint> {
let maker = ConstraintMaker(view: view)
block(make: maker)
return maker.constraints
}
internal class func makeConstraints(view: View, block: (make: ConstraintMaker) -> Void) { internal class func makeConstraints(view: View, block: (make: ConstraintMaker) -> Void) {
#if os(iOS) #if os(iOS)
view.setTranslatesAutoresizingMaskIntoConstraints(false) view.setTranslatesAutoresizingMaskIntoConstraints(false)
@ -69,13 +75,9 @@ public class ConstraintMaker {
#endif #endif
let maker = ConstraintMaker(view: view) let maker = ConstraintMaker(view: view)
block(make: maker) block(make: maker)
var layoutConstraints = Array<LayoutConstraint>(view.snp_installedLayoutConstraints)
for constraint in maker.constraints { for constraint in maker.constraints {
layoutConstraints += constraint.install() constraint.installOnView(updateExisting: false)
} }
view.snp_installedLayoutConstraints = layoutConstraints
} }
internal class func remakeConstraints(view: View, block: (make: ConstraintMaker) -> Void) { internal class func remakeConstraints(view: View, block: (make: ConstraintMaker) -> Void) {
@ -89,15 +91,12 @@ public class ConstraintMaker {
var layoutConstraints = Array<LayoutConstraint>(view.snp_installedLayoutConstraints) var layoutConstraints = Array<LayoutConstraint>(view.snp_installedLayoutConstraints)
for existingLayoutConstraint in layoutConstraints { for existingLayoutConstraint in layoutConstraints {
existingLayoutConstraint.constraint?.uninstall() existingLayoutConstraint.constraint?.uninstallFromView()
} }
layoutConstraints = []
for constraint in maker.constraints { for constraint in maker.constraints {
layoutConstraints += constraint.install() constraint.installOnView(updateExisting: false)
} }
view.snp_installedLayoutConstraints = layoutConstraints
} }
internal class func updateConstraints(view: View, block: (make: ConstraintMaker) -> Void) { internal class func updateConstraints(view: View, block: (make: ConstraintMaker) -> Void) {
@ -109,20 +108,15 @@ public class ConstraintMaker {
let maker = ConstraintMaker(view: view) let maker = ConstraintMaker(view: view)
block(make: maker) block(make: maker)
var layoutConstraints = Array<LayoutConstraint>(view.snp_installedLayoutConstraints)
for constraint in maker.constraints { for constraint in maker.constraints {
layoutConstraints += constraint.installOnView(updateExisting: true) constraint.installOnView(updateExisting: true)
} }
view.snp_installedLayoutConstraints = layoutConstraints
} }
internal class func removeConstraints(view: View) { internal class func removeConstraints(view: View) {
let eixsitingLayoutConstraints = Array<LayoutConstraint>(view.snp_installedLayoutConstraints) let existingLayoutConstraints = Array<LayoutConstraint>(view.snp_installedLayoutConstraints)
for existingLayoutConstraint in eixsitingLayoutConstraints { for existingLayoutConstraint in existingLayoutConstraints {
existingLayoutConstraint.constraint?.uninstall() existingLayoutConstraint.constraint?.uninstallFromView()
} }
view.snp_installedLayoutConstraints = []
} }
} }

View File

@ -68,6 +68,10 @@ public extension View {
public var snp_centerWithinMargins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterWithinMargins) } public var snp_centerWithinMargins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterWithinMargins) }
#endif #endif
public func snp_prepareConstraints(block: (make: ConstraintMaker) -> Void) -> Array<Constraint> {
return ConstraintMaker.prepareConstraints(self, block: block)
}
public func snp_makeConstraints(block: (make: ConstraintMaker) -> Void) { public func snp_makeConstraints(block: (make: ConstraintMaker) -> Void) {
ConstraintMaker.makeConstraints(self, block: block) ConstraintMaker.makeConstraints(self, block: block)
} }