Skip to content

Instantly share code, notes, and snippets.

@gleicon
Created February 7, 2021 13:33
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save gleicon/31206de0f329df60dee90c32309a2090 to your computer and use it in GitHub Desktop.
Swift test to detect light/dark mode
// 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