ConstraintMaker.swift 7.9 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 ConstraintMaker {
  29. public var left: ConstraintMakerExtendable {
  30. return self.makeExtendableWithAttributes(.left)
  31. }
  32. public var top: ConstraintMakerExtendable {
  33. return self.makeExtendableWithAttributes(.top)
  34. }
  35. public var bottom: ConstraintMakerExtendable {
  36. return self.makeExtendableWithAttributes(.bottom)
  37. }
  38. public var right: ConstraintMakerExtendable {
  39. return self.makeExtendableWithAttributes(.right)
  40. }
  41. public var leading: ConstraintMakerExtendable {
  42. return self.makeExtendableWithAttributes(.leading)
  43. }
  44. public var trailing: ConstraintMakerExtendable {
  45. return self.makeExtendableWithAttributes(.trailing)
  46. }
  47. public var width: ConstraintMakerExtendable {
  48. return self.makeExtendableWithAttributes(.width)
  49. }
  50. public var height: ConstraintMakerExtendable {
  51. return self.makeExtendableWithAttributes(.height)
  52. }
  53. public var centerX: ConstraintMakerExtendable {
  54. return self.makeExtendableWithAttributes(.centerX)
  55. }
  56. public var centerY: ConstraintMakerExtendable {
  57. return self.makeExtendableWithAttributes(.centerY)
  58. }
  59. @available(*, deprecated, renamed:"lastBaseline")
  60. public var baseline: ConstraintMakerExtendable {
  61. return self.makeExtendableWithAttributes(.lastBaseline)
  62. }
  63. public var lastBaseline: ConstraintMakerExtendable {
  64. return self.makeExtendableWithAttributes(.lastBaseline)
  65. }
  66. @available(iOS 8.0, OSX 10.11, *)
  67. public var firstBaseline: ConstraintMakerExtendable {
  68. return self.makeExtendableWithAttributes(.firstBaseline)
  69. }
  70. @available(iOS 8.0, *)
  71. public var leftMargin: ConstraintMakerExtendable {
  72. return self.makeExtendableWithAttributes(.leftMargin)
  73. }
  74. @available(iOS 8.0, *)
  75. public var rightMargin: ConstraintMakerExtendable {
  76. return self.makeExtendableWithAttributes(.rightMargin)
  77. }
  78. @available(iOS 8.0, *)
  79. public var topMargin: ConstraintMakerExtendable {
  80. return self.makeExtendableWithAttributes(.topMargin)
  81. }
  82. @available(iOS 8.0, *)
  83. public var bottomMargin: ConstraintMakerExtendable {
  84. return self.makeExtendableWithAttributes(.bottomMargin)
  85. }
  86. @available(iOS 8.0, *)
  87. public var leadingMargin: ConstraintMakerExtendable {
  88. return self.makeExtendableWithAttributes(.leadingMargin)
  89. }
  90. @available(iOS 8.0, *)
  91. public var trailingMargin: ConstraintMakerExtendable {
  92. return self.makeExtendableWithAttributes(.trailingMargin)
  93. }
  94. @available(iOS 8.0, *)
  95. public var centerXWithinMargins: ConstraintMakerExtendable {
  96. return self.makeExtendableWithAttributes(.centerXWithinMargins)
  97. }
  98. @available(iOS 8.0, *)
  99. public var centerYWithinMargins: ConstraintMakerExtendable {
  100. return self.makeExtendableWithAttributes(.centerYWithinMargins)
  101. }
  102. public var edges: ConstraintMakerExtendable {
  103. return self.makeExtendableWithAttributes(.edges)
  104. }
  105. public var horizontalEdges: ConstraintMakerExtendable {
  106. return self.makeExtendableWithAttributes(.horizontalEdges)
  107. }
  108. public var verticalEdges: ConstraintMakerExtendable {
  109. return self.makeExtendableWithAttributes(.verticalEdges)
  110. }
  111. public var directionalEdges: ConstraintMakerExtendable {
  112. return self.makeExtendableWithAttributes(.directionalEdges)
  113. }
  114. public var directionalHorizontalEdges: ConstraintMakerExtendable {
  115. return self.makeExtendableWithAttributes(.directionalHorizontalEdges)
  116. }
  117. public var directionalVerticalEdges: ConstraintMakerExtendable {
  118. return self.makeExtendableWithAttributes(.directionalVerticalEdges)
  119. }
  120. public var size: ConstraintMakerExtendable {
  121. return self.makeExtendableWithAttributes(.size)
  122. }
  123. public var center: ConstraintMakerExtendable {
  124. return self.makeExtendableWithAttributes(.center)
  125. }
  126. @available(iOS 8.0, *)
  127. public var margins: ConstraintMakerExtendable {
  128. return self.makeExtendableWithAttributes(.margins)
  129. }
  130. @available(iOS 8.0, *)
  131. public var directionalMargins: ConstraintMakerExtendable {
  132. return self.makeExtendableWithAttributes(.directionalMargins)
  133. }
  134. @available(iOS 8.0, *)
  135. public var centerWithinMargins: ConstraintMakerExtendable {
  136. return self.makeExtendableWithAttributes(.centerWithinMargins)
  137. }
  138. public let item: LayoutConstraintItem
  139. private var descriptions = [ConstraintDescription]()
  140. internal init(item: LayoutConstraintItem) {
  141. self.item = item
  142. self.item.prepare()
  143. }
  144. internal func makeExtendableWithAttributes(_ attributes: ConstraintAttributes) -> ConstraintMakerExtendable {
  145. let description = ConstraintDescription(item: self.item, attributes: attributes)
  146. self.descriptions.append(description)
  147. return ConstraintMakerExtendable(description)
  148. }
  149. internal static func prepareConstraints(item: LayoutConstraintItem, closure: (_ make: ConstraintMaker) -> Void) -> [Constraint] {
  150. let maker = ConstraintMaker(item: item)
  151. closure(maker)
  152. var constraints: [Constraint] = []
  153. for description in maker.descriptions {
  154. guard let constraint = description.constraint else {
  155. continue
  156. }
  157. constraints.append(constraint)
  158. }
  159. return constraints
  160. }
  161. internal static func makeConstraints(item: LayoutConstraintItem, closure: (_ make: ConstraintMaker) -> Void) {
  162. let constraints = prepareConstraints(item: item, closure: closure)
  163. for constraint in constraints {
  164. constraint.activateIfNeeded(updatingExisting: false)
  165. }
  166. }
  167. internal static func remakeConstraints(item: LayoutConstraintItem, closure: (_ make: ConstraintMaker) -> Void) {
  168. self.removeConstraints(item: item)
  169. self.makeConstraints(item: item, closure: closure)
  170. }
  171. internal static func updateConstraints(item: LayoutConstraintItem, closure: (_ make: ConstraintMaker) -> Void) {
  172. guard item.constraints.count > 0 else {
  173. self.makeConstraints(item: item, closure: closure)
  174. return
  175. }
  176. let constraints = prepareConstraints(item: item, closure: closure)
  177. for constraint in constraints {
  178. constraint.activateIfNeeded(updatingExisting: true)
  179. }
  180. }
  181. internal static func removeConstraints(item: LayoutConstraintItem) {
  182. let constraints = item.constraints
  183. for constraint in constraints {
  184. constraint.deactivateIfNeeded()
  185. }
  186. }
  187. }