ConstraintMakerPriortizable.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 class ConstraintMakerPriortizable: ConstraintMakerFinalizable {
  29. public enum Priority: Strideable, RawRepresentable, Equatable {
  30. case low
  31. case medium
  32. case high
  33. case required
  34. case custom(value: Float)
  35. public init?(rawValue: Float) {
  36. if rawValue < 0 || rawValue > 1000 {
  37. return nil
  38. }
  39. self = .custom(value: rawValue)
  40. }
  41. private var macOSMedium: Float {
  42. #if os(OSX)
  43. return 501.0
  44. #else
  45. return 500.0
  46. #endif
  47. }
  48. public var rawValue: Float {
  49. switch self {
  50. case .low:
  51. return 250.0
  52. case .medium:
  53. return macOSMedium
  54. case .high:
  55. return 750.0
  56. case .required:
  57. return 1000.0
  58. case let .custom(value):
  59. return value
  60. }
  61. }
  62. public func advanced(by n: Float) -> Priority {
  63. return .custom(value: self.rawValue + n)
  64. }
  65. public func distance(to other: Priority) -> Float {
  66. return other.rawValue - self.rawValue
  67. }
  68. public static func ==(lhs: Priority, rhs: Priority) -> Bool {
  69. return lhs.rawValue == rhs.rawValue
  70. }
  71. }
  72. @discardableResult
  73. @available(*, deprecated:3.0, message:"Use priority(level: .custom(Float)) instead.")
  74. public func priority(_ amount: ConstraintPriorityTarget) -> ConstraintMakerFinalizable {
  75. self.description.priority = amount
  76. return self
  77. }
  78. @discardableResult
  79. public func priority(level: Priority) -> ConstraintMakerFinalizable {
  80. return self.priority(level: ConstraintMakerPriortizable.Priority(rawValue: level.rawValue)!)
  81. }
  82. @available(*, deprecated:3.0, message:"Use priority(level: Priority) instead.")
  83. @discardableResult
  84. public func priorityRequired() -> ConstraintMakerFinalizable {
  85. return self.priority(level: .required)
  86. }
  87. @available(*, deprecated:3.0, message:"Use priority(level: Priority) instead.")
  88. @discardableResult
  89. public func priorityHigh() -> ConstraintMakerFinalizable {
  90. return self.priority(level: .high)
  91. }
  92. @available(*, deprecated:3.0, message:"Use priority(level: Priority) instead.")
  93. @discardableResult
  94. public func priorityMedium() -> ConstraintMakerFinalizable {
  95. return self.priority(level: .medium)
  96. }
  97. @available(*, deprecated:3.0, message:"Use priority(level: Priority) instead.")
  98. @discardableResult
  99. public func priorityLow() -> ConstraintMakerFinalizable {
  100. return self.priority(level: .low)
  101. }
  102. }