ConstraintAttributes.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // Snappy
  3. //
  4. // Copyright (c) 2011-2014 Masonry Team - https://github.com/Masonry
  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. * ConstraintAttributes is an options set that maps to NSLayoutAttributes.
  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. func toRaw() -> UInt { return self.rawValue }
  46. static func fromRaw(raw: UInt) -> ConstraintAttributes? { return self(raw) }
  47. static func fromMask(raw: UInt) -> ConstraintAttributes { return self(raw) }
  48. internal static var None: ConstraintAttributes { return self(0) }
  49. internal static var Left: ConstraintAttributes { return self(1) }
  50. internal static var Top: ConstraintAttributes { return self(2) }
  51. internal static var Right: ConstraintAttributes { return self(4) }
  52. internal static var Bottom: ConstraintAttributes { return self(8) }
  53. internal static var Leading: ConstraintAttributes { return self(16) }
  54. internal static var Trailing: ConstraintAttributes { return self(32) }
  55. internal static var Width: ConstraintAttributes { return self(64) }
  56. internal static var Height: ConstraintAttributes { return self(128) }
  57. internal static var CenterX: ConstraintAttributes { return self(256) }
  58. internal static var CenterY: ConstraintAttributes { return self(512) }
  59. internal static var Baseline: ConstraintAttributes { return self(1024) }
  60. internal static var Edges: ConstraintAttributes { return self(15) }
  61. internal static var Size: ConstraintAttributes { return self(192) }
  62. internal static var Center: ConstraintAttributes { return self(768) }
  63. internal var layoutAttributes:Array<NSLayoutAttribute> {
  64. var attrs: Array<NSLayoutAttribute> = []
  65. if (self & ConstraintAttributes.Left) {
  66. attrs.append(.Left)
  67. }
  68. if (self & ConstraintAttributes.Top) {
  69. attrs.append(.Top)
  70. }
  71. if (self & ConstraintAttributes.Right) {
  72. attrs.append(.Right)
  73. }
  74. if (self & ConstraintAttributes.Bottom) {
  75. attrs.append(.Bottom)
  76. }
  77. if (self & ConstraintAttributes.Leading) {
  78. attrs.append(.Leading)
  79. }
  80. if (self & ConstraintAttributes.Trailing) {
  81. attrs.append(.Trailing)
  82. }
  83. if (self & ConstraintAttributes.Width) {
  84. attrs.append(.Width)
  85. }
  86. if (self & ConstraintAttributes.Height) {
  87. attrs.append(.Height)
  88. }
  89. if (self & ConstraintAttributes.CenterX) {
  90. attrs.append(.CenterX)
  91. }
  92. if (self & ConstraintAttributes.CenterY) {
  93. attrs.append(.CenterY)
  94. }
  95. if (self & ConstraintAttributes.Baseline) {
  96. attrs.append(.Baseline)
  97. }
  98. return attrs
  99. }
  100. }
  101. internal func += (inout left: ConstraintAttributes, right: ConstraintAttributes) {
  102. left = (left | right)
  103. }
  104. internal func -= (inout left: ConstraintAttributes, right: ConstraintAttributes) {
  105. left = left & ~right
  106. }
  107. internal func == (left: ConstraintAttributes, right: ConstraintAttributes) -> Bool {
  108. return left.rawValue == right.rawValue
  109. }