Created
February 7, 2021 13:33
Star
You must be signed in to star a gist
Swift test to detect light/dark mode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// swiftc detect.swift -o detect | |
import Cocoa | |
func detectDarkTheme() { | |
let isDark = UserDefaults.standard.string(forKey: "AppleInterfaceStyle") == "Dark" | |
if isDark { | |
print("Mode is Dark") | |
} else { | |
print("Mode is Light") | |
} | |
} | |
detectDarkTheme() | |
DistributedNotificationCenter.default.addObserver( | |
forName: Notification.Name("AppleInterfaceThemeChangedNotification"), | |
object: nil, | |
queue: nil) { | |
(notification) in detectDarkTheme() | |
} | |
NSApplication.shared.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment