ConstraintMakerRelatable.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 class ConstraintMakerRelatable {
  29. internal let description: ConstraintDescription
  30. internal init(_ description: ConstraintDescription) {
  31. self.description = description
  32. }
  33. internal func relatedTo(_ other: ConstraintRelatableTarget, relation: ConstraintRelation, file: String, line: UInt) -> ConstraintMakerEditable {
  34. let related: ConstraintItem
  35. let constant: ConstraintConstantTarget
  36. if let other = other as? ConstraintItem {
  37. guard other.attributes == ConstraintAttributes.none ||
  38. other.attributes.layoutAttributes.count <= 1 ||
  39. other.attributes.layoutAttributes == self.description.attributes.layoutAttributes ||
  40. other.attributes == .edges && self.description.attributes == .margins ||
  41. other.attributes == .margins && self.description.attributes == .edges else {
  42. fatalError("Cannot constraint to multiple non identical attributes. (\(file), \(line))");
  43. }
  44. related = other
  45. constant = 0.0
  46. } else if let other = other as? ConstraintView {
  47. related = ConstraintItem(target: other, attributes: ConstraintAttributes.none)
  48. constant = 0.0
  49. } else if let other = other as? ConstraintConstantTarget {
  50. related = ConstraintItem(target: nil, attributes: ConstraintAttributes.none)
  51. constant = other
  52. } else if #available(iOS 9.0, OSX 10.11, *), let other = other as? ConstraintLayoutGuide {
  53. related = ConstraintItem(target: other, attributes: ConstraintAttributes.none)
  54. constant = 0.0
  55. } else {
  56. fatalError("Invalid constraint. (\(file), \(line))")
  57. }
  58. let editable = ConstraintMakerEditable(self.description)
  59. editable.description.sourceLocation = (file, line)
  60. editable.description.relation = relation
  61. editable.description.related = related
  62. editable.description.constant = constant
  63. return editable
  64. }
  65. @discardableResult
  66. public func equalTo(_ other: ConstraintRelatableTarget, _ file: String = #file, _ line: UInt = #line) -> ConstraintMakerEditable {
  67. return self.relatedTo(other, relation: .equal, file: file, line: line)
  68. }
  69. @discardableResult
  70. public func equalToSuperview(_ file: String = #file, _ line: UInt = #line) -> ConstraintMakerEditable {
  71. guard let other = self.description.item.superview else {
  72. fatalError("Expected superview but found nil when attempting make constraint `equalToSuperview`.")
  73. }
  74. return self.relatedTo(other, relation: .equal, file: file, line: line)
  75. }
  76. @discardableResult
  77. public func lessThanOrEqualTo(_ other: ConstraintRelatableTarget, _ file: String = #file, _ line: UInt = #line) -> ConstraintMakerEditable {
  78. return self.relatedTo(other, relation: .lessThanOrEqual, file: file, line: line)
  79. }
  80. @discardableResult
  81. public func lessThanOrEqualToSuperview(_ file: String = #file, _ line: UInt = #line) -> ConstraintMakerEditable {
  82. guard let other = self.description.item.superview else {
  83. fatalError("Expected superview but found nil when attempting make constraint `lessThanOrEqualToSuperview`.")
  84. }
  85. return self.relatedTo(other, relation: .lessThanOrEqual, file: file, line: line)
  86. }
  87. @discardableResult
  88. public func greaterThanOrEqualTo(_ other: ConstraintRelatableTarget, _ file: String = #file, line: UInt = #line) -> ConstraintMakerEditable {
  89. return self.relatedTo(other, relation: .greaterThanOrEqual, file: file, line: line)
  90. }
  91. @discardableResult
  92. public func greaterThanOrEqualToSuperview(_ file: String = #file, line: UInt = #line) -> ConstraintMakerEditable {
  93. guard let other = self.description.item.superview else {
  94. fatalError("Expected superview but found nil when attempting make constraint `greaterThanOrEqualToSuperview`.")
  95. }
  96. return self.relatedTo(other, relation: .greaterThanOrEqual, file: file, line: line)
  97. }
  98. }