ConstraintDescription.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. //
  2. // SnapKit
  3. //
  4. // Copyright (c) 2011-2015 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. // Reduce duplication by centralizing casts
  29. public protocol FloatConvertible {
  30. var floatValue : Float { get }
  31. }
  32. extension Float : FloatConvertible {
  33. public var floatValue : Float {
  34. return self
  35. }
  36. }
  37. extension Int : FloatConvertible {
  38. public var floatValue : Float {
  39. return Float(self)
  40. }
  41. }
  42. extension UInt : FloatConvertible {
  43. public var floatValue : Float {
  44. return Float(self)
  45. }
  46. }
  47. extension Double : FloatConvertible {
  48. public var floatValue : Float {
  49. return Float(self)
  50. }
  51. }
  52. extension CGFloat : FloatConvertible {
  53. public var floatValue : Float {
  54. return Float(self)
  55. }
  56. }
  57. /**
  58. Used to expose the final API of a `ConstraintDescription` which allows getting a constraint from it
  59. */
  60. public protocol ConstraintDescriptionFinalizable: class {
  61. var constraint: Constraint { get }
  62. }
  63. /**
  64. Used to expose priority APIs
  65. */
  66. public protocol ConstraintDescriptionPriortizable: ConstraintDescriptionFinalizable {
  67. func priority(priority: FloatConvertible) -> ConstraintDescriptionFinalizable
  68. func priorityRequired() -> ConstraintDescriptionFinalizable
  69. func priorityHigh() -> ConstraintDescriptionFinalizable
  70. func priorityMedium() -> ConstraintDescriptionFinalizable
  71. func priorityLow() -> ConstraintDescriptionFinalizable
  72. }
  73. /**
  74. Used to expose multiplier & constant APIs
  75. */
  76. public protocol ConstraintDescriptionEditable: ConstraintDescriptionPriortizable {
  77. func multipliedBy(amount: FloatConvertible) -> ConstraintDescriptionEditable
  78. func dividedBy(amount: FloatConvertible) -> ConstraintDescriptionEditable
  79. func offset(amount: FloatConvertible) -> ConstraintDescriptionEditable
  80. func offset(amount: CGPoint) -> ConstraintDescriptionEditable
  81. func offset(amount: CGSize) -> ConstraintDescriptionEditable
  82. func offset(amount: EdgeInsets) -> ConstraintDescriptionEditable
  83. func inset(amount: FloatConvertible) -> ConstraintDescriptionEditable
  84. func inset(amount: EdgeInsets) -> ConstraintDescriptionEditable
  85. }
  86. /**
  87. Used to expose relation APIs
  88. */
  89. public protocol ConstraintDescriptionRelatable: class {
  90. func equalTo(other: ConstraintItem) -> ConstraintDescriptionEditable
  91. func equalTo(other: View) -> ConstraintDescriptionEditable
  92. @available(iOS 7.0, *)
  93. func equalTo(other: LayoutSupport) -> ConstraintDescriptionEditable
  94. @available(iOS 9.0, OSX 10.11, *)
  95. func equalTo(other: NSLayoutAnchor) -> ConstraintDescriptionEditable
  96. func equalTo(other: FloatConvertible) -> ConstraintDescriptionEditable
  97. func equalTo(other: CGSize) -> ConstraintDescriptionEditable
  98. func equalTo(other: CGPoint) -> ConstraintDescriptionEditable
  99. func equalTo(other: EdgeInsets) -> ConstraintDescriptionEditable
  100. func lessThanOrEqualTo(other: ConstraintItem) -> ConstraintDescriptionEditable
  101. func lessThanOrEqualTo(other: View) -> ConstraintDescriptionEditable
  102. @available(iOS 7.0, *)
  103. func lessThanOrEqualTo(other: LayoutSupport) -> ConstraintDescriptionEditable
  104. @available(iOS 9.0, OSX 10.11, *)
  105. func lessThanOrEqualTo(other: NSLayoutAnchor) -> ConstraintDescriptionEditable
  106. func lessThanOrEqualTo(other: FloatConvertible) -> ConstraintDescriptionEditable
  107. func lessThanOrEqualTo(other: CGSize) -> ConstraintDescriptionEditable
  108. func lessThanOrEqualTo(other: CGPoint) -> ConstraintDescriptionEditable
  109. func lessThanOrEqualTo(other: EdgeInsets) -> ConstraintDescriptionEditable
  110. func greaterThanOrEqualTo(other: ConstraintItem) -> ConstraintDescriptionEditable
  111. func greaterThanOrEqualTo(other: View) -> ConstraintDescriptionEditable
  112. @available(iOS 7.0, *)
  113. func greaterThanOrEqualTo(other: LayoutSupport) -> ConstraintDescriptionEditable
  114. @available(iOS 9.0, OSX 10.11, *)
  115. func greaterThanOrEqualTo(other: NSLayoutAnchor) -> ConstraintDescriptionEditable
  116. func greaterThanOrEqualTo(other: FloatConvertible) -> ConstraintDescriptionEditable
  117. func greaterThanOrEqualTo(other: CGSize) -> ConstraintDescriptionEditable
  118. func greaterThanOrEqualTo(other: CGPoint) -> ConstraintDescriptionEditable
  119. func greaterThanOrEqualTo(other: EdgeInsets) -> ConstraintDescriptionEditable
  120. }
  121. /**
  122. Used to expose chaining APIs
  123. */
  124. public protocol ConstraintDescriptionExtendable: ConstraintDescriptionRelatable {
  125. var left: ConstraintDescriptionExtendable { get }
  126. var top: ConstraintDescriptionExtendable { get }
  127. var bottom: ConstraintDescriptionExtendable { get }
  128. var right: ConstraintDescriptionExtendable { get }
  129. var leading: ConstraintDescriptionExtendable { get }
  130. var trailing: ConstraintDescriptionExtendable { get }
  131. var width: ConstraintDescriptionExtendable { get }
  132. var height: ConstraintDescriptionExtendable { get }
  133. var centerX: ConstraintDescriptionExtendable { get }
  134. var centerY: ConstraintDescriptionExtendable { get }
  135. var baseline: ConstraintDescriptionExtendable { get }
  136. @available(iOS 8.0, *)
  137. var firstBaseline: ConstraintDescriptionExtendable { get }
  138. @available(iOS 8.0, *)
  139. var leftMargin: ConstraintDescriptionExtendable { get }
  140. @available(iOS 8.0, *)
  141. var rightMargin: ConstraintDescriptionExtendable { get }
  142. @available(iOS 8.0, *)
  143. var topMargin: ConstraintDescriptionExtendable { get }
  144. @available(iOS 8.0, *)
  145. var bottomMargin: ConstraintDescriptionExtendable { get }
  146. @available(iOS 8.0, *)
  147. var leadingMargin: ConstraintDescriptionExtendable { get }
  148. @available(iOS 8.0, *)
  149. var trailingMargin: ConstraintDescriptionExtendable { get }
  150. @available(iOS 8.0, *)
  151. var centerXWithinMargins: ConstraintDescriptionExtendable { get }
  152. @available(iOS 8.0, *)
  153. var centerYWithinMargins: ConstraintDescriptionExtendable { get }
  154. }
  155. /**
  156. Used to internally manage building constraint
  157. */
  158. internal class ConstraintDescription: ConstraintDescriptionExtendable, ConstraintDescriptionEditable, ConstraintDescriptionFinalizable {
  159. internal var left: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Left) }
  160. internal var top: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Top) }
  161. internal var right: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Right) }
  162. internal var bottom: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Bottom) }
  163. internal var leading: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Leading) }
  164. internal var trailing: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Trailing) }
  165. internal var width: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Width) }
  166. internal var height: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Height) }
  167. internal var centerX: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterX) }
  168. internal var centerY: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterY) }
  169. internal var baseline: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.Baseline) }
  170. @available(iOS 8.0, *)
  171. internal var firstBaseline: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.FirstBaseline) }
  172. @available(iOS 8.0, *)
  173. internal var leftMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.LeftMargin) }
  174. @available(iOS 8.0, *)
  175. internal var rightMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.RightMargin) }
  176. @available(iOS 8.0, *)
  177. internal var topMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.TopMargin) }
  178. @available(iOS 8.0, *)
  179. internal var bottomMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.BottomMargin) }
  180. @available(iOS 8.0, *)
  181. internal var leadingMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.LeadingMargin) }
  182. @available(iOS 8.0, *)
  183. internal var trailingMargin: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.TrailingMargin) }
  184. @available(iOS 8.0, *)
  185. internal var centerXWithinMargins: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterXWithinMargins) }
  186. @available(iOS 8.0, *)
  187. internal var centerYWithinMargins: ConstraintDescriptionExtendable { return self.addConstraint(ConstraintAttributes.CenterYWithinMargins) }
  188. // MARK: initializer
  189. init(fromItem: ConstraintItem) {
  190. self.fromItem = fromItem
  191. self.toItem = ConstraintItem(object: nil, attributes: ConstraintAttributes.None)
  192. }
  193. // MARK: equalTo
  194. internal func equalTo(other: ConstraintItem) -> ConstraintDescriptionEditable {
  195. return self.constrainTo(other, relation: .Equal)
  196. }
  197. internal func equalTo(other: View) -> ConstraintDescriptionEditable {
  198. return self.constrainTo(other, relation: .Equal)
  199. }
  200. @available(iOS 7.0, *)
  201. internal func equalTo(other: LayoutSupport) -> ConstraintDescriptionEditable {
  202. return self.constrainTo(other, relation: .Equal)
  203. }
  204. @available(iOS 9.0, OSX 10.11, *)
  205. internal func equalTo(other: NSLayoutAnchor) -> ConstraintDescriptionEditable {
  206. return self.constrainTo(other, relation: .Equal)
  207. }
  208. internal func equalTo(other: FloatConvertible) -> ConstraintDescriptionEditable {
  209. return self.constrainTo(other, relation: .Equal)
  210. }
  211. internal func equalTo(other: CGSize) -> ConstraintDescriptionEditable {
  212. return self.constrainTo(other, relation: .Equal)
  213. }
  214. internal func equalTo(other: CGPoint) -> ConstraintDescriptionEditable {
  215. return self.constrainTo(other, relation: .Equal)
  216. }
  217. internal func equalTo(other: EdgeInsets) -> ConstraintDescriptionEditable {
  218. return self.constrainTo(other, relation: .Equal)
  219. }
  220. // MARK: lessThanOrEqualTo
  221. internal func lessThanOrEqualTo(other: ConstraintItem) -> ConstraintDescriptionEditable {
  222. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  223. }
  224. internal func lessThanOrEqualTo(other: View) -> ConstraintDescriptionEditable {
  225. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  226. }
  227. @available(iOS 7.0, *)
  228. internal func lessThanOrEqualTo(other: LayoutSupport) -> ConstraintDescriptionEditable {
  229. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  230. }
  231. @available(iOS 9.0, OSX 10.11, *)
  232. internal func lessThanOrEqualTo(other: NSLayoutAnchor) -> ConstraintDescriptionEditable {
  233. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  234. }
  235. internal func lessThanOrEqualTo(other: FloatConvertible) -> ConstraintDescriptionEditable {
  236. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  237. }
  238. internal func lessThanOrEqualTo(other: CGSize) -> ConstraintDescriptionEditable {
  239. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  240. }
  241. internal func lessThanOrEqualTo(other: CGPoint) -> ConstraintDescriptionEditable {
  242. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  243. }
  244. internal func lessThanOrEqualTo(other: EdgeInsets) -> ConstraintDescriptionEditable {
  245. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  246. }
  247. // MARK: greaterThanOrEqualTo
  248. internal func greaterThanOrEqualTo(other: ConstraintItem) -> ConstraintDescriptionEditable {
  249. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  250. }
  251. internal func greaterThanOrEqualTo(other: View) -> ConstraintDescriptionEditable {
  252. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  253. }
  254. @available(iOS 7.0, *)
  255. internal func greaterThanOrEqualTo(other: LayoutSupport) -> ConstraintDescriptionEditable {
  256. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  257. }
  258. @available(iOS 9.0, OSX 10.11, *)
  259. internal func greaterThanOrEqualTo(other: NSLayoutAnchor) -> ConstraintDescriptionEditable {
  260. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  261. }
  262. internal func greaterThanOrEqualTo(other: FloatConvertible) -> ConstraintDescriptionEditable {
  263. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  264. }
  265. internal func greaterThanOrEqualTo(other: CGSize) -> ConstraintDescriptionEditable {
  266. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  267. }
  268. internal func greaterThanOrEqualTo(other: CGPoint) -> ConstraintDescriptionEditable {
  269. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  270. }
  271. internal func greaterThanOrEqualTo(other: EdgeInsets) -> ConstraintDescriptionEditable {
  272. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  273. }
  274. // MARK: multiplier
  275. internal func multipliedBy(amount: FloatConvertible) -> ConstraintDescriptionEditable {
  276. self.multiplier = amount.floatValue
  277. return self
  278. }
  279. internal func dividedBy(amount: FloatConvertible) -> ConstraintDescriptionEditable {
  280. self.multiplier = 1.0 / amount.floatValue;
  281. return self
  282. }
  283. // MARK: offset
  284. internal func offset(amount: FloatConvertible) -> ConstraintDescriptionEditable {
  285. self.constant = amount.floatValue
  286. return self
  287. }
  288. internal func offset(amount: CGPoint) -> ConstraintDescriptionEditable {
  289. self.constant = amount
  290. return self
  291. }
  292. internal func offset(amount: CGSize) -> ConstraintDescriptionEditable {
  293. self.constant = amount
  294. return self
  295. }
  296. internal func offset(amount: EdgeInsets) -> ConstraintDescriptionEditable {
  297. self.constant = amount
  298. return self
  299. }
  300. // MARK: inset
  301. internal func inset(amount: FloatConvertible) -> ConstraintDescriptionEditable {
  302. let value = CGFloat(amount.floatValue)
  303. self.constant = EdgeInsets(top: value, left: value, bottom: -value, right: -value)
  304. return self
  305. }
  306. internal func inset(amount: EdgeInsets) -> ConstraintDescriptionEditable {
  307. self.constant = EdgeInsets(top: amount.top, left: amount.left, bottom: -amount.bottom, right: -amount.right)
  308. return self
  309. }
  310. // MARK: priority
  311. internal func priority(priority: FloatConvertible) -> ConstraintDescriptionFinalizable {
  312. self.priority = priority.floatValue
  313. return self
  314. }
  315. internal func priorityRequired() -> ConstraintDescriptionFinalizable {
  316. return self.priority(1000.0)
  317. }
  318. internal func priorityHigh() -> ConstraintDescriptionFinalizable {
  319. return self.priority(750.0)
  320. }
  321. internal func priorityMedium() -> ConstraintDescriptionFinalizable {
  322. #if os(iOS) || os(tvOS)
  323. return self.priority(500.0)
  324. #else
  325. return self.priority(501.0)
  326. #endif
  327. }
  328. internal func priorityLow() -> ConstraintDescriptionFinalizable {
  329. return self.priority(250.0)
  330. }
  331. // MARK: Constraint
  332. internal var constraint: Constraint {
  333. if self.concreteConstraint == nil {
  334. if self.relation == nil {
  335. fatalError("Attempting to create a constraint from a ConstraintDescription before it has been fully chained.")
  336. }
  337. self.concreteConstraint = ConcreteConstraint(
  338. fromItem: self.fromItem,
  339. toItem: self.toItem,
  340. relation: self.relation!,
  341. constant: self.constant,
  342. multiplier: self.multiplier,
  343. priority: self.priority)
  344. }
  345. return self.concreteConstraint!
  346. }
  347. // MARK: Private
  348. private let fromItem: ConstraintItem
  349. private var toItem: ConstraintItem {
  350. willSet {
  351. if self.concreteConstraint != nil {
  352. fatalError("Attempting to modify a ConstraintDescription after its constraint has been created.")
  353. }
  354. }
  355. }
  356. private var relation: ConstraintRelation? {
  357. willSet {
  358. if self.concreteConstraint != nil {
  359. fatalError("Attempting to modify a ConstraintDescription after its constraint has been created.")
  360. }
  361. }
  362. }
  363. private var constant: Any = Float(0.0) {
  364. willSet {
  365. if self.concreteConstraint != nil {
  366. fatalError("Attempting to modify a ConstraintDescription after its constraint has been created.")
  367. }
  368. }
  369. }
  370. private var multiplier: Float = 1.0 {
  371. willSet {
  372. if self.concreteConstraint != nil {
  373. fatalError("Attempting to modify a ConstraintDescription after its constraint has been created.")
  374. }
  375. }
  376. }
  377. private var priority: Float = 1000.0 {
  378. willSet {
  379. if self.concreteConstraint != nil {
  380. fatalError("Attempting to modify a ConstraintDescription after its constraint has been created.")
  381. }
  382. }
  383. }
  384. private var concreteConstraint: ConcreteConstraint? = nil
  385. private func addConstraint(attributes: ConstraintAttributes) -> ConstraintDescription {
  386. if self.relation == nil {
  387. self.fromItem.attributes += attributes
  388. }
  389. return self
  390. }
  391. private func constrainTo(other: ConstraintItem, relation: ConstraintRelation) -> ConstraintDescription {
  392. if other.attributes != ConstraintAttributes.None {
  393. let toLayoutAttributes = other.attributes.layoutAttributes
  394. if toLayoutAttributes.count > 1 {
  395. let fromLayoutAttributes = self.fromItem.attributes.layoutAttributes
  396. if toLayoutAttributes != fromLayoutAttributes {
  397. NSException(name: "Invalid Constraint", reason: "Cannot constrain to multiple non identical attributes", userInfo: nil).raise()
  398. return self
  399. }
  400. other.attributes = ConstraintAttributes.None
  401. }
  402. }
  403. self.toItem = other
  404. self.relation = relation
  405. return self
  406. }
  407. private func constrainTo(other: View, relation: ConstraintRelation) -> ConstraintDescription {
  408. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  409. }
  410. @available(iOS 7.0, *)
  411. private func constrainTo(other: LayoutSupport, relation: ConstraintRelation) -> ConstraintDescription {
  412. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  413. }
  414. @available(iOS 9.0, OSX 10.11, *)
  415. private func constrainTo(other: NSLayoutAnchor, relation: ConstraintRelation) -> ConstraintDescription {
  416. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  417. }
  418. private func constrainTo(other: FloatConvertible, relation: ConstraintRelation) -> ConstraintDescription {
  419. self.constant = other.floatValue
  420. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  421. }
  422. private func constrainTo(other: CGSize, relation: ConstraintRelation) -> ConstraintDescription {
  423. self.constant = other
  424. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  425. }
  426. private func constrainTo(other: CGPoint, relation: ConstraintRelation) -> ConstraintDescription {
  427. self.constant = other
  428. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  429. }
  430. private func constrainTo(other: EdgeInsets, relation: ConstraintRelation) -> ConstraintDescription {
  431. self.constant = other
  432. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  433. }
  434. }