forked from robb/Cartography
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Property.swift
209 lines (174 loc) · 7.08 KB
/
Property.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//
// Property.swift
// Cartography
//
// Created by Robert Böhnke on 17/06/14.
// Copyright (c) 2014 Robert Böhnke. All rights reserved.
//
#if os(iOS)
import UIKit
#else
import AppKit
#endif
public protocol Property {
var attribute: NSLayoutAttribute { get }
var context: Context { get }
var view: View { get }
}
// MARK: Equality
/// Properties conforming to this protocol can use the `==` operator with
/// numerical constants.
public protocol NumericalEquality : Property { }
/// Declares a property equal to a numerical constant.
///
/// - parameter lhs: The affected property. The associated view will have
/// `translatesAutoresizingMaskIntoConstraints` set to `false`.
/// - parameter rhs: The numerical constant.
///
/// - returns: An `NSLayoutConstraint`.
///
public func == (lhs: NumericalEquality, rhs: CGFloat) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, coefficients: Coefficients(1, rhs))
}
/// Properties conforming to this protocol can use the `==` operator with other
/// properties of the same type.
public protocol RelativeEquality : Property { }
/// Declares a property equal to a the result of an expression.
///
/// - parameter lhs: The affected property. The associated view will have
/// `translatesAutoresizingMaskIntoConstraints` set to `false`.
/// - parameter rhs: The expression.
///
/// - returns: An `NSLayoutConstraint`.
///
public func == <P: RelativeEquality>(lhs: P, rhs: Expression<P>) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, coefficients: rhs.coefficients[0], to: rhs.value)
}
/// Declares a property equal to another property.
///
/// - parameter lhs: The affected property. The associated view will have
/// `translatesAutoresizingMaskIntoConstraints` set to `false`.
/// - parameter rhs: The other property.
///
public func == <P: RelativeEquality>(lhs: P, rhs: P) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, to: rhs)
}
// MARK: Inequality
/// Properties conforming to this protocol can use the `<=` and `>=` operators
/// with numerical constants.
public protocol NumericalInequality : Property { }
/// Declares a property less than or equal to a numerical constant.
///
/// - parameter lhs: The affected property. The associated view will have
/// `translatesAutoresizingMaskIntoConstraints` set to `false`.
/// - parameter rhs: The numerical constant.
///
/// - returns: An `NSLayoutConstraint`.
///
public func <= (lhs: NumericalInequality, rhs: CGFloat) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, coefficients: Coefficients(1, rhs), relation: NSLayoutRelation.LessThanOrEqual)
}
/// Declares a property greater than or equal to a numerical constant.
///
/// - parameter lhs: The affected property. The associated view will have
/// `translatesAutoresizingMaskIntoConstraints` set to `false`.
/// - parameter rhs: The numerical constant.
///
/// - returns: An `NSLayoutConstraint`.
///
public func >= (lhs: NumericalInequality, rhs: CGFloat) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, coefficients: Coefficients(1, rhs), relation: NSLayoutRelation.GreaterThanOrEqual)
}
/// Properties conforming to this protocol can use the `<=` and `>=` operators
/// with other properties of the same type.
public protocol RelativeInequality : Property { }
/// Declares a property less than or equal to another property.
///
/// - parameter lhs: The affected property. The associated view will have
/// `translatesAutoresizingMaskIntoConstraints` set to `false`.
/// - parameter rhs: The other property.
///
/// - returns: An `NSLayoutConstraint`.
///
public func <= <P: RelativeInequality>(lhs: P, rhs: P) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, to: rhs, relation: NSLayoutRelation.LessThanOrEqual)
}
/// Declares a property greater than or equal to another property.
///
/// - parameter lhs: The affected property. The associated view will have
/// `translatesAutoresizingMaskIntoConstraints` set to `false`.
/// - parameter rhs: The other property.
///
/// - returns: An `NSLayoutConstraint`.
///
public func >= <P: RelativeInequality>(lhs: P, rhs: P) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, to: rhs, relation: NSLayoutRelation.GreaterThanOrEqual)
}
/// Declares a property less than or equal to the result of an expression.
///
/// - parameter lhs: The affected property. The associated view will have
/// `translatesAutoresizingMaskIntoConstraints` set to `false`.
/// - parameter rhs: The other property.
///
/// - returns: An `NSLayoutConstraint`.
///
public func <= <P: RelativeInequality>(lhs: P, rhs: Expression<P>) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, coefficients: rhs.coefficients[0], to: rhs.value, relation: NSLayoutRelation.LessThanOrEqual)
}
/// Declares a property greater than or equal to the result of an expression.
///
/// - parameter lhs: The affected property. The associated view will have
/// `translatesAutoresizingMaskIntoConstraints` set to `false`.
/// - parameter rhs: The other property.
///
/// - returns: An `NSLayoutConstraint`.
///
public func >= <P: RelativeInequality>(lhs: P, rhs: Expression<P>) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, coefficients: rhs.coefficients[0], to: rhs.value, relation: NSLayoutRelation.GreaterThanOrEqual)
}
// Mark: Addition
public protocol Addition : Property { }
public func + <P: Addition>(c: CGFloat, rhs: P) -> Expression<P> {
return Expression(rhs, [ Coefficients(1, c) ])
}
public func + <P: Addition>(lhs: P, rhs: CGFloat) -> Expression<P> {
return rhs + lhs
}
public func + <P: Addition>(c: CGFloat, rhs: Expression<P>) -> Expression<P> {
return Expression(rhs.value, rhs.coefficients.map { $0 + c })
}
public func + <P: Addition>(lhs: Expression<P>, rhs: CGFloat) -> Expression<P> {
return rhs + lhs
}
public func - <P: Addition>(c: CGFloat, rhs: P) -> Expression<P> {
return Expression(rhs, [ Coefficients(1, -c) ])
}
public func - <P: Addition>(lhs: P, rhs: CGFloat) -> Expression<P> {
return rhs - lhs
}
public func - <P: Addition>(c: CGFloat, rhs: Expression<P>) -> Expression<P> {
return Expression(rhs.value, rhs.coefficients.map { $0 - c})
}
public func - <P: Addition>(lhs: Expression<P>, rhs: CGFloat) -> Expression<P> {
return rhs - lhs
}
// MARK: Multiplication
public protocol Multiplication : Property { }
public func * <P: Multiplication>(m: CGFloat, rhs: Expression<P>) -> Expression<P> {
return Expression(rhs.value, rhs.coefficients.map { $0 * m })
}
public func * <P: Multiplication>(lhs: Expression<P>, rhs: CGFloat) -> Expression<P> {
return rhs * lhs
}
public func * <P: Multiplication>(m: CGFloat, rhs: P) -> Expression<P> {
return Expression(rhs, [ Coefficients(m, 0) ])
}
public func * <P: Multiplication>(lhs: P, rhs: CGFloat) -> Expression<P> {
return rhs * lhs
}
public func / <P: Multiplication>(lhs: Expression<P>, rhs: CGFloat) -> Expression<P> {
return lhs * (1 / rhs)
}
public func / <P: Multiplication>(lhs: P, rhs: CGFloat) -> Expression<P> {
return lhs * (1 / rhs)
}