Source code for the WriteFreely SwiftUI app for iOS, iPadOS, and macOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

68 lines
2.2 KiB

  1. import SwiftUI
  2. class PreferencesModel: ObservableObject {
  3. private let defaults = UserDefaults.shared
  4. /* We're stuck dropping into AppKit/UIKit to set light/dark schemes for now,
  5. * because setting the .preferredColorScheme modifier on views in SwiftUI is
  6. * currently unreliable.
  7. *
  8. * Feedback submitted to Apple:
  9. *
  10. * FB8382883: "On macOS 11β4, preferredColorScheme modifier does not respect .light ColorScheme"
  11. * FB8383053: "On iOS 14β4/macOS 11β4, it is not possible to unset preferredColorScheme after setting
  12. * it to either .light or .dark"
  13. */
  14. #if os(iOS)
  15. @available(iOSApplicationExtension, unavailable)
  16. var window: UIWindow? {
  17. guard let scene = UIApplication.shared.connectedScenes.first,
  18. let windowSceneDelegate = scene.delegate as? UIWindowSceneDelegate,
  19. let window = windowSceneDelegate.window else {
  20. return nil
  21. }
  22. return window
  23. }
  24. #endif
  25. @available(iOSApplicationExtension, unavailable)
  26. @Published var selectedColorScheme: ColorScheme?
  27. @available(iOSApplicationExtension, unavailable)
  28. @Published var appearance: Int = 0 {
  29. didSet {
  30. switch appearance {
  31. case 1:
  32. // selectedColorScheme = .light
  33. #if os(macOS)
  34. NSApp.appearance = NSAppearance(named: .aqua)
  35. #else
  36. window?.overrideUserInterfaceStyle = .light
  37. #endif
  38. case 2:
  39. // selectedColorScheme = .dark
  40. #if os(macOS)
  41. NSApp.appearance = NSAppearance(named: .darkAqua)
  42. #else
  43. window?.overrideUserInterfaceStyle = .dark
  44. #endif
  45. default:
  46. // selectedColorScheme = .none
  47. #if os(macOS)
  48. NSApp.appearance = nil
  49. #else
  50. window?.overrideUserInterfaceStyle = .unspecified
  51. #endif
  52. }
  53. defaults.set(appearance, forKey: WFDefaults.colorSchemeIntegerKey)
  54. }
  55. }
  56. @Published var font: Int = 0 {
  57. didSet {
  58. defaults.set(font, forKey: WFDefaults.defaultFontIntegerKey)
  59. }
  60. }
  61. }