ConstraintAttributes.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // SnapKit
  3. //
  4. // Copyright (c) 2011-Present SnapKit Team - https://github.com/SnapKit
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. #if os(iOS) || os(tvOS)
  24. import UIKit
  25. #else
  26. import AppKit
  27. #endif
  28. internal struct ConstraintAttributes : OptionSet, ExpressibleByIntegerLiteral {
  29. typealias IntegerLiteralType = UInt
  30. internal init(rawValue: UInt) {
  31. self.rawValue = rawValue
  32. }
  33. internal init(_ rawValue: UInt) {
  34. self.init(rawValue: rawValue)
  35. }
  36. internal init(nilLiteral: ()) {
  37. self.rawValue = 0
  38. }
  39. internal init(integerLiteral rawValue: IntegerLiteralType) {
  40. self.init(rawValue: rawValue)
  41. }
  42. internal private(set) var rawValue: UInt
  43. internal static var allZeros: ConstraintAttributes { return 0 }
  44. internal static func convertFromNilLiteral() -> ConstraintAttributes { return 0 }
  45. internal var boolValue: Bool { return self.rawValue != 0 }
  46. internal func toRaw() -> UInt { return self.rawValue }
  47. internal static func fromRaw(_ raw: UInt) -> ConstraintAttributes? { return self.init(raw) }
  48. internal static func fromMask(_ raw: UInt) -> ConstraintAttributes { return self.init(raw) }
  49. // normal
  50. internal static let none: ConstraintAttributes = 0
  51. internal static let left: ConstraintAttributes = ConstraintAttributes(UInt(1) << 0)
  52. internal static let top: ConstraintAttributes = ConstraintAttributes(UInt(1) << 1)
  53. internal static let right: ConstraintAttributes = ConstraintAttributes(UInt(1) << 2)
  54. internal static let bottom: ConstraintAttributes = ConstraintAttributes(UInt(1) << 3)
  55. internal static let leading: ConstraintAttributes = ConstraintAttributes(UInt(1) << 4)
  56. internal static let trailing: ConstraintAttributes = ConstraintAttributes(UInt(1) << 5)
  57. internal static let width: ConstraintAttributes = ConstraintAttributes(UInt(1) << 6)
  58. internal static let height: ConstraintAttributes = ConstraintAttributes(UInt(1) << 7)
  59. internal static let centerX: ConstraintAttributes = ConstraintAttributes(UInt(1) << 8)
  60. internal static let centerY: ConstraintAttributes = ConstraintAttributes(UInt(1) << 9)
  61. internal static let lastBaseline: ConstraintAttributes = ConstraintAttributes(UInt(1) << 10)
  62. @available(iOS 8.0, OSX 10.11, *)
  63. internal static let firstBaseline: ConstraintAttributes = ConstraintAttributes(UInt(1) << 11)
  64. @available(iOS 8.0, *)
  65. internal static let leftMargin: ConstraintAttributes = ConstraintAttributes(UInt(1) << 12)
  66. @available(iOS 8.0, *)
  67. internal static let rightMargin: ConstraintAttributes = ConstraintAttributes(UInt(1) << 13)
  68. @available(iOS 8.0, *)
  69. internal static let topMargin: ConstraintAttributes = ConstraintAttributes(UInt(1) << 14)
  70. @available(iOS 8.0, *)
  71. internal static let bottomMargin: ConstraintAttributes = ConstraintAttributes(UInt(1) << 15)
  72. @available(iOS 8.0, *)
  73. internal static let leadingMargin: ConstraintAttributes = ConstraintAttributes(UInt(1) << 16)
  74. @available(iOS 8.0, *)
  75. internal static let trailingMargin: ConstraintAttributes = ConstraintAttributes(UInt(1) << 17)
  76. @available(iOS 8.0, *)
  77. internal static let centerXWithinMargins: ConstraintAttributes = ConstraintAttributes(UInt(1) << 18)
  78. @available(iOS 8.0, *)
  79. internal static let centerYWithinMargins: ConstraintAttributes = ConstraintAttributes(UInt(1) << 19)
  80. // aggregates
  81. internal static let edges: ConstraintAttributes = [.horizontalEdges, .verticalEdges]
  82. internal static let horizontalEdges: ConstraintAttributes = [.left, .right]
  83. internal static let verticalEdges: ConstraintAttributes = [.top, .bottom]
  84. internal static let directionalEdges: ConstraintAttributes = [.directionalHorizontalEdges, .directionalVerticalEdges]
  85. internal static let directionalHorizontalEdges: ConstraintAttributes = [.leading, .trailing]
  86. internal static let directionalVerticalEdges: ConstraintAttributes = [.top, .bottom]
  87. internal static let size: ConstraintAttributes = [.width, .height]
  88. internal static let center: ConstraintAttributes = [.centerX, .centerY]
  89. @available(iOS 8.0, *)
  90. internal static let margins: ConstraintAttributes = [.leftMargin, .topMargin, .rightMargin, .bottomMargin]
  91. @available(iOS 8.0, *)
  92. internal static let directionalMargins: ConstraintAttributes = [.leadingMargin, .topMargin, .trailingMargin, .bottomMargin]
  93. @available(iOS 8.0, *)
  94. internal static let centerWithinMargins: ConstraintAttributes = [.centerXWithinMargins, .centerYWithinMargins]
  95. internal var layoutAttributes:[LayoutAttribute] {
  96. var attrs = [LayoutAttribute]()
  97. if (self.contains(ConstraintAttributes.left)) {
  98. attrs.append(.left)
  99. }
  100. if (self.contains(ConstraintAttributes.top)) {
  101. attrs.append(.top)
  102. }
  103. if (self.contains(ConstraintAttributes.right)) {
  104. attrs.append(.right)
  105. }
  106. if (self.contains(ConstraintAttributes.bottom)) {
  107. attrs.append(.bottom)
  108. }
  109. if (self.contains(ConstraintAttributes.leading)) {
  110. attrs.append(.leading)
  111. }
  112. if (self.contains(ConstraintAttributes.trailing)) {
  113. attrs.append(.trailing)
  114. }
  115. if (self.contains(ConstraintAttributes.width)) {
  116. attrs.append(.width)
  117. }
  118. if (self.contains(ConstraintAttributes.height)) {
  119. attrs.append(.height)
  120. }
  121. if (self.contains(ConstraintAttributes.centerX)) {
  122. attrs.append(.centerX)
  123. }
  124. if (self.contains(ConstraintAttributes.centerY)) {
  125. attrs.append(.centerY)
  126. }
  127. if (self.contains(ConstraintAttributes.lastBaseline)) {
  128. attrs.append(.lastBaseline)
  129. }
  130. #if os(iOS) || os(tvOS)
  131. if (self.contains(ConstraintAttributes.firstBaseline)) {
  132. attrs.append(.firstBaseline)
  133. }
  134. if (self.contains(ConstraintAttributes.leftMargin)) {
  135. attrs.append(.leftMargin)
  136. }
  137. if (self.contains(ConstraintAttributes.rightMargin)) {
  138. attrs.append(.rightMargin)
  139. }
  140. if (self.contains(ConstraintAttributes.topMargin)) {
  141. attrs.append(.topMargin)
  142. }
  143. if (self.contains(ConstraintAttributes.bottomMargin)) {
  144. attrs.append(.bottomMargin)
  145. }
  146. if (self.contains(ConstraintAttributes.leadingMargin)) {
  147. attrs.append(.leadingMargin)
  148. }
  149. if (self.contains(ConstraintAttributes.trailingMargin)) {
  150. attrs.append(.trailingMargin)
  151. }
  152. if (self.contains(ConstraintAttributes.centerXWithinMargins)) {
  153. attrs.append(.centerXWithinMargins)
  154. }
  155. if (self.contains(ConstraintAttributes.centerYWithinMargins)) {
  156. attrs.append(.centerYWithinMargins)
  157. }
  158. #endif
  159. return attrs
  160. }
  161. }
  162. internal func + (left: ConstraintAttributes, right: ConstraintAttributes) -> ConstraintAttributes {
  163. return left.union(right)
  164. }
  165. internal func +=(left: inout ConstraintAttributes, right: ConstraintAttributes) {
  166. left.formUnion(right)
  167. }
  168. internal func -=(left: inout ConstraintAttributes, right: ConstraintAttributes) {
  169. left.subtract(right)
  170. }
  171. internal func ==(left: ConstraintAttributes, right: ConstraintAttributes) -> Bool {
  172. return left.rawValue == right.rawValue
  173. }