You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
5.3 KiB

5 years ago
  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. }
  72. #else
  73. switch layoutAttribute {
  74. case .left, .right, .leading, .trailing, .centerX:
  75. return value.x
  76. case .top, .bottom, .centerY, .lastBaseline, .firstBaseline:
  77. return value.y
  78. case .width, .height, .notAnAttribute:
  79. return 0.0
  80. }
  81. #endif
  82. }
  83. if let value = self as? ConstraintInsets {
  84. #if os(iOS) || os(tvOS)
  85. switch layoutAttribute {
  86. case .left, .leftMargin, .centerX, .centerXWithinMargins:
  87. return value.left
  88. case .top, .topMargin, .centerY, .centerYWithinMargins, .lastBaseline, .firstBaseline:
  89. return value.top
  90. case .right, .rightMargin:
  91. return -value.right
  92. case .bottom, .bottomMargin:
  93. return -value.bottom
  94. case .leading, .leadingMargin:
  95. return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? value.left : value.right
  96. case .trailing, .trailingMargin:
  97. return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? -value.right : -value.left
  98. case .width:
  99. return -(value.left + value.right)
  100. case .height:
  101. return -(value.top + value.bottom)
  102. case .notAnAttribute:
  103. return 0.0
  104. }
  105. #else
  106. switch layoutAttribute {
  107. case .left, .centerX:
  108. return value.left
  109. case .top, .centerY, .lastBaseline, .firstBaseline:
  110. return value.top
  111. case .right:
  112. return -value.right
  113. case .bottom:
  114. return -value.bottom
  115. case .leading:
  116. return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? value.left : value.right
  117. case .trailing:
  118. return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? -value.right : -value.left
  119. case .width:
  120. return -(value.left + value.right)
  121. case .height:
  122. return -(value.top + value.bottom)
  123. case .notAnAttribute:
  124. return 0.0
  125. }
  126. #endif
  127. }
  128. return 0.0
  129. }
  130. }