Netease-Music-Demo by sss
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConstraintAttributes.swift 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 var none: ConstraintAttributes { return 0 }
  51. internal static var left: ConstraintAttributes { return 1 }
  52. internal static var top: ConstraintAttributes { return 2 }
  53. internal static var right: ConstraintAttributes { return 4 }
  54. internal static var bottom: ConstraintAttributes { return 8 }
  55. internal static var leading: ConstraintAttributes { return 16 }
  56. internal static var trailing: ConstraintAttributes { return 32 }
  57. internal static var width: ConstraintAttributes { return 64 }
  58. internal static var height: ConstraintAttributes { return 128 }
  59. internal static var centerX: ConstraintAttributes { return 256 }
  60. internal static var centerY: ConstraintAttributes { return 512 }
  61. internal static var lastBaseline: ConstraintAttributes { return 1024 }
  62. @available(iOS 8.0, OSX 10.11, *)
  63. internal static var firstBaseline: ConstraintAttributes { return 2048 }
  64. @available(iOS 8.0, *)
  65. internal static var leftMargin: ConstraintAttributes { return 4096 }
  66. @available(iOS 8.0, *)
  67. internal static var rightMargin: ConstraintAttributes { return 8192 }
  68. @available(iOS 8.0, *)
  69. internal static var topMargin: ConstraintAttributes { return 16384 }
  70. @available(iOS 8.0, *)
  71. internal static var bottomMargin: ConstraintAttributes { return 32768 }
  72. @available(iOS 8.0, *)
  73. internal static var leadingMargin: ConstraintAttributes { return 65536 }
  74. @available(iOS 8.0, *)
  75. internal static var trailingMargin: ConstraintAttributes { return 131072 }
  76. @available(iOS 8.0, *)
  77. internal static var centerXWithinMargins: ConstraintAttributes { return 262144 }
  78. @available(iOS 8.0, *)
  79. internal static var centerYWithinMargins: ConstraintAttributes { return 524288 }
  80. // aggregates
  81. internal static var edges: ConstraintAttributes { return 15 }
  82. internal static var directionalEdges: ConstraintAttributes { return 58 }
  83. internal static var size: ConstraintAttributes { return 192 }
  84. internal static var center: ConstraintAttributes { return 768 }
  85. @available(iOS 8.0, *)
  86. internal static var margins: ConstraintAttributes { return 61440 }
  87. @available(iOS 8.0, *)
  88. internal static var directionalMargins: ConstraintAttributes { return 245760 }
  89. @available(iOS 8.0, *)
  90. internal static var centerWithinMargins: ConstraintAttributes { return 786432 }
  91. internal var layoutAttributes:[LayoutAttribute] {
  92. var attrs = [LayoutAttribute]()
  93. if (self.contains(ConstraintAttributes.left)) {
  94. attrs.append(.left)
  95. }
  96. if (self.contains(ConstraintAttributes.top)) {
  97. attrs.append(.top)
  98. }
  99. if (self.contains(ConstraintAttributes.right)) {
  100. attrs.append(.right)
  101. }
  102. if (self.contains(ConstraintAttributes.bottom)) {
  103. attrs.append(.bottom)
  104. }
  105. if (self.contains(ConstraintAttributes.leading)) {
  106. attrs.append(.leading)
  107. }
  108. if (self.contains(ConstraintAttributes.trailing)) {
  109. attrs.append(.trailing)
  110. }
  111. if (self.contains(ConstraintAttributes.width)) {
  112. attrs.append(.width)
  113. }
  114. if (self.contains(ConstraintAttributes.height)) {
  115. attrs.append(.height)
  116. }
  117. if (self.contains(ConstraintAttributes.centerX)) {
  118. attrs.append(.centerX)
  119. }
  120. if (self.contains(ConstraintAttributes.centerY)) {
  121. attrs.append(.centerY)
  122. }
  123. if (self.contains(ConstraintAttributes.lastBaseline)) {
  124. attrs.append(.lastBaseline)
  125. }
  126. #if os(iOS) || os(tvOS)
  127. if (self.contains(ConstraintAttributes.firstBaseline)) {
  128. attrs.append(.firstBaseline)
  129. }
  130. if (self.contains(ConstraintAttributes.leftMargin)) {
  131. attrs.append(.leftMargin)
  132. }
  133. if (self.contains(ConstraintAttributes.rightMargin)) {
  134. attrs.append(.rightMargin)
  135. }
  136. if (self.contains(ConstraintAttributes.topMargin)) {
  137. attrs.append(.topMargin)
  138. }
  139. if (self.contains(ConstraintAttributes.bottomMargin)) {
  140. attrs.append(.bottomMargin)
  141. }
  142. if (self.contains(ConstraintAttributes.leadingMargin)) {
  143. attrs.append(.leadingMargin)
  144. }
  145. if (self.contains(ConstraintAttributes.trailingMargin)) {
  146. attrs.append(.trailingMargin)
  147. }
  148. if (self.contains(ConstraintAttributes.centerXWithinMargins)) {
  149. attrs.append(.centerXWithinMargins)
  150. }
  151. if (self.contains(ConstraintAttributes.centerYWithinMargins)) {
  152. attrs.append(.centerYWithinMargins)
  153. }
  154. #endif
  155. return attrs
  156. }
  157. }
  158. internal func + (left: ConstraintAttributes, right: ConstraintAttributes) -> ConstraintAttributes {
  159. return left.union(right)
  160. }
  161. internal func +=(left: inout ConstraintAttributes, right: ConstraintAttributes) {
  162. left.formUnion(right)
  163. }
  164. internal func -=(left: inout ConstraintAttributes, right: ConstraintAttributes) {
  165. left.subtract(right)
  166. }
  167. internal func ==(left: ConstraintAttributes, right: ConstraintAttributes) -> Bool {
  168. return left.rawValue == right.rawValue
  169. }