Constraint.swift 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 Constraint {
  29. internal let sourceLocation: (String, UInt)
  30. internal let label: String?
  31. private let from: ConstraintItem
  32. private let to: ConstraintItem
  33. private let relation: ConstraintRelation
  34. private let multiplier: ConstraintMultiplierTarget
  35. private var constant: ConstraintConstantTarget {
  36. didSet {
  37. self.updateConstantAndPriorityIfNeeded()
  38. }
  39. }
  40. private var priority: ConstraintPriorityTarget {
  41. didSet {
  42. self.updateConstantAndPriorityIfNeeded()
  43. }
  44. }
  45. private let layoutConstraints: NSHashTable<LayoutConstraint>
  46. // MARK: Initialization
  47. internal init(from: ConstraintItem,
  48. to: ConstraintItem,
  49. relation: ConstraintRelation,
  50. sourceLocation: (String, UInt),
  51. label: String?,
  52. multiplier: ConstraintMultiplierTarget,
  53. constant: ConstraintConstantTarget,
  54. priority: ConstraintPriorityTarget) {
  55. self.from = from
  56. self.to = to
  57. self.relation = relation
  58. self.sourceLocation = sourceLocation
  59. self.label = label
  60. self.multiplier = multiplier
  61. self.constant = constant
  62. self.priority = priority
  63. self.layoutConstraints = NSHashTable<LayoutConstraint>.weakObjects()
  64. // get attributes
  65. let layoutFromAttributes = self.from.attributes.layoutAttributes
  66. let layoutToAttributes = self.to.attributes.layoutAttributes
  67. // get layout from
  68. let layoutFrom: ConstraintView = self.from.view!
  69. // get relation
  70. let layoutRelation = self.relation.layoutRelation
  71. for layoutFromAttribute in layoutFromAttributes {
  72. // get layout to attribute
  73. let layoutToAttribute = (layoutToAttributes.count > 0) ? layoutToAttributes[0] : layoutFromAttribute
  74. // get layout constant
  75. let layoutConstant: CGFloat = self.constant.constraintConstantTargetValueFor(layoutAttribute: layoutToAttribute)
  76. // get layout to
  77. #if os(iOS) || os(tvOS)
  78. let layoutTo: AnyObject? = self.to.view ?? self.to.layoutSupport
  79. #else
  80. let layoutTo: AnyObject? = self.to.view
  81. #endif
  82. // create layout constraint
  83. let layoutConstraint = LayoutConstraint(
  84. item: layoutFrom,
  85. attribute: layoutFromAttribute,
  86. relatedBy: layoutRelation,
  87. toItem: layoutTo,
  88. attribute: layoutToAttribute,
  89. multiplier: self.multiplier.constraintMultiplierTargetValue,
  90. constant: layoutConstant
  91. )
  92. // set label
  93. layoutConstraint.label = self.label
  94. // set priority
  95. layoutConstraint.priority = self.priority.constraintPriorityTargetValue
  96. // set constraint
  97. layoutConstraint.constraint = self
  98. // append
  99. self.layoutConstraints.add(layoutConstraint)
  100. }
  101. }
  102. // MARK: Public
  103. public func activate() {
  104. self.activateIfNeeded()
  105. }
  106. public func deactivate() {
  107. self.deactivateIfNeeded()
  108. }
  109. @discardableResult
  110. public func update(offset: ConstraintOffsetTarget) -> Constraint {
  111. self.constant = offset.constraintOffsetTargetValue
  112. return self
  113. }
  114. @discardableResult
  115. public func update(inset: ConstraintInsetTarget) -> Constraint {
  116. self.constant = inset.constraintInsetTargetValue
  117. return self
  118. }
  119. @discardableResult
  120. public func update(priority: ConstraintPriorityTarget) -> Constraint {
  121. self.priority = priority.constraintPriorityTargetValue
  122. return self
  123. }
  124. @available(*, deprecated:0.40.0, message:"Use update(offset: ConstraintOffsetTarget) instead.")
  125. public func updateOffset(amount: ConstraintOffsetTarget) -> Void { self.update(offset: amount) }
  126. @available(*, deprecated:0.40.0, message:"Use update(inset: ConstraintInsetTarget) instead.")
  127. public func updateInsets(amount: ConstraintInsetTarget) -> Void { self.update(inset: amount) }
  128. @available(*, deprecated:0.40.0, message:"Use update(priority: ConstraintPriorityTarget) instead.")
  129. public func updatePriority(amount: ConstraintPriorityTarget) -> Void { self.update(priority: amount) }
  130. @available(*, obsoleted:0.40.0, message:"Use update(priority: ConstraintPriorityTarget) instead.")
  131. public func updatePriorityRequired() -> Void {}
  132. @available(*, obsoleted:0.40.0, message:"Use update(priority: ConstraintPriorityTarget) instead.")
  133. public func updatePriorityHigh() -> Void { fatalError("Must be implemented by Concrete subclass.") }
  134. @available(*, obsoleted:0.40.0, message:"Use update(priority: ConstraintPriorityTarget) instead.")
  135. public func updatePriorityMedium() -> Void { fatalError("Must be implemented by Concrete subclass.") }
  136. @available(*, obsoleted:0.40.0, message:"Use update(priority: ConstraintPriorityTarget) instead.")
  137. public func updatePriorityLow() -> Void { fatalError("Must be implemented by Concrete subclass.") }
  138. // MARK: Internal
  139. internal func updateConstantAndPriorityIfNeeded() {
  140. for layoutConstraint in self.layoutConstraints.allObjects {
  141. let attribute = (layoutConstraint.secondAttribute == .notAnAttribute) ? layoutConstraint.firstAttribute : layoutConstraint.secondAttribute
  142. layoutConstraint.constant = self.constant.constraintConstantTargetValueFor(layoutAttribute: attribute)
  143. layoutConstraint.priority = self.priority.constraintPriorityTargetValue
  144. }
  145. }
  146. internal func activateIfNeeded(updatingExisting: Bool = false) {
  147. let view = self.from.view!
  148. let layoutConstraints = self.layoutConstraints.allObjects
  149. let existingLayoutConstraints = view.snp.layoutConstraints
  150. if updatingExisting && existingLayoutConstraints.count > 0 {
  151. for layoutConstraint in layoutConstraints {
  152. let existingLayoutConstraint = existingLayoutConstraints.first { $0 == layoutConstraint }
  153. guard let updateLayoutConstraint = existingLayoutConstraint else {
  154. fatalError("Updated constraint could not find existing matching constraint to update: \(layoutConstraint)")
  155. }
  156. let updateLayoutAttribute = (updateLayoutConstraint.secondAttribute == .notAnAttribute) ? updateLayoutConstraint.firstAttribute : updateLayoutConstraint.secondAttribute
  157. updateLayoutConstraint.constant = self.constant.constraintConstantTargetValueFor(layoutAttribute: updateLayoutAttribute)
  158. }
  159. } else {
  160. NSLayoutConstraint.activate(layoutConstraints)
  161. view.snp.add(layoutConstraints: layoutConstraints)
  162. }
  163. }
  164. internal func deactivateIfNeeded() {
  165. let view = self.from.view!
  166. let layoutConstraints = self.layoutConstraints.allObjects
  167. NSLayoutConstraint.deactivate(layoutConstraints)
  168. view.snp.remove(layoutConstraints: layoutConstraints)
  169. }
  170. }
  171. //extension Constraint: Hashable {
  172. //
  173. // public var hashValue: Int {
  174. // return self.layoutConstraints.hashValue
  175. // }
  176. //
  177. //}
  178. //
  179. //public func ==(lhs: Constraint, rhs: Constraint) -> Bool {
  180. // return (lhs.from == rhs.from &&
  181. // lhs.to == rhs.to &&
  182. // lhs.relation == rhs.relation &&
  183. // lhs.multiplier.constraintMultiplierTargetValue == rhs.multiplier.constraintMultiplierTargetValue &&
  184. // lhs.priority.constraintPriorityTargetValue == rhs.priority.constraintPriorityTargetValue)
  185. //}