2014-07-25 12:24:17 +08:00
|
|
|
//
|
2015-04-15 19:07:50 +08:00
|
|
|
// SnapKit
|
2014-07-25 12:24:17 +08:00
|
|
|
//
|
2016-01-26 18:49:04 +08:00
|
|
|
// Copyright (c) 2011-Present SnapKit Team - https://github.com/SnapKit
|
2014-07-25 12:24:17 +08:00
|
|
|
//
|
2014-07-29 08:39:59 +08:00
|
|
|
// 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.
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2015-09-25 12:37:33 +08:00
|
|
|
#if os(iOS) || os(tvOS)
|
2016-01-26 18:49:04 +08:00
|
|
|
import UIKit
|
2014-07-30 08:55:31 +08:00
|
|
|
#else
|
2016-01-26 18:49:04 +08:00
|
|
|
import AppKit
|
2014-07-30 08:55:31 +08:00
|
|
|
#endif
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2015-04-22 20:31:40 +08:00
|
|
|
public class ConstraintMaker {
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
public var left: ConstraintMakerExtendable {
|
2016-08-06 18:38:02 +08:00
|
|
|
return self.makeExtendableWithAttributes(.left)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
public var top: ConstraintMakerExtendable {
|
2016-08-06 18:38:02 +08:00
|
|
|
return self.makeExtendableWithAttributes(.top)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
public var bottom: ConstraintMakerExtendable {
|
2016-08-06 18:38:02 +08:00
|
|
|
return self.makeExtendableWithAttributes(.bottom)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
public var right: ConstraintMakerExtendable {
|
2016-08-06 18:38:02 +08:00
|
|
|
return self.makeExtendableWithAttributes(.right)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
public var leading: ConstraintMakerExtendable {
|
2016-08-06 18:38:02 +08:00
|
|
|
return self.makeExtendableWithAttributes(.leading)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
public var trailing: ConstraintMakerExtendable {
|
2016-08-06 18:38:02 +08:00
|
|
|
return self.makeExtendableWithAttributes(.trailing)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
public var width: ConstraintMakerExtendable {
|
2016-08-06 18:38:02 +08:00
|
|
|
return self.makeExtendableWithAttributes(.width)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
public var height: ConstraintMakerExtendable {
|
2016-08-06 18:38:02 +08:00
|
|
|
return self.makeExtendableWithAttributes(.height)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2014-07-29 08:39:59 +08:00
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
public var centerX: ConstraintMakerExtendable {
|
2016-08-06 18:38:02 +08:00
|
|
|
return self.makeExtendableWithAttributes(.centerX)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
public var centerY: ConstraintMakerExtendable {
|
2016-08-06 18:38:02 +08:00
|
|
|
return self.makeExtendableWithAttributes(.centerY)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2016-08-06 18:38:02 +08:00
|
|
|
|
2019-04-03 01:25:22 +08:00
|
|
|
@available(*, deprecated, message:"Use lastBaseline instead")
|
2016-01-26 18:49:04 +08:00
|
|
|
public var baseline: ConstraintMakerExtendable {
|
2016-08-06 18:38:02 +08:00
|
|
|
return self.makeExtendableWithAttributes(.lastBaseline)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2016-08-06 18:38:02 +08:00
|
|
|
public var lastBaseline: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(.lastBaseline)
|
|
|
|
}
|
|
|
|
|
|
|
|
@available(iOS 8.0, OSX 10.11, *)
|
2016-01-26 18:49:04 +08:00
|
|
|
public var firstBaseline: ConstraintMakerExtendable {
|
2016-08-06 18:38:02 +08:00
|
|
|
return self.makeExtendableWithAttributes(.firstBaseline)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2016-01-26 18:49:04 +08:00
|
|
|
public var leftMargin: ConstraintMakerExtendable {
|
2016-08-06 18:38:02 +08:00
|
|
|
return self.makeExtendableWithAttributes(.leftMargin)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2016-01-26 18:49:04 +08:00
|
|
|
public var rightMargin: ConstraintMakerExtendable {
|
2016-08-06 18:38:02 +08:00
|
|
|
return self.makeExtendableWithAttributes(.rightMargin)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2016-10-03 17:47:58 +08:00
|
|
|
@available(iOS 8.0, *)
|
|
|
|
public var topMargin: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(.topMargin)
|
|
|
|
}
|
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2016-01-26 18:49:04 +08:00
|
|
|
public var bottomMargin: ConstraintMakerExtendable {
|
2016-08-06 18:38:02 +08:00
|
|
|
return self.makeExtendableWithAttributes(.bottomMargin)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2016-01-26 18:49:04 +08:00
|
|
|
public var leadingMargin: ConstraintMakerExtendable {
|
2016-08-06 18:38:02 +08:00
|
|
|
return self.makeExtendableWithAttributes(.leadingMargin)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2016-01-26 18:49:04 +08:00
|
|
|
public var trailingMargin: ConstraintMakerExtendable {
|
2016-08-06 18:38:02 +08:00
|
|
|
return self.makeExtendableWithAttributes(.trailingMargin)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2016-01-26 18:49:04 +08:00
|
|
|
public var centerXWithinMargins: ConstraintMakerExtendable {
|
2016-08-06 18:38:02 +08:00
|
|
|
return self.makeExtendableWithAttributes(.centerXWithinMargins)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2016-01-26 18:49:04 +08:00
|
|
|
public var centerYWithinMargins: ConstraintMakerExtendable {
|
2016-08-06 18:38:02 +08:00
|
|
|
return self.makeExtendableWithAttributes(.centerYWithinMargins)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2014-07-25 12:24:17 +08:00
|
|
|
|
2016-01-26 18:49:04 +08:00
|
|
|
public var edges: ConstraintMakerExtendable {
|
2016-08-06 18:38:02 +08:00
|
|
|
return self.makeExtendableWithAttributes(.edges)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
|
|
|
public var size: ConstraintMakerExtendable {
|
2016-08-06 18:38:02 +08:00
|
|
|
return self.makeExtendableWithAttributes(.size)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2016-08-06 19:25:03 +08:00
|
|
|
public var center: ConstraintMakerExtendable {
|
|
|
|
return self.makeExtendableWithAttributes(.center)
|
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2016-01-26 18:49:04 +08:00
|
|
|
public var margins: ConstraintMakerExtendable {
|
2016-08-06 18:38:02 +08:00
|
|
|
return self.makeExtendableWithAttributes(.margins)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2015-09-19 08:21:42 +08:00
|
|
|
@available(iOS 8.0, *)
|
2016-01-26 18:49:04 +08:00
|
|
|
public var centerWithinMargins: ConstraintMakerExtendable {
|
2016-08-06 18:38:02 +08:00
|
|
|
return self.makeExtendableWithAttributes(.centerWithinMargins)
|
2016-01-26 18:49:04 +08:00
|
|
|
}
|
|
|
|
|
2016-10-07 18:58:56 +08:00
|
|
|
private let item: LayoutConstraintItem
|
2016-01-26 18:49:04 +08:00
|
|
|
private var descriptions = [ConstraintDescription]()
|
2015-04-12 18:21:02 +08:00
|
|
|
|
2016-10-07 18:58:56 +08:00
|
|
|
internal init(item: LayoutConstraintItem) {
|
|
|
|
self.item = item
|
|
|
|
self.item.prepare()
|
2014-07-25 12:24:17 +08:00
|
|
|
}
|
|
|
|
|
2016-06-15 09:49:49 +08:00
|
|
|
internal func makeExtendableWithAttributes(_ attributes: ConstraintAttributes) -> ConstraintMakerExtendable {
|
2016-10-07 18:58:56 +08:00
|
|
|
let description = ConstraintDescription(item: self.item, attributes: attributes)
|
2016-01-26 18:49:04 +08:00
|
|
|
self.descriptions.append(description)
|
|
|
|
return ConstraintMakerExtendable(description)
|
2014-07-25 12:24:17 +08:00
|
|
|
}
|
|
|
|
|
2016-10-07 18:58:56 +08:00
|
|
|
internal static func prepareConstraints(item: LayoutConstraintItem, closure: (_ make: ConstraintMaker) -> Void) -> [Constraint] {
|
|
|
|
let maker = ConstraintMaker(item: item)
|
2016-08-16 18:53:27 +08:00
|
|
|
closure(maker)
|
2016-11-23 19:03:41 +08:00
|
|
|
var constraints: [Constraint] = []
|
|
|
|
for description in maker.descriptions {
|
|
|
|
guard let constraint = description.constraint else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
constraints.append(constraint)
|
|
|
|
}
|
2015-07-30 14:49:47 +08:00
|
|
|
return constraints
|
2015-02-17 06:11:58 +08:00
|
|
|
}
|
|
|
|
|
2016-10-07 18:58:56 +08:00
|
|
|
internal static func makeConstraints(item: LayoutConstraintItem, closure: (_ make: ConstraintMaker) -> Void) {
|
2018-06-23 19:15:34 +08:00
|
|
|
let constraints = prepareConstraints(item: item, closure: closure)
|
2015-04-12 18:21:02 +08:00
|
|
|
for constraint in constraints {
|
2016-08-06 18:38:02 +08:00
|
|
|
constraint.activateIfNeeded(updatingExisting: false)
|
2014-07-25 12:24:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-07 18:58:56 +08:00
|
|
|
internal static func remakeConstraints(item: LayoutConstraintItem, closure: (_ make: ConstraintMaker) -> Void) {
|
|
|
|
self.removeConstraints(item: item)
|
|
|
|
self.makeConstraints(item: item, closure: closure)
|
2014-07-25 12:24:17 +08:00
|
|
|
}
|
2014-12-31 07:47:31 +08:00
|
|
|
|
2016-10-07 18:58:56 +08:00
|
|
|
internal static func updateConstraints(item: LayoutConstraintItem, closure: (_ make: ConstraintMaker) -> Void) {
|
|
|
|
guard item.constraints.count > 0 else {
|
|
|
|
self.makeConstraints(item: item, closure: closure)
|
2016-09-10 17:51:20 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-23 19:15:34 +08:00
|
|
|
let constraints = prepareConstraints(item: item, closure: closure)
|
2015-04-12 18:21:02 +08:00
|
|
|
for constraint in constraints {
|
2016-08-06 18:38:02 +08:00
|
|
|
constraint.activateIfNeeded(updatingExisting: true)
|
2015-01-08 14:20:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-07 18:58:56 +08:00
|
|
|
internal static func removeConstraints(item: LayoutConstraintItem) {
|
|
|
|
let constraints = item.constraints
|
2016-08-06 18:38:02 +08:00
|
|
|
for constraint in constraints {
|
|
|
|
constraint.deactivateIfNeeded()
|
2016-07-12 11:56:23 +08:00
|
|
|
}
|
2014-12-31 07:47:31 +08:00
|
|
|
}
|
2016-01-26 18:49:04 +08:00
|
|
|
|
2015-06-17 19:09:54 +08:00
|
|
|
}
|