/
MethodChain.swift
390 lines (303 loc) · 9.9 KB
/
MethodChain.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//
// MethodChain.swift
// TextureSwiftSupport
//
// Created by muukii on 2019/10/09.
// Copyright © 2019 muukii. All rights reserved.
//
import Foundation
import UIKit
import AsyncDisplayKit
public enum Edge: Int8, CaseIterable {
case top = 0
case left = 1
case bottom = 2
case right = 3
public struct Set: OptionSet {
public var rawValue: Int8
public var isEmpty: Bool {
rawValue == 0
}
public init(rawValue: Int8) {
self.rawValue = rawValue
}
public static let top: Set = .init(rawValue: 1 << 1)
public static let left: Set = .init(rawValue: 1 << 2)
public static let bottom: Set = .init(rawValue: 1 << 3)
public static let right: Set = .init(rawValue: 1 << 4)
public static var horizontal: Set {
[.left, .right]
}
public static var vertical: Set {
[.top, .bottom]
}
public static var all: Set {
[.top, .bottom, .right, left]
}
}
}
@inline(__always)
fileprivate func makeInsets(_ padding: CGFloat) -> UIEdgeInsets {
.init(top: padding, left: padding, bottom: padding, right: padding)
}
@inline(__always)
fileprivate func makeInsets(_ edges: Edge.Set, _ padding: CGFloat) -> UIEdgeInsets {
var insets = UIEdgeInsets.zero
if edges.contains(.top) {
insets.top = padding
}
if edges.contains(.left) {
insets.left = padding
}
if edges.contains(.bottom) {
insets.bottom = padding
}
if edges.contains(.right) {
insets.right = padding
}
return insets
}
fileprivate func combineInsets(_ lhs: UIEdgeInsets, rhs: UIEdgeInsets) -> UIEdgeInsets {
var base = lhs
base.top += rhs.top
base.right += rhs.right
base.left += rhs.left
base.bottom += rhs.bottom
return base
}
// MARK: - Padding
extension _ASLayoutElementType {
public func padding(_ padding: CGFloat) -> InsetLayout<Self> {
InsetLayout(insets: makeInsets(padding)) {
self
}
}
public func padding(_ edgeInsets: UIEdgeInsets) -> InsetLayout<Self> {
InsetLayout(insets: edgeInsets) {
self
}
}
public func padding(_ edges: Edge.Set, _ padding: CGFloat) -> InsetLayout<Self> {
return InsetLayout(insets: makeInsets(edges, padding)) {
self
}
}
}
extension InsetLayout {
public func padding(_ padding: CGFloat) -> Self {
var _self = self
_self.insets = combineInsets(_self.insets, rhs: makeInsets(padding))
return _self
}
public func padding(_ edgeInsets: UIEdgeInsets) -> Self {
var _self = self
_self.insets = combineInsets(_self.insets, rhs: edgeInsets)
return _self
}
public func padding(_ edges: Edge.Set, _ padding: CGFloat) -> Self {
var _self = self
_self.insets = combineInsets(_self.insets, rhs: makeInsets(edges, padding))
return _self
}
}
// MARK: - Background
extension _ASLayoutElementType {
public func background<Background: _ASLayoutElementType>(_ backgroundContent: Background) -> BackgroundLayout<Background, Self> {
BackgroundLayout(content: { self }, background: { backgroundContent })
}
public func overlay<Overlay: _ASLayoutElementType>(_ overlayContent: Overlay) -> OverlayLayout<Overlay, Self> {
OverlayLayout(content: { self }, overlay: { overlayContent })
}
/// Make aspectRatio
///
/// - Parameters:
/// - aspectRatio: The ratio of width to height to use for the resulting view
public func aspectRatio(_ aspectRatio: CGFloat) -> AspectRatioLayout<Self> {
AspectRatioLayout(ratio: aspectRatio, content: { self })
}
/// Make aspectRatio
///
/// - Parameters:
/// - aspectRatio: The ratio of CGSize to use for the resulting view
public func aspectRatio(_ aspectRatio: CGSize) -> AspectRatioLayout<Self> {
AspectRatioLayout(ratio: aspectRatio, content: { self })
}
}
public struct FlexGlowModifier: ModifierType {
public func modify(element: ASLayoutElement) -> ASLayoutElement {
element.style.flexGrow = 1
return element
}
}
public struct FlexShrinkModifier: ModifierType {
public func modify(element: ASLayoutElement) -> ASLayoutElement {
element.style.flexShrink = 1
return element
}
}
public struct AlignSelfModifier: ModifierType {
public var alignSelf: ASStackLayoutAlignSelf
public init(alignSelf: ASStackLayoutAlignSelf) {
self.alignSelf = alignSelf
}
public func modify(element: ASLayoutElement) -> ASLayoutElement {
element.style.alignSelf = alignSelf
return element
}
}
public struct SizeModifier: ModifierType {
public var width: ASDimension?
public var height: ASDimension?
public init(size: CGSize) {
self.width = .init(unit: .points, value: size.width)
self.height = .init(unit: .points, value: size.height)
}
public init(width: ASDimension?, height: ASDimension?) {
self.width = width
self.height = height
}
public func modify(element: ASLayoutElement) -> ASLayoutElement {
width.map {
element.style.width = $0
}
height.map {
element.style.height = $0
}
return element
}
}
public struct MinSizeModifier: ModifierType {
public var minWidth: ASDimension?
public var minHeight: ASDimension?
public init(minSize: CGSize) {
self.minWidth = .init(unit: .points, value: minSize.width)
self.minHeight = .init(unit: .points, value: minSize.height)
}
public init(minWidth: ASDimension?, minHeight: ASDimension?) {
self.minWidth = minWidth
self.minHeight = minHeight
}
public func modify(element: ASLayoutElement) -> ASLayoutElement {
minWidth.map {
element.style.minWidth = $0
}
minHeight.map {
element.style.minHeight = $0
}
return element
}
}
public struct MaxSizeModifier: ModifierType {
public var maxWidth: ASDimension?
public var maxHeight: ASDimension?
public init(maxSize: CGSize) {
self.maxWidth = .init(unit: .points, value: maxSize.width)
self.maxHeight = .init(unit: .points, value: maxSize.height)
}
public init(maxWidth: ASDimension?, maxHeight: ASDimension?) {
self.maxWidth = maxWidth
self.maxHeight = maxHeight
}
public func modify(element: ASLayoutElement) -> ASLayoutElement {
maxWidth.map {
element.style.maxWidth = $0
}
maxHeight.map {
element.style.maxHeight = $0
}
return element
}
}
public struct FlexBasisModifieer: ModifierType {
public let flexBasis: ASDimension
public init(flexBasis: ASDimension) {
self.flexBasis = flexBasis
}
public func modify(element: ASLayoutElement) -> ASLayoutElement {
element.style.flexBasis = flexBasis
return element
}
}
extension _ASLayoutElementType {
public func modify(_ modify: @escaping (ASLayoutElement) -> Void) -> ModifiedContent<Self, Modifier> {
modifier(
.init { element in
modify(element)
return element
}
)
}
public func flexGrow(_ flexGlow: CGFloat) -> ModifiedContent<Self, FlexGlowModifier> {
modifier(FlexGlowModifier())
}
public func flexShrink(_ flexGlow: CGFloat) -> ModifiedContent<Self, FlexShrinkModifier> {
modifier(FlexShrinkModifier())
}
public func flexBasis(fraction: CGFloat) -> ModifiedContent<Self, FlexBasisModifieer> {
modifier(FlexBasisModifieer(flexBasis: .init(unit: .fraction, value: fraction)))
}
public func preferredSize(_ preferredSize: CGSize) -> ModifiedContent<Self, SizeModifier> {
modifier(SizeModifier(size: preferredSize))
}
public func alignSelf(_ alignSelf: ASStackLayoutAlignSelf) -> ModifiedContent<Self, AlignSelfModifier> {
modifier(AlignSelfModifier(alignSelf: alignSelf))
}
public func minWidth(_ width: CGFloat) -> ModifiedContent<Self, MinSizeModifier> {
modifier(MinSizeModifier(minWidth: .init(unit: .points, value: width), minHeight: nil))
}
public func minHeight(_ height: CGFloat) -> ModifiedContent<Self, MinSizeModifier> {
modifier(MinSizeModifier(minWidth: nil, minHeight: .init(unit: .points, value: height)))
}
public func width(_ width: CGFloat) -> ModifiedContent<Self, SizeModifier> {
modifier(SizeModifier(width: .init(unit: .points, value: width), height: nil))
}
public func height(_ height: CGFloat) -> ModifiedContent<Self, SizeModifier> {
modifier(SizeModifier(width: nil, height: .init(unit: .points, value: height)))
}
public func minSize(_ size: CGSize) -> ModifiedContent<Self, MinSizeModifier> {
modifier(MinSizeModifier(minSize: size))
}
public func maxWidth(_ width: CGFloat) -> ModifiedContent<Self, MaxSizeModifier> {
modifier(MaxSizeModifier(maxWidth: .init(unit: .points, value: width), maxHeight: nil))
}
public func maxHeight(_ height: CGFloat) -> ModifiedContent<Self, MaxSizeModifier> {
modifier(MaxSizeModifier(maxWidth: nil, maxHeight: .init(unit: .points, value: height)))
}
public func maxSize(_ size: CGSize) -> ModifiedContent<Self, MaxSizeModifier> {
modifier(MaxSizeModifier(maxSize: size))
}
}
extension ModifiedContent where Modifier == MinSizeModifier {
public func minWidth(_ width: CGFloat) -> Self {
var _self = self
_self.modifier.minWidth = .init(unit: .points, value: width)
return _self
}
public func minHeight(_ height: CGFloat) -> Self {
var _self = self
_self.modifier.minWidth = .init(unit: .points, value: height)
return _self
}
public func minSize(_ size: CGSize) -> Self {
var _self = self
_self.modifier = MinSizeModifier(minSize: size)
return _self
}
}
extension ModifiedContent where Modifier == MaxSizeModifier {
public func maxWidth(_ width: CGFloat) -> Self {
var _self = self
_self.modifier.maxWidth = .init(unit: .points, value: width)
return _self
}
public func maxHeight(_ height: CGFloat) -> Self {
var _self = self
_self.modifier.maxWidth = .init(unit: .points, value: height)
return _self
}
public func maxSize(_ size: CGSize) -> Self {
var _self = self
_self.modifier = MaxSizeModifier(maxSize: size)
return _self
}
}