Use custom UITraitDefinition on iOS 17

This commit is contained in:
Shadowfacts 2023-09-06 13:43:56 -04:00
parent f2485f0ba1
commit 3a3b7aaee4
3 changed files with 51 additions and 12 deletions

View File

@ -81,13 +81,30 @@ extension Color {
static let appFill = Color(uiColor: .appFill) static let appFill = Color(uiColor: .appFill)
} }
@available(iOS, obsoleted: 17.0)
private let traitsKey: String = ["Traits", "Defined", "client", "_"].reversed().joined() private let traitsKey: String = ["Traits", "Defined", "client", "_"].reversed().joined()
@available(iOS, obsoleted: 17.0)
private let key = "tusker_usePureBlackDarkMode" private let key = "tusker_usePureBlackDarkMode"
@available(iOS 17.0, *)
private struct PureBlackDarkModeTrait: UITraitDefinition {
static let defaultValue = true
static let affectsColorAppearance = true
}
extension UITraitCollection { extension UITraitCollection {
var pureBlackDarkMode: Bool { var pureBlackDarkMode: Bool {
if #available(iOS 17.0, *) {
return self[PureBlackDarkModeTrait.self]
} else {
return obsoletePureBlackDarkMode
}
}
@available(iOS, obsoleted: 17.0)
var obsoletePureBlackDarkMode: Bool {
get { get {
// default to true to mach OS behavior // default to true to match OS behavior
(value(forKey: traitsKey) as? [String: Any])?[key] as? Bool ?? true (value(forKey: traitsKey) as? [String: Any])?[key] as? Bool ?? true
} }
set { set {
@ -98,7 +115,19 @@ extension UITraitCollection {
} }
convenience init(pureBlackDarkMode: Bool) { convenience init(pureBlackDarkMode: Bool) {
self.init() if #available(iOS 17.0, *) {
self.pureBlackDarkMode = pureBlackDarkMode self.init(PureBlackDarkModeTrait.self, value: pureBlackDarkMode)
} else {
self.init()
self.obsoletePureBlackDarkMode = pureBlackDarkMode
}
}
}
@available(iOS 17.0, *)
extension UIMutableTraits {
var pureBlackDarkMode: Bool {
get { self[PureBlackDarkModeTrait.self] }
set { self[PureBlackDarkModeTrait.self] = newValue }
} }
} }

View File

@ -32,14 +32,18 @@ extension TuskerSceneDelegate {
guard let window else { return } guard let window else { return }
window.overrideUserInterfaceStyle = Preferences.shared.theme window.overrideUserInterfaceStyle = Preferences.shared.theme
window.tintColor = Preferences.shared.accentColor.color window.tintColor = Preferences.shared.accentColor.color
let exception = catchNSException { if #available(iOS 17.0, *) {
let key = ["Controller", "Presentation", "root", "_"].reversed().joined() window.traitOverrides.pureBlackDarkMode = Preferences.shared.pureBlackDarkMode
if let rootPresentationController = window.value(forKey: key) as? UIPresentationController { } else {
rootPresentationController.overrideTraitCollection = UITraitCollection(pureBlackDarkMode: Preferences.shared.pureBlackDarkMode) let exception = catchNSException {
let key = ["Controller", "Presentation", "root", "_"].reversed().joined()
if let rootPresentationController = window.value(forKey: key) as? UIPresentationController {
rootPresentationController.overrideTraitCollection = UITraitCollection(pureBlackDarkMode: Preferences.shared.pureBlackDarkMode)
}
}
if let exception {
SentrySDK.capture(exception: exception)
} }
}
if let exception {
SentrySDK.capture(exception: exception)
} }
} }
} }

View File

@ -76,8 +76,14 @@ private struct ScrollBackgroundModifier: ViewModifier {
// otherwise the pureBlackDarkMode isn't propagated, for some reason? // otherwise the pureBlackDarkMode isn't propagated, for some reason?
// even though it is for ReportSelectRulesView?? // even though it is for ReportSelectRulesView??
let traits: UITraitCollection = { let traits: UITraitCollection = {
let t = UITraitCollection(userInterfaceStyle: colorScheme == .dark ? .dark : .light) var t = UITraitCollection(userInterfaceStyle: colorScheme == .dark ? .dark : .light)
t.pureBlackDarkMode = true if #available(iOS 17.0, *) {
t = t.modifyingTraits({ mutableTraits in
mutableTraits.pureBlackDarkMode = true
})
} else {
t.obsoletePureBlackDarkMode = true
}
return t return t
}() }()
Color(uiColor: .appGroupedBackground.resolvedColor(with: traits)) Color(uiColor: .appGroupedBackground.resolvedColor(with: traits))