ConstraintConstantTarget.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. extension ConstraintConstantTarget {
  37. internal func constraintConstantTargetValueFor(layoutAttribute: LayoutAttribute) -> CGFloat {
  38. if let value = self as? CGFloat {
  39. return value
  40. }
  41. if let value = self as? Float {
  42. return CGFloat(value)
  43. }
  44. if let value = self as? Double {
  45. return CGFloat(value)
  46. }
  47. if let value = self as? Int {
  48. return CGFloat(value)
  49. }
  50. if let value = self as? UInt {
  51. return CGFloat(value)
  52. }
  53. if let value = self as? CGSize {
  54. if layoutAttribute == .width {
  55. return value.width
  56. } else if layoutAttribute == .height {
  57. return value.height
  58. } else {
  59. return 0.0
  60. }
  61. }
  62. if let value = self as? CGPoint {
  63. #if os(iOS) || os(tvOS)
  64. switch layoutAttribute {
  65. case .left, .right, .leading, .trailing, .centerX, .leftMargin, .rightMargin, .leadingMargin, .trailingMargin, .centerXWithinMargins:
  66. return value.x
  67. case .top, .bottom, .centerY, .topMargin, .bottomMargin, .centerYWithinMargins, .lastBaseline, .firstBaseline:
  68. return value.y
  69. case .width, .height, .notAnAttribute:
  70. return 0.0
  71. #if swift(>=5.0)
  72. @unknown default:
  73. return 0.0
  74. #endif
  75. }
  76. #else
  77. switch layoutAttribute {
  78. case .left, .right, .leading, .trailing, .centerX:
  79. return value.x
  80. case .top, .bottom, .centerY, .lastBaseline, .firstBaseline:
  81. return value.y
  82. case .width, .height, .notAnAttribute:
  83. return 0.0
  84. #if swift(>=5.0)
  85. @unknown default:
  86. return 0.0
  87. #endif
  88. }
  89. #endif
  90. }
  91. if let value = self as? ConstraintInsets {
  92. #if os(iOS) || os(tvOS)
  93. switch layoutAttribute {
  94. case .left, .leftMargin:
  95. return value.left
  96. case .top, .topMargin, .firstBaseline:
  97. return value.top
  98. case .right, .rightMargin:
  99. return -value.right
  100. case .bottom, .bottomMargin, .lastBaseline:
  101. return -value.bottom
  102. case .leading, .leadingMargin:
  103. return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? value.left : value.right
  104. case .trailing, .trailingMargin:
  105. return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? -value.right : -value.left
  106. case .centerX, .centerXWithinMargins:
  107. return (value.left - value.right) / 2
  108. case .centerY, .centerYWithinMargins:
  109. return (value.top - value.bottom) / 2
  110. case .width:
  111. return -(value.left + value.right)
  112. case .height:
  113. return -(value.top + value.bottom)
  114. case .notAnAttribute:
  115. return 0.0
  116. #if swift(>=5.0)
  117. @unknown default:
  118. return 0.0
  119. #endif
  120. }
  121. #else
  122. switch layoutAttribute {
  123. case .left:
  124. return value.left
  125. case .top, .firstBaseline:
  126. return value.top
  127. case .right:
  128. return -value.right
  129. case .bottom, .lastBaseline:
  130. return -value.bottom
  131. case .leading:
  132. return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? value.left : value.right
  133. case .trailing:
  134. return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? -value.right : -value.left
  135. case .centerX:
  136. return (value.left - value.right) / 2
  137. case .centerY:
  138. return (value.top - value.bottom) / 2
  139. case .width:
  140. return -(value.left + value.right)
  141. case .height:
  142. return -(value.top + value.bottom)
  143. case .notAnAttribute:
  144. return 0.0
  145. #if swift(>=5.0)
  146. @unknown default:
  147. return 0.0
  148. #endif
  149. }
  150. #endif
  151. }
  152. return 0.0
  153. }
  154. }