2014-09-23 12:28:00 +08:00
|
|
|
//
|
2015-01-09 04:43:43 +08:00
|
|
|
// Snap
|
2014-09-23 12:28:00 +08:00
|
|
|
//
|
|
|
|
// Copyright (c) 2011-2014 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
|
|
|
|
public typealias View = UIView
|
|
|
|
#else
|
|
|
|
import AppKit
|
|
|
|
public typealias View = NSView
|
|
|
|
#endif
|
|
|
|
|
|
|
|
public extension View {
|
2015-01-08 14:04:29 +08:00
|
|
|
|
|
|
|
// normal
|
|
|
|
|
2015-04-11 13:13:02 +08:00
|
|
|
final public var snp_left: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Left) }
|
|
|
|
final public var snp_top: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Top) }
|
|
|
|
final public var snp_right: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Right) }
|
|
|
|
final public var snp_bottom: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Bottom) }
|
|
|
|
final public var snp_leading: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Leading) }
|
|
|
|
final public var snp_trailing: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Trailing) }
|
|
|
|
final public var snp_width: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Width) }
|
|
|
|
final public var snp_height: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Height) }
|
|
|
|
final public var snp_centerX: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterX) }
|
|
|
|
final public var snp_centerY: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterY) }
|
|
|
|
final public var snp_baseline: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Baseline) }
|
2014-09-23 12:28:00 +08:00
|
|
|
|
2015-01-08 14:04:29 +08:00
|
|
|
#if os(iOS)
|
2015-04-11 13:13:02 +08:00
|
|
|
final public var snp_firstBaseline: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.FirstBaseline) }
|
|
|
|
final public var snp_leftMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.LeftMargin) }
|
|
|
|
final public var snp_rightMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.RightMargin) }
|
|
|
|
final public var snp_topMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.TopMargin) }
|
|
|
|
final public var snp_bottomMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.BottomMargin) }
|
|
|
|
final public var snp_leadingMargin: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.LeadingMargin) }
|
|
|
|
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) }
|
2015-01-08 14:04:29 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// aggregates
|
2014-10-29 05:30:07 +08:00
|
|
|
|
2015-04-11 13:13:02 +08:00
|
|
|
final public var snp_edges: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Edges) }
|
|
|
|
final public var snp_size: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Size) }
|
|
|
|
final public var snp_center: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Center) }
|
2014-09-23 12:28:00 +08:00
|
|
|
|
2015-01-08 14:04:29 +08:00
|
|
|
#if os(iOS)
|
2015-04-11 13:13:02 +08:00
|
|
|
final public var snp_margins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.Margins) }
|
|
|
|
final public var snp_centerWithinMargins: ConstraintItem { return ConstraintItem(object: self, attributes: ConstraintAttributes.CenterWithinMargins) }
|
2015-01-08 14:04:29 +08:00
|
|
|
#endif
|
2014-10-29 05:30:07 +08:00
|
|
|
|
2015-04-11 13:14:52 +08:00
|
|
|
final public func snp_prepareConstraints(@noescape block: (make: ConstraintMaker) -> Void) -> [Constraint] {
|
2015-02-17 06:11:58 +08:00
|
|
|
return ConstraintMaker.prepareConstraints(self, block: block)
|
|
|
|
}
|
|
|
|
|
2015-04-11 13:14:52 +08:00
|
|
|
final public func snp_makeConstraints(@noescape block: (make: ConstraintMaker) -> Void) {
|
2014-09-23 12:28:00 +08:00
|
|
|
ConstraintMaker.makeConstraints(self, block: block)
|
|
|
|
}
|
|
|
|
|
2015-04-11 13:14:52 +08:00
|
|
|
final public func snp_updateConstraints(@noescape block: (make: ConstraintMaker) -> Void) {
|
2015-01-08 14:04:29 +08:00
|
|
|
ConstraintMaker.updateConstraints(self, block: block)
|
|
|
|
}
|
|
|
|
|
2015-04-11 13:14:52 +08:00
|
|
|
final public func snp_remakeConstraints(@noescape block: (make: ConstraintMaker) -> Void) {
|
2014-09-23 12:28:00 +08:00
|
|
|
ConstraintMaker.remakeConstraints(self, block: block)
|
|
|
|
}
|
2014-10-06 08:32:41 +08:00
|
|
|
|
2015-04-11 13:13:02 +08:00
|
|
|
final public func snp_removeConstraints() {
|
2014-12-31 07:47:31 +08:00
|
|
|
ConstraintMaker.removeConstraints(self)
|
2014-10-06 08:32:41 +08:00
|
|
|
}
|
2015-01-08 14:04:29 +08:00
|
|
|
|
|
|
|
// internal
|
|
|
|
|
2015-04-11 13:13:02 +08:00
|
|
|
final internal var snp_installedLayoutConstraints: [LayoutConstraint] {
|
2015-01-08 14:04:29 +08:00
|
|
|
get {
|
2015-04-11 13:13:02 +08:00
|
|
|
if let constraints = objc_getAssociatedObject(self, &installedLayoutConstraintsKey) as? [LayoutConstraint] {
|
2015-02-14 18:37:27 +08:00
|
|
|
return constraints
|
2015-01-08 14:04:29 +08:00
|
|
|
}
|
2015-04-11 13:13:02 +08:00
|
|
|
return []
|
2015-01-08 14:04:29 +08:00
|
|
|
}
|
|
|
|
set {
|
|
|
|
objc_setAssociatedObject(self, &installedLayoutConstraintsKey, newValue, UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-11 13:14:52 +08:00
|
|
|
private var installedLayoutConstraintsKey = ""
|