-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathItemInfo.swift
More file actions
498 lines (425 loc) · 17 KB
/
ItemInfo.swift
File metadata and controls
498 lines (425 loc) · 17 KB
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// 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
class ItemInfo {
unowned let view: UIView
unowned let stackItem: StackItemImpl
unowned let container: Container
private var _width: CGFloat?
var width: CGFloat? {
get {
return _width
}
set {
_width = newValue?.roundUsingDisplayScale()
}
}
var minWidth: CGFloat?
var maxWidth: CGFloat?
private var _height: CGFloat?
var height: CGFloat? {
get {
return _height
}
set {
_height = newValue?.roundUsingDisplayScale()
}
}
var minHeight: CGFloat?
var maxHeight: CGFloat?
var mainAxisLength: CGFloat? {
get {
return direction == .column ? height! : width!
}
set {
let value = newValue != nil ? max(0, newValue!) : nil
if direction == .column {
height = applyHeightMinMax(value)
} else {
width = applyWidthMinMax(value)
}
}
}
var mainAxisMaxLength: CGFloat? {
assert(width != nil || height != nil)
return direction == .column ? maxHeight : maxWidth
}
var mainAxisMinLength: CGFloat? {
return direction == .column ? minHeight : minWidth
}
var crossAxisLength: CGFloat {
get {
assert(width != nil || height != nil)
return direction == .column ? width! : height!
}
set {
let value = max(0, newValue)
if direction == .column {
width = applyWidthMinMax(value)
} else {
height = applyHeightMinMax(value)
}
}
}
let mainAxisStartMargin: CGFloat
let mainAxisEndMargin: CGFloat
var basis: CGFloat {
return mainAxisLength ?? 1
}
var measureType: MeasureType? {
get {
return stackItem.measureType
}
set {
stackItem.measureType = newValue
}
}
private var direction: SDirection {
return container.direction
}
init(_ stackItem: StackItemImpl, container: Container, mode: LayoutMode) {
self.stackItem = stackItem
self.view = stackItem.view
self.container = container
minWidth = stackItem.minWidth?.resolveWidth(container: container)
maxWidth = stackItem.maxWidth?.resolveWidth(container: container)
minHeight = stackItem.minHeight?.resolveHeight(container: container)
maxHeight = stackItem.maxHeight?.resolveHeight(container: container)
mainAxisStartMargin = stackItem.mainAxisStartMargin(container: container)
mainAxisEndMargin = stackItem.mainAxisEndMargin(container: container)
if mode == .measuring {
measureType = nil
}
resetToStackItemProperties()
}
func isWidthDefined() -> Bool {
return stackItem.width != nil || minWidth != nil
}
func isHeightDefined() -> Bool {
return stackItem.height != nil || minHeight != nil
}
func isCrossAxisFlexible() -> Bool {
if direction == .column {
return stackItem.width == nil
} else {
return stackItem.height == nil
}
}
func limitCrossAxisToContainer() -> Bool {
return isCrossAxisFlexible() && measureType != .aspectRatio
}
func resetToStackItemProperties() {
self.width = stackItem.width?.resolveWidth(container: container)
if let minWidth = minWidth, let width = width, width < minWidth {
self.width = minWidth
}
self.height = stackItem.height?.resolveHeight(container: container)
if let minHeight = minHeight, let height = height, height < minHeight {
self.height = minHeight
}
}
func measureItem(initialMeasure: Bool) {
let stretchedCrossAxisLength = resolveStretchedCrossAxisLength()
var isMeasured = false
if initialMeasure && stackItem._aspectRatio != nil {
// AspectRatio has a higher priority, apply it first if the width or the height is defined
if stackItem.width != nil {
applyAspectRatioIfNeeded(.adjustHeight)
measureType = .aspectRatio
isMeasured = true
} else if stackItem.height != nil {
applyAspectRatioIfNeeded(.adjustWidth)
measureType = .aspectRatio
isMeasured = true
} else if let stretchedCrossAxisLength = stretchedCrossAxisLength {
crossAxisLength = stretchedCrossAxisLength
applyAspectRatioIfNeeded(.adjustMainAxis)
measureType = .aspectRatio
isMeasured = true
}
}
if !isMeasured && (width == nil || height == nil) {
var applyMargins = initialMeasure
var fitWidth: CGFloat?
var fitHeight: CGFloat?
if let itemWidth = width {
fitWidth = itemWidth
applyMargins = false
} else if let itemHeight = height {
fitHeight = itemHeight
applyMargins = false
} else if let containerInnerWidth = container.innerWidth, measureType == nil || measureType == .sizeThatFitsWidth {
fitWidth = containerInnerWidth
measureType = .sizeThatFitsWidth
} else if let containerHeight = container.innerHeight, measureType == nil || measureType == .sizeThatFitsHeight {
fitHeight = containerHeight
measureType = .sizeThatFitsHeight
}
// Measure the view using sizeThatFits(:CGSize)
if let fitWidth = fitWidth, height == nil {
if fitWidth > 0 {
var adjustedFitWidth = applyMargins ? stackItem.applyMargins(toWidth: fitWidth) : fitWidth
adjustedFitWidth = adjustWidthToAspectRatioAndContainer(width: adjustedFitWidth)
adjustedFitWidth = applyWidthMin(adjustedFitWidth)
let newSize = view.sizeThatFits(CGSize(width: adjustedFitWidth, height: .greatestFiniteMagnitude))
height = minValueOptional(newSize.height, container.innerHeight)
if width == nil {
width = min(newSize.width, adjustedFitWidth)
}
} else if height == nil {
height = 0
}
} else if let fitHeight = fitHeight, width == nil {
if fitHeight > 0 {
var adjustedFitHeight = applyMargins ? stackItem.applyMargins(toHeight: fitHeight) : fitHeight
adjustedFitHeight = adjustHeightToAspectRatioAndContainer(height: adjustedFitHeight)
adjustedFitHeight = applyHeightMin(adjustedFitHeight)
let newSize = view.sizeThatFits(CGSize(width: .greatestFiniteMagnitude, height: adjustedFitHeight))
width = minValueOptional(newSize.width, container.innerWidth)
if height == nil {
height = min(newSize.height, adjustedFitHeight)
}
} else if width == nil {
width = 0
}
}
assert(height != nil && width != nil, "should not occurred")
applySizeMinMax()
applyAspectRatioIfNeeded(.adjustCrossAxis)
if let stretchedCrossAxisLength = stretchedCrossAxisLength {
if stretchedCrossAxisLength > self.crossAxisLength {
self.crossAxisLength = stretchedCrossAxisLength
}
}
}
applySizeMinMax()
}
private func resolveStretchedCrossAxisLength() -> CGFloat? {
if stackItem.resolveStackItemAlign(stackAlignItems: container.alignItems) == .stretch, let containerCrossAxisInnerLength = container.crossAxisInnerLength {
if isCrossAxisFlexible() {
var crossAxisLength = stackItem.applyMargins(toCrossAxisLength: containerCrossAxisInnerLength, container: container)
crossAxisLength = applyMinMax(toCrossAxisLength: crossAxisLength)
return crossAxisLength
}
}
return nil
}
private func measureAterGrowShrink() {
if stackItem._aspectRatio != nil {
applyAspectRatioIfNeeded(.adjustCrossAxis)
applySizeMinMax()
} else {
measureItem(initialMeasure: false)
}
}
enum AdjustType {
case adjustMainAxis
case adjustCrossAxis
case adjustWidth
case adjustHeight
}
private func applyAspectRatioIfNeeded(_ adjustType: AdjustType) {
if let aspectRatio = stackItem._aspectRatio {
let adjustWidth: Bool
switch adjustType {
case .adjustCrossAxis:
adjustWidth = direction == .column
case .adjustMainAxis:
adjustWidth = direction == .row
case .adjustWidth:
adjustWidth = true
case .adjustHeight:
adjustWidth = false
}
applyAspectRatio(aspectRatio, adjustWidth: adjustWidth)
// Check if the new width/height respect maxWidth/maxHeight and the containe size,
// if not reapply the aspect ratio
if let width = width, adjustWidth {
if let maxWidth = maxWidth, width > maxWidth {
self.width = applyWidthMinMax(width)
applyAspectRatioIfNeeded(.adjustHeight)
} else if let availableWidth = container.innerWidth, direction == .column && width > availableWidth {
// direction is column, so the width must not be greater than the container's width
self.width = availableWidth
applyAspectRatioIfNeeded(.adjustHeight)
}
} else if let height = height, !adjustWidth {
if let maxHeight = maxHeight, height > maxHeight {
self.height = applyHeightMinMax(height)
applyAspectRatioIfNeeded(.adjustWidth)
} else if let availableHeight = container.innerHeight, direction == .row && height > availableHeight {
// direction is row, so the width must not be greater than the container's height
self.height = availableHeight
applyAspectRatioIfNeeded(.adjustWidth)
}
}
}
}
private func hasReachedMaxAspectRatio() -> Bool {
guard stackItem._aspectRatio != nil else { return false }
if direction == .column {
if var availableWidth = container.innerWidth {
availableWidth = stackItem.applyMargins(toWidth: availableWidth)
if let width = width, width >= availableWidth {
return true
}
}
} else {
if var availableHeight = container.innerHeight {
availableHeight = stackItem.applyMargins(toHeight: availableHeight)
if let height = height, height >= availableHeight {
return true
}
}
}
return false
}
private func applyAspectRatio(_ aspectRatio: CGFloat, adjustWidth: Bool) {
if adjustWidth {
width = (height! * aspectRatio)
} else {
height = (width! / aspectRatio)
}
}
private func adjustWidthToAspectRatioAndContainer(width: CGFloat) -> CGFloat {
guard direction == .row else { return width }
guard let aspectRatio = stackItem._aspectRatio else { return width }
guard let availableHeight = container.innerHeight else { return width }
// aspectRatio and container's height are set => limit the specified width to the maximum width
// respecting the aspectRatio.
let maxWidth = availableHeight * aspectRatio
if width > maxWidth {
return maxWidth
} else {
return width
}
}
private func adjustHeightToAspectRatioAndContainer(height: CGFloat) -> CGFloat {
guard direction == .column else { return height }
guard let aspectRatio = stackItem._aspectRatio else { return height }
guard let availableWidth = container.innerWidth else { return height }
// aspectRatio and container's width are set => limit the specified height to the maximum height
// respecting the aspectRatio.
let maxHeight = availableWidth / aspectRatio
if height > maxHeight {
return maxHeight
} else {
return height
}
}
func growFactor() -> CGFloat {
guard let mainAxisLength = mainAxisLength, mainAxisLength != 0 else { return 0 }
if let mainAxisMaxLength = mainAxisMaxLength, mainAxisLength >= mainAxisMaxLength {
return 0
} else if hasReachedMaxAspectRatio() {
return 0
} else if let growFactor = stackItem.grow {
return growFactor
} else {
return 0
}
}
func grow(mainAxisLength: CGFloat) {
resetToStackItemProperties()
self.mainAxisLength = mainAxisLength
measureAterGrowShrink()
}
func shrinkFactor() -> CGFloat {
guard let mainAxisLength = mainAxisLength, mainAxisLength != 0 else { return 0 }
if let mainAxisMinLength = mainAxisMinLength, mainAxisLength <= mainAxisMinLength {
return 0
} else if let shrink = stackItem.shrink {
return shrink * basis
} else {
return 0
}
}
func shrink(mainAxisLength: CGFloat) {
resetToStackItemProperties()
self.mainAxisLength = mainAxisLength
measureAterGrowShrink()
}
private func applySizeMinMax() {
width = applyWidthMinMax(width)
height = applyHeightMinMax(height)
}
private func applySizeMin() {
width = applyWidthMin(width)
height = applyHeightMin(height)
}
func applyMinMax(toMainAxisLength mainAxisLength: CGFloat) -> CGFloat {
if direction == .column {
return applyHeightMinMax(mainAxisLength)!
} else {
return applyWidthMinMax(mainAxisLength)!
}
}
func applyMinMax(toCrossAxisLength crossAxisLength: CGFloat) -> CGFloat {
if direction == .column {
return applyWidthMinMax(crossAxisLength)!
} else {
return applyHeightMinMax(crossAxisLength)!
}
}
func applyWidthMinMax(_ width: CGFloat?) -> CGFloat? {
let result = applyWidthMin(width)
return applyWidthMax(result)
}
func applyWidthMin(_ width: CGFloat?) -> CGFloat? {
guard let width = width else { return minWidth }
return applyWidthMin(width)
}
func applyWidthMin(_ width: CGFloat) -> CGFloat {
if let minWidth = minWidth, width < minWidth {
return minWidth
} else {
return width
}
}
func applyWidthMax(_ width: CGFloat?) -> CGFloat? {
if let maxWidth = maxWidth, maxWidth < (width ?? CGFloat.greatestFiniteMagnitude) {
return maxWidth
} else {
return width
}
}
func applyHeightMinMax(_ height: CGFloat?) -> CGFloat? {
let result = applyHeightMin(height)
return applyHeightMax(result)
}
func applyHeightMin(_ height: CGFloat?) -> CGFloat? {
guard let height = height else { return minHeight }
return applyHeightMin(height)
}
func applyHeightMin(_ height: CGFloat) -> CGFloat {
if let minHeight = minHeight, height < minHeight {
return minHeight
} else {
return height
}
}
func applyHeightMax(_ height: CGFloat?) -> CGFloat? {
if let maxHeight = maxHeight, maxHeight < (height ?? CGFloat.greatestFiniteMagnitude) {
return maxHeight
} else {
return height
}
}
}
#endif