Fix Theme not overriding UserInterfaceStyle when not following system theme (#508)

This commit is contained in:
David Walter 2023-01-29 13:24:51 +01:00 committed by GitHub
parent e5b6e79fa9
commit 329e21f43c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -38,6 +38,7 @@ struct ThemeApplier: ViewModifier {
theme.selectedSet = colorScheme == .dark ? sets.dark.name : sets.light.name
}
setWindowTint(theme.tintColor)
setWindowUserInterfaceStyle(from: theme.selectedScheme)
setBarsColor(theme.primaryBackgroundColor)
}
.onChange(of: theme.tintColor) { newValue in
@ -46,6 +47,9 @@ struct ThemeApplier: ViewModifier {
.onChange(of: theme.primaryBackgroundColor) { newValue in
setBarsColor(newValue)
}
.onChange(of: theme.selectedScheme) { newValue in
setWindowUserInterfaceStyle(from: newValue)
}
.onChange(of: colorScheme) { newColorScheme in
if theme.followSystemColorScheme,
let sets = availableColorsSets
@ -58,6 +62,26 @@ struct ThemeApplier: ViewModifier {
}
#if canImport(UIKit)
private func setWindowUserInterfaceStyle(from colorScheme: ColorScheme) {
guard !theme.followSystemColorScheme else {
setWindowUserInterfaceStyle(.unspecified)
return
}
switch colorScheme {
case .dark:
setWindowUserInterfaceStyle(.dark)
case .light:
setWindowUserInterfaceStyle(.light)
}
}
private func setWindowUserInterfaceStyle(_ userInterfaceStyle: UIUserInterfaceStyle) {
allWindows()
.forEach {
$0.overrideUserInterfaceStyle = userInterfaceStyle
}
}
private func setWindowTint(_ color: Color) {
allWindows()
.forEach {