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) {
if #available(iOS 17.0, *) {
self.init(PureBlackDarkModeTrait.self, value: pureBlackDarkMode)
} else {
self.init() self.init()
self.pureBlackDarkMode = pureBlackDarkMode self.obsoletePureBlackDarkMode = pureBlackDarkMode
}
}
}
@available(iOS 17.0, *)
extension UIMutableTraits {
var pureBlackDarkMode: Bool {
get { self[PureBlackDarkModeTrait.self] }
set { self[PureBlackDarkModeTrait.self] = newValue }
} }
} }

View File

@ -32,6 +32,9 @@ 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
if #available(iOS 17.0, *) {
window.traitOverrides.pureBlackDarkMode = Preferences.shared.pureBlackDarkMode
} else {
let exception = catchNSException { let exception = catchNSException {
let key = ["Controller", "Presentation", "root", "_"].reversed().joined() let key = ["Controller", "Presentation", "root", "_"].reversed().joined()
if let rootPresentationController = window.value(forKey: key) as? UIPresentationController { if let rootPresentationController = window.value(forKey: key) as? UIPresentationController {
@ -43,3 +46,4 @@ extension TuskerSceneDelegate {
} }
} }
} }
}

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))