ConstraintAttributes.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // SnapKit
  3. //
  4. // Copyright (c) 2011-2015 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)
  24. import UIKit
  25. #else
  26. import AppKit
  27. #endif
  28. /**
  29. Used to define `NSLayoutAttributes` in a more concise and composite manner
  30. */
  31. internal struct ConstraintAttributes: RawOptionSetType, BooleanType {
  32. internal init(rawValue: UInt) {
  33. self.rawValue = rawValue
  34. }
  35. internal init(_ rawValue: UInt) {
  36. self.init(rawValue: rawValue)
  37. }
  38. internal init(nilLiteral: ()) {
  39. self.rawValue = 0
  40. }
  41. internal private(set) var rawValue: UInt
  42. internal static var allZeros: ConstraintAttributes { return self(0) }
  43. internal static func convertFromNilLiteral() -> ConstraintAttributes { return self(0) }
  44. internal var boolValue: Bool { return self.rawValue != 0 }
  45. internal func toRaw() -> UInt { return self.rawValue }
  46. internal static func fromRaw(raw: UInt) -> ConstraintAttributes? { return self(raw) }
  47. internal static func fromMask(raw: UInt) -> ConstraintAttributes { return self(raw) }
  48. // normal
  49. internal static var None: ConstraintAttributes { return self(0) }
  50. internal static var Left: ConstraintAttributes { return self(1) }
  51. internal static var Top: ConstraintAttributes { return self(2) }
  52. internal static var Right: ConstraintAttributes { return self(4) }
  53. internal static var Bottom: ConstraintAttributes { return self(8) }
  54. internal static var Leading: ConstraintAttributes { return self(16) }
  55. internal static var Trailing: ConstraintAttributes { return self(32) }
  56. internal static var Width: ConstraintAttributes { return self(64) }
  57. internal static var Height: ConstraintAttributes { return self(128) }
  58. internal static var CenterX: ConstraintAttributes { return self(256) }
  59. internal static var CenterY: ConstraintAttributes { return self(512) }
  60. internal static var Baseline: ConstraintAttributes { return self(1024) }
  61. #if os(iOS)
  62. internal static var FirstBaseline: ConstraintAttributes { return self(2048) }
  63. internal static var LeftMargin: ConstraintAttributes { return self(4096) }
  64. internal static var RightMargin: ConstraintAttributes { return self(8192) }
  65. internal static var TopMargin: ConstraintAttributes { return self(16384) }
  66. internal static var BottomMargin: ConstraintAttributes { return self(32768) }
  67. internal static var LeadingMargin: ConstraintAttributes { return self(65536) }
  68. internal static var TrailingMargin: ConstraintAttributes { return self(131072) }
  69. internal static var CenterXWithinMargins: ConstraintAttributes { return self(262144) }
  70. internal static var CenterYWithinMargins: ConstraintAttributes { return self(524288) }
  71. #endif
  72. // aggregates
  73. internal static var Edges: ConstraintAttributes { return self(15) }
  74. internal static var Size: ConstraintAttributes { return self(192) }
  75. internal static var Center: ConstraintAttributes { return self(768) }
  76. #if os(iOS)
  77. internal static var Margins: ConstraintAttributes { return self(61440) }
  78. internal static var CenterWithinMargins: ConstraintAttributes { return self(786432) }
  79. #endif
  80. internal var layoutAttributes:[NSLayoutAttribute] {
  81. var attrs = [NSLayoutAttribute]()
  82. if (self & ConstraintAttributes.Left) {
  83. attrs.append(.Left)
  84. }
  85. if (self & ConstraintAttributes.Top) {
  86. attrs.append(.Top)
  87. }
  88. if (self & ConstraintAttributes.Right) {
  89. attrs.append(.Right)
  90. }
  91. if (self & ConstraintAttributes.Bottom) {
  92. attrs.append(.Bottom)
  93. }
  94. if (self & ConstraintAttributes.Leading) {
  95. attrs.append(.Leading)
  96. }
  97. if (self & ConstraintAttributes.Trailing) {
  98. attrs.append(.Trailing)
  99. }
  100. if (self & ConstraintAttributes.Width) {
  101. attrs.append(.Width)
  102. }
  103. if (self & ConstraintAttributes.Height) {
  104. attrs.append(.Height)
  105. }
  106. if (self & ConstraintAttributes.CenterX) {
  107. attrs.append(.CenterX)
  108. }
  109. if (self & ConstraintAttributes.CenterY) {
  110. attrs.append(.CenterY)
  111. }
  112. if (self & ConstraintAttributes.Baseline) {
  113. attrs.append(.Baseline)
  114. }
  115. #if os(iOS)
  116. if (self & ConstraintAttributes.FirstBaseline) {
  117. attrs.append(.FirstBaseline)
  118. }
  119. if (self & ConstraintAttributes.LeftMargin) {
  120. attrs.append(.LeftMargin)
  121. }
  122. if (self & ConstraintAttributes.RightMargin) {
  123. attrs.append(.RightMargin)
  124. }
  125. if (self & ConstraintAttributes.TopMargin) {
  126. attrs.append(.TopMargin)
  127. }
  128. if (self & ConstraintAttributes.BottomMargin) {
  129. attrs.append(.BottomMargin)
  130. }
  131. if (self & ConstraintAttributes.LeadingMargin) {
  132. attrs.append(.LeadingMargin)
  133. }
  134. if (self & ConstraintAttributes.TrailingMargin) {
  135. attrs.append(.TrailingMargin)
  136. }
  137. if (self & ConstraintAttributes.CenterXWithinMargins) {
  138. attrs.append(.CenterXWithinMargins)
  139. }
  140. if (self & ConstraintAttributes.CenterYWithinMargins) {
  141. attrs.append(.CenterYWithinMargins)
  142. }
  143. #endif
  144. return attrs
  145. }
  146. }
  147. internal func +=(inout left: ConstraintAttributes, right: ConstraintAttributes) {
  148. left = (left | right)
  149. }
  150. internal func -=(inout left: ConstraintAttributes, right: ConstraintAttributes) {
  151. left = left & ~right
  152. }
  153. internal func ==(left: ConstraintAttributes, right: ConstraintAttributes) -> Bool {
  154. return left.rawValue == right.rawValue
  155. }