Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if OS X is in dark mode?

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?

like image 297
Augustus1 Avatar asked Aug 08 '14 15:08

Augustus1


People also ask

Which macOS version has Dark 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.

How do I know if Dark Mode is enabled IOS?

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.

Does macOS have Dark Mode?

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.


Video Answer


5 Answers

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.

like image 174
TheAmateurProgrammer Avatar answered Oct 18 '22 20:10

TheAmateurProgrammer


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()
like image 40
Andrey Avatar answered Oct 18 '22 22:10

Andrey


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
    }
}
like image 21
James Eunson Avatar answered Oct 18 '22 22:10

James Eunson


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

like image 13
J.beenie Avatar answered Oct 18 '22 21:10

J.beenie


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.)

like image 13
ruiaureliano Avatar answered Oct 18 '22 21:10

ruiaureliano