From 275e1da4bb1e5231249599747326d123514a922d Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Thu, 2 Feb 2023 03:45:49 +0400 Subject: [PATCH] Add custom proximity sensor screen dimming since it's broken on iPhone 14 --- .../DeviceProximity/DeviceProximityManager.h | 2 ++ .../Sources/DeviceProximityManager.m | 9 ++++++++ submodules/Display/Source/WindowContent.swift | 22 ++++++++++++++----- .../TelegramUI/Sources/AppDelegate.swift | 8 +++++++ .../Sources/ApplicationContext.swift | 5 ----- 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/submodules/DeviceProximity/PublicHeaders/DeviceProximity/DeviceProximityManager.h b/submodules/DeviceProximity/PublicHeaders/DeviceProximity/DeviceProximityManager.h index b9b9f23535..2a6c4f4010 100644 --- a/submodules/DeviceProximity/PublicHeaders/DeviceProximity/DeviceProximityManager.h +++ b/submodules/DeviceProximity/PublicHeaders/DeviceProximity/DeviceProximityManager.h @@ -2,6 +2,8 @@ @interface DeviceProximityManager : NSObject +@property (nonatomic, copy) void(^ _Nullable proximityChanged)(bool); + + (DeviceProximityManager * _Nonnull)shared; - (bool)currentValue; diff --git a/submodules/DeviceProximity/Sources/DeviceProximityManager.m b/submodules/DeviceProximity/Sources/DeviceProximityManager.m index 94ef975a8d..3b14080244 100644 --- a/submodules/DeviceProximity/Sources/DeviceProximityManager.m +++ b/submodules/DeviceProximity/Sources/DeviceProximityManager.m @@ -46,6 +46,9 @@ for (void (^f)(bool) in [strongSelf->_subscribers copyItems]) { f(proximityState); } + if (strongSelf.proximityChanged != nil) { + strongSelf.proximityChanged(proximityState); + } } else if (!strongSelf->_proximityState && [strongSelf->_subscribers isEmpty]) { [UIDevice currentDevice].proximityMonitoringEnabled = false; } @@ -90,6 +93,9 @@ for (void (^f)(bool) in [_subscribers copyItems]) { f(_proximityState); } + if (self.proximityChanged != nil) { + self.proximityChanged(_proximityState); + } } } else { if (_proximityState) { @@ -97,6 +103,9 @@ for (void (^f)(bool) in [_subscribers copyItems]) { f(_proximityState); } + if (self.proximityChanged != nil) { + self.proximityChanged(_proximityState); + } } else { [UIDevice currentDevice].proximityMonitoringEnabled = false; } diff --git a/submodules/Display/Source/WindowContent.swift b/submodules/Display/Source/WindowContent.swift index 9981e42339..fbf0c016ab 100644 --- a/submodules/Display/Source/WindowContent.swift +++ b/submodules/Display/Source/WindowContent.swift @@ -241,6 +241,7 @@ public final class WindowKeyboardGestureRecognizerDelegate: NSObject, UIGestureR public class Window1 { public let hostView: WindowHostView public let badgeView: UIImageView + private let customProximityDimView: UIView private var deviceMetrics: DeviceMetrics @@ -257,8 +258,6 @@ public class Window1 { private var updatingLayout: UpdatingLayout? private var updatedContainerLayout: ContainerViewLayout? private var upperKeyboardInputPositionBound: CGFloat? - private var cachedWindowSubviewCount: Int = 0 - private var cachedHasPreview: Bool = false private let presentationContext: PresentationContext private let overlayPresentationContext: GlobalOverlayPresentationContext @@ -270,10 +269,7 @@ public class Window1 { private var shouldInvalidateSupportedOrientations = false private var statusBarHidden = false - - public var previewThemeAccentColor: UIColor = .blue - public var previewThemeDarkBlur: Bool = false - + private var shouldNotAnimateLikelyKeyboardAutocorrectionSwitch: Bool = false public private(set) var forceInCallStatusBarText: String? = nil @@ -333,6 +329,10 @@ public class Window1 { self.badgeView.image = UIImage(bundleImageName: "Components/AppBadge") self.badgeView.isHidden = true + self.customProximityDimView = UIView() + self.customProximityDimView.backgroundColor = .black + self.customProximityDimView.isHidden = true + self.systemUserInterfaceStyle = hostView.systemUserInterfaceStyle let boundsSize = self.hostView.eventView.bounds.size @@ -652,6 +652,7 @@ public class Window1 { self.windowPanRecognizer = recognizer self.hostView.containerView.addGestureRecognizer(recognizer) self.hostView.containerView.addSubview(self.badgeView) + self.hostView.containerView.addSubview(self.customProximityDimView) } public required init(coder aDecoder: NSCoder) { @@ -685,6 +686,13 @@ public class Window1 { self.updateBadgeVisibility() } + public func setProximityDimHidden(_ hidden: Bool) { + guard hidden != self.customProximityDimView.isHidden else { + return + } + self.customProximityDimView.isHidden = hidden + } + private func updateBadgeVisibility() { let badgeIsHidden = !self.deviceMetrics.showAppBadge || self.forceBadgeHidden || self.windowLayout.size.width > self.windowLayout.size.height if badgeIsHidden != self.badgeView.isHidden && !badgeIsHidden { @@ -1141,6 +1149,8 @@ public class Window1 { self.updateBadgeVisibility() self.badgeView.frame = CGRect(origin: CGPoint(x: floorToScreenPixels((self.windowLayout.size.width - image.size.width) / 2.0), y: 5.0), size: image.size) } + + self.customProximityDimView.frame = CGRect(origin: .zero, size: self.windowLayout.size) } } } diff --git a/submodules/TelegramUI/Sources/AppDelegate.swift b/submodules/TelegramUI/Sources/AppDelegate.swift index 9ae5443d09..9e064fa2cb 100644 --- a/submodules/TelegramUI/Sources/AppDelegate.swift +++ b/submodules/TelegramUI/Sources/AppDelegate.swift @@ -36,6 +36,7 @@ import UIKitRuntimeUtils import StoreKit import PhoneNumberFormat import AuthorizationUI +import DeviceProximity #if canImport(AppCenter) import AppCenter @@ -1316,6 +1317,13 @@ private func extractAccountManagerState(records: AccountRecordsView deliverOnMainQueue).start(next: { [weak self] presentationData in if let strongSelf = self { if previousTheme.swap(presentationData.theme) !== presentationData.theme { - strongSelf.mainWindow.previewThemeAccentColor = presentationData.theme.rootController.navigationBar.accentTextColor - strongSelf.mainWindow.previewThemeDarkBlur = presentationData.theme.rootController.keyboardColor == .dark strongSelf.lockedCoveringView.updateTheme(presentationData.theme) strongSelf.rootController.updateTheme(NavigationControllerTheme(presentationTheme: presentationData.theme)) }