Drop into AppKit/UIKit to reliably set/unset preferred color scheme

This commit is contained in:
Angelo Stavrow 2020-08-13 14:54:56 -04:00
parent e2efecb210
commit 454b7bf09b
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
4 changed files with 46 additions and 12 deletions

View File

@ -1,19 +1,53 @@
import SwiftUI
class PreferencesModel: ObservableObject {
@Environment(\.colorScheme) var currentColorScheme
@Published var preferredColorScheme: ColorScheme = .light
/* We're stuck dropping into AppKit/UIKit to set light/dark schemes for now,
* because setting the .preferredColorScheme modifier on views in SwiftUI is
* currently unreliable.
*
* Feedback submitted to Apple:
*
* FB8382883: "On macOS 11β4, preferredColorScheme modifier does not respect .light ColorScheme"
* FB8383053: "On iOS 14β4/macOS 11β4, it is not possible to unset preferredColorScheme after setting
* it to either .light or .dark"
*/
#if os(iOS)
var window: UIWindow? {
guard let scene = UIApplication.shared.connectedScenes.first,
let windowSceneDelegate = scene.delegate as? UIWindowSceneDelegate,
let window = windowSceneDelegate.window else {
return nil
}
return window
}
#endif
@Published var selectedColorScheme: ColorScheme?
@Published var appearance: Int = 0 {
didSet {
switch appearance {
case 0:
preferredColorScheme = currentColorScheme
case 1:
preferredColorScheme = .light
// selectedColorScheme = .light
#if os(macOS)
NSApp.appearance = NSAppearance(named: .aqua)
#else
window?.overrideUserInterfaceStyle = .light
#endif
case 2:
preferredColorScheme = .dark
// selectedColorScheme = .dark
#if os(macOS)
NSApp.appearance = NSAppearance(named: .darkAqua)
#else
window?.overrideUserInterfaceStyle = .dark
#endif
default:
print("Unknown option selected, failing gracefully...")
// selectedColorScheme = .none
#if os(macOS)
NSApp.appearance = nil
#else
window?.overrideUserInterfaceStyle = .unspecified
#endif
}
}
}

View File

@ -18,7 +18,7 @@ struct WriteFreely_MultiPlatformApp: App {
var body: some Scene {
WindowGroup {
ContentView(postStore: store, preferences: preferences, account: account)
.preferredColorScheme(preferences.preferredColorScheme)
// .preferredColorScheme(preferences.selectedColorScheme) // See PreferencesModel for info.
}
#if os(macOS)
@ -39,7 +39,7 @@ struct WriteFreely_MultiPlatformApp: App {
}
.frame(minWidth: 300, maxWidth: 300, minHeight: 200, maxHeight: 200)
.padding()
.preferredColorScheme(preferences.preferredColorScheme)
// .preferredColorScheme(preferences.selectedColorScheme) // See PreferencesModel for info.
}
#endif
}

View File

@ -7,12 +7,12 @@
<key>WriteFreely-MultiPlatform (iOS).xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
<integer>0</integer>
</dict>
<key>WriteFreely-MultiPlatform (macOS).xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
<integer>1</integer>
</dict>
</dict>
</dict>

View File

@ -18,7 +18,7 @@ struct SettingsView: View {
}
}
}
.preferredColorScheme(preferences.preferredColorScheme)
// .preferredColorScheme(preferences.selectedColorScheme) // See PreferencesModel for info.
}
}