ConstraintMakerRelatable.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 {
  53. fatalError("Invalid constraint. (\(file), \(line))")
  54. }
  55. let editable = ConstraintMakerEditable(self.description)
  56. editable.description.sourceLocation = (file, line)
  57. editable.description.relation = relation
  58. editable.description.related = related
  59. editable.description.constant = constant
  60. return editable
  61. }
  62. @discardableResult
  63. public func equalTo(_ other: ConstraintRelatableTarget, _ file: String = #file, _ line: UInt = #line) -> ConstraintMakerEditable {
  64. return self.relatedTo(other, relation: .equal, file: file, line: line)
  65. }
  66. @discardableResult
  67. public func equalToSuperview(_ file: String = #file, _ line: UInt = #line) -> ConstraintMakerEditable {
  68. guard let other = self.description.view.superview else {
  69. fatalError("Expected superview but found nil when attempting make constraint `equalToSuperview`.")
  70. }
  71. return self.relatedTo(other, relation: .equal, file: file, line: line)
  72. }
  73. @discardableResult
  74. public func lessThanOrEqualTo(_ other: ConstraintRelatableTarget, _ file: String = #file, _ line: UInt = #line) -> ConstraintMakerEditable {
  75. return self.relatedTo(other, relation: .lessThanOrEqual, file: file, line: line)
  76. }
  77. @discardableResult
  78. public func lessThanOrEqualToSuperview(_ file: String = #file, _ line: UInt = #line) -> ConstraintMakerEditable {
  79. guard let other = self.description.view.superview else {
  80. fatalError("Expected superview but found nil when attempting make constraint `lessThanOrEqualToSuperview`.")
  81. }
  82. return self.relatedTo(other, relation: .lessThanOrEqual, file: file, line: line)
  83. }
  84. @discardableResult
  85. public func greaterThanOrEqualTo(_ other: ConstraintRelatableTarget, _ file: String = #file, line: UInt = #line) -> ConstraintMakerEditable {
  86. return self.relatedTo(other, relation: .greaterThanOrEqual, file: file, line: line)
  87. }
  88. @discardableResult
  89. public func greaterThanOrEqualToSuperview(_ file: String = #file, line: UInt = #line) -> ConstraintMakerEditable {
  90. guard let other = self.description.view.superview else {
  91. fatalError("Expected superview but found nil when attempting make constraint `greaterThanOrEqualToSuperview`.")
  92. }
  93. return self.relatedTo(other, relation: .greaterThanOrEqual, file: file, line: line)
  94. }
  95. }