mirror of https://github.com/SnapKit/SnapKit
Refactoring & Documentation
This commit is contained in:
parent
7cb3a61c14
commit
c9b89ad974
|
@ -35,8 +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 */; };
|
||||
EEFCF3321AD9432400A425FA /* ConstraintDescription.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEFCF3311AD9432400A425FA /* ConstraintDescription.swift */; };
|
||||
EEFCF3331AD9432400A425FA /* ConstraintDescription.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEFCF3311AD9432400A425FA /* ConstraintDescription.swift */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXContainerItemProxy section */
|
||||
|
@ -70,7 +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 = "<group>"; };
|
||||
EEFCF32E1AD926AE00A425FA /* Snap.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Snap.swift; sourceTree = "<group>"; };
|
||||
EEFCF3311AD9432400A425FA /* ConstraintBuilder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConstraintBuilder.swift; sourceTree = "<group>"; };
|
||||
EEFCF3311AD9432400A425FA /* ConstraintDescription.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConstraintDescription.swift; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
|
@ -144,15 +144,15 @@
|
|||
EECDB3661AC0C95C006BBC11 /* Snap.h */,
|
||||
EEFCF32E1AD926AE00A425FA /* Snap.swift */,
|
||||
EECDB35E1AC0C95C006BBC11 /* Constraint.swift */,
|
||||
EEFCF3311AD9432400A425FA /* ConstraintBuilder.swift */,
|
||||
EECDB35F1AC0C95C006BBC11 /* ConstraintAttributes.swift */,
|
||||
EECDB3601AC0C95C006BBC11 /* ConstraintItem.swift */,
|
||||
EEFCF3311AD9432400A425FA /* ConstraintDescription.swift */,
|
||||
EECDB3611AC0C95C006BBC11 /* ConstraintMaker.swift */,
|
||||
EECDB3621AC0C95C006BBC11 /* ConstraintRelation.swift */,
|
||||
EECDB3671AC0C95C006BBC11 /* View+Snap.swift */,
|
||||
EECDB3601AC0C95C006BBC11 /* ConstraintItem.swift */,
|
||||
EECDB3631AC0C95C006BBC11 /* EdgeInsets.swift */,
|
||||
EECDB3651AC0C95C006BBC11 /* LayoutConstraint.swift */,
|
||||
EECDB3671AC0C95C006BBC11 /* View+Snap.swift */,
|
||||
EEFCF32B1AD910B900A425FA /* Debugging.swift */,
|
||||
EECDB3621AC0C95C006BBC11 /* ConstraintRelation.swift */,
|
||||
EECDB35F1AC0C95C006BBC11 /* ConstraintAttributes.swift */,
|
||||
EECDB36B1AC0C967006BBC11 /* Supporting Files */,
|
||||
EECDB3681AC0C95C006BBC11 /* Tests */,
|
||||
);
|
||||
|
@ -354,7 +354,7 @@
|
|||
EEFCF32F1AD926AE00A425FA /* Snap.swift in Sources */,
|
||||
EEFCF32C1AD910B900A425FA /* Debugging.swift in Sources */,
|
||||
EECDB3701AC0C9A6006BBC11 /* ConstraintRelation.swift in Sources */,
|
||||
EEFCF3321AD9432400A425FA /* ConstraintBuilder.swift in Sources */,
|
||||
EEFCF3321AD9432400A425FA /* ConstraintDescription.swift in Sources */,
|
||||
EECDB3731AC0C9A6006BBC11 /* View+Snap.swift in Sources */,
|
||||
EECDB3711AC0C9A6006BBC11 /* EdgeInsets.swift in Sources */,
|
||||
EECDB36F1AC0C9A6006BBC11 /* ConstraintMaker.swift in Sources */,
|
||||
|
@ -380,7 +380,7 @@
|
|||
EEFCF3301AD92C2200A425FA /* Snap.swift in Sources */,
|
||||
EEFCF32D1AD9250C00A425FA /* Debugging.swift in Sources */,
|
||||
EECDB3991AC0CBFF006BBC11 /* ConstraintRelation.swift in Sources */,
|
||||
EEFCF3331AD9432400A425FA /* ConstraintBuilder.swift in Sources */,
|
||||
EEFCF3331AD9432400A425FA /* ConstraintDescription.swift in Sources */,
|
||||
EECDB39C1AC0CBFF006BBC11 /* View+Snap.swift in Sources */,
|
||||
EECDB39A1AC0CBFF006BBC11 /* EdgeInsets.swift in Sources */,
|
||||
EECDB3981AC0CBFF006BBC11 /* ConstraintMaker.swift in Sources */,
|
||||
|
|
|
@ -27,14 +27,334 @@ import UIKit
|
|||
import AppKit
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Constraint is a protocol that exposes the public methods on a Constraint
|
||||
*/
|
||||
public protocol Constraint: class {
|
||||
public class Constraint {
|
||||
|
||||
func install() -> [LayoutConstraint]
|
||||
func uninstall() -> Void
|
||||
func activate() -> Void
|
||||
func deactivate() -> Void
|
||||
public func install() -> [LayoutConstraint] {
|
||||
return self.installOnView(updateExisting: false)
|
||||
}
|
||||
|
||||
public func uninstall() {
|
||||
self.uninstallFromView()
|
||||
}
|
||||
|
||||
public 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()
|
||||
}
|
||||
}
|
||||
|
||||
public 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()
|
||||
}
|
||||
}
|
||||
|
||||
private let fromItem: ConstraintItem
|
||||
private let toItem: ConstraintItem
|
||||
private let relation: ConstraintRelation
|
||||
private var constant: Any
|
||||
private var multiplier: Float
|
||||
private var priority: Float
|
||||
|
||||
private var installInfo: ConstraintInstallInfo? = nil
|
||||
|
||||
internal init(fromItem: ConstraintItem, toItem: ConstraintItem, relation: ConstraintRelation, constant: Any, multiplier: Float, priority: Float) {
|
||||
self.fromItem = fromItem
|
||||
self.toItem = toItem
|
||||
self.relation = relation
|
||||
self.constant = constant
|
||||
self.multiplier = multiplier
|
||||
self.priority = priority
|
||||
}
|
||||
|
||||
internal func installOnView(updateExisting: Bool = false) -> [LayoutConstraint] {
|
||||
var installOnView: View? = nil
|
||||
if self.toItem.view != nil {
|
||||
installOnView = closestCommonSuperviewBetween(self.fromItem.view, 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.layoutRelation
|
||||
|
||||
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)
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
internal 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
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private struct ConstraintInstallInfo {
|
||||
|
||||
weak var view: View? = nil
|
||||
let layoutConstraints: NSHashTable
|
||||
|
||||
}
|
||||
|
||||
private extension NSLayoutAttribute {
|
||||
|
||||
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, .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)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
return CGFloat(0);
|
||||
}
|
||||
}
|
||||
|
||||
private func closestCommonSuperviewBetween(fromView: View?, toView: View?) -> View? {
|
||||
var views = Set<View>()
|
||||
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 func ==(left: Constraint, right: Constraint) -> Bool {
|
||||
return (left.fromItem == right.fromItem &&
|
||||
left.toItem == right.toItem &&
|
||||
left.relation == right.relation &&
|
||||
left.multiplier == right.multiplier &&
|
||||
left.priority == right.priority)
|
||||
}
|
||||
|
||||
//public protocol Constraint: class {
|
||||
//
|
||||
// func install() -> [LayoutConstraint]
|
||||
// func uninstall() -> Void
|
||||
// func activate() -> Void
|
||||
// func deactivate() -> Void
|
||||
//
|
||||
//}
|
||||
|
|
|
@ -28,8 +28,8 @@ import AppKit
|
|||
#endif
|
||||
|
||||
/**
|
||||
* ConstraintAttributes is an options set that maps to NSLayoutAttributes.
|
||||
*/
|
||||
Used to define `NSLayoutAttributes` in a more concise and composite manner
|
||||
*/
|
||||
internal struct ConstraintAttributes: RawOptionSetType, BooleanType {
|
||||
|
||||
internal init(rawValue: UInt) {
|
||||
|
@ -47,9 +47,9 @@ internal struct ConstraintAttributes: RawOptionSetType, BooleanType {
|
|||
internal static func convertFromNilLiteral() -> ConstraintAttributes { return self(0) }
|
||||
internal var boolValue: Bool { return self.rawValue != 0 }
|
||||
|
||||
func toRaw() -> UInt { return self.rawValue }
|
||||
static func fromRaw(raw: UInt) -> ConstraintAttributes? { return self(raw) }
|
||||
static func fromMask(raw: UInt) -> ConstraintAttributes { return self(raw) }
|
||||
internal func toRaw() -> UInt { return self.rawValue }
|
||||
internal static func fromRaw(raw: UInt) -> ConstraintAttributes? { return self(raw) }
|
||||
internal static func fromMask(raw: UInt) -> ConstraintAttributes { return self(raw) }
|
||||
|
||||
// normal
|
||||
|
||||
|
|
|
@ -1,802 +0,0 @@
|
|||
//
|
||||
// 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 / inset
|
||||
*/
|
||||
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 right: 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.constant = 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.constant = amount
|
||||
return self
|
||||
}
|
||||
func offset(amount: CGSize) -> ConstraintBuilderMultipliable {
|
||||
self.constant = amount
|
||||
return self
|
||||
}
|
||||
func offset(amount: EdgeInsets) -> ConstraintBuilderMultipliable {
|
||||
self.constant = amount
|
||||
return self
|
||||
}
|
||||
|
||||
// MARK: insets
|
||||
|
||||
func insets(amount: EdgeInsets) -> ConstraintBuilderMultipliable {
|
||||
self.constant = EdgeInsets(top: amount.top, left: amount.left, bottom: -amount.bottom, right: -amount.right)
|
||||
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)
|
||||
|
||||
// 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 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<View>()
|
||||
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_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, .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)
|
||||
}
|
||||
#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)
|
||||
}
|
|
@ -0,0 +1,547 @@
|
|||
//
|
||||
// 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
|
||||
|
||||
/**
|
||||
Used to expose the final API of a `ConstraintDescription` which allows getting a constraint from it
|
||||
*/
|
||||
public protocol ConstraintDescriptionFinalizable: class {
|
||||
|
||||
var constraint: Constraint { get }
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
Used to expose priority APIs
|
||||
*/
|
||||
public protocol ConstraintDescriptionPriortizable: ConstraintDescriptionFinalizable {
|
||||
|
||||
func priority(priority: Float) -> ConstraintDescriptionFinalizable
|
||||
func priority(priority: Double) -> ConstraintDescriptionFinalizable
|
||||
func priority(priority: CGFloat) -> ConstraintDescriptionFinalizable
|
||||
func priority(priority: UInt) -> ConstraintDescriptionFinalizable
|
||||
func priority(priority: Int) -> ConstraintDescriptionFinalizable
|
||||
func priorityRequired() -> ConstraintDescriptionFinalizable
|
||||
func priorityHigh() -> ConstraintDescriptionFinalizable
|
||||
func priorityMedium() -> ConstraintDescriptionFinalizable
|
||||
func priorityLow() -> ConstraintDescriptionFinalizable
|
||||
}
|
||||
|
||||
/**
|
||||
Used to expose multiplier APIs
|
||||
*/
|
||||
public protocol ConstraintDescriptionMultipliable: ConstraintDescriptionPriortizable {
|
||||
|
||||
func multipliedBy(amount: Float) -> ConstraintDescriptionPriortizable
|
||||
func multipliedBy(amount: Double) -> ConstraintDescriptionPriortizable
|
||||
func multipliedBy(amount: CGFloat) -> ConstraintDescriptionPriortizable
|
||||
func multipliedBy(amount: Int) -> ConstraintDescriptionPriortizable
|
||||
func multipliedBy(amount: UInt) -> ConstraintDescriptionPriortizable
|
||||
|
||||
func dividedBy(amount: Float) -> ConstraintDescriptionPriortizable
|
||||
func dividedBy(amount: Double) -> ConstraintDescriptionPriortizable
|
||||
func dividedBy(amount: CGFloat) -> ConstraintDescriptionPriortizable
|
||||
func dividedBy(amount: Int) -> ConstraintDescriptionPriortizable
|
||||
func dividedBy(amount: UInt) -> ConstraintDescriptionPriortizable
|
||||
}
|
||||
|
||||
/**
|
||||
Used to expose constant APIs
|
||||
*/
|
||||
public protocol ConstraintDescriptionOffsetable: ConstraintDescriptionMultipliable {
|
||||
|
||||
func offset(amount: Float) -> ConstraintDescriptionMultipliable
|
||||
func offset(amount: Double) -> ConstraintDescriptionMultipliable
|
||||
func offset(amount: CGFloat) -> ConstraintDescriptionMultipliable
|
||||
func offset(amount: Int) -> ConstraintDescriptionMultipliable
|
||||
func offset(amount: UInt) -> ConstraintDescriptionMultipliable
|
||||
func offset(amount: CGPoint) -> ConstraintDescriptionMultipliable
|
||||
func offset(amount: CGSize) -> ConstraintDescriptionMultipliable
|
||||
func offset(amount: EdgeInsets) -> ConstraintDescriptionMultipliable
|
||||
|
||||
func insets(amount: EdgeInsets) -> ConstraintDescriptionMultipliable
|
||||
}
|
||||
|
||||
/**
|
||||
Used to expose relation APIs
|
||||
*/
|
||||
public protocol ConstraintDescriptionRelatable: class {
|
||||
|
||||
func equalTo(other: ConstraintItem) -> ConstraintDescriptionOffsetable
|
||||
func equalTo(other: View) -> ConstraintDescriptionOffsetable
|
||||
#if os(iOS)
|
||||
func equalTo(other: UILayoutSupport) -> ConstraintDescriptionOffsetable
|
||||
#endif
|
||||
func equalTo(other: Float) -> ConstraintDescriptionMultipliable
|
||||
func equalTo(other: Double) -> ConstraintDescriptionMultipliable
|
||||
func equalTo(other: CGFloat) -> ConstraintDescriptionMultipliable
|
||||
func equalTo(other: Int) -> ConstraintDescriptionMultipliable
|
||||
func equalTo(other: UInt) -> ConstraintDescriptionMultipliable
|
||||
func equalTo(other: CGSize) -> ConstraintDescriptionMultipliable
|
||||
func equalTo(other: CGPoint) -> ConstraintDescriptionMultipliable
|
||||
func equalTo(other: EdgeInsets) -> ConstraintDescriptionMultipliable
|
||||
|
||||
func lessThanOrEqualTo(other: ConstraintItem) -> ConstraintDescriptionOffsetable
|
||||
func lessThanOrEqualTo(other: View) -> ConstraintDescriptionOffsetable
|
||||
#if os(iOS)
|
||||
func lessThanOrEqualTo(other: UILayoutSupport) -> ConstraintDescriptionOffsetable
|
||||
#endif
|
||||
func lessThanOrEqualTo(other: Float) -> ConstraintDescriptionMultipliable
|
||||
func lessThanOrEqualTo(other: Double) -> ConstraintDescriptionMultipliable
|
||||
func lessThanOrEqualTo(other: CGFloat) -> ConstraintDescriptionMultipliable
|
||||
func lessThanOrEqualTo(other: Int) -> ConstraintDescriptionMultipliable
|
||||
func lessThanOrEqualTo(other: UInt) -> ConstraintDescriptionMultipliable
|
||||
func lessThanOrEqualTo(other: CGSize) -> ConstraintDescriptionMultipliable
|
||||
func lessThanOrEqualTo(other: CGPoint) -> ConstraintDescriptionMultipliable
|
||||
func lessThanOrEqualTo(other: EdgeInsets) -> ConstraintDescriptionMultipliable
|
||||
|
||||
func greaterThanOrEqualTo(other: ConstraintItem) -> ConstraintDescriptionOffsetable
|
||||
func greaterThanOrEqualTo(other: View) -> ConstraintDescriptionOffsetable
|
||||
#if os(iOS)
|
||||
func greaterThanOrEqualTo(other: UILayoutSupport) -> ConstraintDescriptionOffsetable
|
||||
#endif
|
||||
func greaterThanOrEqualTo(other: Float) -> ConstraintDescriptionMultipliable
|
||||
func greaterThanOrEqualTo(other: Double) -> ConstraintDescriptionMultipliable
|
||||
func greaterThanOrEqualTo(other: CGFloat) -> ConstraintDescriptionMultipliable
|
||||
func greaterThanOrEqualTo(other: Int) -> ConstraintDescriptionMultipliable
|
||||
func greaterThanOrEqualTo(other: UInt) -> ConstraintDescriptionMultipliable
|
||||
func greaterThanOrEqualTo(other: CGSize) -> ConstraintDescriptionMultipliable
|
||||
func greaterThanOrEqualTo(other: CGPoint) -> ConstraintDescriptionMultipliable
|
||||
func greaterThanOrEqualTo(other: EdgeInsets) -> ConstraintDescriptionMultipliable
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
Used to expose chaining APIs
|
||||
*/
|
||||
public protocol ConstraintDescriptionExtendable: ConstraintDescriptionRelatable {
|
||||
|
||||
var left: ConstraintDescriptionExtendable { get }
|
||||
var top: ConstraintDescriptionExtendable { get }
|
||||
var bottom: ConstraintDescriptionExtendable { get }
|
||||
var right: ConstraintDescriptionExtendable { get }
|
||||
var leading: ConstraintDescriptionExtendable { get }
|
||||
var trailing: ConstraintDescriptionExtendable { get }
|
||||
var width: ConstraintDescriptionExtendable { get }
|
||||
var height: ConstraintDescriptionExtendable { get }
|
||||
var centerX: ConstraintDescriptionExtendable { get }
|
||||
var centerY: ConstraintDescriptionExtendable { get }
|
||||
var baseline: ConstraintDescriptionExtendable { get }
|
||||
|
||||
#if os(iOS)
|
||||
var firstBaseline: ConstraintDescriptionExtendable { get }
|
||||
var leftMargin: ConstraintDescriptionExtendable { get }
|
||||
var rightMargin: ConstraintDescriptionExtendable { get }
|
||||
var topMargin: ConstraintDescriptionExtendable { get }
|
||||
var bottomMargin: ConstraintDescriptionExtendable { get }
|
||||
var leadingMargin: ConstraintDescriptionExtendable { get }
|
||||
var trailingMargin: ConstraintDescriptionExtendable { get }
|
||||
var centerXWithinMargins: ConstraintDescriptionExtendable { get }
|
||||
var centerYWithinMargins: ConstraintDescriptionExtendable { get }
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
Used to internally manage building constraint
|
||||
*/
|
||||
final internal class ConstraintDescription: ConstraintDescriptionExtendable, ConstraintDescriptionOffsetable, ConstraintDescriptionFinalizable {
|
||||
|
||||
internal var left: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Left) }
|
||||
internal var top: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Top) }
|
||||
internal var right: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Right) }
|
||||
internal var bottom: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Bottom) }
|
||||
internal var leading: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Leading) }
|
||||
internal var trailing: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Trailing) }
|
||||
internal var width: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Width) }
|
||||
internal var height: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Height) }
|
||||
internal var centerX: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterX) }
|
||||
internal var centerY: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterY) }
|
||||
internal var baseline: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Baseline) }
|
||||
|
||||
#if os(iOS)
|
||||
internal var firstBaseline: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.FirstBaseline) }
|
||||
internal var leftMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.LeftMargin) }
|
||||
internal var rightMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.RightMargin) }
|
||||
internal var topMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.TopMargin) }
|
||||
internal var bottomMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.BottomMargin) }
|
||||
internal var leadingMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.LeadingMargin) }
|
||||
internal var trailingMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.TrailingMargin) }
|
||||
internal var centerXWithinMargins: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterXWithinMargins) }
|
||||
internal var centerYWithinMargins: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterYWithinMargins) }
|
||||
#endif
|
||||
|
||||
// MARK: initializer
|
||||
|
||||
init(fromItem: ConstraintItem) {
|
||||
self.fromItem = fromItem
|
||||
self.toItem = ConstraintItem(object: nil, attributes: ConstraintAttributes.None)
|
||||
}
|
||||
|
||||
// MARK: equalTo
|
||||
|
||||
internal func equalTo(other: ConstraintItem) -> ConstraintDescriptionOffsetable {
|
||||
return self.constrainTo(other, relation: .Equal)
|
||||
}
|
||||
internal func equalTo(other: View) -> ConstraintDescriptionOffsetable {
|
||||
return self.constrainTo(other, relation: .Equal)
|
||||
}
|
||||
|
||||
#if os(iOS)
|
||||
internal func equalTo(other: UILayoutSupport) -> ConstraintDescriptionOffsetable {
|
||||
return self.constrainTo(other, relation: .Equal)
|
||||
}
|
||||
#endif
|
||||
internal func equalTo(other: Float) -> ConstraintDescriptionMultipliable {
|
||||
return self.constrainTo(other, relation: .Equal)
|
||||
}
|
||||
internal func equalTo(other: Double) -> ConstraintDescriptionMultipliable {
|
||||
return self.constrainTo(Float(other), relation: .Equal)
|
||||
}
|
||||
internal func equalTo(other: CGFloat) -> ConstraintDescriptionMultipliable {
|
||||
return self.constrainTo(Float(other), relation: .Equal)
|
||||
}
|
||||
internal func equalTo(other: Int) -> ConstraintDescriptionMultipliable {
|
||||
return self.constrainTo(Float(other), relation: .Equal)
|
||||
}
|
||||
internal func equalTo(other: UInt) -> ConstraintDescriptionMultipliable {
|
||||
return self.constrainTo(Float(other), relation: .Equal)
|
||||
}
|
||||
internal func equalTo(other: CGSize) -> ConstraintDescriptionMultipliable {
|
||||
return self.constrainTo(other, relation: .Equal)
|
||||
}
|
||||
internal func equalTo(other: CGPoint) -> ConstraintDescriptionMultipliable {
|
||||
return self.constrainTo(other, relation: .Equal)
|
||||
}
|
||||
internal func equalTo(other: EdgeInsets) -> ConstraintDescriptionMultipliable {
|
||||
return self.constrainTo(other, relation: .Equal)
|
||||
}
|
||||
|
||||
// MARK: lessThanOrEqualTo
|
||||
|
||||
internal func lessThanOrEqualTo(other: ConstraintItem) -> ConstraintDescriptionOffsetable {
|
||||
return self.constrainTo(other, relation: .LessThanOrEqualTo)
|
||||
}
|
||||
internal func lessThanOrEqualTo(other: View) -> ConstraintDescriptionOffsetable {
|
||||
return self.constrainTo(other, relation: .LessThanOrEqualTo)
|
||||
}
|
||||
#if os(iOS)
|
||||
internal func lessThanOrEqualTo(other: UILayoutSupport) -> ConstraintDescriptionOffsetable {
|
||||
return self.constrainTo(other, relation: .LessThanOrEqualTo)
|
||||
}
|
||||
#endif
|
||||
internal func lessThanOrEqualTo(other: Float) -> ConstraintDescriptionMultipliable {
|
||||
return self.constrainTo(other, relation: .LessThanOrEqualTo)
|
||||
}
|
||||
internal func lessThanOrEqualTo(other: Double) -> ConstraintDescriptionMultipliable {
|
||||
return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
|
||||
}
|
||||
internal func lessThanOrEqualTo(other: CGFloat) -> ConstraintDescriptionMultipliable {
|
||||
return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
|
||||
}
|
||||
internal func lessThanOrEqualTo(other: Int) -> ConstraintDescriptionMultipliable {
|
||||
return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
|
||||
}
|
||||
internal func lessThanOrEqualTo(other: UInt) -> ConstraintDescriptionMultipliable {
|
||||
return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
|
||||
}
|
||||
internal func lessThanOrEqualTo(other: CGSize) -> ConstraintDescriptionMultipliable {
|
||||
return self.constrainTo(other, relation: .LessThanOrEqualTo)
|
||||
}
|
||||
internal func lessThanOrEqualTo(other: CGPoint) -> ConstraintDescriptionMultipliable {
|
||||
return self.constrainTo(other, relation: .LessThanOrEqualTo)
|
||||
}
|
||||
internal func lessThanOrEqualTo(other: EdgeInsets) -> ConstraintDescriptionMultipliable {
|
||||
return self.constrainTo(other, relation: .LessThanOrEqualTo)
|
||||
}
|
||||
|
||||
// MARK: greaterThanOrEqualTo
|
||||
|
||||
internal func greaterThanOrEqualTo(other: ConstraintItem) -> ConstraintDescriptionOffsetable {
|
||||
return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
|
||||
}
|
||||
internal func greaterThanOrEqualTo(other: View) -> ConstraintDescriptionOffsetable {
|
||||
return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
|
||||
}
|
||||
#if os(iOS)
|
||||
internal func greaterThanOrEqualTo(other: UILayoutSupport) -> ConstraintDescriptionOffsetable {
|
||||
return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
|
||||
}
|
||||
#endif
|
||||
internal func greaterThanOrEqualTo(other: Float) -> ConstraintDescriptionMultipliable {
|
||||
return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
|
||||
}
|
||||
internal func greaterThanOrEqualTo(other: Double) -> ConstraintDescriptionMultipliable {
|
||||
return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
|
||||
}
|
||||
internal func greaterThanOrEqualTo(other: CGFloat) -> ConstraintDescriptionMultipliable {
|
||||
return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
|
||||
}
|
||||
internal func greaterThanOrEqualTo(other: Int) -> ConstraintDescriptionMultipliable {
|
||||
return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
|
||||
}
|
||||
internal func greaterThanOrEqualTo(other: UInt) -> ConstraintDescriptionMultipliable {
|
||||
return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
|
||||
}
|
||||
internal func greaterThanOrEqualTo(other: CGSize) -> ConstraintDescriptionMultipliable {
|
||||
return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
|
||||
}
|
||||
internal func greaterThanOrEqualTo(other: CGPoint) -> ConstraintDescriptionMultipliable {
|
||||
return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
|
||||
}
|
||||
internal func greaterThanOrEqualTo(other: EdgeInsets) -> ConstraintDescriptionMultipliable {
|
||||
return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
|
||||
}
|
||||
|
||||
// MARK: multiplier
|
||||
|
||||
internal func multipliedBy(amount: Float) -> ConstraintDescriptionPriortizable {
|
||||
self.multiplier = amount
|
||||
return self
|
||||
}
|
||||
internal func multipliedBy(amount: Double) -> ConstraintDescriptionPriortizable {
|
||||
return self.multipliedBy(Float(amount))
|
||||
}
|
||||
internal func multipliedBy(amount: CGFloat) -> ConstraintDescriptionPriortizable {
|
||||
return self.multipliedBy(Float(amount))
|
||||
}
|
||||
internal func multipliedBy(amount: Int) -> ConstraintDescriptionPriortizable {
|
||||
return self.multipliedBy(Float(amount))
|
||||
}
|
||||
internal func multipliedBy(amount: UInt) -> ConstraintDescriptionPriortizable {
|
||||
return self.multipliedBy(Float(amount))
|
||||
}
|
||||
|
||||
internal func dividedBy(amount: Float) -> ConstraintDescriptionPriortizable {
|
||||
self.multiplier = 1.0 / amount;
|
||||
return self
|
||||
}
|
||||
internal func dividedBy(amount: Double) -> ConstraintDescriptionPriortizable {
|
||||
return self.dividedBy(Float(amount))
|
||||
}
|
||||
internal func dividedBy(amount: CGFloat) -> ConstraintDescriptionPriortizable {
|
||||
return self.dividedBy(Float(amount))
|
||||
}
|
||||
internal func dividedBy(amount: Int) -> ConstraintDescriptionPriortizable {
|
||||
return self.dividedBy(Float(amount))
|
||||
}
|
||||
internal func dividedBy(amount: UInt) -> ConstraintDescriptionPriortizable {
|
||||
return self.dividedBy(Float(amount))
|
||||
}
|
||||
|
||||
// MARK: priority
|
||||
|
||||
internal func priority(priority: Float) -> ConstraintDescriptionFinalizable {
|
||||
self.priority = priority
|
||||
return self
|
||||
}
|
||||
internal func priority(priority: Double) -> ConstraintDescriptionFinalizable {
|
||||
return self.priority(Float(priority))
|
||||
}
|
||||
internal func priority(priority: CGFloat) -> ConstraintDescriptionFinalizable {
|
||||
return self.priority(Float(priority))
|
||||
}
|
||||
func priority(priority: UInt) -> ConstraintDescriptionFinalizable {
|
||||
return self.priority(Float(priority))
|
||||
}
|
||||
internal func priority(priority: Int) -> ConstraintDescriptionFinalizable {
|
||||
return self.priority(Float(priority))
|
||||
}
|
||||
internal func priorityRequired() -> ConstraintDescriptionFinalizable {
|
||||
return self.priority(1000.0)
|
||||
}
|
||||
internal func priorityHigh() -> ConstraintDescriptionFinalizable {
|
||||
return self.priority(750.0)
|
||||
}
|
||||
internal func priorityMedium() -> ConstraintDescriptionFinalizable {
|
||||
#if os(iOS)
|
||||
return self.priority(500.0)
|
||||
#else
|
||||
return self.priority(501.0)
|
||||
#endif
|
||||
}
|
||||
internal func priorityLow() -> ConstraintDescriptionFinalizable {
|
||||
return self.priority(250.0)
|
||||
}
|
||||
|
||||
// MARK: offset
|
||||
|
||||
internal func offset(amount: Float) -> ConstraintDescriptionMultipliable {
|
||||
self.constant = amount
|
||||
return self
|
||||
}
|
||||
internal func offset(amount: Double) -> ConstraintDescriptionMultipliable {
|
||||
return self.offset(Float(amount))
|
||||
}
|
||||
internal func offset(amount: CGFloat) -> ConstraintDescriptionMultipliable {
|
||||
return self.offset(Float(amount))
|
||||
}
|
||||
internal func offset(amount: Int) -> ConstraintDescriptionMultipliable {
|
||||
return self.offset(Float(amount))
|
||||
}
|
||||
internal func offset(amount: UInt) -> ConstraintDescriptionMultipliable {
|
||||
return self.offset(Float(amount))
|
||||
}
|
||||
internal func offset(amount: CGPoint) -> ConstraintDescriptionMultipliable {
|
||||
self.constant = amount
|
||||
return self
|
||||
}
|
||||
internal func offset(amount: CGSize) -> ConstraintDescriptionMultipliable {
|
||||
self.constant = amount
|
||||
return self
|
||||
}
|
||||
internal func offset(amount: EdgeInsets) -> ConstraintDescriptionMultipliable {
|
||||
self.constant = amount
|
||||
return self
|
||||
}
|
||||
|
||||
// MARK: insets
|
||||
|
||||
internal func insets(amount: EdgeInsets) -> ConstraintDescriptionMultipliable {
|
||||
self.constant = EdgeInsets(top: amount.top, left: amount.left, bottom: -amount.bottom, right: -amount.right)
|
||||
return self
|
||||
}
|
||||
|
||||
// MARK: Constraint
|
||||
|
||||
internal var constraint: Constraint {
|
||||
if self.createdConstraint == nil {
|
||||
if self.relation == nil {
|
||||
fatalError("Attempting to create a constraint from a ConstraintDescription before it has been fully chained.")
|
||||
}
|
||||
self.createdConstraint = Constraint(
|
||||
fromItem: self.fromItem,
|
||||
toItem: self.toItem,
|
||||
relation: self.relation!,
|
||||
constant: self.constant,
|
||||
multiplier: self.multiplier,
|
||||
priority: self.priority)
|
||||
}
|
||||
return self.createdConstraint!
|
||||
}
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private let fromItem: ConstraintItem
|
||||
private var toItem: ConstraintItem {
|
||||
willSet {
|
||||
if self.createdConstraint != nil {
|
||||
fatalError("Attempting to modify a ConstraintDescription after it’s constraint has been created.")
|
||||
}
|
||||
}
|
||||
}
|
||||
private var relation: ConstraintRelation? {
|
||||
willSet {
|
||||
if self.createdConstraint != nil {
|
||||
fatalError("Attempting to modify a ConstraintDescription after it’s constraint has been created.")
|
||||
}
|
||||
}
|
||||
}
|
||||
private var constant: Any = Float(0.0) {
|
||||
willSet {
|
||||
if self.createdConstraint != nil {
|
||||
fatalError("Attempting to modify a ConstraintDescription after it’s constraint has been created.")
|
||||
}
|
||||
}
|
||||
}
|
||||
private var multiplier: Float = 1.0 {
|
||||
willSet {
|
||||
if self.createdConstraint != nil {
|
||||
fatalError("Attempting to modify a ConstraintDescription after it’s constraint has been created.")
|
||||
}
|
||||
}
|
||||
}
|
||||
private var priority: Float = 1000.0 {
|
||||
willSet {
|
||||
if self.createdConstraint != nil {
|
||||
fatalError("Attempting to modify a ConstraintDescription after it’s constraint has been created.")
|
||||
}
|
||||
}
|
||||
}
|
||||
private var createdConstraint: Constraint? = nil
|
||||
|
||||
private func addConstraint(attributes: ConstraintAttributes) -> ConstraintDescription {
|
||||
if self.relation == nil {
|
||||
self.fromItem.attributes += attributes
|
||||
}
|
||||
return self
|
||||
}
|
||||
|
||||
private func constrainTo(other: ConstraintItem, relation: ConstraintRelation) -> ConstraintDescription {
|
||||
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) -> ConstraintDescription {
|
||||
return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
|
||||
}
|
||||
|
||||
#if os(iOS)
|
||||
|
||||
private func constrainTo(other: UILayoutSupport, relation: ConstraintRelation) -> ConstraintDescription {
|
||||
return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
private func constrainTo(other: Float, relation: ConstraintRelation) -> ConstraintDescription {
|
||||
self.constant = other
|
||||
return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
|
||||
}
|
||||
|
||||
private func constrainTo(other: Double, relation: ConstraintRelation) -> ConstraintDescription {
|
||||
self.constant = other
|
||||
return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
|
||||
}
|
||||
|
||||
private func constrainTo(other: CGSize, relation: ConstraintRelation) -> ConstraintDescription {
|
||||
self.constant = other
|
||||
return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
|
||||
}
|
||||
|
||||
private func constrainTo(other: CGPoint, relation: ConstraintRelation) -> ConstraintDescription {
|
||||
self.constant = other
|
||||
return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
|
||||
}
|
||||
|
||||
private func constrainTo(other: EdgeInsets, relation: ConstraintRelation) -> ConstraintDescription {
|
||||
self.constant = other
|
||||
return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
|
||||
}
|
||||
}
|
|
@ -28,8 +28,8 @@ import AppKit
|
|||
#endif
|
||||
|
||||
/**
|
||||
* ConstraintItem is a class that is used while building constraints.
|
||||
*/
|
||||
Used to assist in building a constraint
|
||||
*/
|
||||
final public class ConstraintItem {
|
||||
|
||||
internal init(object: AnyObject?, attributes: ConstraintAttributes) {
|
||||
|
@ -45,9 +45,11 @@ final public class ConstraintItem {
|
|||
}
|
||||
|
||||
#if os(iOS)
|
||||
|
||||
internal var layoutSupport: UILayoutSupport? {
|
||||
return self.object as? UILayoutSupport
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
|
@ -28,95 +28,155 @@ import AppKit
|
|||
#endif
|
||||
|
||||
/**
|
||||
* ConstraintMaker is the maker in snap that gets all constraints kickstarted
|
||||
*/
|
||||
Used to make constraints
|
||||
*/
|
||||
final public class ConstraintMaker {
|
||||
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) }
|
||||
|
||||
/// left edge
|
||||
public var left: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Left) }
|
||||
|
||||
/// top edge
|
||||
public var top: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Top) }
|
||||
|
||||
/// right edge
|
||||
public var right: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Right) }
|
||||
|
||||
/// bottom edge
|
||||
public var bottom: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Bottom) }
|
||||
|
||||
/// leading edge
|
||||
public var leading: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Leading) }
|
||||
|
||||
/// trailing edge
|
||||
public var trailing: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Trailing) }
|
||||
|
||||
/// width dimension
|
||||
public var width: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Width) }
|
||||
|
||||
/// height dimension
|
||||
public var height: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Height) }
|
||||
|
||||
/// centerX dimension
|
||||
public var centerX: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.CenterX) }
|
||||
|
||||
/// centerY dimension
|
||||
public var centerY: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.CenterY) }
|
||||
|
||||
/// baseline position
|
||||
public var baseline: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Baseline) }
|
||||
|
||||
#if os(iOS)
|
||||
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) }
|
||||
|
||||
/// firse baseline position
|
||||
public var firstBaseline: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.FirstBaseline) }
|
||||
|
||||
/// left margin
|
||||
public var leftMargin: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.LeftMargin) }
|
||||
|
||||
/// right margin
|
||||
public var rightMargin: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.RightMargin) }
|
||||
|
||||
/// top margin
|
||||
public var topMargin: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.TopMargin) }
|
||||
|
||||
/// bottom margin
|
||||
public var bottomMargin: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.BottomMargin) }
|
||||
|
||||
/// leading margin
|
||||
public var leadingMargin: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.LeadingMargin) }
|
||||
|
||||
/// trailing margin
|
||||
public var trailingMargin: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.TrailingMargin) }
|
||||
|
||||
/// centerX within margins
|
||||
public var centerXWithinMargins: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.CenterXWithinMargins) }
|
||||
|
||||
/// centerY within margins
|
||||
public var centerYWithinMargins: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.CenterYWithinMargins) }
|
||||
#endif
|
||||
|
||||
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) }
|
||||
/// top + left + bottom + right edges
|
||||
public var edges: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Edges) }
|
||||
|
||||
init(view: View) {
|
||||
/// width + height dimensions
|
||||
public var size: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Size) }
|
||||
|
||||
// centerX + centerY positions
|
||||
public var center: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Center) }
|
||||
|
||||
#if os(iOS)
|
||||
|
||||
// top + left + bottom + right margins
|
||||
public var snp_margins: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.Margins) }
|
||||
|
||||
// centerX + centerY within margins
|
||||
public var snp_centerWithinMargins: ConstraintDescriptionExtendable { return self.makeConstraintDescription(ConstraintAttributes.CenterWithinMargins) }
|
||||
|
||||
#endif
|
||||
|
||||
internal init(view: View) {
|
||||
self.view = view
|
||||
}
|
||||
|
||||
internal weak var view: View?
|
||||
internal var constraints = [ConstraintBuilder]()
|
||||
internal var constraintDescriptions = [ConstraintDescription]()
|
||||
|
||||
internal func addConstraint(attributes: ConstraintAttributes) -> ConstraintBuilder {
|
||||
internal func makeConstraintDescription(attributes: ConstraintAttributes) -> ConstraintDescription {
|
||||
let item = ConstraintItem(object: self.view, attributes: attributes)
|
||||
let constraint = ConstraintBuilder(fromItem: item)
|
||||
self.constraints.append(constraint)
|
||||
return constraint
|
||||
let constraintDescription = ConstraintDescription(fromItem: item)
|
||||
self.constraintDescriptions.append(constraintDescription)
|
||||
return constraintDescription
|
||||
}
|
||||
|
||||
internal class func prepareConstraints(view: View, @noescape block: (make: ConstraintMaker) -> Void) -> [ConstraintBuilder] {
|
||||
internal class func prepareConstraints(view: View, @noescape closure: (make: ConstraintMaker) -> Void) -> [Constraint] {
|
||||
let maker = ConstraintMaker(view: view)
|
||||
block(make: maker)
|
||||
return maker.constraints
|
||||
closure(make: maker)
|
||||
|
||||
return maker.constraintDescriptions.map { $0.constraint }
|
||||
}
|
||||
|
||||
internal class func makeConstraints(view: View, @noescape block: (make: ConstraintMaker) -> Void) {
|
||||
internal class func makeConstraints(view: View, @noescape closure: (make: ConstraintMaker) -> Void) {
|
||||
#if os(iOS)
|
||||
view.setTranslatesAutoresizingMaskIntoConstraints(false)
|
||||
#else
|
||||
view.translatesAutoresizingMaskIntoConstraints = false
|
||||
#endif
|
||||
let maker = ConstraintMaker(view: view)
|
||||
block(make: maker)
|
||||
for constraint in maker.constraints {
|
||||
closure(make: maker)
|
||||
|
||||
let constraints = maker.constraintDescriptions.map { $0.constraint }
|
||||
for constraint in constraints {
|
||||
constraint.installOnView(updateExisting: false)
|
||||
}
|
||||
}
|
||||
|
||||
internal class func remakeConstraints(view: View, @noescape block: (make: ConstraintMaker) -> Void) {
|
||||
internal class func remakeConstraints(view: View, @noescape closure: (make: ConstraintMaker) -> Void) {
|
||||
#if os(iOS)
|
||||
view.setTranslatesAutoresizingMaskIntoConstraints(false)
|
||||
#else
|
||||
view.translatesAutoresizingMaskIntoConstraints = false
|
||||
#endif
|
||||
let maker = ConstraintMaker(view: view)
|
||||
block(make: maker)
|
||||
closure(make: maker)
|
||||
|
||||
self.removeConstraints(view)
|
||||
for constraint in maker.constraints {
|
||||
let constraints = maker.constraintDescriptions.map { $0.constraint }
|
||||
for constraint in constraints {
|
||||
constraint.installOnView(updateExisting: false)
|
||||
}
|
||||
}
|
||||
|
||||
internal class func updateConstraints(view: View, @noescape block: (make: ConstraintMaker) -> Void) {
|
||||
internal class func updateConstraints(view: View, @noescape closure: (make: ConstraintMaker) -> Void) {
|
||||
#if os(iOS)
|
||||
view.setTranslatesAutoresizingMaskIntoConstraints(false)
|
||||
#else
|
||||
view.translatesAutoresizingMaskIntoConstraints = false
|
||||
#endif
|
||||
let maker = ConstraintMaker(view: view)
|
||||
block(make: maker)
|
||||
closure(make: maker)
|
||||
|
||||
for constraint in maker.constraints {
|
||||
let constraints = maker.constraintDescriptions.map { $0.constraint }
|
||||
for constraint in constraints {
|
||||
constraint.installOnView(updateExisting: true)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,8 +28,8 @@ import AppKit
|
|||
#endif
|
||||
|
||||
/**
|
||||
* ConstraintRelation is an Int enum that maps to NSLayoutRelation.
|
||||
*/
|
||||
Used to define `NSLayoutRelation`
|
||||
*/
|
||||
internal enum ConstraintRelation: Int {
|
||||
case Equal = 1, LessThanOrEqualTo, GreaterThanOrEqualTo
|
||||
|
||||
|
|
|
@ -28,8 +28,8 @@ import AppKit
|
|||
#endif
|
||||
|
||||
/**
|
||||
* View extension that exposes snp_label debugging api
|
||||
*/
|
||||
Used to allow adding a snp_label to a View for debugging purposes
|
||||
*/
|
||||
public extension View {
|
||||
|
||||
public var snp_label: String? {
|
||||
|
@ -44,8 +44,8 @@ public extension View {
|
|||
}
|
||||
|
||||
/**
|
||||
* LayoutConstraint extension that exposes snp_label debugging api
|
||||
*/
|
||||
Used to allow adding a snp_label to a LayoutConstraint for debugging purposes
|
||||
*/
|
||||
public extension LayoutConstraint {
|
||||
|
||||
public var snp_label: String? {
|
||||
|
|
|
@ -31,7 +31,7 @@ public let EdgeInsetsZero = EdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
|
|||
#else
|
||||
import AppKit
|
||||
public struct EdgeInsets {
|
||||
public var top: CGFloat // specify amount to inset (positive) for each of the edges. values can be negative to 'outset'
|
||||
public var top: CGFloat
|
||||
public var left: CGFloat
|
||||
public var bottom: CGFloat
|
||||
public var right: CGFloat
|
||||
|
|
|
@ -28,13 +28,15 @@ import AppKit
|
|||
#endif
|
||||
|
||||
/**
|
||||
* LayoutConstraint is a subclass of NSLayoutConstraint to assist Snap and also provide better debugging
|
||||
Used to add extra information to the actual `NSLayoutConstraint`'s that will UIKit/AppKit will utilize
|
||||
*/
|
||||
final public class LayoutConstraint: NSLayoutConstraint {
|
||||
internal var snp_constraint: ConstraintBuilder? = nil
|
||||
|
||||
internal var snp_constraint: Constraint? = nil
|
||||
|
||||
}
|
||||
|
||||
public func ==(left: LayoutConstraint, right: LayoutConstraint) -> Bool {
|
||||
internal func ==(left: LayoutConstraint, right: LayoutConstraint) -> Bool {
|
||||
if left.firstItem !== right.firstItem {
|
||||
return false
|
||||
}
|
||||
|
|
|
@ -23,17 +23,18 @@
|
|||
|
||||
#if os(iOS)
|
||||
import UIKit
|
||||
typealias InterfaceLayoutDirection = UIUserInterfaceLayoutDirection
|
||||
public typealias InterfaceLayoutDirection = UIUserInterfaceLayoutDirection
|
||||
#else
|
||||
import AppKit
|
||||
typealias InterfaceLayoutDirection = NSUserInterfaceLayoutDirection
|
||||
public typealias InterfaceLayoutDirection = NSUserInterfaceLayoutDirection
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Config that can be used to configure Snap
|
||||
*/
|
||||
Used to configure different parts of Snap
|
||||
*/
|
||||
public struct Config {
|
||||
|
||||
/// The interface layout direction
|
||||
public static var interfaceLayoutDirection = InterfaceLayoutDirection.LeftToRight
|
||||
|
||||
}
|
|
@ -30,69 +30,140 @@ public typealias View = NSView
|
|||
#endif
|
||||
|
||||
/**
|
||||
* View extension that exposes primary api
|
||||
*/
|
||||
Used to expose public API on views
|
||||
*/
|
||||
public extension View {
|
||||
|
||||
// normal
|
||||
|
||||
/// left edge
|
||||
final public var snp_left: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Left) }
|
||||
|
||||
/// top edge
|
||||
final public var snp_top: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Top) }
|
||||
|
||||
/// right edge
|
||||
final public var snp_right: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Right) }
|
||||
|
||||
/// bottom edge
|
||||
final public var snp_bottom: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Bottom) }
|
||||
|
||||
/// leading edge
|
||||
final public var snp_leading: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Leading) }
|
||||
|
||||
/// trailing edge
|
||||
final public var snp_trailing: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Trailing) }
|
||||
|
||||
/// width dimension
|
||||
final public var snp_width: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Width) }
|
||||
|
||||
/// height dimension
|
||||
final public var snp_height: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Height) }
|
||||
|
||||
/// centerX position
|
||||
final public var snp_centerX: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterX) }
|
||||
|
||||
/// centerY position
|
||||
final public var snp_centerY: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterY) }
|
||||
|
||||
/// baseline position
|
||||
final public var snp_baseline: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Baseline) }
|
||||
|
||||
#if os(iOS)
|
||||
|
||||
/// first baseline position
|
||||
final public var snp_firstBaseline: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.FirstBaseline) }
|
||||
|
||||
/// left margin
|
||||
final public var snp_leftMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.LeftMargin) }
|
||||
|
||||
/// right margin
|
||||
final public var snp_rightMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.RightMargin) }
|
||||
|
||||
/// top margin
|
||||
final public var snp_topMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.TopMargin) }
|
||||
|
||||
/// bottom margin
|
||||
final public var snp_bottomMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.BottomMargin) }
|
||||
|
||||
/// leading margin
|
||||
final public var snp_leadingMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.LeadingMargin) }
|
||||
|
||||
/// trailing margin
|
||||
final public var snp_trailingMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.TrailingMargin) }
|
||||
final public var snp_centerXWithinMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterXWithinMargins) }
|
||||
final public var snp_centerYWithinMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterYWithinMargins) }
|
||||
|
||||
/// centerX within margins
|
||||
final public var snp_centerXWithinMargins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterXWithinMargins) }
|
||||
|
||||
/// centerY within margins
|
||||
final public var snp_centerYWithinMargins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterYWithinMargins) }
|
||||
|
||||
#endif
|
||||
|
||||
// aggregates
|
||||
|
||||
// top + left + bottom + right edges
|
||||
final public var snp_edges: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Edges) }
|
||||
|
||||
// width + height dimensions
|
||||
final public var snp_size: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Size) }
|
||||
|
||||
// centerX + centerY positions
|
||||
final public var snp_center: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Center) }
|
||||
|
||||
#if os(iOS)
|
||||
|
||||
// top + left + bottom + right margins
|
||||
final public var snp_margins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Margins) }
|
||||
|
||||
// centerX + centerY within margins
|
||||
final public var snp_centerWithinMargins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterWithinMargins) }
|
||||
|
||||
#endif
|
||||
|
||||
final public func snp_prepareConstraints(@noescape block: (make: ConstraintMaker) -> Void) -> [Constraint] {
|
||||
return ConstraintMaker.prepareConstraints(self, block: block).map { return $0 as Constraint }
|
||||
/**
|
||||
Prepares constraints with a `ConstraintMaker` and returns the made constraints but does not install them.
|
||||
|
||||
:param: closure that will be passed the `ConstraintMaker` to make the constraints with
|
||||
|
||||
:returns: the constraints made
|
||||
*/
|
||||
final public func snp_prepareConstraints(@noescape closure: (make: ConstraintMaker) -> Void) -> [Constraint] {
|
||||
return ConstraintMaker.prepareConstraints(self, closure: closure)
|
||||
}
|
||||
|
||||
final public func snp_makeConstraints(@noescape block: (make: ConstraintMaker) -> Void) {
|
||||
ConstraintMaker.makeConstraints(self, block: block)
|
||||
/**
|
||||
Makes constraints with a `ConstraintMaker` and installs them along side any previous made constraints.
|
||||
|
||||
:param: closure that will be passed the `ConstraintMaker` to make the constraints with
|
||||
*/
|
||||
final public func snp_makeConstraints(@noescape closure: (make: ConstraintMaker) -> Void) -> Void {
|
||||
ConstraintMaker.makeConstraints(self, closure: closure)
|
||||
}
|
||||
|
||||
final public func snp_updateConstraints(@noescape block: (make: ConstraintMaker) -> Void) {
|
||||
ConstraintMaker.updateConstraints(self, block: block)
|
||||
/**
|
||||
Updates constraints with a `ConstraintMaker` that will replace existing constraints that match and install new ones.
|
||||
|
||||
For constraints to match only the constant can be updated.
|
||||
|
||||
:param: closure that will be passed the `ConstraintMaker` to update the constraints with
|
||||
*/
|
||||
final public func snp_updateConstraints(@noescape closure: (make: ConstraintMaker) -> Void) -> Void {
|
||||
ConstraintMaker.updateConstraints(self, closure: closure)
|
||||
}
|
||||
|
||||
final public func snp_remakeConstraints(@noescape block: (make: ConstraintMaker) -> Void) {
|
||||
ConstraintMaker.remakeConstraints(self, block: block)
|
||||
/**
|
||||
Remakes constraints with a `ConstraintMaker` that will first remove all previously made constraints and make and install new ones.
|
||||
|
||||
:param: closure that will be passed the `ConstraintMaker` to remake the constraints with
|
||||
*/
|
||||
final public func snp_remakeConstraints(@noescape closure: (make: ConstraintMaker) -> Void) -> Void {
|
||||
ConstraintMaker.remakeConstraints(self, closure: closure)
|
||||
}
|
||||
|
||||
/**
|
||||
Removes all previously made constraints.
|
||||
*/
|
||||
final public func snp_removeConstraints() {
|
||||
ConstraintMaker.removeConstraints(self)
|
||||
}
|
||||
|
||||
// internal
|
||||
|
||||
final internal var snp_installedLayoutConstraints: [LayoutConstraint] {
|
||||
get {
|
||||
if let constraints = objc_getAssociatedObject(self, &installedLayoutConstraintsKey) as? [LayoutConstraint] {
|
||||
|
|
|
@ -156,6 +156,7 @@ class SnapTests: XCTestCase {
|
|||
return
|
||||
}
|
||||
|
||||
|
||||
XCTAssertEqual(self.container.snp_constraints.count, 0, "Should have 0 constraints installed")
|
||||
|
||||
for constraint in constraints {
|
||||
|
|
Loading…
Reference in New Issue