如何检测OS X是否处于黑暗模式?

Aug*_*us1 38 macos cocoa themes

我的cocoa应用程序在新的OS X"黑暗模式"下运行时必须改变其行为.

有没有办法检测OS X样式是否设置为此模式?

The*_*mer 59

不要认为有可靠的方法来检测它,但是你可以defaults read用来检查OSX是否处于黑暗模式.

defaults read -g AppleInterfaceStyle
Run Code Online (Sandbox Code Playgroud)

返回Dark(暗模式)或返回域对不存在.

编辑:

正如Ken Thomases所说,你可以通过NSUserDefaults访问.GlobalPreferences,所以

NSString *osxMode = [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"];
Run Code Online (Sandbox Code Playgroud)

如果是osxMode nil那么它不是处于黑暗模式,但是如果是osxMode @"Dark"那么它处于黑暗模式.

  • 不适用于 Catalina:“(kCFPreferencesAnyApplication、AppleInterfaceStyle)的域/默认对不存在” (7认同)
  • @bas“不存在”可能意味着“灯光模式”。 (5认同)
  • 我可以确认(在 Catalina 上),如果处于深色模式,则 `defaults read -g AppleInterfaceStyle` 返回“Dark”,否则返回“(kCFPreferencesAnyApplication,AppleInterfaceStyle) 的域/默认对不存在”。相当不直观:) (4认同)
  • `defaults`命令只是`CFPreferences` API的包装器,就像`NSUserDefaults`一样.因此,您可以使用这些API中的任何一个而不是调用`defaults`. (2认同)
  • @houbysoft使用[KVO](http://stackoverflow.com/questions/1141388/cocoa-notification-on-nsuserdefaults-value-change) (2认同)

And*_*rey 34

Swift 2 - > String("Dark","Light")

let appearance = NSUserDefaults.standardUserDefaults().stringForKey("AppleInterfaceStyle") ?? "Light"
Run Code Online (Sandbox Code Playgroud)

Swift 3 - > Enum(黑暗,光明)

enum InterfaceStyle : String {
   case Dark, Light

   init() {
      let type = UserDefaults.standard.string(forKey: "AppleInterfaceStyle") ?? "Light"
      self = InterfaceStyle(rawValue: type)!
    }
}

let currentStyle = InterfaceStyle()
Run Code Online (Sandbox Code Playgroud)

  • 不错,我不知道枚举可以有一个 `init()` 方法! (4认同)

Jam*_*son 13

您可以通过检查来检测使用此NSAppearanceCustomization方法。effectiveAppearancedarkAqua

Swift 4示例:

extension NSView {
    var isDarkMode: Bool {
        if #available(OSX 10.14, *) {
            if effectiveAppearance.name == .darkAqua {
                return true
            }
        }
        return false
    }
}
Run Code Online (Sandbox Code Playgroud)


Eri*_*ner 10

我会像这样检查所有黑暗的外表

extension NSView {

    var hasDarkAppearance: Bool {
        if #available(OSX 10.14, *) {
            switch effectiveAppearance.name {
            case .darkAqua, .vibrantDark, .accessibilityHighContrastDarkAqua, .accessibilityHighContrastVibrantDark:
                return true
            default:
                return false
            }
        } else {
            switch effectiveAppearance.name {
            case .vibrantDark:
                return true
            default:
                return false
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


J.b*_*nie 8

如果您不想处理枚举和switch语句,也可以将其包装为布尔值:

/// True if the application is in dark mode, and false otherwise
var inDarkMode: Bool {
    let mode = UserDefaults.standard.string(forKey: "AppleInterfaceStyle")
    return mode == "Dark"
}
Run Code Online (Sandbox Code Playgroud)

适用于Swift 4.2


rui*_*ano 8

要使用新的 macOS Catalina,您需要结合AppleInterfaceStyle引入的这个新值AppleInterfaceStyleSwitchesAutomatically

这里有一些伪代码解释了如何:

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
Run Code Online (Sandbox Code Playgroud)

您可以在此处查看 macOS 示例应用程序:https : //github.com/ruiaureliano/macOS-Appearance

(免责声明:我是此示例应用程序的作者。)