ConstraintMakerRelatable.swift 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 greaterThanOrEqualTo(_ other: ConstraintRelatableTarget, _ file: String = #file, line: UInt = #line) -> ConstraintMakerEditable {
  79. return self.relatedTo(other, relation: .greaterThanOrEqual, file: file, line: line)
  80. }
  81. }