-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathInsetLayout.swift
More file actions
65 lines (55 loc) · 1.77 KB
/
InsetLayout.swift
File metadata and controls
65 lines (55 loc) · 1.77 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
//
// InsetLayout.swift
// CollectionKit
//
// Created by Luke Zhao on 2017-09-08.
// Copyright © 2017 lkzhao. All rights reserved.
//
import UIKit
open class InsetLayout: WrapperLayout {
public var insets: UIEdgeInsets
public var insetProvider: ((CGSize) -> UIEdgeInsets)?
struct InsetLayoutContext: LayoutContext {
var original: LayoutContext
var insets: UIEdgeInsets
var collectionSize: CGSize {
return original.collectionSize.insets(by: insets)
}
var numberOfItems: Int {
return original.numberOfItems
}
func data(at: Int) -> Any {
return original.data(at: at)
}
func identifier(at: Int) -> String {
return original.identifier(at: at)
}
func size(at: Int, collectionSize: CGSize) -> CGSize {
return original.size(at: at, collectionSize: collectionSize)
}
}
public init(_ rootLayout: Layout, insets: UIEdgeInsets = .zero) {
self.insets = insets
super.init(rootLayout)
}
public init(_ rootLayout: Layout, insetProvider: @escaping ((CGSize) -> UIEdgeInsets)) {
self.insets = .zero
self.insetProvider = insetProvider
super.init(rootLayout)
}
open override var contentSize: CGSize {
return rootLayout.contentSize.insets(by: -insets)
}
open override func layout(context: LayoutContext) {
if let insetProvider = insetProvider {
insets = insetProvider(context.collectionSize)
}
rootLayout.layout(context: InsetLayoutContext(original: context, insets: insets))
}
open override func visible(in visibleFrame: CGRect) -> (indexes: [Int], frame: CGRect) {
return rootLayout.visible(in: visibleFrame.inset(by: -insets))
}
open override func frame(at: Int) -> CGRect {
return rootLayout.frame(at: at) + CGPoint(x: insets.left, y: insets.top)
}
}