Constraint.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. //
  2. // Snap
  3. //
  4. // Copyright (c) 2011-2014 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. * Constraint is a single item that defines all the properties for a single ConstraintMaker chain
  30. */
  31. final public class Constraint {
  32. public var left: Constraint { return self.addConstraint(ConstraintAttributes.Left) }
  33. public var top: Constraint { return self.addConstraint(ConstraintAttributes.Top) }
  34. public var right: Constraint { return self.addConstraint(ConstraintAttributes.Right) }
  35. public var bottom: Constraint { return self.addConstraint(ConstraintAttributes.Bottom) }
  36. public var leading: Constraint { return self.addConstraint(ConstraintAttributes.Leading) }
  37. public var trailing: Constraint { return self.addConstraint(ConstraintAttributes.Trailing) }
  38. public var width: Constraint { return self.addConstraint(ConstraintAttributes.Width) }
  39. public var height: Constraint { return self.addConstraint(ConstraintAttributes.Height) }
  40. public var centerX: Constraint { return self.addConstraint(ConstraintAttributes.CenterX) }
  41. public var centerY: Constraint { return self.addConstraint(ConstraintAttributes.CenterY) }
  42. public var baseline: Constraint { return self.addConstraint(ConstraintAttributes.Baseline) }
  43. public var and: Constraint {
  44. if self.relation != nil {
  45. fatalError("And is semantic only and can only be used before a relation is set.")
  46. }
  47. return self
  48. }
  49. public var with: Constraint { return self }
  50. // MARK: initializer
  51. internal init(fromItem: ConstraintItem) {
  52. self.fromItem = fromItem
  53. self.toItem = ConstraintItem(object: nil, attributes: ConstraintAttributes.None)
  54. }
  55. // MARK: equalTo
  56. public func equalTo(other: ConstraintItem) -> Constraint {
  57. return self.constrainTo(other, relation: .Equal)
  58. }
  59. public func equalTo(other: View) -> Constraint {
  60. return self.constrainTo(other, relation: .Equal)
  61. }
  62. #if os(iOS)
  63. public func equalTo(other: UILayoutSupport) -> Constraint {
  64. return self.constrainTo(other, relation: .Equal)
  65. }
  66. #endif
  67. public func equalTo(other: Float) -> Constraint {
  68. return self.constrainTo(other, relation: .Equal)
  69. }
  70. public func equalTo(other: Double) -> Constraint {
  71. return self.constrainTo(Float(other), relation: .Equal)
  72. }
  73. public func equalTo(other: CGFloat) -> Constraint {
  74. return self.constrainTo(Float(other), relation: .Equal)
  75. }
  76. public func equalTo(other: Int) -> Constraint {
  77. return self.constrainTo(Float(other), relation: .Equal)
  78. }
  79. public func equalTo(other: UInt) -> Constraint {
  80. return self.constrainTo(Float(other), relation: .Equal)
  81. }
  82. public func equalTo(other: CGSize) -> Constraint {
  83. return self.constrainTo(other, relation: .Equal)
  84. }
  85. public func equalTo(other: CGPoint) -> Constraint {
  86. return self.constrainTo(other, relation: .Equal)
  87. }
  88. public func equalTo(other: EdgeInsets) -> Constraint {
  89. return self.constrainTo(other, relation: .Equal)
  90. }
  91. // MARK: lessThanOrEqualTo
  92. public func lessThanOrEqualTo(other: ConstraintItem) -> Constraint {
  93. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  94. }
  95. public func lessThanOrEqualTo(other: View) -> Constraint {
  96. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  97. }
  98. #if os(iOS)
  99. public func lessThanOrEqualTo(other: UILayoutSupport) -> Constraint {
  100. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  101. }
  102. #endif
  103. public func lessThanOrEqualTo(other: Float) -> Constraint {
  104. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  105. }
  106. public func lessThanOrEqualTo(other: Double) -> Constraint {
  107. return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
  108. }
  109. public func lessThanOrEqualTo(other: CGFloat) -> Constraint {
  110. return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
  111. }
  112. public func lessThanOrEqualTo(other: Int) -> Constraint {
  113. return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
  114. }
  115. public func lessThanOrEqualTo(other: UInt) -> Constraint {
  116. return self.constrainTo(Float(other), relation: .LessThanOrEqualTo)
  117. }
  118. public func lessThanOrEqualTo(other: CGSize) -> Constraint {
  119. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  120. }
  121. public func lessThanOrEqualTo(other: CGPoint) -> Constraint {
  122. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  123. }
  124. public func lessThanOrEqualTo(other: EdgeInsets) -> Constraint {
  125. return self.constrainTo(other, relation: .LessThanOrEqualTo)
  126. }
  127. // MARK: greaterThanOrEqualTo
  128. public func greaterThanOrEqualTo(other: ConstraintItem) -> Constraint {
  129. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  130. }
  131. public func greaterThanOrEqualTo(other: View) -> Constraint {
  132. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  133. }
  134. #if os(iOS)
  135. public func greaterThanOrEqualTo(other: UILayoutSupport) -> Constraint {
  136. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  137. }
  138. #endif
  139. public func greaterThanOrEqualTo(other: Float) -> Constraint {
  140. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  141. }
  142. public func greaterThanOrEqualTo(other: Double) -> Constraint {
  143. return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  144. }
  145. public func greaterThanOrEqualTo(other: CGFloat) -> Constraint {
  146. return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  147. }
  148. public func greaterThanOrEqualTo(other: Int) -> Constraint {
  149. return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  150. }
  151. public func greaterThanOrEqualTo(other: UInt) -> Constraint {
  152. return self.constrainTo(Float(other), relation: .GreaterThanOrEqualTo)
  153. }
  154. public func greaterThanOrEqualTo(other: CGSize) -> Constraint {
  155. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  156. }
  157. public func greaterThanOrEqualTo(other: CGPoint) -> Constraint {
  158. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  159. }
  160. public func greaterThanOrEqualTo(other: EdgeInsets) -> Constraint {
  161. return self.constrainTo(other, relation: .GreaterThanOrEqualTo)
  162. }
  163. // MARK: multiplier
  164. public func multipliedBy(amount: Float) -> Constraint {
  165. self.multiplier = amount
  166. return self
  167. }
  168. public func multipliedBy(amount: Double) -> Constraint {
  169. return self.multipliedBy(Float(amount))
  170. }
  171. public func multipliedBy(amount: CGFloat) -> Constraint {
  172. return self.multipliedBy(Float(amount))
  173. }
  174. public func multipliedBy(amount: Int) -> Constraint {
  175. return self.multipliedBy(Float(amount))
  176. }
  177. public func multipliedBy(amount: UInt) -> Constraint {
  178. return self.multipliedBy(Float(amount))
  179. }
  180. public func dividedBy(amount: Float) -> Constraint {
  181. self.multiplier = 1.0 / amount;
  182. return self
  183. }
  184. public func dividedBy(amount: Double) -> Constraint {
  185. return self.dividedBy(Float(amount))
  186. }
  187. public func dividedBy(amount: CGFloat) -> Constraint {
  188. return self.dividedBy(Float(amount))
  189. }
  190. public func dividedBy(amount: Int) -> Constraint {
  191. return self.dividedBy(Float(amount))
  192. }
  193. public func dividedBy(amount: UInt) -> Constraint {
  194. return self.dividedBy(Float(amount))
  195. }
  196. // MARK: priority
  197. public func priority(priority: Float) -> Constraint {
  198. self.priority = priority
  199. return self
  200. }
  201. public func priority(priority: Double) -> Constraint {
  202. return self.priority(Float(priority))
  203. }
  204. public func priority(priority: CGFloat) -> Constraint {
  205. return self.priority(Float(priority))
  206. }
  207. public func priority(priority: UInt) -> Constraint {
  208. return self.priority(Float(priority))
  209. }
  210. public func priority(priority: Int) -> Constraint {
  211. return self.priority(Float(priority))
  212. }
  213. public func priorityRequired() -> Constraint {
  214. return self.priority(1000.0)
  215. }
  216. public func priorityHigh() -> Constraint {
  217. return self.priority(750.0)
  218. }
  219. public func priorityLow() -> Constraint {
  220. return self.priority(250.0)
  221. }
  222. // MARK: offset
  223. public func offset(amount: Float) -> Constraint {
  224. self.offset = amount
  225. return self
  226. }
  227. public func offset(amount: Double) -> Constraint {
  228. return self.offset(Float(amount))
  229. }
  230. public func offset(amount: CGFloat) -> Constraint {
  231. return self.offset(Float(amount))
  232. }
  233. public func offset(amount: Int) -> Constraint {
  234. return self.offset(Float(amount))
  235. }
  236. public func offset(amount: UInt) -> Constraint {
  237. return self.offset(Float(amount))
  238. }
  239. public func offset(amount: CGPoint) -> Constraint {
  240. self.offset = amount
  241. return self
  242. }
  243. public func offset(amount: CGSize) -> Constraint {
  244. self.offset = amount
  245. return self
  246. }
  247. public func offset(amount: EdgeInsets) -> Constraint {
  248. self.offset = amount
  249. return self
  250. }
  251. // MARK: insets
  252. public func insets(amount: EdgeInsets) -> Constraint {
  253. self.offset = amount
  254. return self
  255. }
  256. // MARK: install / uninstall
  257. public func install() -> [LayoutConstraint] {
  258. return self.installOnView(updateExisting: false)
  259. }
  260. public func uninstall() {
  261. self.uninstallFromView()
  262. }
  263. // MARK: internal
  264. internal func installOnView(updateExisting: Bool = false) -> [LayoutConstraint] {
  265. var installOnView: View? = nil
  266. if self.toItem.view != nil {
  267. installOnView = Constraint.closestCommonSuperviewFromView(self.fromItem.view, toView: self.toItem.view)
  268. if installOnView == nil {
  269. NSException(name: "Cannot Install Constraint", reason: "No common superview between views", userInfo: nil).raise()
  270. return []
  271. }
  272. } else {
  273. installOnView = self.fromItem.view?.superview
  274. if installOnView == nil {
  275. if self.fromItem.attributes == ConstraintAttributes.Width || self.fromItem.attributes == ConstraintAttributes.Height {
  276. installOnView = self.fromItem.view
  277. }
  278. if installOnView == nil {
  279. NSException(name: "Cannot Install Constraint", reason: "Missing superview", userInfo: nil).raise()
  280. return []
  281. }
  282. }
  283. }
  284. if let installedOnView = self.installInfo?.view {
  285. if installedOnView != installOnView {
  286. NSException(name: "Cannot Install Constraint", reason: "Already installed on different view.", userInfo: nil).raise()
  287. return []
  288. }
  289. return self.installInfo?.layoutConstraints.allObjects as? [LayoutConstraint] ?? []
  290. }
  291. var newLayoutConstraints = [LayoutConstraint]()
  292. let layoutFromAttributes = self.fromItem.attributes.layoutAttributes
  293. let layoutToAttributes = self.toItem.attributes.layoutAttributes
  294. // get layout from
  295. let layoutFrom: View? = self.fromItem.view
  296. // get layout relation
  297. let layoutRelation: NSLayoutRelation = (self.relation != nil) ? self.relation!.layoutRelation : .Equal
  298. for layoutFromAttribute in layoutFromAttributes {
  299. // get layout to attribute
  300. let layoutToAttribute = (layoutToAttributes.count > 0) ? layoutToAttributes[0] : layoutFromAttribute
  301. // get layout constant
  302. var layoutConstant: CGFloat = layoutToAttribute.snp_constantForValue(self.constant)
  303. layoutConstant += layoutToAttribute.snp_offsetForValue(self.offset)
  304. // get layout to
  305. var layoutTo: View? = self.toItem.view
  306. if layoutTo == nil && layoutToAttribute != .Width && layoutToAttribute != .Height {
  307. layoutTo = installOnView
  308. }
  309. // create layout constraint
  310. let layoutConstraint = LayoutConstraint(
  311. item: layoutFrom!,
  312. attribute: layoutFromAttribute,
  313. relatedBy: layoutRelation,
  314. toItem: layoutTo,
  315. attribute: layoutToAttribute,
  316. multiplier: CGFloat(self.multiplier),
  317. constant: layoutConstant)
  318. // set priority
  319. layoutConstraint.priority = self.priority
  320. // set constraint
  321. layoutConstraint.snp_constraint = self
  322. newLayoutConstraints.append(layoutConstraint)
  323. }
  324. // special logic for updating
  325. if updateExisting {
  326. // get existing constraints for this view
  327. let existingLayoutConstraints = reverse(layoutFrom!.snp_installedLayoutConstraints)
  328. // array that will contain only new layout constraints to keep
  329. var newLayoutConstraintsToKeep = [LayoutConstraint]()
  330. // begin looping
  331. for layoutConstraint in newLayoutConstraints {
  332. // layout constraint that should be updated
  333. var updateLayoutConstraint: LayoutConstraint? = nil
  334. // loop through existing and check for match
  335. for existingLayoutConstraint in existingLayoutConstraints {
  336. if existingLayoutConstraint == layoutConstraint {
  337. updateLayoutConstraint = existingLayoutConstraint
  338. break
  339. }
  340. }
  341. // if we have existing one lets just update the constant
  342. if updateLayoutConstraint != nil {
  343. updateLayoutConstraint!.constant = layoutConstraint.constant
  344. }
  345. // otherwise add this layout constraint to new keep list
  346. else {
  347. newLayoutConstraintsToKeep.append(layoutConstraint)
  348. }
  349. }
  350. // set constraints to only new ones
  351. newLayoutConstraints = newLayoutConstraintsToKeep
  352. }
  353. // add constraints
  354. installOnView!.addConstraints(newLayoutConstraints)
  355. // set install info
  356. self.installInfo = ConstraintInstallInfo(view: installOnView, layoutConstraints: NSHashTable.weakObjectsHashTable())
  357. // store which layout constraints are installed for this constraint
  358. for layoutConstraint in newLayoutConstraints {
  359. self.installInfo!.layoutConstraints.addObject(layoutConstraint)
  360. }
  361. // store the layout constraints against the installed on view
  362. layoutFrom!.snp_installedLayoutConstraints += newLayoutConstraints
  363. // return the new constraints
  364. return newLayoutConstraints
  365. }
  366. internal func uninstallFromView() {
  367. if let installInfo = self.installInfo,
  368. let installedOnView = installInfo.view,
  369. let installedLayoutConstraints = installInfo.layoutConstraints.allObjects as? [LayoutConstraint] {
  370. if installedLayoutConstraints.count > 0 {
  371. // remove the constraints from the UIView's storage
  372. installedOnView.removeConstraints(installedLayoutConstraints)
  373. // remove the constraints from our associated object storage
  374. installedOnView.snp_installedLayoutConstraints = installedOnView.snp_installedLayoutConstraints.filter {
  375. return !contains(installedLayoutConstraints, $0)
  376. }
  377. }
  378. }
  379. self.installInfo = nil
  380. }
  381. // MARK: private
  382. private let fromItem: ConstraintItem
  383. private var toItem: ConstraintItem
  384. private var relation: ConstraintRelation?
  385. private var constant: Any = Float(0.0)
  386. private var multiplier: Float = 1.0
  387. private var priority: Float = 1000.0
  388. private var offset: Any = Float(0.0)
  389. private var installInfo: ConstraintInstallInfo?
  390. private var installed: Bool {
  391. return (self.installInfo != nil)
  392. }
  393. private func addConstraint(attributes: ConstraintAttributes) -> Constraint {
  394. if self.relation == nil {
  395. self.fromItem.attributes += attributes
  396. }
  397. return self
  398. }
  399. private func constrainTo(other: ConstraintItem, relation: ConstraintRelation) -> Constraint {
  400. if other.attributes != ConstraintAttributes.None {
  401. let toLayoutAttributes = other.attributes.layoutAttributes
  402. if toLayoutAttributes.count > 1 {
  403. let fromLayoutAttributes = self.fromItem.attributes.layoutAttributes
  404. if toLayoutAttributes != fromLayoutAttributes {
  405. NSException(name: "Invalid Constraint", reason: "Cannot constrain to multiple non identical attributes", userInfo: nil).raise()
  406. return self
  407. }
  408. other.attributes = ConstraintAttributes.None
  409. }
  410. }
  411. self.toItem = other
  412. self.relation = relation
  413. return self
  414. }
  415. private func constrainTo(other: View, relation: ConstraintRelation) -> Constraint {
  416. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  417. }
  418. #if os(iOS)
  419. private func constrainTo(other: UILayoutSupport, relation: ConstraintRelation) -> Constraint {
  420. return constrainTo(ConstraintItem(object: other, attributes: ConstraintAttributes.None), relation: relation)
  421. }
  422. #endif
  423. private func constrainTo(other: Float, relation: ConstraintRelation) -> Constraint {
  424. self.constant = other
  425. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  426. }
  427. private func constrainTo(other: Double, relation: ConstraintRelation) -> Constraint {
  428. self.constant = other
  429. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  430. }
  431. private func constrainTo(other: CGSize, relation: ConstraintRelation) -> Constraint {
  432. self.constant = other
  433. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  434. }
  435. private func constrainTo(other: CGPoint, relation: ConstraintRelation) -> Constraint {
  436. self.constant = other
  437. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  438. }
  439. private func constrainTo(other: EdgeInsets, relation: ConstraintRelation) -> Constraint {
  440. self.constant = other
  441. return constrainTo(ConstraintItem(object: nil, attributes: ConstraintAttributes.None), relation: relation)
  442. }
  443. private class func closestCommonSuperviewFromView(fromView: View?, toView: View?) -> View? {
  444. var views = Set<View>()
  445. var fromView = fromView
  446. var toView = toView
  447. do {
  448. if let view = toView {
  449. if views.contains(view) {
  450. return view
  451. }
  452. views.insert(view)
  453. toView = view.superview
  454. }
  455. if let view = fromView {
  456. if views.contains(view) {
  457. return view
  458. }
  459. views.insert(view)
  460. fromView = view.superview
  461. }
  462. } while (fromView != nil || toView != nil)
  463. return nil
  464. }
  465. }
  466. private extension NSLayoutAttribute {
  467. private func snp_offsetForValue(value: Any?) -> CGFloat {
  468. // Float
  469. if let float = value as? Float {
  470. return CGFloat(float)
  471. }
  472. // Double
  473. else if let double = value as? Double {
  474. return CGFloat(double)
  475. }
  476. // UInt
  477. else if let int = value as? Int {
  478. return CGFloat(int)
  479. }
  480. // Int
  481. else if let uint = value as? UInt {
  482. return CGFloat(uint)
  483. }
  484. // CGFloat
  485. else if let float = value as? CGFloat {
  486. return float
  487. }
  488. // CGSize
  489. else if let size = value as? CGSize {
  490. if self == .Width {
  491. return size.width
  492. } else if self == .Height {
  493. return size.height
  494. }
  495. }
  496. // CGPoint
  497. else if let point = value as? CGPoint {
  498. if self == .Left || self == .CenterX {
  499. return point.x
  500. } else if self == .Top || self == .CenterY {
  501. return point.y
  502. } else if self == .Right {
  503. return -point.x
  504. } else if self == .Bottom {
  505. return -point.y
  506. }
  507. }
  508. // EdgeInsets
  509. else if let insets = value as? EdgeInsets {
  510. if self == .Left {
  511. return insets.left
  512. } else if self == .Top {
  513. return insets.top
  514. } else if self == .Right {
  515. return -insets.right
  516. } else if self == .Bottom {
  517. return -insets.bottom
  518. }
  519. }
  520. return CGFloat(0)
  521. }
  522. private func snp_constantForValue(value: Any?) -> CGFloat {
  523. // Float
  524. if let float = value as? Float {
  525. return CGFloat(float)
  526. }
  527. // Double
  528. else if let double = value as? Double {
  529. return CGFloat(double)
  530. }
  531. // UInt
  532. else if let int = value as? Int {
  533. return CGFloat(int)
  534. }
  535. // Int
  536. else if let uint = value as? UInt {
  537. return CGFloat(uint)
  538. }
  539. // CGFloat
  540. else if let float = value as? CGFloat {
  541. return float
  542. }
  543. // CGSize
  544. else if let size = value as? CGSize {
  545. if self == .Width {
  546. return size.width
  547. } else if self == .Height {
  548. return size.height
  549. }
  550. }
  551. // CGPoint
  552. else if let point = value as? CGPoint {
  553. if self == .Left || self == .CenterX {
  554. return point.x
  555. } else if self == .Top || self == .CenterY {
  556. return point.y
  557. } else if self == .Right {
  558. return point.x
  559. } else if self == .Bottom {
  560. return point.y
  561. }
  562. }
  563. // EdgeInsets
  564. else if let insets = value as? EdgeInsets {
  565. if self == .Left {
  566. return insets.left
  567. } else if self == .Top {
  568. return insets.top
  569. } else if self == .Right {
  570. return -insets.right
  571. } else if self == .Bottom {
  572. return -insets.bottom
  573. }
  574. }
  575. return CGFloat(0);
  576. }
  577. }
  578. private struct ConstraintInstallInfo {
  579. weak var view: UIView? = nil
  580. let layoutConstraints: NSHashTable
  581. }
  582. internal func ==(left: Constraint, right: Constraint) -> Bool {
  583. return (left.fromItem == right.fromItem &&
  584. left.toItem == right.toItem &&
  585. left.relation == right.relation &&
  586. left.multiplier == right.multiplier &&
  587. left.priority == right.priority)
  588. }