2014-09-23 12:28:00 +08:00
|
|
|
//
|
2015-04-15 19:07:50 +08:00
|
|
|
// SnapKit
|
2014-09-23 12:28:00 +08:00
|
|
|
//
|
2016-01-26 18:49:04 +08:00
|
|
|
// Copyright (c) 2011-Present SnapKit Team - https://github.com/SnapKit
|
2014-09-23 12:28:00 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2015-09-25 12:37:33 +08:00
|
|
|
#if os(iOS) || os(tvOS)
|
2016-01-26 18:49:04 +08:00
|
|
|
import UIKit
|
2014-09-23 12:28:00 +08:00
|
|
|
#else
|
2016-01-26 18:49:04 +08:00
|
|
|
import AppKit
|
2014-09-23 12:28:00 +08:00
|
|
|
#endif
|
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
|
2018-06-23 19:14:50 +08:00
|
|
|
internal struct ConstraintAttributes : OptionSet, ExpressibleByIntegerLiteral {
|
|
|
|
|
|
|
|
typealias IntegerLiteralType = UInt
|
2014-09-23 12:28:00 +08:00
|
|
|
|
|
|
|
internal init(rawValue: UInt) {
|
|
|
|
self.rawValue = rawValue
|
|
|
|
}
|
|
|
|
internal init(_ rawValue: UInt) {
|
|
|
|
self.init(rawValue: rawValue)
|
|
|
|
}
|
2014-10-06 07:29:19 +08:00
|
|
|
internal init(nilLiteral: ()) {
|
|
|
|
self.rawValue = 0
|
|
|
|
}
|
2018-06-23 19:14:50 +08:00
|
|
|
internal init(integerLiteral rawValue: IntegerLiteralType) {
|
|
|
|
self.init(rawValue: rawValue)
|
|
|
|
}
|
2014-09-23 12:28:00 +08:00
|
|
|
|
|
|
|
internal private(set) var rawValue: UInt
|
2018-06-23 19:14:50 +08:00
|
|
|
internal static var allZeros: ConstraintAttributes { return 0 }
|
|
|
|
internal static func convertFromNilLiteral() -> ConstraintAttributes { return 0 }
|
2014-09-23 12:28:00 +08:00
|
|
|
internal var boolValue: Bool { return self.rawValue != 0 }
|
|
|
|
|
2015-04-12 18:21:02 +08:00
|
|
|
internal func toRaw() -> UInt { return self.rawValue }
|
2016-06-15 09:49:49 +08:00
|
|
|
internal static func fromRaw(_ raw: UInt) -> ConstraintAttributes? { return self.init(raw) }
|
|
|
|
internal static func fromMask(_ raw: UInt) -> ConstraintAttributes { return self.init(raw) }
|
2014-09-23 12:28:00 +08:00
|
|
|
|
2015-01-08 09:58:37 +08:00
|
|
|
// normal
|
|
|
|
|
2018-06-23 19:14:50 +08:00
|
|
|
internal static var none: ConstraintAttributes { return 0 }
|
|
|
|
internal static var left: ConstraintAttributes { return 1 }
|
|
|
|
internal static var top: ConstraintAttributes { return 2 }
|
|
|
|
internal static var right: ConstraintAttributes { return 4 }
|
|
|
|
internal static var bottom: ConstraintAttributes { return 8 }
|
|
|
|
internal static var leading: ConstraintAttributes { return 16 }
|
|
|
|
internal static var trailing: ConstraintAttributes { return 32 }
|
|
|
|
internal static var width: ConstraintAttributes { return 64 }
|
|
|
|
internal static var height: ConstraintAttributes { return 128 }
|
|
|
|
internal static var centerX: ConstraintAttributes { return 256 }
|
|
|
|
internal static var centerY: ConstraintAttributes { return 512 }
|
|
|
|
internal static var lastBaseline: ConstraintAttributes { return 1024 }
|
2016-08-06 18:38:02 +08:00
|
|
|
|
|
|
|
@available(iOS 8.0, OSX 10.11, *)
|
2018-06-23 19:14:50 +08:00
|
|
|
internal static var firstBaseline: ConstraintAttributes { return 2048 }
|
2014-09-23 12:28:00 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2018-06-23 19:14:50 +08:00
|
|
|
internal static var leftMargin: ConstraintAttributes { return 4096 }
|
2016-08-06 18:38:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2018-06-23 19:14:50 +08:00
|
|
|
internal static var rightMargin: ConstraintAttributes { return 8192 }
|
2016-08-06 18:38:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2018-06-23 19:14:50 +08:00
|
|
|
internal static var topMargin: ConstraintAttributes { return 16384 }
|
2016-08-06 18:38:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2018-06-23 19:14:50 +08:00
|
|
|
internal static var bottomMargin: ConstraintAttributes { return 32768 }
|
2016-08-06 18:38:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2018-06-23 19:14:50 +08:00
|
|
|
internal static var leadingMargin: ConstraintAttributes { return 65536 }
|
2016-08-06 18:38:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2018-06-23 19:14:50 +08:00
|
|
|
internal static var trailingMargin: ConstraintAttributes { return 131072 }
|
2016-08-06 18:38:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2018-06-23 19:14:50 +08:00
|
|
|
internal static var centerXWithinMargins: ConstraintAttributes { return 262144 }
|
2016-08-06 18:38:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2018-06-23 19:14:50 +08:00
|
|
|
internal static var centerYWithinMargins: ConstraintAttributes { return 524288 }
|
2015-01-08 09:58:37 +08:00
|
|
|
|
|
|
|
// aggregates
|
2014-10-29 05:30:07 +08:00
|
|
|
|
2018-06-23 19:14:50 +08:00
|
|
|
internal static var edges: ConstraintAttributes { return 15 }
|
2019-08-05 13:16:17 +08:00
|
|
|
internal static var directionalEdges: ConstraintAttributes { return 58 }
|
2018-06-23 19:14:50 +08:00
|
|
|
internal static var size: ConstraintAttributes { return 192 }
|
|
|
|
internal static var center: ConstraintAttributes { return 768 }
|
2015-01-08 09:58:37 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2018-06-23 19:14:50 +08:00
|
|
|
internal static var margins: ConstraintAttributes { return 61440 }
|
2015-09-22 15:38:50 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2019-08-05 13:16:17 +08:00
|
|
|
internal static var directionalMargins: ConstraintAttributes { return 245760 }
|
|
|
|
|
|
|
|
@available(iOS 8.0, *)
|
2018-06-23 19:14:50 +08:00
|
|
|
internal static var centerWithinMargins: ConstraintAttributes { return 786432 }
|
2014-09-23 12:28:00 +08:00
|
|
|
|
2017-09-17 10:15:34 +08:00
|
|
|
internal var layoutAttributes:[LayoutAttribute] {
|
|
|
|
var attrs = [LayoutAttribute]()
|
2016-08-06 18:38:02 +08:00
|
|
|
if (self.contains(ConstraintAttributes.left)) {
|
2016-06-15 09:49:49 +08:00
|
|
|
attrs.append(.left)
|
2015-04-11 13:12:05 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
if (self.contains(ConstraintAttributes.top)) {
|
2016-06-15 09:49:49 +08:00
|
|
|
attrs.append(.top)
|
2015-04-11 13:12:05 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
if (self.contains(ConstraintAttributes.right)) {
|
2016-06-15 09:49:49 +08:00
|
|
|
attrs.append(.right)
|
2015-04-11 13:12:05 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
if (self.contains(ConstraintAttributes.bottom)) {
|
2016-06-15 09:49:49 +08:00
|
|
|
attrs.append(.bottom)
|
2015-04-11 13:12:05 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
if (self.contains(ConstraintAttributes.leading)) {
|
2016-06-15 09:49:49 +08:00
|
|
|
attrs.append(.leading)
|
2015-04-11 13:12:05 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
if (self.contains(ConstraintAttributes.trailing)) {
|
2016-06-15 09:49:49 +08:00
|
|
|
attrs.append(.trailing)
|
2015-04-11 13:12:05 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
if (self.contains(ConstraintAttributes.width)) {
|
2016-06-15 09:49:49 +08:00
|
|
|
attrs.append(.width)
|
2015-04-11 13:12:05 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
if (self.contains(ConstraintAttributes.height)) {
|
2016-06-15 09:49:49 +08:00
|
|
|
attrs.append(.height)
|
2015-04-11 13:12:05 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
if (self.contains(ConstraintAttributes.centerX)) {
|
2016-06-15 09:49:49 +08:00
|
|
|
attrs.append(.centerX)
|
2015-04-11 13:12:05 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
if (self.contains(ConstraintAttributes.centerY)) {
|
2016-06-15 09:49:49 +08:00
|
|
|
attrs.append(.centerY)
|
2015-04-11 13:12:05 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
if (self.contains(ConstraintAttributes.lastBaseline)) {
|
2016-06-15 09:49:49 +08:00
|
|
|
attrs.append(.lastBaseline)
|
2015-04-11 13:12:05 +08:00
|
|
|
}
|
2015-09-22 15:38:50 +08:00
|
|
|
|
2015-09-25 12:37:33 +08:00
|
|
|
#if os(iOS) || os(tvOS)
|
2016-08-06 18:38:02 +08:00
|
|
|
if (self.contains(ConstraintAttributes.firstBaseline)) {
|
2016-06-15 09:49:49 +08:00
|
|
|
attrs.append(.firstBaseline)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
if (self.contains(ConstraintAttributes.leftMargin)) {
|
2016-06-15 09:49:49 +08:00
|
|
|
attrs.append(.leftMargin)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
if (self.contains(ConstraintAttributes.rightMargin)) {
|
2016-06-15 09:49:49 +08:00
|
|
|
attrs.append(.rightMargin)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
if (self.contains(ConstraintAttributes.topMargin)) {
|
2016-06-15 09:49:49 +08:00
|
|
|
attrs.append(.topMargin)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
if (self.contains(ConstraintAttributes.bottomMargin)) {
|
2016-06-15 09:49:49 +08:00
|
|
|
attrs.append(.bottomMargin)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
if (self.contains(ConstraintAttributes.leadingMargin)) {
|
2016-06-15 09:49:49 +08:00
|
|
|
attrs.append(.leadingMargin)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
if (self.contains(ConstraintAttributes.trailingMargin)) {
|
2016-06-15 09:49:49 +08:00
|
|
|
attrs.append(.trailingMargin)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
if (self.contains(ConstraintAttributes.centerXWithinMargins)) {
|
2016-06-15 09:49:49 +08:00
|
|
|
attrs.append(.centerXWithinMargins)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
if (self.contains(ConstraintAttributes.centerYWithinMargins)) {
|
2016-06-15 09:49:49 +08:00
|
|
|
attrs.append(.centerYWithinMargins)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2015-04-11 13:12:05 +08:00
|
|
|
#endif
|
2015-09-22 15:38:50 +08:00
|
|
|
|
2015-04-11 13:12:05 +08:00
|
|
|
return attrs
|
2014-09-23 12:28:00 +08:00
|
|
|
}
|
|
|
|
}
|
2016-01-26 18:49:04 +08:00
|
|
|
|
|
|
|
internal func + (left: ConstraintAttributes, right: ConstraintAttributes) -> ConstraintAttributes {
|
|
|
|
return left.union(right)
|
|
|
|
}
|
|
|
|
|
2016-06-15 09:49:49 +08:00
|
|
|
internal func +=(left: inout ConstraintAttributes, right: ConstraintAttributes) {
|
|
|
|
left.formUnion(right)
|
2014-09-23 12:28:00 +08:00
|
|
|
}
|
2016-01-26 18:49:04 +08:00
|
|
|
|
2016-06-15 09:49:49 +08:00
|
|
|
internal func -=(left: inout ConstraintAttributes, right: ConstraintAttributes) {
|
|
|
|
left.subtract(right)
|
2014-09-23 12:28:00 +08:00
|
|
|
}
|
2016-01-26 18:49:04 +08:00
|
|
|
|
2015-01-08 14:04:29 +08:00
|
|
|
internal func ==(left: ConstraintAttributes, right: ConstraintAttributes) -> Bool {
|
2014-09-23 12:28:00 +08:00
|
|
|
return left.rawValue == right.rawValue
|
|
|
|
}
|