In my Qt application I want to give the user the option to set the light/dark theme of their app to:
For 1. I can just work out the operating system theme using this Objective C:
bool macIsInDarkTheme()
{
if (__builtin_available(macOS 10.14, *))
{
auto appearance = [NSApp.effectiveAppearance bestMatchFromAppearancesWithNames:
@[ NSAppearanceNameAqua, NSAppearanceNameDarkAqua ]];
return [appearance isEqualToString:NSAppearanceNameDarkAqua];
}
return false;
}
For 2. I can call this Objective C:
void macSetToLightTheme()
{
if (__builtin_available(macOS 10.14, *))
{
[NSApp setAppearance:[NSAppearance appearanceNamed:NSAppearanceNameAqua]];
}
}
For 3. I can call this Objective C:
void macSetToDarkTheme()
{
if (__builtin_available(macOS 10.14, *))
{
[NSApp setAppearance:[NSAppearance appearanceNamed:NSAppearanceNameDarkAqua]];
}
}
This all works. The issue comes if the user want to go from 2. or 3. back to 1. How do you set the appearance back to the default of getting it from the operating system? I'm a C++ programmer and don't really have any experience in Objective C.
Setting the app's appearance to nil should fallback to the default behavior (appearance inherited from system preference):
[NSApp setAppearance:nil];
https://developer.apple.com/documentation/appkit/nsapplication/2967170-appearance?language=objc
When the value of this property is nil (the default), AppKit applies the current system appearance to the app’s user interface elements, including its windows, views, panels, and popovers. Assigning an NSAppearance object to this property causes the app’s interface elements to adopt the specified appearance instead.