SnapKit/Source/ConstraintAttributes.swift

187 lines
7.5 KiB
Swift
Raw Normal View History

//
2015-04-15 19:07:50 +08:00
// SnapKit
//
2015-04-15 19:07:50 +08:00
// Copyright (c) 2011-2015 SnapKit Team - https://github.com/SnapKit
//
// 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) || os(tvOS)
import UIKit
#else
import AppKit
#endif
/**
2015-04-12 18:21:02 +08:00
Used to define `NSLayoutAttributes` in a more concise and composite manner
*/
2015-06-25 02:28:28 +08:00
internal struct ConstraintAttributes: OptionSetType, BooleanType {
internal init(rawValue: UInt) {
self.rawValue = rawValue
}
internal init(_ rawValue: UInt) {
self.init(rawValue: rawValue)
}
internal init(nilLiteral: ()) {
self.rawValue = 0
}
internal private(set) var rawValue: UInt
2015-06-25 02:28:28 +08:00
internal static var allZeros: ConstraintAttributes { return self.init(0) }
internal static func convertFromNilLiteral() -> ConstraintAttributes { return self.init(0) }
internal var boolValue: Bool { return self.rawValue != 0 }
2015-04-12 18:21:02 +08:00
internal func toRaw() -> UInt { return self.rawValue }
2015-06-25 02:28:28 +08:00
internal static func fromRaw(raw: UInt) -> ConstraintAttributes? { return self.init(raw) }
internal static func fromMask(raw: UInt) -> ConstraintAttributes { return self.init(raw) }
2015-01-08 09:58:37 +08:00
// normal
2015-06-25 02:28:28 +08:00
internal static var None: ConstraintAttributes { return self.init(0) }
internal static var Left: ConstraintAttributes { return self.init(1) }
internal static var Top: ConstraintAttributes { return self.init(2) }
internal static var Right: ConstraintAttributes { return self.init(4) }
internal static var Bottom: ConstraintAttributes { return self.init(8) }
internal static var Leading: ConstraintAttributes { return self.init(16) }
internal static var Trailing: ConstraintAttributes { return self.init(32) }
internal static var Width: ConstraintAttributes { return self.init(64) }
internal static var Height: ConstraintAttributes { return self.init(128) }
internal static var CenterX: ConstraintAttributes { return self.init(256) }
internal static var CenterY: ConstraintAttributes { return self.init(512) }
internal static var Baseline: ConstraintAttributes { return self.init(1024) }
@available(iOS 8.0, *)
2015-06-25 02:28:28 +08:00
internal static var FirstBaseline: ConstraintAttributes { return self.init(2048) }
@available(iOS 8.0, *)
2015-06-25 02:28:28 +08:00
internal static var LeftMargin: ConstraintAttributes { return self.init(4096) }
@available(iOS 8.0, *)
2015-06-25 02:28:28 +08:00
internal static var RightMargin: ConstraintAttributes { return self.init(8192) }
@available(iOS 8.0, *)
2015-06-25 02:28:28 +08:00
internal static var TopMargin: ConstraintAttributes { return self.init(16384) }
@available(iOS 8.0, *)
2015-06-25 02:28:28 +08:00
internal static var BottomMargin: ConstraintAttributes { return self.init(32768) }
@available(iOS 8.0, *)
2015-06-25 02:28:28 +08:00
internal static var LeadingMargin: ConstraintAttributes { return self.init(65536) }
@available(iOS 8.0, *)
2015-06-25 02:28:28 +08:00
internal static var TrailingMargin: ConstraintAttributes { return self.init(131072) }
@available(iOS 8.0, *)
2015-06-25 02:28:28 +08:00
internal static var CenterXWithinMargins: ConstraintAttributes { return self.init(262144) }
@available(iOS 8.0, *)
2015-06-25 02:28:28 +08:00
internal static var CenterYWithinMargins: ConstraintAttributes { return self.init(524288) }
2015-01-08 09:58:37 +08:00
// aggregates
2014-10-29 05:30:07 +08:00
2015-06-25 02:28:28 +08:00
internal static var Edges: ConstraintAttributes { return self.init(15) }
internal static var Size: ConstraintAttributes { return self.init(192) }
internal static var Center: ConstraintAttributes { return self.init(768) }
2015-01-08 09:58:37 +08:00
@available(iOS 8.0, *)
2015-06-25 02:28:28 +08:00
internal static var Margins: ConstraintAttributes { return self.init(61440) }
@available(iOS 8.0, *)
2015-06-25 02:28:28 +08:00
internal static var CenterWithinMargins: ConstraintAttributes { return self.init(786432) }
2015-04-11 13:12:05 +08:00
internal var layoutAttributes:[NSLayoutAttribute] {
var attrs = [NSLayoutAttribute]()
2015-06-25 02:28:28 +08:00
if (self.contains(ConstraintAttributes.Left)) {
2015-04-11 13:12:05 +08:00
attrs.append(.Left)
}
2015-06-25 02:28:28 +08:00
if (self.contains(ConstraintAttributes.Top)) {
2015-04-11 13:12:05 +08:00
attrs.append(.Top)
}
2015-06-25 02:28:28 +08:00
if (self.contains(ConstraintAttributes.Right)) {
2015-04-11 13:12:05 +08:00
attrs.append(.Right)
}
2015-06-25 02:28:28 +08:00
if (self.contains(ConstraintAttributes.Bottom)) {
2015-04-11 13:12:05 +08:00
attrs.append(.Bottom)
}
2015-06-25 02:28:28 +08:00
if (self.contains(ConstraintAttributes.Leading)) {
2015-04-11 13:12:05 +08:00
attrs.append(.Leading)
}
2015-06-25 02:28:28 +08:00
if (self.contains(ConstraintAttributes.Trailing)) {
2015-04-11 13:12:05 +08:00
attrs.append(.Trailing)
}
2015-06-25 02:28:28 +08:00
if (self.contains(ConstraintAttributes.Width)) {
2015-04-11 13:12:05 +08:00
attrs.append(.Width)
}
2015-06-25 02:28:28 +08:00
if (self.contains(ConstraintAttributes.Height)) {
2015-04-11 13:12:05 +08:00
attrs.append(.Height)
}
2015-06-25 02:28:28 +08:00
if (self.contains(ConstraintAttributes.CenterX)) {
2015-04-11 13:12:05 +08:00
attrs.append(.CenterX)
}
2015-06-25 02:28:28 +08:00
if (self.contains(ConstraintAttributes.CenterY)) {
2015-04-11 13:12:05 +08:00
attrs.append(.CenterY)
}
2015-06-25 02:28:28 +08:00
if (self.contains(ConstraintAttributes.Baseline)) {
2016-06-21 14:56:32 +08:00
#if swift(>=2.3)
attrs.append(.LastBaseline)
#else
2015-04-11 13:12:05 +08:00
attrs.append(.Baseline)
2016-06-21 14:56:32 +08:00
#endif
2015-04-11 13:12:05 +08:00
}
#if os(iOS) || os(tvOS)
#if SNAPKIT_DEPLOYMENT_LEGACY
guard #available(iOS 8.0, *) else {
return attrs
}
#endif
2015-06-25 02:28:28 +08:00
if (self.contains(ConstraintAttributes.FirstBaseline)) {
2015-04-11 13:12:05 +08:00
attrs.append(.FirstBaseline)
}
2015-06-25 02:28:28 +08:00
if (self.contains(ConstraintAttributes.LeftMargin)) {
2015-04-11 13:12:05 +08:00
attrs.append(.LeftMargin)
}
2015-06-25 02:28:28 +08:00
if (self.contains(ConstraintAttributes.RightMargin)) {
2015-04-11 13:12:05 +08:00
attrs.append(.RightMargin)
}
2015-06-25 02:28:28 +08:00
if (self.contains(ConstraintAttributes.TopMargin)) {
2015-04-11 13:12:05 +08:00
attrs.append(.TopMargin)
}
2015-06-25 02:28:28 +08:00
if (self.contains(ConstraintAttributes.BottomMargin)) {
2015-04-11 13:12:05 +08:00
attrs.append(.BottomMargin)
}
2015-06-25 02:28:28 +08:00
if (self.contains(ConstraintAttributes.LeadingMargin)) {
2015-04-11 13:12:05 +08:00
attrs.append(.LeadingMargin)
}
2015-06-25 02:28:28 +08:00
if (self.contains(ConstraintAttributes.TrailingMargin)) {
2015-04-11 13:12:05 +08:00
attrs.append(.TrailingMargin)
}
2015-06-25 02:28:28 +08:00
if (self.contains(ConstraintAttributes.CenterXWithinMargins)) {
2015-04-11 13:12:05 +08:00
attrs.append(.CenterXWithinMargins)
}
2015-06-25 02:28:28 +08:00
if (self.contains(ConstraintAttributes.CenterYWithinMargins)) {
2015-04-11 13:12:05 +08:00
attrs.append(.CenterYWithinMargins)
}
#endif
2015-04-11 13:12:05 +08:00
return attrs
}
}
internal func +=(inout left: ConstraintAttributes, right: ConstraintAttributes) {
left.unionInPlace(right)
}
internal func -=(inout left: ConstraintAttributes, right: ConstraintAttributes) {
left.subtractInPlace(right)
}
internal func ==(left: ConstraintAttributes, right: ConstraintAttributes) -> Bool {
return left.rawValue == right.rawValue
}