ConstraintAttributes.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 canImport(UIKit)
  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 = [.leading, .trailing]
  83. internal static let verticalEdges: ConstraintAttributes = [.top, .bottom]
  84. internal static let size: ConstraintAttributes = [.width, .height]
  85. internal static let center: ConstraintAttributes = [.centerX, .centerY]
  86. @available(iOS 8.0, *)
  87. internal static let margins: ConstraintAttributes = [.leadingMargin, .topMargin, .trailingMargin, .bottomMargin]
  88. @available(iOS 8.0, *)
  89. internal static let centerWithinMargins: ConstraintAttributes = [.centerXWithinMargins, .centerYWithinMargins]
  90. internal var layoutAttributes:[LayoutAttribute] {
  91. var attrs = [LayoutAttribute]()
  92. if (self.contains(ConstraintAttributes.left)) {
  93. attrs.append(.left)
  94. }
  95. if (self.contains(ConstraintAttributes.top)) {
  96. attrs.append(.top)
  97. }
  98. if (self.contains(ConstraintAttributes.right)) {
  99. attrs.append(.right)
  100. }
  101. if (self.contains(ConstraintAttributes.bottom)) {
  102. attrs.append(.bottom)
  103. }
  104. if (self.contains(ConstraintAttributes.leading)) {
  105. attrs.append(.leading)
  106. }
  107. if (self.contains(ConstraintAttributes.trailing)) {
  108. attrs.append(.trailing)
  109. }
  110. if (self.contains(ConstraintAttributes.width)) {
  111. attrs.append(.width)
  112. }
  113. if (self.contains(ConstraintAttributes.height)) {
  114. attrs.append(.height)
  115. }
  116. if (self.contains(ConstraintAttributes.centerX)) {
  117. attrs.append(.centerX)
  118. }
  119. if (self.contains(ConstraintAttributes.centerY)) {
  120. attrs.append(.centerY)
  121. }
  122. if (self.contains(ConstraintAttributes.lastBaseline)) {
  123. attrs.append(.lastBaseline)
  124. }
  125. #if canImport(UIKit)
  126. if (self.contains(ConstraintAttributes.firstBaseline)) {
  127. attrs.append(.firstBaseline)
  128. }
  129. if (self.contains(ConstraintAttributes.leftMargin)) {
  130. attrs.append(.leftMargin)
  131. }
  132. if (self.contains(ConstraintAttributes.rightMargin)) {
  133. attrs.append(.rightMargin)
  134. }
  135. if (self.contains(ConstraintAttributes.topMargin)) {
  136. attrs.append(.topMargin)
  137. }
  138. if (self.contains(ConstraintAttributes.bottomMargin)) {
  139. attrs.append(.bottomMargin)
  140. }
  141. if (self.contains(ConstraintAttributes.leadingMargin)) {
  142. attrs.append(.leadingMargin)
  143. }
  144. if (self.contains(ConstraintAttributes.trailingMargin)) {
  145. attrs.append(.trailingMargin)
  146. }
  147. if (self.contains(ConstraintAttributes.centerXWithinMargins)) {
  148. attrs.append(.centerXWithinMargins)
  149. }
  150. if (self.contains(ConstraintAttributes.centerYWithinMargins)) {
  151. attrs.append(.centerYWithinMargins)
  152. }
  153. #endif
  154. return attrs
  155. }
  156. }
  157. internal func + (left: ConstraintAttributes, right: ConstraintAttributes) -> ConstraintAttributes {
  158. return left.union(right)
  159. }
  160. internal func +=(left: inout ConstraintAttributes, right: ConstraintAttributes) {
  161. left.formUnion(right)
  162. }
  163. internal func -=(left: inout ConstraintAttributes, right: ConstraintAttributes) {
  164. left.subtract(right)
  165. }
  166. internal func ==(left: ConstraintAttributes, right: ConstraintAttributes) -> Bool {
  167. return left.rawValue == right.rawValue
  168. }