如何在 Objective C 中将 Mac 应用程序用户界面 light/dark 主题设置回默认值?
How to set Mac application user interface light/dark theme back to default in Objective C?
在我的 Qt 应用程序中,我想让用户可以选择将其应用程序的 light/dark 主题设置为:
- 与操作系统主题相同
- 浅色(忽略操作系统主题)
- 深色(忽略操作系统主题)
对于 1. 我可以使用这个 Objective C:
来计算操作系统主题
bool macIsInDarkTheme()
{
if (__builtin_available(macOS 10.14, *))
{
auto appearance = [NSApp.effectiveAppearance bestMatchFromAppearancesWithNames:
@[ NSAppearanceNameAqua, NSAppearanceNameDarkAqua ]];
return [appearance isEqualToString:NSAppearanceNameDarkAqua];
}
return false;
}
对于 2。我可以称之为 Objective C:
void macSetToLightTheme()
{
if (__builtin_available(macOS 10.14, *))
{
[NSApp setAppearance:[NSAppearance appearanceNamed:NSAppearanceNameAqua]];
}
}
对于 3。我可以称之为 Objective C:
void macSetToDarkTheme()
{
if (__builtin_available(macOS 10.14, *))
{
[NSApp setAppearance:[NSAppearance appearanceNamed:NSAppearanceNameDarkAqua]];
}
}
一切正常。如果用户想从 2. 或 3. 回到 1,问题就来了。如何将外观设置回从操作系统获取的默认值?我是一名 C++ 程序员,在 Objective C.
方面没有任何经验
将应用程序的外观设置为 nil 应该回退到默认行为(从系统偏好继承的外观):
[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.
在我的 Qt 应用程序中,我想让用户可以选择将其应用程序的 light/dark 主题设置为:
- 与操作系统主题相同
- 浅色(忽略操作系统主题)
- 深色(忽略操作系统主题)
对于 1. 我可以使用这个 Objective C:
来计算操作系统主题bool macIsInDarkTheme()
{
if (__builtin_available(macOS 10.14, *))
{
auto appearance = [NSApp.effectiveAppearance bestMatchFromAppearancesWithNames:
@[ NSAppearanceNameAqua, NSAppearanceNameDarkAqua ]];
return [appearance isEqualToString:NSAppearanceNameDarkAqua];
}
return false;
}
对于 2。我可以称之为 Objective C:
void macSetToLightTheme()
{
if (__builtin_available(macOS 10.14, *))
{
[NSApp setAppearance:[NSAppearance appearanceNamed:NSAppearanceNameAqua]];
}
}
对于 3。我可以称之为 Objective C:
void macSetToDarkTheme()
{
if (__builtin_available(macOS 10.14, *))
{
[NSApp setAppearance:[NSAppearance appearanceNamed:NSAppearanceNameDarkAqua]];
}
}
一切正常。如果用户想从 2. 或 3. 回到 1,问题就来了。如何将外观设置回从操作系统获取的默认值?我是一名 C++ 程序员,在 Objective C.
方面没有任何经验将应用程序的外观设置为 nil 应该回退到默认行为(从系统偏好继承的外观):
[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.