From a6a4448368a7583f6eddb8ed5ca4c11ad9fb7314 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Thu, 25 Jul 2019 17:37:55 +0200 Subject: [PATCH] Fixed status bar proxy time drawing on pre-X devices --- .../Display/Display/StatusBarProxyNode.swift | 62 ++++++++++++++++--- 1 file changed, 55 insertions(+), 7 deletions(-) diff --git a/submodules/Display/Display/StatusBarProxyNode.swift b/submodules/Display/Display/StatusBarProxyNode.swift index be2f0f3795..ab1b02f81c 100644 --- a/submodules/Display/Display/StatusBarProxyNode.swift +++ b/submodules/Display/Display/StatusBarProxyNode.swift @@ -51,6 +51,14 @@ private func maxSubviewBounds(_ view: UIView) -> CGRect { return bounds } +private let formatter: DateFormatter? = { + let formatter = DateFormatter() + formatter.timeStyle = .short + formatter.locale = Locale.current + return formatter +}() + + private class StatusBarItemNode: ASDisplayNode { var statusBarStyle: StatusBarStyle var targetView: UIView @@ -103,11 +111,42 @@ private class StatusBarItemNode: ASDisplayNode { } } } else { - context.withContext { c in - c.translateBy(x: containingBounds.minX, y: -containingBounds.minY) - UIGraphicsPushContext(c) - self.targetView.layer.render(in: c) - UIGraphicsPopContext() + 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: [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 12.0), NSAttributedStringKey.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) @@ -136,8 +175,9 @@ private class StatusBarItemNode: ASDisplayNode { type = .Activity } } - tintStatusBarItem(context, type: type, style: statusBarStyle) - self.contentNode.contents = context.generateImage()?.cgImage + 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 @@ -356,6 +396,14 @@ private let stringClass: AnyClass? = { return NSClassFromString("_UI" + nameString) }() +private let timeViewClass: AnyClass? = { + var nameString = "StatusBar" + if CFAbsoluteTimeGetCurrent() > 0 { + nameString += "TimeItemView" + } + return NSClassFromString("UI" + nameString) +}() + private func containsSubviewOfClass(view: UIView, of subviewClass: AnyClass?) -> Bool { guard let subviewClass = subviewClass else { return false