Browse Source

Fix system theming issues.

pull/113/head
Abhishek Banthia 3 years ago
parent
commit
5ad686b378
  1. 30
      Clocker/Overall App/Themer.swift

30
Clocker/Overall App/Themer.swift

@ -24,6 +24,7 @@ enum Theme: Int {
class Themer: NSObject { class Themer: NSObject {
private static var sharedInstance = Themer() private static var sharedInstance = Themer()
private var effectiveApperanceObserver: NSKeyValueObservation?
private var themeIndex: Theme { private var themeIndex: Theme {
didSet { didSet {
@ -57,6 +58,15 @@ class Themer: NSObject {
selector: #selector(respondToInterfaceStyle), selector: #selector(respondToInterfaceStyle),
name: .interfaceStyleDidChange, name: .interfaceStyleDidChange,
object: nil) object: nil)
if #available(macOS 10.14, *) {
effectiveApperanceObserver = NSApp.observe(\.effectiveAppearance) { [weak self] (app, _) in
if let sSelf = self {
sSelf.respondToInterfaceStyle()
NotificationCenter.default.post(name: .themeDidChangeNotification, object: nil)
}
}
}
} }
} }
@ -101,7 +111,7 @@ extension Themer {
if themeIndex == .dark { if themeIndex == .dark {
appAppearance = NSAppearance(named: .darkAqua) appAppearance = NSAppearance(named: .darkAqua)
} else if themeIndex == .system { } else if themeIndex == .system {
appAppearance = NSAppearance.current appAppearance = retrieveCurrentSystem() == .dark ? NSAppearance(named: .darkAqua) : NSAppearance(named: .aqua)
} }
NSApp.appearance = appAppearance NSApp.appearance = appAppearance
} }
@ -135,12 +145,7 @@ extension Themer {
} }
private func retrieveCurrentSystem() -> Theme { private func retrieveCurrentSystem() -> Theme {
if #available(OSX 10.15, *) { if #available(OSX 10.14, *) {
let appearanceDescription = NSApplication.shared.effectiveAppearance.debugDescription.lowercased()
if appearanceDescription.contains("dark") {
return .dark
}
} else if #available(OSX 10.14, *) {
if let appleInterfaceStyle = UserDefaults.standard.object(forKey: "AppleInterfaceStyle") as? String { if let appleInterfaceStyle = UserDefaults.standard.object(forKey: "AppleInterfaceStyle") as? String {
if appleInterfaceStyle.lowercased().contains("dark") { if appleInterfaceStyle.lowercased().contains("dark") {
return .dark return .dark
@ -537,4 +542,15 @@ extension Themer {
return nil return nil
} }
override var debugDescription: String {
if themeIndex == .system {
return "System Theme is \(retrieveCurrentSystem())"
}
return "Current Theme is \(themeIndex)"
}
override var description: String {
return debugDescription
}
} }

Loading…
Cancel
Save