Improve Priority API's

This commit is contained in:
Robert Payne 2017-02-11 22:49:00 +13:00
parent a7053adc4a
commit a15821b791
4 changed files with 81 additions and 64 deletions

View File

@ -33,6 +33,7 @@
EE235FC31C5785DC00C08960 /* ConstraintLayoutSupportDSL.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE235FBF1C5785DC00C08960 /* ConstraintLayoutSupportDSL.swift */; };
EE235FC81C5785E200C08960 /* ConstraintView+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE235FC61C5785E200C08960 /* ConstraintView+Extensions.swift */; };
EE4910991B19A40200A54F1F /* SnapKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EEBCC9D819CC627D0083B827 /* SnapKit.framework */; };
EE6087751E4F133E0029CF84 /* ConstraintPriority.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE6087741E4F133E0029CF84 /* ConstraintPriority.swift */; };
EE6898CB1DA7B3A100D47F33 /* LayoutConstraintItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE6898CA1DA7B3A100D47F33 /* LayoutConstraintItem.swift */; };
EECDB3741AC0C9B6006BBC11 /* SnapKit.h in Headers */ = {isa = PBXBuildFile; fileRef = EECDB3661AC0C95C006BBC11 /* SnapKit.h */; settings = {ATTRIBUTES = (Public, ); }; };
EECDB3931AC0CB52006BBC11 /* Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EECDB36A1AC0C95C006BBC11 /* Tests.swift */; };
@ -71,6 +72,7 @@
EE235FBE1C5785DC00C08960 /* ConstraintViewDSL.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConstraintViewDSL.swift; sourceTree = "<group>"; };
EE235FBF1C5785DC00C08960 /* ConstraintLayoutSupportDSL.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConstraintLayoutSupportDSL.swift; sourceTree = "<group>"; };
EE235FC61C5785E200C08960 /* ConstraintView+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "ConstraintView+Extensions.swift"; sourceTree = "<group>"; };
EE6087741E4F133E0029CF84 /* ConstraintPriority.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConstraintPriority.swift; sourceTree = "<group>"; };
EE6898CA1DA7B3A100D47F33 /* LayoutConstraintItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LayoutConstraintItem.swift; sourceTree = "<group>"; };
EE94F6081AC0F10A008767FF /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
EE94F60A1AC0F10F008767FF /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/System/Library/Frameworks/AppKit.framework; sourceTree = DEVELOPER_DIR; };
@ -187,6 +189,7 @@
EE235F6B1C5785C600C08960 /* ConstraintItem.swift */,
EE235F6C1C5785C600C08960 /* LayoutConstraint.swift */,
EE6898CA1DA7B3A100D47F33 /* LayoutConstraintItem.swift */,
EE6087741E4F133E0029CF84 /* ConstraintPriority.swift */,
);
name = Models;
sourceTree = "<group>";
@ -365,6 +368,7 @@
EE235FB51C5785D400C08960 /* ConstraintMakerEditable.swift in Sources */,
EEF68FBC1D78653000980C26 /* ConstraintLayoutGuide.swift in Sources */,
EE235FAC1C5785D400C08960 /* ConstraintMaker.swift in Sources */,
EE6087751E4F133E0029CF84 /* ConstraintPriority.swift in Sources */,
EE235F941C5785CE00C08960 /* ConstraintRelatableTarget.swift in Sources */,
EEF68FA61D784A5300980C26 /* ConstraintDSL.swift in Sources */,
EE235FBB1C5785D400C08960 /* ConstraintMakerExtendable.swift in Sources */,

View File

@ -242,7 +242,7 @@ public final class Constraint {
let attribute = (layoutConstraint.secondAttribute == .notAnAttribute) ? layoutConstraint.firstAttribute : layoutConstraint.secondAttribute
layoutConstraint.constant = self.constant.constraintConstantTargetValueFor(layoutAttribute: attribute)
let requiredPriority = ConstraintMakerPriortizable.Priority.required.rawValue
let requiredPriority = ConstraintPriority.required.value
if (layoutConstraint.priority < requiredPriority), (self.priority.constraintPriorityTargetValue != requiredPriority) {
layoutConstraint.priority = self.priority.constraintPriorityTargetValue
}

View File

@ -30,90 +30,39 @@
public class ConstraintMakerPriortizable: ConstraintMakerFinalizable {
public enum Priority: Strideable, RawRepresentable, Equatable {
case low
case medium
case high
case required
case custom(value: Float)
public init?(rawValue: Float) {
if rawValue < 0 || rawValue > 1000 {
return nil
}
self = .custom(value: rawValue)
}
private var macOSMedium: Float {
#if os(OSX)
return 501.0
#else
return 500.0
#endif
}
public var rawValue: Float {
switch self {
case .low:
return 250.0
case .medium:
return macOSMedium
case .high:
return 750.0
case .required:
return 1000.0
case let .custom(value):
return value
}
}
public func advanced(by n: Float) -> Priority {
return .custom(value: self.rawValue + n)
}
public func distance(to other: Priority) -> Float {
return other.rawValue - self.rawValue
}
public static func ==(lhs: Priority, rhs: Priority) -> Bool {
return lhs.rawValue == rhs.rawValue
}
@discardableResult
public func priority(_ amount: ConstraintPriority) -> ConstraintMakerFinalizable {
self.description.priority = amount.value
return self
}
@discardableResult
@available(*, deprecated:3.0, message:"Use priority(level: .custom(Float)) instead.")
public func priority(_ amount: ConstraintPriorityTarget) -> ConstraintMakerFinalizable {
self.description.priority = amount
return self
}
@discardableResult
public func priority(level: Priority) -> ConstraintMakerFinalizable {
return self.priority(level: ConstraintMakerPriortizable.Priority(rawValue: level.rawValue)!)
}
@available(*, deprecated:3.0, message:"Use priority(level: Priority) instead.")
@available(*, deprecated:3.0, message:"Use priority(.required) instead.")
@discardableResult
public func priorityRequired() -> ConstraintMakerFinalizable {
return self.priority(level: .required)
return self.priority(.required)
}
@available(*, deprecated:3.0, message:"Use priority(level: Priority) instead.")
@available(*, deprecated:3.0, message:"Use priority(.high) instead.")
@discardableResult
public func priorityHigh() -> ConstraintMakerFinalizable {
return self.priority(level: .high)
return self.priority(.high)
}
@available(*, deprecated:3.0, message:"Use priority(level: Priority) instead.")
@available(*, deprecated:3.0, message:"Use priority(.medium) instead.")
@discardableResult
public func priorityMedium() -> ConstraintMakerFinalizable {
return self.priority(level: .medium)
return self.priority(.medium)
}
@available(*, deprecated:3.0, message:"Use priority(level: Priority) instead.")
@available(*, deprecated:3.0, message:"Use priority(.low) instead.")
@discardableResult
public func priorityLow() -> ConstraintMakerFinalizable {
return self.priority(level: .low)
return self.priority(.low)
}
}

View File

@ -0,0 +1,64 @@
//
// SnapKit
//
// Copyright (c) 2011-Present SnapKit Team - https://github.com/SnapKit
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#if os(iOS) || os(tvOS)
import UIKit
#else
import AppKit
#endif
public struct ConstraintPriority : ExpressibleByFloatLiteral {
public typealias FloatLiteralType = Float
public let value: FloatLiteralType
public init(floatLiteral value: FloatLiteralType) {
self.value = value
}
public init(_ value: Float) {
self.value = value
}
public static var required: ConstraintPriority {
return 1000.0
}
public static var high: ConstraintPriority {
return 750.0
}
public static var medium: ConstraintPriority {
#if os(OSX)
return 501.0
#else
return 500.0
#endif
}
public static var low: ConstraintPriority {
return 250.0
}
}