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.

160 lines
5.9 KiB

5 years ago
  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. public extension LayoutConstraint {
  29. override public var description: String {
  30. var description = "<"
  31. description += descriptionForObject(self)
  32. if let firstItem = conditionalOptional(from: self.firstItem) {
  33. description += " \(descriptionForObject(firstItem))"
  34. }
  35. if self.firstAttribute != .notAnAttribute {
  36. description += ".\(descriptionForAttribute(self.firstAttribute))"
  37. }
  38. description += " \(descriptionForRelation(self.relation))"
  39. if let secondItem = self.secondItem {
  40. description += " \(descriptionForObject(secondItem))"
  41. }
  42. if self.secondAttribute != .notAnAttribute {
  43. description += ".\(descriptionForAttribute(self.secondAttribute))"
  44. }
  45. if self.multiplier != 1.0 {
  46. description += " * \(self.multiplier)"
  47. }
  48. if self.secondAttribute == .notAnAttribute {
  49. description += " \(self.constant)"
  50. } else {
  51. if self.constant > 0.0 {
  52. description += " + \(self.constant)"
  53. } else if self.constant < 0.0 {
  54. description += " - \(abs(self.constant))"
  55. }
  56. }
  57. if self.priority.rawValue != 1000.0 {
  58. description += " ^\(self.priority)"
  59. }
  60. description += ">"
  61. return description
  62. }
  63. }
  64. private func descriptionForRelation(_ relation: LayoutRelation) -> String {
  65. switch relation {
  66. case .equal: return "=="
  67. case .greaterThanOrEqual: return ">="
  68. case .lessThanOrEqual: return "<="
  69. }
  70. }
  71. private func descriptionForAttribute(_ attribute: LayoutAttribute) -> String {
  72. #if os(iOS) || os(tvOS)
  73. switch attribute {
  74. case .notAnAttribute: return "notAnAttribute"
  75. case .top: return "top"
  76. case .left: return "left"
  77. case .bottom: return "bottom"
  78. case .right: return "right"
  79. case .leading: return "leading"
  80. case .trailing: return "trailing"
  81. case .width: return "width"
  82. case .height: return "height"
  83. case .centerX: return "centerX"
  84. case .centerY: return "centerY"
  85. case .lastBaseline: return "lastBaseline"
  86. case .firstBaseline: return "firstBaseline"
  87. case .topMargin: return "topMargin"
  88. case .leftMargin: return "leftMargin"
  89. case .bottomMargin: return "bottomMargin"
  90. case .rightMargin: return "rightMargin"
  91. case .leadingMargin: return "leadingMargin"
  92. case .trailingMargin: return "trailingMargin"
  93. case .centerXWithinMargins: return "centerXWithinMargins"
  94. case .centerYWithinMargins: return "centerYWithinMargins"
  95. }
  96. #else
  97. switch attribute {
  98. case .notAnAttribute: return "notAnAttribute"
  99. case .top: return "top"
  100. case .left: return "left"
  101. case .bottom: return "bottom"
  102. case .right: return "right"
  103. case .leading: return "leading"
  104. case .trailing: return "trailing"
  105. case .width: return "width"
  106. case .height: return "height"
  107. case .centerX: return "centerX"
  108. case .centerY: return "centerY"
  109. case .lastBaseline: return "lastBaseline"
  110. case .firstBaseline: return "firstBaseline"
  111. }
  112. #endif
  113. }
  114. private func conditionalOptional<T>(from object: Optional<T>) -> Optional<T> {
  115. return object
  116. }
  117. private func conditionalOptional<T>(from object: T) -> Optional<T> {
  118. return Optional.some(object)
  119. }
  120. private func descriptionForObject(_ object: AnyObject) -> String {
  121. let pointerDescription = String(format: "%p", UInt(bitPattern: ObjectIdentifier(object)))
  122. var desc = ""
  123. desc += type(of: object).description()
  124. if let object = object as? ConstraintView {
  125. desc += ":\(object.snp.label() ?? pointerDescription)"
  126. } else if let object = object as? LayoutConstraint {
  127. desc += ":\(object.label ?? pointerDescription)"
  128. } else {
  129. desc += ":\(pointerDescription)"
  130. }
  131. if let object = object as? LayoutConstraint, let file = object.constraint?.sourceLocation.0, let line = object.constraint?.sourceLocation.1 {
  132. desc += "@\((file as NSString).lastPathComponent)#\(line)"
  133. }
  134. desc += ""
  135. return desc
  136. }