Darkmode : App level vs System Level

I can alter the darkMode of an app independent of the darkmode of the rest of the system by doing this


private func setDarkMode(darkMode:Bool) {
	if !darkMode {
		NSApp.appearance = NSAppearance(named: .aqua)
	} else {
		NSApp.appearance = NSAppearance(named: .darkAqua)
	}
}

but how can I determine what the current SYSTEM level darkmode is?
obviously the app (using above, only indicates ITS mode, not the mode of anything else)

And I can’t cache the value before changing the app level, since the system can be changed at any point during the apps lifetime

NSApp.Appearance only checks the apps current state
Not the systems so if you set the app to run in light and the system is set to dark it would be wrong (or vice versa)

I wonder if you can read the system preference using UserDefaults to access the system domain ?
Using defaults read
when the system is in dark mode you find

 "Apple Global Domain" =     {
        AppleInterfaceStyle = Dark;
 }

In light mode that key is not there
Perhaps just reading for that key will suffice since user defaults should use the entire search path
Like

      UserDefaults.standard.object(forKey: kAppleInterfaceStyle) 

then check & see if it says “Dark” ?

Wouldn’t that be in currentDrawingAppearance?

Is that the current APP appearance ?
Which could be different than the system one

I found a NSAppeance.current.appearance… but it was “Aqua” [light] no matter what I did

Can you read the key AppleInterfaceStyle from “Apple Global Domain” ?

defaults read "Apple Global Domain" AppleInterfaceStyle

that key definitely switches with the system
Its present when the system is in dark mode and absent when not

Norman had the right idea.
This one line works for me on “Catalina”

let interfaceStyle = UserDefaults.standard.string(forKey: "AppleInterfaceStyle") ?? "Light"
print(interfaceStyle)

The call to ‘UserDefaults’ returns nil if the ’ AppleInterfaceStyle’ does not exist, hence the optional value.
Unable to check it on later OS’s

@MarkFX … that works on Monterey as well

Now to figure how to integrate that… .since the autoswitching is disabled once you execute

NSApp.appearance = NSAppearance(named: xxx)

Now this has me questioning my entire approach… So I’ll ask opinions :slight_smile:

I am creating an IDE. Right now the APP is either light/dark based on an in-App setting, making it independent of the system settings. The idea was to allow the developer to visualize the layout in either mode with out disrupting the system.

So… should I

  1. leave it as is, and the app itself decides which mode [Light|Dark]
  2. remove it, and tie to the system level, and change via System Prefs only
  3. add a 3rd option so it would be either #1 or #2 [Light|Auto|Dark]

I find it particularly nice to be able switch the mode of an App independent from the system preferences. I like some apps more in dark, some (like browsers) more in light. Those apps I’m aware of being able to change the mode themselves, are offering both:

ScreenShot 2022-05-27 at 18.33.28@2x

I think to reset to tracking the system you have to set it back to NIL