Home telegram - peerInfoPaneTabsContainerNode
Post
Cancel

telegram - peerInfoPaneTabsContainerNode

PeerInfoPaneTabsContainerNode

enum PeerInfoPaneKey

1
2
3
4
5
6
7
8
case media
case files
case links
case voice
case music
case gifs
case groupsInCommon
case members

struct PeerInfoPaneSpecifier

1
2
var key: PeerInfoPaneKey
var title: String

class PeerInfoPaneTabsContainerPaneNode

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
private let pressed: () -> Void

private let titleNode: ImmediateTextNode
private let buttonNode: HighlightTrackingButtonNode

private var isSelected: Bool = false

@objc private func buttonPressed() {
    self.pressed()
}

func updateText(_ title: String, isSelected: Bool, presentationData: PresentationData) {
    self.isSelected = isSelected
    self.titleNode.attributedText = NSAttributedString(string: title, font: Font.medium(14.0), textColor: isSelected ? presentationData.theme.list.itemAccentColor : presentationData.theme.list.itemSecondaryTextColor)
    
    self.buttonNode.accessibilityLabel = title
    self.buttonNode.accessibilityTraits = [.button]
    if isSelected {
        self.buttonNode.accessibilityTraits.insert(.selected)
    }
}

func updateLayout(height: CGFloat) -> CGFloat {
    let titleSize = self.titleNode.updateLayout(CGSize(width: 200.0, height: .greatestFiniteMagnitude))
    self.titleNode.frame = CGRect(origin: CGPoint(x: 0.0, y: floor((height - titleSize.height) / 2.0)), size: titleSize)
    return titleSize.width
}

func updateArea(size: CGSize, sideInset: CGFloat) {
    self.buttonNode.frame = CGRect(origin: CGPoint(x: -sideInset, y: 0.0), size: CGSize(width: size.width + sideInset * 2.0, height: size.height))
}

class PeerInfoPaneTabsContainerNode

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
private let scrollNode: ASScrollNode
private var paneNodes: [PeerInfoPaneKey: PeerInfoPaneTabsContainerPaneNode] = [:]
private let selectedLineNode: ASImageNode

private var currentParams: ([PeerInfoPaneSpecifier], PeerInfoPaneKey?, PresentationData)?

var requestSelectPane: ((PeerInfoPaneKey) -> Void)?

override init() {
    self.scrollNode = ASScrollNode()
    
    self.selectedLineNode = ASImageNode()
    self.selectedLineNode.displaysAsynchronously = false
    self.selectedLineNode.displayWithoutProcessing = true
    
    super.init()
    
    self.scrollNode.view.disablesInteractiveTransitionGestureRecognizerNow = { [weak self] in
        guard let strongSelf = self else {
            return false
        }
        return strongSelf.scrollNode.view.contentOffset.x > .ulpOfOne
    }
    self.scrollNode.view.showsHorizontalScrollIndicator = false
    self.scrollNode.view.scrollsToTop = false
    if #available(iOS 11.0, *) {
        self.scrollNode.view.contentInsetAdjustmentBehavior = .never
    }
    
    self.addSubnode(self.scrollNode)
    self.scrollNode.addSubnode(self.selectedLineNode)
}

func update(size: CGSize, presentationData: PresentationData, paneList: [PeerInfoPaneSpecifier], selectedPane: PeerInfoPaneKey?, transitionFraction: CGFloat, transition: ContainedViewLayoutTransition) {
    transition.updateFrame(node: self.scrollNode, frame: CGRect(origin: CGPoint(), size: size))

    let focusOnSelectedPane = self.currentParams?.1 != selectedPane

    if self.currentParams?.2.theme !== presentationData.theme {
      // generate selectedLine image
        self.selectedLineNode.image = generateImage(CGSize(width: 7.0, height: 4.0), rotatedContext: { size, context in
            context.clear(CGRect(origin: CGPoint(), size: size))
            context.setFillColor(presentationData.theme.list.itemAccentColor.cgColor)
            context.fillEllipse(in: CGRect(origin: CGPoint(), size: CGSize(width: size.width, height: size.width)))
        })?.stretchableImage(withLeftCapWidth: 4, topCapHeight: 1)
    }

    if self.currentParams?.0 != paneList || self.currentParams?.1 != selectedPane || self.currentParams?.2 !== presentationData {
        self.currentParams = (paneList, selectedPane, presentationData)
      // update tabs
        for specifier in paneList {
            let paneNode: PeerInfoPaneTabsContainerPaneNode
            if let current = self.paneNodes[specifier.key] {
                paneNode = current
            } else {
                paneNode = PeerInfoPaneTabsContainerPaneNode(pressed: { [weak self] in
                    self?.paneSelected(specifier.key)
                })
                self.paneNodes[specifier.key] = paneNode
            }
            paneNode.updateText(specifier.title, isSelected: selectedPane == specifier.key, presentationData: presentationData)
        }
      // remove tabs
        var removeKeys: [PeerInfoPaneKey] = []
        for (key, _) in self.paneNodes {
            if !paneList.contains(where: { $0.key == key }) {
                removeKeys.append(key)
            }
        }
        for key in removeKeys {
            if let paneNode = self.paneNodes.removeValue(forKey: key) {
                paneNode.removeFromSupernode()
            }
        }
    }

  // collecting layout info
    var tabSizes: [(CGSize, PeerInfoPaneTabsContainerPaneNode, Bool)] = []
    var totalRawTabSize: CGFloat = 0.0
    var selectionFrames: [CGRect] = []

    for specifier in paneList {
        guard let paneNode = self.paneNodes[specifier.key] else {
            continue
        }
        let wasAdded = paneNode.supernode == nil
        if wasAdded {
            self.scrollNode.addSubnode(paneNode)
        }
        let paneNodeWidth = paneNode.updateLayout(height: size.height)
        let paneNodeSize = CGSize(width: paneNodeWidth, height: size.height)
        tabSizes.append((paneNodeSize, paneNode, wasAdded))
        totalRawTabSize += paneNodeSize.width
    }

    let minSpacing: CGFloat = 26.0
  // layout
    if tabSizes.count <= 1 {
        for i in 0 ..< tabSizes.count {
            let (paneNodeSize, paneNode, wasAdded) = tabSizes[i]
            let leftOffset: CGFloat = 16.0

            let paneFrame = CGRect(origin: CGPoint(x: leftOffset, y: floor((size.height - paneNodeSize.height) / 2.0)), size: paneNodeSize)
            if wasAdded {// animate alpha in
                paneNode.frame = paneFrame
                paneNode.alpha = 0.0
                transition.updateAlpha(node: paneNode, alpha: 1.0)
            } else {// animate frame
                transition.updateFrameAdditiveToCenter(node: paneNode, frame: paneFrame)
            }
          // update interactive area
            let areaSideInset: CGFloat = 16.0
            paneNode.updateArea(size: paneFrame.size, sideInset: areaSideInset)
            paneNode.hitTestSlop = UIEdgeInsets(top: 0.0, left: -areaSideInset, bottom: 0.0, right: -areaSideInset)

            selectionFrames.append(paneFrame)
        }
        self.scrollNode.view.contentSize = CGSize(width: size.width, height: size.height)
    } else if totalRawTabSize + CGFloat(tabSizes.count + 1) * minSpacing <= size.width {
      // calcuate tab spacing
        let availableSpace = size.width
        let availableSpacing = availableSpace - totalRawTabSize
        let perTabSpacing = floor(availableSpacing / CGFloat(tabSizes.count + 1))

        let normalizedPerTabWidth = floor(availableSpace / CGFloat(tabSizes.count))
        var maxSpacing: CGFloat = 0.0
        var minSpacing: CGFloat = .greatestFiniteMagnitude
        for i in 0 ..< tabSizes.count - 1 {
            let distanceToNextBoundary = (normalizedPerTabWidth - tabSizes[i].0.width) / 2.0
            let nextDistanceToBoundary = (normalizedPerTabWidth - tabSizes[i + 1].0.width) / 2.0
            let distance = nextDistanceToBoundary + distanceToNextBoundary
            maxSpacing = max(distance, maxSpacing)
            minSpacing = min(distance, minSpacing)
        }

        if minSpacing >= 100.0 || (maxSpacing / minSpacing) < 0.2 {
            for i in 0 ..< tabSizes.count {
                let (paneNodeSize, paneNode, wasAdded) = tabSizes[i]

                let paneFrame = CGRect(origin: CGPoint(x: CGFloat(i) * normalizedPerTabWidth + floor((normalizedPerTabWidth - paneNodeSize.width) / 2.0), y: floor((size.height - paneNodeSize.height) / 2.0)), size: paneNodeSize)
                if wasAdded {
                    paneNode.frame = paneFrame
                    paneNode.alpha = 0.0
                    transition.updateAlpha(node: paneNode, alpha: 1.0)
                } else {
                    transition.updateFrameAdditiveToCenter(node: paneNode, frame: paneFrame)
                }
                let areaSideInset = floor((normalizedPerTabWidth - paneNodeSize.width) / 2.0)
                paneNode.updateArea(size: paneFrame.size, sideInset: areaSideInset)
                paneNode.hitTestSlop = UIEdgeInsets(top: 0.0, left: -areaSideInset, bottom: 0.0, right: -areaSideInset)

                selectionFrames.append(paneFrame)
            }
        } else {
            var leftOffset = perTabSpacing
            for i in 0 ..< tabSizes.count {
                let (paneNodeSize, paneNode, wasAdded) = tabSizes[i]

                let paneFrame = CGRect(origin: CGPoint(x: leftOffset, y: floor((size.height - paneNodeSize.height) / 2.0)), size: paneNodeSize)
                if wasAdded {
                    paneNode.frame = paneFrame
                    paneNode.alpha = 0.0
                    transition.updateAlpha(node: paneNode, alpha: 1.0)
                } else {
                    transition.updateFrameAdditiveToCenter(node: paneNode, frame: paneFrame)
                }
                let areaSideInset = floor(perTabSpacing / 2.0)
                paneNode.updateArea(size: paneFrame.size, sideInset: areaSideInset)
                paneNode.hitTestSlop = UIEdgeInsets(top: 0.0, left: -areaSideInset, bottom: 0.0, right: -areaSideInset)

                leftOffset += paneNodeSize.width + perTabSpacing

                selectionFrames.append(paneFrame)
            }
        }
        self.scrollNode.view.contentSize = CGSize(width: size.width, height: size.height)
    } else {
        let sideInset: CGFloat = 16.0
        var leftOffset: CGFloat = sideInset
        for i in 0 ..< tabSizes.count {
            let (paneNodeSize, paneNode, wasAdded) = tabSizes[i]
            let paneFrame = CGRect(origin: CGPoint(x: leftOffset, y: floor((size.height - paneNodeSize.height) / 2.0)), size: paneNodeSize)
            if wasAdded {
                paneNode.frame = paneFrame
                paneNode.alpha = 0.0
                transition.updateAlpha(node: paneNode, alpha: 1.0)
            } else {
                transition.updateFrameAdditiveToCenter(node: paneNode, frame: paneFrame)
            }
            paneNode.updateArea(size: paneFrame.size, sideInset: minSpacing)
            paneNode.hitTestSlop = UIEdgeInsets(top: 0.0, left: -minSpacing, bottom: 0.0, right: -minSpacing)

            selectionFrames.append(paneFrame)

            leftOffset += paneNodeSize.width + minSpacing
        }
        self.scrollNode.view.contentSize = CGSize(width: leftOffset - minSpacing + sideInset, height: size.height)
    }

    var selectedFrame: CGRect?
    if let selectedPane = selectedPane, let currentIndex = paneList.firstIndex(where: { $0.key == selectedPane }) {
        if currentIndex != 0 && transitionFraction > 0.0 {
            let currentFrame = selectionFrames[currentIndex]
            let previousFrame = selectionFrames[currentIndex - 1]
            selectedFrame = interpolateFrame(from: currentFrame, to: previousFrame, t: abs(transitionFraction))
        } else if currentIndex != paneList.count - 1 && transitionFraction < 0.0 {
            let currentFrame = selectionFrames[currentIndex]
            let previousFrame = selectionFrames[currentIndex + 1]
            selectedFrame = interpolateFrame(from: currentFrame, to: previousFrame, t: abs(transitionFraction))
        } else {
            selectedFrame = selectionFrames[currentIndex]
        }
    }

    if let selectedFrame = selectedFrame {
        let wasAdded = self.selectedLineNode.isHidden
        self.selectedLineNode.isHidden = false
        let lineFrame = CGRect(origin: CGPoint(x: selectedFrame.minX, y: size.height - 4.0), size: CGSize(width: selectedFrame.width, height: 4.0))
        if wasAdded {
            self.selectedLineNode.frame = lineFrame
            self.selectedLineNode.alpha = 0.0
            transition.updateAlpha(node: self.selectedLineNode, alpha: 1.0)
        } else {
            transition.updateFrame(node: self.selectedLineNode, frame: lineFrame)
        }
        if focusOnSelectedPane {
            if selectedPane == paneList.first?.key {
                transition.updateBounds(node: self.scrollNode, bounds: CGRect(origin: CGPoint(), size: self.scrollNode.bounds.size))
            } else if selectedPane == paneList.last?.key {
                transition.updateBounds(node: self.scrollNode, bounds: CGRect(origin: CGPoint(x: max(0.0, self.scrollNode.view.contentSize.width - self.scrollNode.bounds.width), y: 0.0), size: self.scrollNode.bounds.size))
            } else {
                let contentOffsetX = max(0.0, min(self.scrollNode.view.contentSize.width - self.scrollNode.bounds.width, floor(selectedFrame.midX - self.scrollNode.bounds.width / 2.0)))
                transition.updateBounds(node: self.scrollNode, bounds: CGRect(origin: CGPoint(x: contentOffsetX, y: 0.0), size: self.scrollNode.bounds.size))
            }
        }
    } else {
        self.selectedLineNode.isHidden = true
    }
}
This post is licensed under CC BY 4.0 by the author.