-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathPinLayout+Relative.swift
319 lines (279 loc) · 14.1 KB
/
PinLayout+Relative.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright (c) 2017 Luc Dion
// 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
extension PinLayout {
//
// above(of ...)
//
@discardableResult
public func above(of relativeView: PinView, aligned: HorizontalAlign = .none) -> PinLayout {
func context() -> String { return relativeContext("above", [relativeView], aligned) }
return above(of: [relativeView], aligned: aligned, context: context)
}
@discardableResult
public func above(of relativeViews: [PinView], aligned: HorizontalAlign = .none) -> PinLayout {
func context() -> String { return relativeContext("above", relativeViews, aligned) }
return above(of: relativeViews, aligned: aligned, context: context)
}
//
// below(of ...)
//
@discardableResult
public func below(of relativeView: PinView, aligned: HorizontalAlign = .none) -> PinLayout {
func context() -> String { return relativeContext("below", [relativeView], aligned) }
return below(of: [relativeView], aligned: aligned, context: context)
}
@discardableResult
public func below(of relativeViews: [PinView], aligned: HorizontalAlign = .none) -> PinLayout {
func context() -> String { return relativeContext("below", relativeViews, aligned) }
return below(of: relativeViews, aligned: aligned, context: context)
}
//
// left(of ...)
//
@discardableResult
public func left(of relativeView: PinView, aligned: VerticalAlign = .none) -> PinLayout {
func context() -> String { return relativeContext("left", [relativeView], aligned) }
return left(of: [relativeView], aligned: aligned, context: context)
}
@discardableResult
public func left(of relativeViews: [PinView], aligned: VerticalAlign = .none) -> PinLayout {
func context() -> String { return relativeContext("left", relativeViews, aligned) }
return left(of: relativeViews, aligned: aligned, context: context)
}
//
// right(of ...)
//
@discardableResult
public func right(of relativeView: PinView, aligned: VerticalAlign = .none) -> PinLayout {
func context() -> String { return relativeContext("right", [relativeView], aligned) }
return right(of: [relativeView], aligned: aligned, context: context)
}
@discardableResult
public func right(of relativeViews: [PinView], aligned: VerticalAlign = .none) -> PinLayout {
func context() -> String { return relativeContext("right", relativeViews, aligned) }
return right(of: relativeViews, aligned: aligned, context: context)
}
//
// before(of ...)
//
@discardableResult
public func before(of relativeView: PinView, aligned: VerticalAlign = .none) -> PinLayout {
func context() -> String { return relativeContext("before", [relativeView], aligned) }
if isLTR() {
return left(of: [relativeView], aligned: aligned, context: context)
} else {
return right(of: [relativeView], aligned: aligned, context: context)
}
}
@discardableResult
public func before(of relativeViews: [PinView], aligned: VerticalAlign = .none) -> PinLayout {
func context() -> String { return relativeContext("before", relativeViews, aligned) }
if isLTR() {
return left(of: relativeViews, aligned: aligned, context: context)
} else {
return right(of: relativeViews, aligned: aligned, context: context)
}
}
//
// after(of ...)
//
@discardableResult
public func after(of relativeView: PinView, aligned: VerticalAlign = .none) -> PinLayout {
func context() -> String { return relativeContext("after", [relativeView], aligned) }
if isLTR() {
return right(of: [relativeView], aligned: aligned, context: context)
} else {
return left(of: [relativeView], aligned: aligned, context: context)
}
}
@discardableResult
public func after(of relativeViews: [PinView], aligned: VerticalAlign = .none) -> PinLayout {
func context() -> String { return relativeContext("after", relativeViews, aligned) }
if isLTR() {
return right(of: relativeViews, aligned: aligned, context: context)
} else {
return left(of: relativeViews, aligned: aligned, context: context)
}
}
}
// MARK: private
extension PinLayout {
fileprivate func above(of relativeViews: [PinView], aligned: HorizontalAlign, context: Context) -> PinLayout {
guard let relativeViews = validateRelativeViews(relativeViews, context: context) else { return self }
let anchors: [Anchor]
switch aligned {
case .left: anchors = relativeViews.map({ $0.anchor.topLeft })
case .center: anchors = relativeViews.map({ $0.anchor.topCenter })
case .right: anchors = relativeViews.map({ $0.anchor.topRight })
case .start: anchors = isLTR() ? relativeViews.map({ $0.anchor.topLeft }) : relativeViews.map({ $0.anchor.topRight })
case .end: anchors = isLTR() ? relativeViews.map({ $0.anchor.topRight }) : relativeViews.map({ $0.anchor.topLeft })
case .none: anchors = relativeViews.map({ $0.anchor.topLeft })
}
if let coordinates = computeCoordinates(forAnchors: anchors, context) {
setBottom(getTopMostCoordinate(list: coordinates), context)
applyHorizontalAlignment(aligned, coordinates: coordinates, context: context)
}
return self
}
fileprivate func below(of relativeViews: [PinView], aligned: HorizontalAlign, context: Context) -> PinLayout {
guard let relativeViews = validateRelativeViews(relativeViews, context: context) else { return self }
let anchors: [Anchor]
switch aligned {
case .left: anchors = relativeViews.map({ $0.anchor.bottomLeft })
case .center: anchors = relativeViews.map({ $0.anchor.bottomCenter })
case .right: anchors = relativeViews.map({ $0.anchor.bottomRight })
case .start: anchors = isLTR() ? relativeViews.map({ $0.anchor.bottomLeft }) : relativeViews.map({ $0.anchor.bottomRight })
case .end: anchors = isLTR() ? relativeViews.map({ $0.anchor.bottomRight }) : relativeViews.map({ $0.anchor.bottomLeft })
case .none: anchors = relativeViews.map({ $0.anchor.bottomLeft })
}
if let coordinates = computeCoordinates(forAnchors: anchors, context) {
setTop(getBottomMostCoordinate(list: coordinates), context)
applyHorizontalAlignment(aligned, coordinates: coordinates, context: context)
}
return self
}
fileprivate func left(of relativeViews: [PinView], aligned: VerticalAlign, context: Context) -> PinLayout {
guard let relativeViews = validateRelativeViews(relativeViews, context: context) else { return self }
let anchors: [Anchor]
switch aligned {
case .top: anchors = relativeViews.map({ $0.anchor.topLeft })
case .center: anchors = relativeViews.map({ $0.anchor.centerLeft })
case .bottom: anchors = relativeViews.map({ $0.anchor.bottomLeft })
case .none: anchors = relativeViews.map({ $0.anchor.topLeft })
}
if let coordinates = computeCoordinates(forAnchors: anchors, context) {
setRight(getLeftMostCoordinate(list: coordinates), context)
applyVerticalAlignment(aligned, coordinates: coordinates, context: context)
}
return self
}
fileprivate func right(of relativeViews: [PinView], aligned: VerticalAlign, context: Context) -> PinLayout {
guard let relativeViews = validateRelativeViews(relativeViews, context: context) else { return self }
let anchors: [Anchor]
switch aligned {
case .top: anchors = relativeViews.map({ $0.anchor.topRight })
case .center: anchors = relativeViews.map({ $0.anchor.centerRight })
case .bottom: anchors = relativeViews.map({ $0.anchor.bottomRight })
case .none: anchors = relativeViews.map({ $0.anchor.topRight })
}
if let coordinates = computeCoordinates(forAnchors: anchors, context) {
setLeft(getRightMostCoordinate(list: coordinates), context)
applyVerticalAlignment(aligned, coordinates: coordinates, context: context)
}
return self
}
internal func applyHorizontalAlignment(_ aligned: HorizontalAlign?, coordinates: [CGPoint], context: Context) {
guard let aligned = aligned, aligned != .none else { return }
switch aligned {
case .left: setLeft(getLeftMostCoordinate(list: coordinates), context)
case .center: setHorizontalCenter(getAverageHCenterCoordinate(list: coordinates), context)
case .right: setRight(getRightMostCoordinate(list: coordinates), context)
case .start:
isLTR() ? setLeft(getLeftMostCoordinate(list: coordinates), context) :
setRight(getRightMostCoordinate(list: coordinates), context)
case .end:
isLTR() ? setRight(getRightMostCoordinate(list: coordinates), context) :
setLeft(getLeftMostCoordinate(list: coordinates), context)
case .none: break
}
}
internal func applyVerticalAlignment(_ aligned: VerticalAlign?, coordinates: [CGPoint], context: Context) {
guard let aligned = aligned, aligned != .none else { return }
switch aligned {
case .top: setTop(getTopMostCoordinate(list: coordinates), context)
case .center: setVerticalCenter(getAverageVCenterCoordinate(list: coordinates), context)
case .bottom: setBottom(getBottomMostCoordinate(list: coordinates), context)
case .none: break
}
}
private func getTopMostCoordinate(list: [CGPoint]) -> CGFloat {
assert(list.count > 0)
let firstCoordinate = list[0].y
return list.dropFirst().reduce(firstCoordinate, { (bestCoordinate, otherCoordinates) -> CGFloat in
return (otherCoordinates.y < bestCoordinate) ? otherCoordinates.y : bestCoordinate
})
}
private func getBottomMostCoordinate(list: [CGPoint]) -> CGFloat {
assert(list.count > 0)
let firstCoordinate = list[0].y
return list.dropFirst().reduce(firstCoordinate, { (bestCoordinate, otherCoordinates) -> CGFloat in
return (otherCoordinates.y > bestCoordinate) ? otherCoordinates.y : bestCoordinate
})
}
private func getLeftMostCoordinate(list: [CGPoint]) -> CGFloat {
assert(list.count > 0)
let firstCoordinate = list[0].x
return list.dropFirst().reduce(firstCoordinate, { (bestCoordinate, otherCoordinates) -> CGFloat in
return (otherCoordinates.x < bestCoordinate) ? otherCoordinates.x : bestCoordinate
})
}
private func getRightMostCoordinate(list: [CGPoint]) -> CGFloat {
assert(list.count > 0)
let firstCoordinate = list[0].x
return list.dropFirst().reduce(firstCoordinate, { (bestCoordinate, otherCoordinates) -> CGFloat in
return (otherCoordinates.x > bestCoordinate) ? otherCoordinates.x : bestCoordinate
})
}
private func getAverageHCenterCoordinate(list: [CGPoint]) -> CGFloat {
assert(list.count > 0)
let sum = list.reduce(0, { (result, point) -> CGFloat in
return result + point.x
})
return sum / CGFloat(list.count)
}
private func getAverageVCenterCoordinate(list: [CGPoint]) -> CGFloat {
assert(list.count > 0)
let sum = list.reduce(0, { (result, point) -> CGFloat in
return result + point.y
})
return sum / CGFloat(list.count)
}
private func validateRelativeViews(_ relativeViews: [PinView], context: Context) -> [PinView]? {
guard layoutSuperview(context) != nil else { return nil }
guard relativeViews.count > 0 else {
warnWontBeApplied("At least one view must be visible (i.e. UIView.isHidden != true) ", context)
return nil
}
return relativeViews
}
fileprivate func relativeContext(_ type: String, _ relativeViews: [PinView], _ aligned: HorizontalAlign) -> String {
return relativeContext(type, relativeViews, aligned: aligned == HorizontalAlign.none ? nil : aligned.description)
}
fileprivate func relativeContext(_ type: String, _ relativeViews: [PinView], _ aligned: VerticalAlign) -> String {
return relativeContext(type, relativeViews, aligned: aligned == VerticalAlign.none ? nil : aligned.description)
}
fileprivate func relativeContext(_ type: String, _ relativeViews: [PinView], aligned: String?) -> String {
let relativeViewsText: String
if relativeViews.count == 1 {
relativeViewsText = viewDescription(relativeViews[0])
} else {
relativeViewsText = "[\(relativeViews.map({ viewDescription($0) }).joined(separator: ", "))]"
}
if let aligned = aligned {
return "\(type)(of: \(relativeViewsText), aligned: .\(aligned))"
} else {
return "\(type)(of: \(relativeViewsText))"
}
}
}