My cocoa app has to change its behaviour when run in the new OS X "dark mode".
Is there a way to detect if OS X style is set to this mode?
Dark Mode was first introduced to Mac with OS X 10.10 Yosemite, and it has become a staple of all subsequent versions of macOS.
Dark mode can be detected by using the userInterfaceStyle property on the current trait collection. When it's set to dark you know that the current appearance is set to dark.
Turn on Dark ModeChoose Apple menu > System Preferences, click General, then select one of the Appearance options at the top of the window: Light: Use the light appearance. Dark: Use the dark appearance.
Don't think there's a cocoa way of detecting it yet, however you can use defaults read
to check whether or not OSX is in dark mode.
defaults read -g AppleInterfaceStyle
Either returns Dark
(dark mode) or returns domain pair does not exist.
EDIT:
As Ken Thomases said you can access .GlobalPreferences via NSUserDefaults, so
NSString *osxMode = [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"];
If osxMode is nil
then it isn't in dark mode, but if osxMode is @"Dark"
then it is in dark mode.
Swift 2 -> String ("Dark", "Light")
let appearance = NSUserDefaults.standardUserDefaults().stringForKey("AppleInterfaceStyle") ?? "Light"
Swift 3 -> Enum (Dark, Light)
enum InterfaceStyle : String {
case Dark, Light
init() {
let type = UserDefaults.standard.string(forKey: "AppleInterfaceStyle") ?? "Light"
self = InterfaceStyle(rawValue: type)!
}
}
let currentStyle = InterfaceStyle()
You can detect this using NSAppearanceCustomization
method effectiveAppearance
, by checking for darkAqua
.
Swift 4 example:
extension NSView {
var isDarkMode: Bool {
if #available(OSX 10.14, *) {
if effectiveAppearance.name == .darkAqua {
return true
}
}
return false
}
}
You can also wrap it in a boolean if you don't feel like dealing with enums and switch statements:
/// True if the application is in dark mode, and false otherwise
var inDarkMode: Bool {
let mode = UserDefaults.standard.string(forKey: "AppleInterfaceStyle")
return mode == "Dark"
}
Works on Swift 4.2
For working with the new macOS Catalina you need to combine AppleInterfaceStyle
with this new value introduced AppleInterfaceStyleSwitchesAutomatically
.
Here is some pseudo-code explaining how to:
theme = light //default is light
if macOS_10.15
if UserDefaults(AppleInterfaceStyleSwitchesAutomatically) == TRUE
if UserDefaults(AppleInterfaceStyle) == NIL
theme = dark // is nil, means it's dark and will switch in future to light
else
theme = light //means it's light and will switch in future to dark
endif
else
if UserDefaults(AppleInterfaceStyle) == NIL
theme = light
else
theme = dark
endif
endif
else if macOS_10.14
if UserDefaults(AppleInterfaceStyle) == NIL
theme = light
else
theme = dark
endif
endif
You can check a macOS sample app here: https://github.com/ruiaureliano/macOS-Appearance.
(Disclaimer: I am the author of this sample app.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With