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)
}
@available(iOS, obsoleted: 17.0)
private let traitsKey: String = ["Traits", "Defined", "client", "_"].reversed().joined()
@available(iOS, obsoleted: 17.0)
private let key = "tusker_usePureBlackDarkMode"
@available(iOS 17.0, *)
private struct PureBlackDarkModeTrait: UITraitDefinition {
static let defaultValue = true
static let affectsColorAppearance = true
}
extension UITraitCollection {
var pureBlackDarkMode: Bool {
if #available(iOS 17.0, *) {
return self[PureBlackDarkModeTrait.self]
} else {
return obsoletePureBlackDarkMode
}
}
@available(iOS, obsoleted: 17.0)
var obsoletePureBlackDarkMode: Bool {
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
}
set {
@ -98,7 +115,19 @@ extension UITraitCollection {
}
convenience init(pureBlackDarkMode: Bool) {
self.init()
self.pureBlackDarkMode = pureBlackDarkMode
if #available(iOS 17.0, *) {
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 }
window.overrideUserInterfaceStyle = Preferences.shared.theme
window.tintColor = Preferences.shared.accentColor.color
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 #available(iOS 17.0, *) {
window.traitOverrides.pureBlackDarkMode = Preferences.shared.pureBlackDarkMode
} else {
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?
// even though it is for ReportSelectRulesView??
let traits: UITraitCollection = {
let t = UITraitCollection(userInterfaceStyle: colorScheme == .dark ? .dark : .light)
t.pureBlackDarkMode = true
var t = UITraitCollection(userInterfaceStyle: colorScheme == .dark ? .dark : .light)
if #available(iOS 17.0, *) {
t = t.modifyingTraits({ mutableTraits in
mutableTraits.pureBlackDarkMode = true
})
} else {
t.obsoletePureBlackDarkMode = true
}
return t
}()
Color(uiColor: .appGroupedBackground.resolvedColor(with: traits))