123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- #if os(iOS) || os(tvOS)
- import UIKit
- #else
- import AppKit
- #endif
- public class ConstraintMakerRelatable {
-
- internal let description: ConstraintDescription
-
- internal init(_ description: ConstraintDescription) {
- self.description = description
- }
-
- internal func relatedTo(_ other: ConstraintRelatableTarget, relation: ConstraintRelation, file: String, line: UInt) -> ConstraintMakerEditable {
- let related: ConstraintItem
- let constant: ConstraintConstantTarget
-
- if let other = other as? ConstraintItem {
- guard other.attributes == ConstraintAttributes.none ||
- other.attributes.layoutAttributes.count <= 1 ||
- other.attributes.layoutAttributes == self.description.attributes.layoutAttributes ||
- other.attributes == .edges && self.description.attributes == .margins ||
- other.attributes == .margins && self.description.attributes == .edges else {
- fatalError("Cannot constraint to multiple non identical attributes. (\(file), \(line))");
- }
-
- related = other
- constant = 0.0
- } else if let other = other as? ConstraintView {
- related = ConstraintItem(target: other, attributes: ConstraintAttributes.none)
- constant = 0.0
- } else if let other = other as? ConstraintConstantTarget {
- related = ConstraintItem(target: nil, attributes: ConstraintAttributes.none)
- constant = other
- } else if #available(iOS 9.0, OSX 10.11, *), let other = other as? ConstraintLayoutGuide {
- related = ConstraintItem(target: other, attributes: ConstraintAttributes.none)
- constant = 0.0
- } else {
- fatalError("Invalid constraint. (\(file), \(line))")
- }
-
- let editable = ConstraintMakerEditable(self.description)
- editable.description.sourceLocation = (file, line)
- editable.description.relation = relation
- editable.description.related = related
- editable.description.constant = constant
- return editable
- }
-
- @discardableResult
- public func equalTo(_ other: ConstraintRelatableTarget, _ file: String = #file, _ line: UInt = #line) -> ConstraintMakerEditable {
- return self.relatedTo(other, relation: .equal, file: file, line: line)
- }
-
- @discardableResult
- public func equalToSuperview(_ file: String = #file, _ line: UInt = #line) -> ConstraintMakerEditable {
- guard let other = self.description.item.superview else {
- fatalError("Expected superview but found nil when attempting make constraint `equalToSuperview`.")
- }
- return self.relatedTo(other, relation: .equal, file: file, line: line)
- }
-
- @discardableResult
- public func lessThanOrEqualTo(_ other: ConstraintRelatableTarget, _ file: String = #file, _ line: UInt = #line) -> ConstraintMakerEditable {
- return self.relatedTo(other, relation: .lessThanOrEqual, file: file, line: line)
- }
-
- @discardableResult
- public func lessThanOrEqualToSuperview(_ file: String = #file, _ line: UInt = #line) -> ConstraintMakerEditable {
- guard let other = self.description.item.superview else {
- fatalError("Expected superview but found nil when attempting make constraint `lessThanOrEqualToSuperview`.")
- }
- return self.relatedTo(other, relation: .lessThanOrEqual, file: file, line: line)
- }
-
- @discardableResult
- public func greaterThanOrEqualTo(_ other: ConstraintRelatableTarget, _ file: String = #file, line: UInt = #line) -> ConstraintMakerEditable {
- return self.relatedTo(other, relation: .greaterThanOrEqual, file: file, line: line)
- }
-
- @discardableResult
- public func greaterThanOrEqualToSuperview(_ file: String = #file, line: UInt = #line) -> ConstraintMakerEditable {
- guard let other = self.description.item.superview else {
- fatalError("Expected superview but found nil when attempting make constraint `greaterThanOrEqualToSuperview`.")
- }
- return self.relatedTo(other, relation: .greaterThanOrEqual, file: file, line: line)
- }
- }
|