-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAppDelegate.swift
79 lines (66 loc) · 2.58 KB
/
AppDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import Cocoa
private let kAppleInterfaceThemeChangedNotification = "AppleInterfaceThemeChangedNotification"
private let kAppleInterfaceStyle = "AppleInterfaceStyle"
private let kAppleInterfaceStyleSwitchesAutomatically = "AppleInterfaceStyleSwitchesAutomatically"
enum OSAppearance: Int {
case light
case dark
}
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
@IBOutlet weak var textfield: NSTextField!
@IBOutlet weak var textview: NSTextView!
var osAppearance: OSAppearance = .light
func applicationDidFinishLaunching(_ aNotification: Notification) {
textview.font = NSFont(name: "Menlo", size: 14)
/**
** KVO Notification
**/
DistributedNotificationCenter.default().addObserver(
self,
selector: #selector(self.appleInterfaceThemeChangedNotification(notification:)),
name: NSNotification.Name(rawValue: kAppleInterfaceThemeChangedNotification),
object: nil
)
/**
* UPDATE APPEARANCE
**/
getAppearance()
}
func applicationWillTerminate(_ aNotification: Notification) {
}
@objc func appleInterfaceThemeChangedNotification(notification: Notification) {
getAppearance()
}
func getAppearance() {
self.osAppearance = .light
if #available(OSX 10.15, *) {
let appearanceDescription = NSApplication.shared.effectiveAppearance.debugDescription.lowercased()
self.textview.textStorage?.append(NSAttributedString(string: "appearanceDescription: \(appearanceDescription)\n"))
if appearanceDescription.contains("dark") {
self.osAppearance = .dark
}
} else if #available(OSX 10.14, *) {
if let appleInterfaceStyle = UserDefaults.standard.object(forKey: kAppleInterfaceStyle) as? String {
self.textview.textStorage?.append(NSAttributedString(string: "appleInterfaceStyle: \(appleInterfaceStyle)\n"))
if appleInterfaceStyle.lowercased().contains("dark") {
self.osAppearance = .dark
}
}
}
updateAppearance()
}
func updateAppearance() {
DispatchQueue.main.async {
switch self.osAppearance {
case .light:
self.textfield.stringValue = "Light"
self.textview.textColor = .black
case .dark:
self.textfield.stringValue = "Dark"
self.textview.textColor = .white
}
}
}
}