ConstraintDescription.swift 25 KB

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