class
StatusBar
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
private var _statusBarStyle: StatusBarStyle = .Black
public var statusBarStyle: StatusBarStyle {
get {
return self._statusBarStyle
} set(value) {
if self._statusBarStyle != value {
self._statusBarStyle = value
self.alphaUpdated?(.immediate)
}
}
}
public func updateStatusBarStyle(_ statusBarStyle: StatusBarStyle, animated: Bool) {
if self._statusBarStyle != statusBarStyle {
self._statusBarStyle = statusBarStyle
self.alphaUpdated?(animated ? .animated(duration: 0.3, curve: .easeInOut) : .immediate)
}
}
var alphaUpdated: ((ContainedViewLayoutTransition) -> Void)?
let offsetNode = ASDisplayNode()
private let inCallBackgroundNode = ASDisplayNode()
private let inCallLabel: StatusBarLabelNode
private var inCallText: String? = nil
public var verticalOffset: CGFloat = 0.0 {
didSet {
if !self.verticalOffset.isEqual(to: oldValue) {
self.offsetNode.frame = CGRect(origin: CGPoint(x: 0.0, y: -self.verticalOffset), size: CGSize())
/*---> Ferris: commented by Ferris
self.layer.invalidateUpTheTree()
<--- Ferris */
}
}
}
public override init() {
self.inCallLabel = StatusBarLabelNode()
self.inCallLabel.isUserInteractionEnabled = false
self.offsetNode.isUserInteractionEnabled = false
let labelSize = self.inCallLabel.measure(CGSize(width: 300.0, height: 300.0))
self.inCallLabel.frame = CGRect(origin: CGPoint(x: 10.0, y: 20.0 + 4.0), size: labelSize)
super.init()
self.setViewBlock({
return StatusBarView()
})
(self.view as! StatusBarView).node = self
self.addSubnode(self.offsetNode)
self.addSubnode(self.inCallBackgroundNode)
// self.layer.setTraceableInfo(CATracingLayerInfo(shouldBeAdjustedToInverseTransform: true, userData: self, tracingTag: WindowTracingTags.statusBar, disableChildrenTracingTags: 0)) // commented by Ferris
self.clipsToBounds = true
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.tapGesture(_:))))
}
func updateState(statusBar: UIView?, withSafeInsets: Bool, inCallText: String?, animated: Bool) {
if let statusBar = statusBar {
self.removeProxyNodeScheduled = false
let resolvedStyle: StatusBarStyle
if inCallText != nil && !self.ignoreInCall {// inCall
resolvedStyle = .White
} else {
resolvedStyle = self.statusBarStyle
}
// proxyNode
if let proxyNode = self.proxyNode {
proxyNode.statusBarStyle = resolvedStyle
} else {
self.proxyNode = StatusBarProxyNode(statusBarStyle: resolvedStyle, statusBar: statusBar)
self.proxyNode!.isHidden = false
self.addSubnode(self.proxyNode!)
}
} else {
self.removeProxyNodeScheduled = true
DispatchQueue.main.async(execute: { [weak self] in
if let strongSelf = self {
if strongSelf.removeProxyNodeScheduled {
strongSelf.removeProxyNodeScheduled = false
strongSelf.proxyNode?.isHidden = true
strongSelf.proxyNode?.removeFromSupernode()
strongSelf.proxyNode = nil
}
}
})
}
// 处理inCall state
var ignoreInCall = self.ignoreInCall
switch self.statusBarStyle {
case .Black, .White:
break
default:
ignoreInCall = true
}
var resolvedInCallText: String? = inCallText
if ignoreInCall {
resolvedInCallText = nil
}
if (resolvedInCallText != nil) != (self.inCallText != nil) {
if let _ = resolvedInCallText {
if !withSafeInsets {
self.addSubnode(self.inCallLabel)
}
addInCallAnimation(self.inCallLabel.layer)
self.inCallBackgroundNode.layer.backgroundColor = inCallBackgroundColor.cgColor
if animated {
self.inCallBackgroundNode.layer.animate(from: UIColor.clear.cgColor, to: inCallBackgroundColor.cgColor, keyPath: "backgroundColor", timingFunction: CAMediaTimingFunctionName.easeInEaseOut.rawValue, duration: 0.3)
}
} else {
self.inCallLabel.removeFromSupernode()
self.inCallBackgroundNode.layer.backgroundColor = UIColor.clear.cgColor
if animated {
self.inCallBackgroundNode.layer.animate(from: inCallBackgroundColor.cgColor, to: UIColor.clear.cgColor, keyPath: "backgroundColor", timingFunction: CAMediaTimingFunctionName.easeInEaseOut.rawValue, duration: 0.3)
}
}
}
if let resolvedInCallText = resolvedInCallText {
if self.inCallText != resolvedInCallText {
self.inCallLabel.attributedText = NSAttributedString(string: resolvedInCallText, font: Font.regular(14.0), textColor: .white)
}
self.layoutInCallLabel()
}
self.inCallText = resolvedInCallText
}
class
StatusBarProxyNode
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
private let statusBar: UIView
var timer: Timer?
var statusBarStyle: StatusBarStyle {
didSet {
if oldValue != self.statusBarStyle {
if !self.isHidden {
self.updateItems()
}
}
}
}
private var itemNodes: [StatusBarItemNode] = []
override var isHidden: Bool {
get {
return super.isHidden
} set(value) {
if super.isHidden != value {
super.isHidden = value
if !value {// 如果not hidden, 每隔5秒去检查更新状态
self.updateItems()
// 这里的做法也比较新颖: StatusBarProxyNodeTimerTarget
self.timer = Timer(timeInterval: 5.0, target: StatusBarProxyNodeTimerTarget { [weak self] in
self?.updateItems()
}, selector: #selector(StatusBarProxyNodeTimerTarget.tick), userInfo: nil, repeats: true)
RunLoop.main.add(self.timer!, forMode: .common)
} else {
self.timer?.invalidate()
self.timer = nil
}
}
}
}
init(statusBarStyle: StatusBarStyle, statusBar: UIView) {
self.statusBarStyle = statusBarStyle
self.statusBar = statusBar
super.init()
self.isHidden = true
self.clipsToBounds = true
//self.backgroundColor = UIColor.blueColor().colorWithAlphaComponent(0.2)
//dumpViews(statusBar)
forEachSubview(statusBar: statusBar, { rootView, subview in
let itemNode = StatusBarItemNode(statusBarStyle: statusBarStyle, targetView: subview, rootView: rootView) // 将`statusBar`的每一个view包装成`StatusBarItemNode`
self.itemNodes.append(itemNode)
self.addSubnode(itemNode)
return true
})
self.frame = statusBar.bounds
}
private func updateItems() {
let statusBar = self.statusBar
// 遍历当前itemNodes, 移除需要移除的,更新statusBarStyle
var i = 0
while i < self.itemNodes.count {
var found = false
forEachSubview(statusBar: statusBar, { rootView, subview in
if self.itemNodes[i].rootView === rootView && self.itemNodes[i].targetView === subview {
found = true
return false
} else {
return true
}
})
if !found {
self.itemNodes[i].removeFromSupernode()
self.itemNodes.remove(at: i)
} else {
self.itemNodes[i].statusBarStyle = self.statusBarStyle
self.itemNodes[i].update()
i += 1
}
}
forEachSubview(statusBar: statusBar, { rootView, subview in
var found = false
for itemNode in self.itemNodes {
if itemNode.targetView == subview {
found = true
break
}
}
if !found {
let itemNode = StatusBarItemNode(statusBarStyle: self.statusBarStyle, targetView: subview, rootView: rootView)
itemNode.update()
self.itemNodes.append(itemNode)
self.addSubnode(itemNode)
}
return true
})
}
Hacking
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
private let foregroundClass: AnyClass? = {
var nameString = "StatusBar"
if CFAbsoluteTimeGetCurrent() > 0 {
nameString += "ForegroundView"
}
return NSClassFromString("_UI" + nameString)
}()
private let foregroundClass2: AnyClass? = {
var nameString = "StatusBar"
if CFAbsoluteTimeGetCurrent() > 0 {
nameString += "ForegroundView"
}
return NSClassFromString("UI" + nameString)
}()
private let batteryItemClass: AnyClass? = {
var nameString = "StatusBarBattery"
if CFAbsoluteTimeGetCurrent() > 0 {
nameString += "ItemView"
}
return NSClassFromString("UI" + nameString)
}()
private let batteryViewClass: AnyClass? = {
var nameString = "Battery"
if CFAbsoluteTimeGetCurrent() > 0 {
nameString += "View"
}
return NSClassFromString("_UI" + nameString)
}()
private let activityClass: AnyClass? = {
var nameString = "StatusBarBackground"
if CFAbsoluteTimeGetCurrent() > 0 {
nameString += "ActivityView"
}
return NSClassFromString("_UI" + nameString)
}()
private let stringClass: AnyClass? = {
var nameString = "StatusBar"
if CFAbsoluteTimeGetCurrent() > 0 {
nameString += "StringView"
}
return NSClassFromString("_UI" + nameString)
}()
private let timeViewClass: AnyClass? = {
var nameString = "StatusBar"
if CFAbsoluteTimeGetCurrent() > 0 {
nameString += "TimeItemView"
}
return NSClassFromString("UI" + nameString)
}()
private func forEachSubview(statusBar: UIView, _ f: (UIView, UIView) -> Bool) {
var rootView: UIView = statusBar
for subview in statusBar.subviews {
if let foregroundClass = foregroundClass, subview.checkIsKind(of: foregroundClass) {
rootView = subview
break
} else if let foregroundClass2 = foregroundClass2, subview.checkIsKind(of: foregroundClass2) {
rootView = subview
break
}
}
for subview in rootView.subviews {
if true || subview.subviews.isEmpty {
if !f(rootView, subview) {
break
}
} else {
for subSubview in subview.subviews {
if !f(rootView, subSubview) {
break
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
private class StatusBarProxyNodeTimerTarget: NSObject {
let action: () -> Void
init(action: @escaping () -> Void) {
self.action = action
}
@objc func tick() {
action()
}
}
看看这个包装类
class
StatusBarItemNode
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
var statusBarStyle: StatusBarStyle
var targetView: UIView
var rootView: UIView
private let contentNode: ASDisplayNode
init(statusBarStyle: StatusBarStyle, targetView: UIView, rootView: UIView) {
self.statusBarStyle = statusBarStyle
self.targetView = targetView
self.rootView = rootView
self.contentNode = ASDisplayNode()
self.contentNode.isUserInteractionEnabled = false
super.init()
self.addSubnode(self.contentNode)
}
func update() {
let containingBounds = maxSubviewBounds(self.targetView)
let context = DrawingContext(size: containingBounds.size, clear: true)
if let contents = self.targetView.layer.contents, (self.targetView.layer.sublayers?.count ?? 0) == 0 && CFGetTypeID(contents as CFTypeRef) == CGImage.typeID && false {
let image = contents as! CGImage
context.withFlippedContext { c in
c.setAlpha(CGFloat(self.targetView.layer.opacity))
c.draw(image, in: CGRect(origin: CGPoint(), size: context.size))
c.setAlpha(1.0)
}
if let sublayers = self.targetView.layer.sublayers {
for sublayer in sublayers {
let origin = sublayer.frame.origin
if let contents = sublayer.contents , CFGetTypeID(contents as CFTypeRef) == CGImage.typeID {
let image = contents as! CGImage
context.withFlippedContext { c in
c.translateBy(x: origin.x, y: origin.y)
c.draw(image, in: CGRect(origin: CGPoint(), size: context.size))
c.translateBy(x: -origin.x, y: -origin.y)
}
} else {
context.withContext { c in
UIGraphicsPushContext(c)
c.translateBy(x: origin.x, y: origin.y)
sublayer.render(in: c)
c.translateBy(x: -origin.x, y: -origin.y)
UIGraphicsPopContext()
}
}
}
}
} else {
if self.targetView.checkIsKind(of: timeViewClass) {
context.withContext { c in
c.translateBy(x: containingBounds.minX, y: -containingBounds.minY)
UIGraphicsPushContext(c)
let color: UIColor
switch self.statusBarStyle {
case .Black, .Ignore, .Hide:
color = UIColor.black
case .White:
color = UIColor.white
}
formatter?.locale = Locale.current
if let string = formatter?.string(from: Date()) {
let attributedString = NSAttributedString(string: string, attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 12.0), NSAttributedString.Key.foregroundColor: color])
let line = CTLineCreateWithAttributedString(attributedString)
c.translateBy(x: containingBounds.width / 2.0, y: containingBounds.height / 2.0)
c.scaleBy(x: 1.0, y: -1.0)
c.translateBy(x: -containingBounds.width / 2.0, y: -containingBounds.height / 2.0)
c.translateBy(x: 0.0, y: 5.0 + UIScreenPixel)
CTLineDraw(line, c)
}
UIGraphicsPopContext()
}
} else {
context.withContext { c in
c.translateBy(x: containingBounds.minX, y: -containingBounds.minY)
UIGraphicsPushContext(c)
self.targetView.layer.render(in: c)
UIGraphicsPopContext()
}
}
}
//dumpViews(self.targetView)
var type: StatusBarItemType = .Generic
if let batteryItemClass = batteryItemClass {
if self.targetView.checkIsKind(of: batteryItemClass) {
type = .Battery
}
}
if let batteryViewClass = batteryViewClass {
if self.targetView.checkIsKind(of: batteryViewClass) {
type = .Battery
}
}
if case .Generic = type {
var hasActivityBackground = false
var hasText = false
for subview in self.targetView.subviews {
if let stringClass = stringClass, subview.checkIsKind(of: stringClass) {
hasText = true
} else if let activityClass = activityClass, subview.checkIsKind(of: activityClass) {
hasActivityBackground = true
}
}
if hasActivityBackground && hasText {
type = .Activity
}
}
tintStatusBarItem(context, type: type, style: self.statusBarStyle)
let image = context.generateImage()?.cgImage
self.contentNode.contents = image
let mappedFrame = self.targetView.convert(self.targetView.bounds, to: self.rootView)
self.frame = mappedFrame
self.contentNode.frame = containingBounds
}
1
2
3
4
5
6
7
8
9
private func maxSubviewBounds(_ view: UIView) -> CGRect {
var bounds = view.bounds
for subview in view.subviews {
let subviewFrame = subview.frame
let subviewBounds = maxSubviewBounds(subview).offsetBy(dx: subviewFrame.minX, dy: subviewFrame.minY)
bounds = bounds.union(subviewBounds)
}
return bounds
}
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
private func tintStatusBarItem(_ context: DrawingContext, type: StatusBarItemType, style: StatusBarStyle) {
switch type {
case .Battery:
let minY = 0
let minX = 0
let maxY = Int(context.size.height * context.scale)
let maxX = Int(context.size.width * context.scale)
if minY < maxY && minX < maxX {
let basePixel = context.bytes.assumingMemoryBound(to: UInt32.self)
let pixelsPerRow = context.bytesPerRow / 4
let midX = (maxX + minX) / 2
let midY = (maxY + minY) / 2
let baseMidRow = basePixel + pixelsPerRow * midY
var baseX = minX
while baseX < maxX {
let pixel = baseMidRow + baseX
let alpha = pixel.pointee & 0xff000000
if alpha != 0 {
break
}
baseX += 1
}
while baseX < maxX {
let pixel = baseMidRow + baseX
let alpha = pixel.pointee & 0xff000000
if alpha == 0 {
break
}
baseX += 1
}
while baseX < maxX {
let pixel = baseMidRow + baseX
let alpha = pixel.pointee & 0xff000000
if alpha != 0 {
break
}
baseX += 1
}
var targetX = baseX
while targetX < maxX {
let pixel = baseMidRow + targetX
let alpha = pixel.pointee & 0xff000000
if alpha == 0 {
break
}
targetX += 1
}
let batteryColor = (baseMidRow + baseX + 2).pointee
let batteryR = (batteryColor >> 16) & 0xff
let batteryG = (batteryColor >> 8) & 0xff
let batteryB = batteryColor & 0xff
var baseY = minY
while baseY < maxY {
let baseRow = basePixel + pixelsPerRow * baseY
let pixel = baseRow + midX
let alpha = pixel.pointee & 0xff000000
if alpha != 0 {
break
}
baseY += 1
}
var targetY = maxY - 1
while targetY >= baseY {
let baseRow = basePixel + pixelsPerRow * targetY
let pixel = baseRow + midX
let alpha = pixel.pointee & 0xff000000
if alpha != 0 {
break
}
targetY -= 1
}
targetY -= 1
let baseColor: UInt32
switch style {
case .Black, .Ignore, .Hide:
baseColor = 0x000000
case .White:
baseColor = 0xffffff
}
let baseR = (baseColor >> 16) & 0xff
let baseG = (baseColor >> 8) & 0xff
let baseB = baseColor & 0xff
var pixel = context.bytes.assumingMemoryBound(to: UInt32.self)
let end = context.bytes.advanced(by: context.length).assumingMemoryBound(to: UInt32.self)
while pixel != end {
let alpha = (pixel.pointee & 0xff000000) >> 24
let r = (baseR * alpha) / 255
let g = (baseG * alpha) / 255
let b = (baseB * alpha) / 255
pixel.pointee = (alpha << 24) | (r << 16) | (g << 8) | b
pixel += 1
}
let whiteColor: UInt32 = 0xffffffff as UInt32
let blackColor: UInt32 = 0xff000000 as UInt32
if batteryColor != whiteColor && batteryColor != blackColor {
var y = baseY + 2
while y < targetY {
let baseRow = basePixel + pixelsPerRow * y
var x = baseX
while x < targetX {
let pixel = baseRow + x
let alpha = (pixel.pointee >> 24) & 0xff
let r = (batteryR * alpha) / 255
let g = (batteryG * alpha) / 255
let b = (batteryB * alpha) / 255
pixel.pointee = (alpha << 24) | (r << 16) | (g << 8) | b
x += 1
}
y += 1
}
}
}
case .Activity:
break
case .Generic:
var pixel = context.bytes.assumingMemoryBound(to: UInt32.self)
let end = context.bytes.advanced(by: context.length).assumingMemoryBound(to: UInt32.self)
let baseColor: UInt32
switch style {
case .Black, .Ignore, .Hide:
baseColor = 0x000000
case .White:
baseColor = 0xffffff
}
let baseR = (baseColor >> 16) & 0xff
let baseG = (baseColor >> 8) & 0xff
let baseB = baseColor & 0xff
while pixel != end {
let alpha = (pixel.pointee & 0xff000000) >> 24
let r = (baseR * alpha) / 255
let g = (baseG * alpha) / 255
let b = (baseB * alpha) / 255
pixel.pointee = (alpha << 24) | (r << 16) | (g << 8) | b
pixel += 1
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
private func addInCallAnimation(_ layer: CALayer) {
let animation = CAKeyframeAnimation(keyPath: "opacity")
animation.keyTimes = [0.0 as NSNumber, 0.1 as NSNumber, 0.5 as NSNumber, 0.9 as NSNumber, 1.0 as NSNumber]
animation.values = [1.0 as NSNumber, 1.0 as NSNumber, 0.0 as NSNumber, 1.0 as NSNumber, 1.0 as NSNumber]
animation.duration = 1.8
animation.autoreverses = true
animation.repeatCount = Float.infinity
animation.beginTime = 1.0
layer.add(animation, forKey: "blink")
}
private final class StatusBarLabelNode: ASTextNode {
override func willEnterHierarchy() {
super.willEnterHierarchy()
addInCallAnimation(self.layer)
}
override func didExitHierarchy() {
super.didExitHierarchy()
self.layer.removeAnimation(forKey: "blink")
}
}
class
StatusBarView: UITracingLayerView
1
2
3
4
5
6
7
weak var node: StatusBar?
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
if let node = self.node {
return node.hitTest(point, with: event)
}
return nil
}
class
StatusBarSurface
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var statusBars: [StatusBar] = []
func addStatusBar(_ statusBar: StatusBar) {
self.removeStatusBar(statusBar)
self.statusBars.append(statusBar)
}
func insertStatusBar(_ statusBar: StatusBar, atIndex index: Int) {
self.removeStatusBar(statusBar)
self.statusBars.insert(statusBar, at: index)
}
func removeStatusBar(_ statusBar: StatusBar) {
for i in 0 ..< self.statusBars.count {
if self.statusBars[i] === statusBar {
self.statusBars.remove(at: i)
break
}
}
}
struct
MappedStatusBar
1
2
3
let style: StatusBarStyle
let frame: CGRect
let statusBar: StatusBar?
struct
MappedStatusBarSurface
1
2
let statusBars: [MappedStatusBar]
let surface: StatusBarSurface
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
private func mapStatusBar(_ statusBar: StatusBar, forceInCall: Bool) -> MappedStatusBar {
let frame = CGRect(origin: statusBar.view.convert(CGPoint(), to: nil), size: statusBar.frame.size)
let resolvedStyle: StatusBarStyle
switch statusBar.statusBarStyle {
case .Black, .White:
if forceInCall {
resolvedStyle = .White
} else {
resolvedStyle = statusBar.statusBarStyle
}
default:
resolvedStyle = statusBar.statusBarStyle
}
return MappedStatusBar(style: resolvedStyle, frame: frame, statusBar: statusBar)
}
private func mappedSurface(_ surface: StatusBarSurface, forceInCall: Bool) -> MappedStatusBarSurface {
var statusBars: [MappedStatusBar] = []
for statusBar in surface.statusBars {
if statusBar.statusBarStyle != .Ignore {
statusBars.append(mapStatusBar(statusBar, forceInCall: forceInCall))
}
}
return MappedStatusBarSurface(statusBars: statusBars, surface: surface)
}
private func optimizeMappedSurface(statusBarSize: CGSize, surface: MappedStatusBarSurface, forceInCall: Bool) -> MappedStatusBarSurface {
if surface.statusBars.count > 1 {
for i in 1 ..< surface.statusBars.count {
if (!forceInCall && surface.statusBars[i].style != surface.statusBars[i - 1].style) || abs(surface.statusBars[i].frame.origin.y - surface.statusBars[i - 1].frame.origin.y) > CGFloat.ulpOfOne {
return surface
}
if let lhsStatusBar = surface.statusBars[i - 1].statusBar, let rhsStatusBar = surface.statusBars[i].statusBar , !lhsStatusBar.alpha.isEqual(to: rhsStatusBar.alpha) {
return surface
}
}
let size = statusBarSize
return MappedStatusBarSurface(statusBars: [MappedStatusBar(style: forceInCall ? .White : surface.statusBars[0].style, frame: CGRect(origin: CGPoint(x: 0.0, y: surface.statusBars[0].frame.origin.y), size: size), statusBar: nil)], surface: surface.surface)
} else {
return surface
}
}
class
StatusBarManager
1
2
3
4
5
6
7
8
9
10
private var host: StatusBarHost
private let volumeControlStatusBar: VolumeControlStatusBar
private let volumeControlStatusBarNode: VolumeControlStatusBarNode
private var surfaces: [StatusBarSurface] = []
private var validParams: (withSafeInsets: Bool, forceInCallStatusBarText: String?, forceHiddenBySystemWindows:
Bool)?
var inCallNavigate: (() -> Void)?
private var volumeTimer: SwiftSignalKit.Timer?
1
事实上这种方式已经被Telegram废弃了, 目前用到的就是alphaUpdated
in Window1
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
private var _topLevelOverlayControllers: [ContainableController] = []
public var topLevelOverlayControllers: [ContainableController] {
get {
return _topLevelOverlayControllers
}
set(value) {
for controller in self._topLevelOverlayControllers {
//---> Ferris: for statusBarStyle
if let controller = controller as? UChatViewController {
controller.statusBar.alphaUpdated = nil
}
// <--- Ferris
controller.view.removeFromSuperview()
}
self._topLevelOverlayControllers = value
let layout = containedLayoutForWindowLayout(self.windowLayout, deviceMetrics: self.deviceMetrics)
for controller in self._topLevelOverlayControllers {
//---> Ferris:
controller.displayNode.frame = CGRect(origin: CGPoint(), size: self.windowLayout.size)
// <--- Ferris
controller.containerLayoutUpdated(layout, transition: .immediate)
if let coveringView = self.coveringView {
self.hostView.containerView.insertSubview(controller.view, belowSubview: coveringView)
} else {
self.hostView.containerView.insertSubview(controller.view, belowSubview:
self.volumeControlStatusBarNode.view)
}
//---> Ferris: for statusBarStyle
if let controller = controller as? UChatViewController {
controller.statusBar.alphaUpdated = { [weak self] transition in
guard let strongSelf = self, let navigationController = strongSelf._rootController as?
NavigationController else {
return
}
var isStatusBarHidden: Bool = false
for controller in strongSelf._topLevelOverlayControllers {
if let controller = controller as? UChatViewController {
if case .Hide = controller.statusBar.statusBarStyle {
isStatusBarHidden = true
}
}
}
navigationController.updateExternalStatusBarHidden(isStatusBarHidden, transition:
.animated(duration: 0.3, curve: .easeInOut))
}
}
// <--- Ferris
}
self.presentationContext.topLevelSubview = self._topLevelOverlayControllers.first?.view
}
}
View Hierarchy
enum
RootNavigationLayout
1
2
case split([UChatViewController], [UChatViewController])
case flat([UChatViewController])
struct
ModalContainerLayout
1
2
3
var controllers: [UChatViewController]
var isFlat: Bool
var isStandalone: Bool
struct
NavigationLayout
1
2
var root: RootNavigationLayout
var modal: [ModalContainerLayout]
enum
ViewControllerNavigationPresentation
1
2
3
4
5
6
case `default`
case master
case modal
case flatModal
case standaloneModal
case modalInLargeLayout
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
func makeNavigationLayout(mode: NavigationControllerMode, layout: ContainerViewLayout, controllers: [UChatViewController]) -> NavigationLayout {
var rootControllers: [UChatViewController] = []
var modalStack: [ModalContainerLayout] = []
for controller in controllers {
let requiresModal: Bool
var beginsModal: Bool = false
var isFlat: Bool = false
var isStandalone: Bool = false
switch controller.navigationPresentation {
case .default:
requiresModal = false
case .master:
requiresModal = false
case .modal:
requiresModal = true
beginsModal = true
case .flatModal:
requiresModal = true
beginsModal = true
isFlat = true
case .standaloneModal:
requiresModal = true
beginsModal = true
isStandalone = true
case .modalInLargeLayout:
switch layout.metrics.widthClass {
case .compact:
requiresModal = false
case .regular:
requiresModal = true
}
}
if requiresModal {// requireModel,则需要加入当前那个最后的modelContainer,如果最后那个modelContainer是standAlone的,则自己新开一个modelContainer并加入
controller._presentedInModal = true
if beginsModal || modalStack.isEmpty || modalStack[modalStack.count - 1].isStandalone {
modalStack.append(ModalContainerLayout(controllers: [controller], isFlat: isFlat, isStandalone: isStandalone))
} else {
modalStack[modalStack.count - 1].controllers.append(controller)
}
} else if !modalStack.isEmpty {// not requireModel, 此时如果已经有存在的modelContainer,则也需要加入到当前最后那个modelContainer, 如果最后那个modelContainer是standAlone的,则自己新开一个modelContainer并加入
controller._presentedInModal = true
if modalStack[modalStack.count - 1].isStandalone {
modalStack.append(ModalContainerLayout(controllers: [controller], isFlat: isFlat, isStandalone: isStandalone))
} else {
modalStack[modalStack.count - 1].controllers.append(controller)
}
} else {// 否则将直接做为rootController
controller._presentedInModal = false
rootControllers.append(controller)
}
}
let rootLayout: RootNavigationLayout
switch mode {
case .single:
rootLayout = .flat(rootControllers)
case .automaticMasterDetail:
switch layout.metrics.widthClass {
case .compact:
rootLayout = .flat(rootControllers)
case .regular:
let masterControllers = rootControllers.filter {
if case .master = $0.navigationPresentation {
return true
} else {
return false
}
}
let detailControllers = rootControllers.filter {
if case .master = $0.navigationPresentation {
return false
} else {
return true
}
}
rootLayout = .split(masterControllers, detailControllers)
}
}
return NavigationLayout(root: rootLayout, modal: modalStack)
}
in NavigationController
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
private var globalOverlayContainerParent: GlobalOverlayContainerParent?
var statusBarHost: StatusBarHost?
private var globalScrollToTopNode: ScrollToTopNode?
private var inCallStatusBar: StatusBar?
private var modalContainers: [NavigationModalContainer] = []
private var globalOverlayContainers: [NavigationOverlayContainer] = []
private weak var currentTopVisibleOverlayContainerStatusBar: NavigationOverlayContainer? = nil
private var currentStatusBarExternalHidden: Bool = false
func updateExternalStatusBarHidden(_ value: Bool, transition: ContainedViewLayoutTransition) {
if self.currentStatusBarExternalHidden != value {
self.currentStatusBarExternalHidden = value
if let layout = self.validLayout {
self.updateContainers(layout: layout, transition: transition)
}
}
}
// inCallSatusBar
public func setForceInCallStatusBar(_ forceInCallStatusBarText: String?, transition: ContainedViewLayoutTransition = .animated(duration: 0.3, curve: .easeInOut)) {
if let forceInCallStatusBarText = forceInCallStatusBarText {
let inCallStatusBar: StatusBar
if let current = self.inCallStatusBar {
inCallStatusBar = current
} else {
// 穿件inCallStatusBar
inCallStatusBar = StatusBar()
inCallStatusBar.inCallNavigate = { [weak self] in
self?.scrollToTop(.master)
}
inCallStatusBar.alpha = 0.0
self.inCallStatusBar = inCallStatusBar
var bottomOverlayContainer: NavigationOverlayContainer?
for overlayContainer in self.overlayContainers {
if overlayContainer.supernode != nil {
bottomOverlayContainer = overlayContainer // set `bottomOverlayContainer`
break
}
}
if let bottomOverlayContainer = bottomOverlayContainer {
self.displayNode.insertSubnode(inCallStatusBar, belowSubnode: bottomOverlayContainer) // 添加`inCallStatusBar` below `bottomOverlayContainer`
} else if let globalScrollToTopNode = self.globalScrollToTopNode {
self.displayNode.insertSubnode(inCallStatusBar, belowSubnode: globalScrollToTopNode) // 添加`inCallStatusBar` below `globalScrollToTopNode`
} else {
self.displayNode.addSubnode(inCallStatusBar) // 添加`inCallStatusBar` 到 `self.displayNode`
}
transition.updateAlpha(node: inCallStatusBar, alpha: 1.0)
}
if let layout = self.validLayout {
self.containerLayoutUpdated(layout, transition: transition)
inCallStatusBar.updateState(statusBar: nil, withSafeInsets: !layout.safeInsets.top.isZero, inCallText: forceInCallStatusBarText, animated: false)
}
} else if let inCallStatusBar = self.inCallStatusBar {
self.inCallStatusBar = nil
transition.updateAlpha(node: inCallStatusBar, alpha: 0.0, completion: { [weak inCallStatusBar] _ in
inCallStatusBar?.removeFromSupernode()
})
if let layout = self.validLayout {
self.containerLayoutUpdated(layout, transition: transition)
}
}
}
// 这是重头戏
private func updateContainers(layout rawLayout: ContainerViewLayout, transition: ContainedViewLayoutTransition) {
self.isUpdatingContainers = true
var layout = rawLayout
if self.ignoreInputHeight {
if layout.inputHeight == nil {
self.ignoreInputHeight = false
} else {
layout = layout.withUpdatedInputHeight(nil)
}
}
var overlayLayout = layout
if let globalOverlayContainerParent = self.globalOverlayContainerParent {
let portraitSize = CGSize(width: min(layout.size.width, layout.size.height), height: max(layout.size.width, layout.size.height))
let screenSize = UIScreen.main.bounds.size
let portraitScreenSize = CGSize(width: min(screenSize.width, screenSize.height), height: max(screenSize.width, screenSize.height))
if portraitSize.width != portraitScreenSize.width || portraitSize.height != portraitScreenSize.height {
if globalOverlayContainerParent.view.superview != self.displayNode.view {
self.displayNode.addSubnode(globalOverlayContainerParent) // 1. 添加 `globalOverlayContainerParent` 到 `self.displayNode`
}
overlayLayout.size.height = overlayLayout.size.height - (layout.inputHeight ?? 0.0)
overlayLayout.inputHeight = nil
overlayLayout.inputHeightIsInteractivellyChanging = false
} else if layout.inputHeight == nil {
if globalOverlayContainerParent.view.superview != self.displayNode.view {
self.displayNode.addSubnode(globalOverlayContainerParent)// 1. 添加 `globalOverlayContainerParent` 到 `self.displayNode`
}
} else {
if let statusBarHost = self.statusBarHost, let keyboardWindow = statusBarHost.keyboardWindow, let keyboardView = statusBarHost.keyboardView, !keyboardView.frame.height.isZero, isViewVisibleInHierarchy(keyboardView) {
if globalOverlayContainerParent.view.superview != keyboardWindow {
keyboardWindow.addSubnode(globalOverlayContainerParent)// 1. 添加 `globalOverlayContainerParent` 到 `keyboardWindow`
}
} else if globalOverlayContainerParent.view.superview !== self.displayNode.view {
self.displayNode.addSubnode(globalOverlayContainerParent)// 1. 添加 `globalOverlayContainerParent` 到 `self.displayNode`
}
}
}
// update `globalScrollToTopNode`
if let globalScrollToTopNode = self.globalScrollToTopNode {
globalScrollToTopNode.frame = CGRect(origin: CGPoint(x: 0.0, y: -1.0), size: CGSize(width: layout.size.width, height: 1.0))
}
// update `inCallStatusBar`
if let inCallStatusBar = self.inCallStatusBar {
let inCallStatusBarFrame = CGRect(origin: CGPoint(), size: CGSize(width: layout.size.width, height: max(40.0, layout.safeInsets.top)))
if inCallStatusBar.frame.isEmpty {
inCallStatusBar.frame = inCallStatusBarFrame
} else {
transition.updateFrame(node: inCallStatusBar, frame: inCallStatusBarFrame)
}
layout.statusBarHeight = inCallStatusBarFrame.height
self.inCallStatusBar?.frame = inCallStatusBarFrame
}
// update `globalOverlayContainerParent`
if let globalOverlayContainerParent = self.globalOverlayContainerParent {
transition.updateFrame(node: globalOverlayContainerParent, frame: CGRect(origin: CGPoint(), size: layout.size))
}
// layout `children(controllers)`
let navigationLayout = makeNavigationLayout(mode: self.mode, layout: layout, controllers: self._viewControllers)
var transition = transition
var statusBarStyle: StatusBarStyle = .Ignore // `statusBarStyle` 初始为 `ignore`
var statusBarHidden = false // `statusBarHidden` 初始为 `false`
var animateStatusBarStyleTransition = transition.isAnimated
// layouot `modalContainers`
var modalContainers: [NavigationModalContainer] = []
for i in 0 ..< navigationLayout.modal.count {
var existingModalContainer: NavigationModalContainer?
loop: for currentModalContainer in self.modalContainers {
for controller in navigationLayout.modal[i].controllers {
if currentModalContainer.container.controllers.contains(where: { $0 === controller }) {
existingModalContainer = currentModalContainer
break loop
}
}
}
let modalContainer: NavigationModalContainer
if let existingModalContainer = existingModalContainer {
modalContainer = existingModalContainer
} else {
// 创建 `modalContainer`
modalContainer = NavigationModalContainer(theme: self.theme, isFlat: navigationLayout.modal[i].isFlat, controllerRemoved: { [weak self] controller in
self?.controllerRemoved(controller)
})
self.modalContainers.append(modalContainer)
if !modalContainer.isReady {
modalContainer.isReadyUpdated = { [weak self, weak modalContainer] in
guard let strongSelf = self, let _ = modalContainer else {
return
}
strongSelf.updateContainersNonReentrant(transition: .animated(duration: 0.5, curve: .spring))
}
}
modalContainer.updateDismissProgress = { [weak self, weak modalContainer] _, transition in
guard let strongSelf = self, let _ = modalContainer else {
return
}
strongSelf.updateContainersNonReentrant(transition: transition)
}
modalContainer.interactivelyDismissed = { [weak self, weak modalContainer] hadInputFocus in
guard let strongSelf = self, let modalContainer = modalContainer else {
return
}
let controllers = strongSelf._viewControllers.filter { controller in
return !modalContainer.container.controllers.contains(where: { $0 === controller })
}
strongSelf.ignoreInputHeight = hadInputFocus
strongSelf.setViewControllers(controllers, animated: false)
strongSelf.ignoreInputHeight = false
}
}
modalContainers.append(modalContainer)
}
// 移除不应该再存在的
for container in self.modalContainers {
if !modalContainers.contains(where: { $0 === container }) {
if viewTreeContainsFirstResponder(view: container.view) {
self.ignoreInputHeight = true
container.view.endEditing(true)
}
transition = container.dismiss(transition: transition, completion: { [weak container] in
container?.removeFromSupernode()
})
}
}
self.modalContainers = modalContainers
// globalOverlayContainers
var topVisibleOverlayContainerWithStatusBar: NavigationOverlayContainer?
var notifyGlobalOverlayControllersUpdated = false
var modalStyleOverlayTransitionFactor: CGFloat = 0.0
var previousGlobalOverlayContainer: NavigationOverlayContainer?
for i in (0 ..< self.globalOverlayContainers.count).reversed() {// reverse
let overlayContainer = self.globalOverlayContainers[i]
let containerTransition: ContainedViewLayoutTransition
if overlayContainer.supernode == nil {
containerTransition = .immediate
} else {
containerTransition = transition
}
containerTransition.updateFrame(node: overlayContainer, frame: CGRect(origin: CGPoint(), size: overlayLayout.size))
overlayContainer.update(layout: overlayLayout, transition: containerTransition)
modalStyleOverlayTransitionFactor = max(modalStyleOverlayTransitionFactor, overlayContainer.controller.modalStyleOverlayTransitionFactor)
if overlayContainer.supernode == nil && overlayContainer.isReady {
if let previousGlobalOverlayContainer = previousGlobalOverlayContainer {
self.globalOverlayContainerParent?.insertSubnode(overlayContainer, belowSubnode: previousGlobalOverlayContainer) // 添加`globalOverlayContainer` below `previousGlobalOverlayContainer, 可见index越大越在上层(越靠近用户)
} else {
self.globalOverlayContainerParent?.addSubnode(overlayContainer) // 添加`globalOverlayContainer` 到 `globalOverlayContainerParent`
}
overlayContainer.transitionIn()
notifyGlobalOverlayControllersUpdated = true
}
if overlayContainer.supernode != nil {
previousGlobalOverlayContainer = overlayContainer
let controllerStatusBarStyle = overlayContainer.controller.statusBar.statusBarStyle
switch controllerStatusBarStyle {
case .Black, .White, .Hide:
if topVisibleOverlayContainerWithStatusBar == nil {// update `topVisibleOverlayContainerWithStatusBar`
topVisibleOverlayContainerWithStatusBar = overlayContainer
}
if case .Hide = controllerStatusBarStyle {
statusBarHidden = true // update `statusBarHidden`
} else {
statusBarHidden = overlayContainer.controller.statusBar.alpha.isZero// update `statusBarHidden`
}
case .Ignore:
break
}
}
}
// overlayContainers
var previousOverlayContainer: NavigationOverlayContainer?
for i in (0 ..< self.overlayContainers.count).reversed() {// reverse
let overlayContainer = self.overlayContainers[i]
let containerTransition: ContainedViewLayoutTransition
if overlayContainer.supernode == nil {
containerTransition = .immediate
} else {
containerTransition = transition
}
containerTransition.updateFrame(node: overlayContainer, frame: CGRect(origin: CGPoint(), size: layout.size))
overlayContainer.update(layout: layout, transition: containerTransition)
modalStyleOverlayTransitionFactor = max(modalStyleOverlayTransitionFactor, overlayContainer.controller.modalStyleOverlayTransitionFactor)
if overlayContainer.supernode == nil && overlayContainer.isReady {
if let previousOverlayContainer = previousOverlayContainer {
self.displayNode.insertSubnode(overlayContainer, belowSubnode: previousOverlayContainer) // 添加 `overlayContainer` below `previousOverlayContainer`
} else if let globalScrollToTopNode = self.globalScrollToTopNode {
self.displayNode.insertSubnode(overlayContainer, belowSubnode: globalScrollToTopNode) // 添加 `overlayContainer` below `globalScrollToTopNode`
} else if let globalOverlayContainerParent = self.globalOverlayContainerParent {
self.displayNode.insertSubnode(overlayContainer, belowSubnode: globalOverlayContainerParent) // 添加 `overlayContainer` below `globalOverlayContainerParent`
}else {
self.displayNode.addSubnode(overlayContainer)// 添加 `overlayContainer` below `self.displayNode`
}
overlayContainer.transitionIn()
}
if overlayContainer.supernode != nil {
previousOverlayContainer = overlayContainer
let controllerStatusBarStyle = overlayContainer.controller.statusBar.statusBarStyle
switch controllerStatusBarStyle {
case .Black, .White, .Hide:
if topVisibleOverlayContainerWithStatusBar == nil {
topVisibleOverlayContainerWithStatusBar = overlayContainer// update `topVisibleOverlayContainerWithStatusBar`
}
if case .Hide = controllerStatusBarStyle {
statusBarHidden = true // update `statusBarHidden`
} else {
statusBarHidden = overlayContainer.controller.statusBar.alpha.isZero // update `statusBarHidden`
}
case .Ignore:
break
}
}
}
if self.currentTopVisibleOverlayContainerStatusBar !== topVisibleOverlayContainerWithStatusBar {
animateStatusBarStyleTransition = true
self.currentTopVisibleOverlayContainerStatusBar = topVisibleOverlayContainerWithStatusBar // update `currentTopVisibleOverlayContainerStatusBar`
}
var previousModalContainer: NavigationModalContainer?
var visibleModalCount = 0
var topModalIsFlat = false
var hasVisibleStandaloneModal = false
var topModalDismissProgress: CGFloat = 0.0
for i in (0 ..< navigationLayout.modal.count).reversed() {
let modalContainer = self.modalContainers[i]
var isStandaloneModal = false
if let controller = modalContainer.container.controllers.first, case .standaloneModal = controller.navigationPresentation {
isStandaloneModal = true
}
let containerTransition: ContainedViewLayoutTransition
if modalContainer.supernode == nil {
containerTransition = .immediate
} else {
containerTransition = transition
}
let effectiveModalTransition: CGFloat
if visibleModalCount == 0 {
effectiveModalTransition = 0.0
} else if visibleModalCount == 1 {
effectiveModalTransition = 1.0 - topModalDismissProgress
} else {
effectiveModalTransition = 1.0
}
containerTransition.updateFrame(node: modalContainer, frame: CGRect(origin: CGPoint(), size: layout.size))
modalContainer.update(layout: layout, controllers: navigationLayout.modal[i].controllers, coveredByModalTransition: effectiveModalTransition, transition: containerTransition)
if modalContainer.supernode == nil && modalContainer.isReady {
// 从较底层找起
if let previousModalContainer = previousModalContainer {
self.displayNode.insertSubnode(modalContainer, belowSubnode: previousModalContainer) // 添加 `modalContainer` below `previousModalContainer`
} else if let inCallStatusBar = self.inCallStatusBar {
self.displayNode.insertSubnode(modalContainer, belowSubnode: inCallStatusBar) // 添加 `modalContainer` below `inCallStatusBar`
} else if let previousOverlayContainer = previousOverlayContainer {
self.displayNode.insertSubnode(modalContainer, belowSubnode: previousOverlayContainer) // 添加 `modalContainer` below `previousOverlayContainer(最底下那一个)`
} else if let globalScrollToTopNode = self.globalScrollToTopNode {
self.displayNode.insertSubnode(modalContainer, belowSubnode: globalScrollToTopNode)// 添加 `modalContainer` below `globalScrollToTopNode`
} else {
self.displayNode.addSubnode(modalContainer)// 添加 `modalContainer` 到 `self.displayNode`
}
modalContainer.animateIn(transition: transition)
}
if modalContainer.supernode != nil {
if !hasVisibleStandaloneModal && !isStandaloneModal {
visibleModalCount += 1
}
if isStandaloneModal {
hasVisibleStandaloneModal = true
visibleModalCount = 0
}
if previousModalContainer == nil {
topModalDismissProgress = modalContainer.dismissProgress
if case .compact = layout.metrics.widthClass {
modalContainer.keyboardViewManager = self.keyboardViewManager
modalContainer.canHaveKeyboardFocus = true
} else {
modalContainer.keyboardViewManager = nil
modalContainer.canHaveKeyboardFocus = true
}
} else {
modalContainer.keyboardViewManager = nil
modalContainer.canHaveKeyboardFocus = false
}
previousModalContainer = modalContainer
if isStandaloneModal {
switch modalContainer.container.statusBarStyle {
case .Hide:
statusBarHidden = true // update `statusBarHidden`
default:
break
}
}
}
topModalIsFlat = modalContainer.isFlat
}
switch navigationLayout.root {
case let .flat(controllers):
if let rootContainer = self.rootContainer {
switch rootContainer {
case let .flat(flatContainer):
if previousModalContainer == nil {
flatContainer.keyboardViewManager = self.keyboardViewManager
flatContainer.canHaveKeyboardFocus = true
} else {
flatContainer.keyboardViewManager = nil
flatContainer.canHaveKeyboardFocus = false
}
transition.updateFrame(node: flatContainer, frame: CGRect(origin: CGPoint(), size: layout.size))
flatContainer.update(layout: layout, canBeClosed: false, controllers: controllers, transition: transition)
case let .split(splitContainer):
// 创建 `flatContainer`
let flatContainer = NavigationContainer(controllerRemoved: { [weak self] controller in
self?.controllerRemoved(controller)
})
flatContainer.statusBarStyleUpdated = { [weak self] transition in
guard let strongSelf = self else {
return
}
strongSelf.updateContainersNonReentrant(transition: transition)
}
if previousModalContainer == nil {
flatContainer.keyboardViewManager = self.keyboardViewManager
flatContainer.canHaveKeyboardFocus = true
} else {
flatContainer.keyboardViewManager = nil
flatContainer.canHaveKeyboardFocus = false
}
// 添加 `flatContainer` 到 `self.displayNode.0`
self.displayNode.insertSubnode(flatContainer, at: 0)
// update `rootContainer`
self.rootContainer = .flat(flatContainer)
flatContainer.frame = CGRect(origin: CGPoint(), size: layout.size)
flatContainer.update(layout: layout, canBeClosed: false, controllers: controllers, transition: .immediate)
splitContainer.removeFromSupernode()
}
} else {
// 创建 `rootFlatContainer`
let flatContainer = NavigationContainer(controllerRemoved: { [weak self] controller in
self?.controllerRemoved(controller)
})
flatContainer.statusBarStyleUpdated = { [weak self] transition in
guard let strongSelf = self else {
return
}
strongSelf.updateContainersNonReentrant(transition: transition)
}
if previousModalContainer == nil {
flatContainer.keyboardViewManager = self.keyboardViewManager
flatContainer.canHaveKeyboardFocus = true
} else {
flatContainer.keyboardViewManager = nil
flatContainer.canHaveKeyboardFocus = false
}
// 添加 `flatContainer` 到 `self.displayNode.0`
self.displayNode.insertSubnode(flatContainer, at: 0)
self.rootContainer = .flat(flatContainer)
flatContainer.frame = CGRect(origin: CGPoint(), size: layout.size)
flatContainer.update(layout: layout, canBeClosed: false, controllers: controllers, transition: .immediate)
}
case let .split(masterControllers, detailControllers):
if let rootContainer = self.rootContainer {
switch rootContainer {
case let .flat(flatContainer):
// 创建splitContainer
let splitContainer = NavigationSplitContainer(theme: self.theme, controllerRemoved: { [weak self] controller in
self?.controllerRemoved(controller)
}, scrollToTop: { [weak self] subject in
self?.scrollToTop(subject)
})
// 添加 `splitContainer` 到 `self.displayNode.0`
self.displayNode.insertSubnode(splitContainer, at: 0)
self.rootContainer = .split(splitContainer)
if previousModalContainer == nil {
splitContainer.canHaveKeyboardFocus = true
} else {
splitContainer.canHaveKeyboardFocus = false
}
splitContainer.frame = CGRect(origin: CGPoint(), size: layout.size)
splitContainer.update(layout: layout, masterControllers: masterControllers, detailControllers: detailControllers, transition: .immediate)
flatContainer.statusBarStyleUpdated = nil
flatContainer.removeFromSupernode()
case let .split(splitContainer):
if previousModalContainer == nil {
splitContainer.canHaveKeyboardFocus = true
} else {
splitContainer.canHaveKeyboardFocus = false
}
transition.updateFrame(node: splitContainer, frame: CGRect(origin: CGPoint(), size: layout.size))
splitContainer.update(layout: layout, masterControllers: masterControllers, detailControllers: detailControllers, transition: transition)
}
} else {
// 创建 `rootSplitContainer`
let splitContainer = NavigationSplitContainer(theme: self.theme, controllerRemoved: { [weak self] controller in
self?.controllerRemoved(controller)
}, scrollToTop: { [weak self] subject in
self?.scrollToTop(subject)
})
// 添加 `splitContainer` 到 `self.displayNode.0`
self.displayNode.insertSubnode(splitContainer, at: 0)
self.rootContainer = .split(splitContainer)
if previousModalContainer == nil {
splitContainer.canHaveKeyboardFocus = true
} else {
splitContainer.canHaveKeyboardFocus = false
}
splitContainer.frame = CGRect(origin: CGPoint(), size: layout.size)
splitContainer.update(layout: layout, masterControllers: masterControllers, detailControllers: detailControllers, transition: .immediate)
}
}
if let rootContainer = self.rootContainer {
switch rootContainer {
case let .flat(container):
statusBarStyle = container.statusBarStyle // update `statusBarStyle`
self.globalScrollToTopNode?.isHidden = false // update `globalScrollToTopNode.isHidden`
case .split:
self.globalScrollToTopNode?.isHidden = true// update `globalScrollToTopNode.isHidden`
}
}
// `topModalDismissProgress`
topModalDismissProgress = max(topModalDismissProgress, modalStyleOverlayTransitionFactor)
switch layout.metrics.widthClass {
case .compact:
if visibleModalCount != 0 || !modalStyleOverlayTransitionFactor.isZero {
let effectiveRootModalDismissProgress: CGFloat
let visibleRootModalDismissProgress: CGFloat
var additionalModalFrameProgress: CGFloat
if visibleModalCount == 1 {
effectiveRootModalDismissProgress = topModalIsFlat ? 1.0 : topModalDismissProgress
visibleRootModalDismissProgress = effectiveRootModalDismissProgress
additionalModalFrameProgress = 0.0
} else if visibleModalCount >= 2 {
effectiveRootModalDismissProgress = 0.0
visibleRootModalDismissProgress = topModalDismissProgress
additionalModalFrameProgress = 1.0 - topModalDismissProgress
} else {
effectiveRootModalDismissProgress = 1.0 - modalStyleOverlayTransitionFactor
visibleRootModalDismissProgress = effectiveRootModalDismissProgress
if visibleModalCount == 0 {
additionalModalFrameProgress = 0.0
} else {
additionalModalFrameProgress = 1.0
}
}
let rootModalFrame: NavigationModalFrame
let modalFrameTransition: ContainedViewLayoutTransition = transition
var forceStatusBarAnimation = false
if let current = self.rootModalFrame {
rootModalFrame = current
transition.updateFrame(node: rootModalFrame, frame: CGRect(origin: CGPoint(), size: layout.size))
rootModalFrame.update(layout: layout, transition: modalFrameTransition)
rootModalFrame.updateDismissal(transition: transition, progress: effectiveRootModalDismissProgress, additionalProgress: additionalModalFrameProgress, completion: {})
forceStatusBarAnimation = true
} else {
rootModalFrame = NavigationModalFrame(theme: self.theme)
self.rootModalFrame = rootModalFrame
if let rootContainer = self.rootContainer {
var rootContainerNode: ASDisplayNode
switch rootContainer {
case let .flat(container):
rootContainerNode = container
case let .split(container):
rootContainerNode = container
}
self.displayNode.insertSubnode(rootModalFrame, aboveSubnode: rootContainerNode)
}
rootModalFrame.frame = CGRect(origin: CGPoint(), size: layout.size)
rootModalFrame.update(layout: layout, transition: .immediate)
rootModalFrame.updateDismissal(transition: transition, progress: effectiveRootModalDismissProgress, additionalProgress: additionalModalFrameProgress, completion: {})
}
if effectiveRootModalDismissProgress < 0.5 {
statusBarStyle = .White // update `statusBarStyle`
if forceStatusBarAnimation {
animateStatusBarStyleTransition = true
}
} else {
if forceStatusBarAnimation {
animateStatusBarStyleTransition = true
}
}
if let rootContainer = self.rootContainer {
var rootContainerNode: ASDisplayNode
switch rootContainer {
case let .flat(container):
rootContainerNode = container
case let .split(container):
rootContainerNode = container
}
var topInset: CGFloat = 0.0
if let statusBarHeight = layout.statusBarHeight {
topInset += statusBarHeight
}
let maxScale: CGFloat
let maxOffset: CGFloat
if topModalIsFlat {
maxScale = 1.0
maxOffset = 0.0
} else if visibleModalCount <= 1 {
maxScale = (layout.size.width - 16.0 * 2.0) / layout.size.width
maxOffset = (topInset - (layout.size.height - layout.size.height * maxScale) / 2.0)
} else {
maxScale = (layout.size.width - 16.0 * 2.0 * 2.0) / layout.size.width
maxOffset = (topInset + 10.0 - (layout.size.height - layout.size.height * maxScale) / 2.0)
}
let scale = 1.0 * visibleRootModalDismissProgress + (1.0 - visibleRootModalDismissProgress) * maxScale
let offset = (1.0 - visibleRootModalDismissProgress) * maxOffset
transition.updateSublayerTransformScaleAndOffset(node: rootContainerNode, scale: scale, offset: CGPoint(x: 0.0, y: offset), beginWithCurrentState: true)
}
} else {
if let rootModalFrame = self.rootModalFrame {
self.rootModalFrame = nil
rootModalFrame.updateDismissal(transition: transition, progress: 1.0, additionalProgress: 0.0, completion: { [weak rootModalFrame] in
rootModalFrame?.removeFromSupernode()
})
}
if let rootContainer = self.rootContainer {
var rootContainerNode: ASDisplayNode
switch rootContainer {
case let .flat(container):
rootContainerNode = container
case let .split(container):
rootContainerNode = container
}
transition.updateSublayerTransformScaleAndOffset(node: rootContainerNode, scale: 1.0, offset: CGPoint())
}
}
case .regular:
if let rootModalFrame = self.rootModalFrame {
self.rootModalFrame = nil
rootModalFrame.updateDismissal(transition: .immediate, progress: 1.0, additionalProgress: 0.0, completion: { [weak rootModalFrame] in
rootModalFrame?.removeFromSupernode()
})
}
if let rootContainer = self.rootContainer {
var rootContainerNode: ASDisplayNode
switch rootContainer {
case let .flat(container):
rootContainerNode = container
case let .split(container):
rootContainerNode = container
}
ContainedViewLayoutTransition.immediate.updateSublayerTransformScaleAndOffset(node: rootContainerNode, scale: 1.0, offset: CGPoint())
}
}
if self.inCallStatusBar != nil {
statusBarStyle = .White // update `statusBarStyle`
}
if let topVisibleOverlayContainerWithStatusBar = topVisibleOverlayContainerWithStatusBar {
statusBarStyle = topVisibleOverlayContainerWithStatusBar.controller.statusBar.statusBarStyle // update `statusBarStyle`
}
if self.currentStatusBarExternalHidden {
statusBarHidden = true // update `statusBarHidden`
}
// `resolvedStatusBarStyle`
let resolvedStatusBarStyle: NavigationStatusBarStyle
switch statusBarStyle {
case .Ignore, .Hide:
resolvedStatusBarStyle = self.theme.statusBar
case .Black:
resolvedStatusBarStyle = .black
case .White:
resolvedStatusBarStyle = .white
}
if self.validStatusBarStyle != resolvedStatusBarStyle || isFirstLayout {
self.validStatusBarStyle = resolvedStatusBarStyle // update `validStatusBarStyle`
let normalStatusBarStyle: UIStatusBarStyle
switch resolvedStatusBarStyle {
case .black:
if #available(iOS 13.0, *) {
normalStatusBarStyle = .darkContent
} else {
normalStatusBarStyle = .default
}
case .white:
normalStatusBarStyle = .lightContent
}
self.statusBarHost?.setStatusBarStyle(normalStatusBarStyle, animated: animateStatusBarStyleTransition) // update `statusBarStyle`
}
if self.validStatusBarHidden != statusBarHidden || isFirstLayout {
self.validStatusBarHidden = statusBarHidden
self.statusBarHost?.setStatusBarHidden(statusBarHidden, animated: animateStatusBarStyleTransition)
}
var foundControllerInFocus = false
for container in self.overlayContainers.reversed() {
if foundControllerInFocus {
container.controller.isInFocus = false
} else if container.controller.acceptsFocusWhenInOverlay {
foundControllerInFocus = true
container.controller.isInFocus = true
}
}
for container in self.modalContainers.reversed() {
if foundControllerInFocus {
container.container.isInFocus = false
} else {
foundControllerInFocus = true
container.container.isInFocus = true
}
}
if let rootContainer = self.rootContainer {
switch rootContainer {
case let .flat(container):
if foundControllerInFocus {
container.isInFocus = false
} else {
foundControllerInFocus = true
container.isInFocus = true
}
case let .split(split):
if foundControllerInFocus {
split.isInFocus = false
} else {
foundControllerInFocus = true
split.isInFocus = true
}
}
}
self.isUpdatingContainers = false
if notifyGlobalOverlayControllersUpdated {
self.globalOverlayControllersUpdated?()
}
self.updateSupportedOrientations?()
self.isFirstLayout = false
}
最终view层级从上到下为:globalOverlayContainers
, gloabalOverlayContainerParent
, globalScrollToTopNode
, overlayContainers
, inCallStatusBar
, modelContainers
, rootContainer
, self.displayNode
statusBarStyle解析依次为:Ignore
, flatRootContainer
, effectiveRootModalDismissProgress < 0.5
, inCallStatusBar
, topVisibleOverlayContainerWithStatusBar
statusBarHidden
的解析依次为:false
, globalOverlayContainer.controller.statusBar.statusBarStyle == Hide
, globalOverlayContainer.controller.statusBar.alpha == 0
, overlayContainer.controller.statusBar.statusBarStyle == Hide
, overlayContainer.controller.statusBar.alpha == 0
, isStandaloneModal && modalContainer.container.statusBarStyle == hidden
, currentStatusBarExternalHidden
那么顺便分析TelegramRootController
, 继承自NavigationController
ViewController
默认的 navigationPresentation
为default
,那么大部分情况下(如果没有任何一个requireModel),都将直接作为rootController
最终statusBarStyle通过这里更新
class
ApplicationStatusBarHost
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
private let application = UIApplication.shared
var statusBarFrame: CGRect {
return self.application.statusBarFrame
}
var statusBarStyle: UIStatusBarStyle {
get {
return self.application.statusBarStyle
} set(value) {
self.setStatusBarStyle(value, animated: false)
}
}
func setStatusBarStyle(_ style: UIStatusBarStyle, animated: Bool) {
#if DEBUG
print("FerrisLogger: \((#file as NSString).lastPathComponent) [\(#line)] \(style)")
#endif
self.application.setStatusBarStyle(style, animated: animated)
}
func setStatusBarHidden(_ value: Bool, animated: Bool) {
#if DEBUG
print("FerrisLogger: \((#file as NSString).lastPathComponent) [\(#line)] hidden: \(value)")
#endif
self.application.setStatusBarHidden(value, with: animated ? .fade : .none)
}