ConstraintConstantTarget.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 protocol ConstraintConstantTarget {
  29. }
  30. extension CGPoint: ConstraintConstantTarget {
  31. }
  32. extension CGSize: ConstraintConstantTarget {
  33. }
  34. extension ConstraintInsets: ConstraintConstantTarget {
  35. }
  36. #if os(iOS) || os(tvOS)
  37. @available(iOS 11.0, tvOS 11.0, *)
  38. extension ConstraintDirectionalInsets: ConstraintConstantTarget {
  39. }
  40. #endif
  41. extension ConstraintConstantTarget {
  42. internal func constraintConstantTargetValueFor(layoutAttribute: LayoutAttribute) -> CGFloat {
  43. if let value = self as? CGFloat {
  44. return value
  45. }
  46. if let value = self as? Float {
  47. return CGFloat(value)
  48. }
  49. if let value = self as? Double {
  50. return CGFloat(value)
  51. }
  52. if let value = self as? Int {
  53. return CGFloat(value)
  54. }
  55. if let value = self as? UInt {
  56. return CGFloat(value)
  57. }
  58. if let value = self as? CGSize {
  59. if layoutAttribute == .width {
  60. return value.width
  61. } else if layoutAttribute == .height {
  62. return value.height
  63. } else {
  64. return 0.0
  65. }
  66. }
  67. if let value = self as? CGPoint {
  68. #if os(iOS) || os(tvOS)
  69. switch layoutAttribute {
  70. case .left, .right, .leading, .trailing, .centerX, .leftMargin, .rightMargin, .leadingMargin, .trailingMargin, .centerXWithinMargins:
  71. return value.x
  72. case .top, .bottom, .centerY, .topMargin, .bottomMargin, .centerYWithinMargins, .lastBaseline, .firstBaseline:
  73. return value.y
  74. case .width, .height, .notAnAttribute:
  75. return 0.0
  76. #if swift(>=5.0)
  77. @unknown default:
  78. return 0.0
  79. #endif
  80. }
  81. #else
  82. switch layoutAttribute {
  83. case .left, .right, .leading, .trailing, .centerX:
  84. return value.x
  85. case .top, .bottom, .centerY, .lastBaseline, .firstBaseline:
  86. return value.y
  87. case .width, .height, .notAnAttribute:
  88. return 0.0
  89. #if swift(>=5.0)
  90. @unknown default:
  91. return 0.0
  92. #endif
  93. }
  94. #endif
  95. }
  96. if let value = self as? ConstraintInsets {
  97. #if os(iOS) || os(tvOS)
  98. switch layoutAttribute {
  99. case .left, .leftMargin:
  100. return value.left
  101. case .top, .topMargin, .firstBaseline:
  102. return value.top
  103. case .right, .rightMargin:
  104. return -value.right
  105. case .bottom, .bottomMargin, .lastBaseline:
  106. return -value.bottom
  107. case .leading, .leadingMargin:
  108. return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? value.left : value.right
  109. case .trailing, .trailingMargin:
  110. return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? -value.right : -value.left
  111. case .centerX, .centerXWithinMargins:
  112. return (value.left - value.right) / 2
  113. case .centerY, .centerYWithinMargins:
  114. return (value.top - value.bottom) / 2
  115. case .width:
  116. return -(value.left + value.right)
  117. case .height:
  118. return -(value.top + value.bottom)
  119. case .notAnAttribute:
  120. return 0.0
  121. #if swift(>=5.0)
  122. @unknown default:
  123. return 0.0
  124. #endif
  125. }
  126. #else
  127. switch layoutAttribute {
  128. case .left:
  129. return value.left
  130. case .top, .firstBaseline:
  131. return value.top
  132. case .right:
  133. return -value.right
  134. case .bottom, .lastBaseline:
  135. return -value.bottom
  136. case .leading:
  137. return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? value.left : value.right
  138. case .trailing:
  139. return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? -value.right : -value.left
  140. case .centerX:
  141. return (value.left - value.right) / 2
  142. case .centerY:
  143. return (value.top - value.bottom) / 2
  144. case .width:
  145. return -(value.left + value.right)
  146. case .height:
  147. return -(value.top + value.bottom)
  148. case .notAnAttribute:
  149. return 0.0
  150. #if swift(>=5.0)
  151. @unknown default:
  152. return 0.0
  153. #endif
  154. }
  155. #endif
  156. }
  157. #if os(iOS) || os(tvOS)
  158. if #available(iOS 11.0, tvOS 11.0, *), let value = self as? ConstraintDirectionalInsets {
  159. switch layoutAttribute {
  160. case .left, .leftMargin:
  161. return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? value.leading : value.trailing
  162. case .top, .topMargin, .firstBaseline:
  163. return value.top
  164. case .right, .rightMargin:
  165. return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? -value.trailing : -value.leading
  166. case .bottom, .bottomMargin, .lastBaseline:
  167. return -value.bottom
  168. case .leading, .leadingMargin:
  169. return value.leading
  170. case .trailing, .trailingMargin:
  171. return -value.trailing
  172. case .centerX, .centerXWithinMargins:
  173. return (value.leading - value.trailing) / 2
  174. case .centerY, .centerYWithinMargins:
  175. return (value.top - value.bottom) / 2
  176. case .width:
  177. return -(value.leading + value.trailing)
  178. case .height:
  179. return -(value.top + value.bottom)
  180. case .notAnAttribute:
  181. return 0.0
  182. #if swift(>=5.0)
  183. @unknown default:
  184. return 0.0
  185. #else
  186. default:
  187. return 0.0
  188. #endif
  189. }
  190. }
  191. #endif
  192. return 0.0
  193. }
  194. }