123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- #if os(iOS) || os(tvOS)
- import UIKit
- #else
- import AppKit
- #endif
- public protocol ConstraintConstantTarget {
- }
- extension CGPoint: ConstraintConstantTarget {
- }
- extension CGSize: ConstraintConstantTarget {
- }
- extension ConstraintInsets: ConstraintConstantTarget {
- }
- extension ConstraintConstantTarget {
-
- internal func constraintConstantTargetValueFor(layoutAttribute: LayoutAttribute) -> CGFloat {
- if let value = self as? CGFloat {
- return value
- }
-
- if let value = self as? Float {
- return CGFloat(value)
- }
-
- if let value = self as? Double {
- return CGFloat(value)
- }
-
- if let value = self as? Int {
- return CGFloat(value)
- }
-
- if let value = self as? UInt {
- return CGFloat(value)
- }
-
- if let value = self as? CGSize {
- if layoutAttribute == .width {
- return value.width
- } else if layoutAttribute == .height {
- return value.height
- } else {
- return 0.0
- }
- }
-
- if let value = self as? CGPoint {
- #if os(iOS) || os(tvOS)
- switch layoutAttribute {
- case .left, .right, .leading, .trailing, .centerX, .leftMargin, .rightMargin, .leadingMargin, .trailingMargin, .centerXWithinMargins:
- return value.x
- case .top, .bottom, .centerY, .topMargin, .bottomMargin, .centerYWithinMargins, .lastBaseline, .firstBaseline:
- return value.y
- case .width, .height, .notAnAttribute:
- return 0.0
- }
- #else
- switch layoutAttribute {
- case .left, .right, .leading, .trailing, .centerX:
- return value.x
- case .top, .bottom, .centerY, .lastBaseline, .firstBaseline:
- return value.y
- case .width, .height, .notAnAttribute:
- return 0.0
- }
- #endif
- }
-
- if let value = self as? ConstraintInsets {
- #if os(iOS) || os(tvOS)
- switch layoutAttribute {
- case .left, .leftMargin, .centerX, .centerXWithinMargins:
- return value.left
- case .top, .topMargin, .centerY, .centerYWithinMargins, .lastBaseline, .firstBaseline:
- return value.top
- case .right, .rightMargin:
- return -value.right
- case .bottom, .bottomMargin:
- return -value.bottom
- case .leading, .leadingMargin:
- return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? value.left : value.right
- case .trailing, .trailingMargin:
- return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? -value.right : -value.left
- case .width:
- return -(value.left + value.right)
- case .height:
- return -(value.top + value.bottom)
- case .notAnAttribute:
- return 0.0
- }
- #else
- switch layoutAttribute {
- case .left, .centerX:
- return value.left
- case .top, .centerY, .lastBaseline, .firstBaseline:
- return value.top
- case .right:
- return -value.right
- case .bottom:
- return -value.bottom
- case .leading:
- return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? value.left : value.right
- case .trailing:
- return (ConstraintConfig.interfaceLayoutDirection == .leftToRight) ? -value.right : -value.left
- case .width:
- return -(value.left + value.right)
- case .height:
- return -(value.top + value.bottom)
- case .notAnAttribute:
- return 0.0
- }
- #endif
- }
-
- return 0.0
- }
-
- }
|