Fix NSAppearance conversion to GLFWtheme

No longer attempts to set high contrast modes, as Cocoa does not allow this.
This commit is contained in:
ws909 2023-01-26 04:06:14 +01:00
parent 5ee6c4526d
commit 1259135f1a
3 changed files with 41 additions and 27 deletions

View File

@ -35,6 +35,19 @@
// Needed for _NSGetProgname // Needed for _NSGetProgname
#include <crt_externs.h> #include <crt_externs.h>
#if MAC_OS_X_VERSION_MIN_REQUIRED < 101000
NSAppearanceName const NSAppearanceNameVibrantLight = @"NSAppearanceNameVibrantLight";
NSAppearanceName const NSAppearanceNameVibrantDark = @"NSAppearanceNameVibrantDark";
#endif
#if MAC_OS_X_VERSION_MIN_REQUIRED < 101400
NSAppearanceName const NSAppearanceNameDarkAqua = @"NSAppearanceNameDarkAqua";
NSAppearanceName const NSAppearanceNameAccessibilityHighContrastAqua = @"NSAppearanceNameAccessibilityAqua";
NSAppearanceName const NSAppearanceNameAccessibilityHighContrastDarkAqua = @"NSAppearanceNameAccessibilityDarkAqua";
NSAppearanceName const NSAppearanceNameAccessibilityHighContrastVibrantLight = @"NSAppearanceNameAccessibilityVibrantLight";
NSAppearanceName const NSAppearanceNameAccessibilityHighContrastVibrantDark = @"NSAppearanceNameAccessibilityVibrantDark";
#endif
// Change to our application bundle's resources directory, if present // Change to our application bundle's resources directory, if present
// //
static void changeToResourcesDirectory(void) static void changeToResourcesDirectory(void)
@ -177,42 +190,51 @@ static void createMenuBar(void)
void nsAppearanceToGLFWTheme(NSAppearance* appearance, GLFWtheme* theme) void nsAppearanceToGLFWTheme(NSAppearance* appearance, GLFWtheme* theme)
{ {
NSAppearanceName name = appearance.name; NSAppearanceName name = [appearance bestMatchFromAppearancesWithNames:@[
NSAppearanceNameAqua,
NSAppearanceNameDarkAqua,
NSAppearanceNameVibrantLight,
NSAppearanceNameVibrantDark,
NSAppearanceNameAccessibilityHighContrastAqua,
NSAppearanceNameAccessibilityHighContrastDarkAqua,
NSAppearanceNameAccessibilityHighContrastVibrantLight,
NSAppearanceNameAccessibilityHighContrastVibrantDark
]];
if (name == NSAppearanceNameAqua) if ([name isEqualToString:NSAppearanceNameAqua])
{ {
theme->baseTheme = GLFW_BASE_THEME_LIGHT; theme->baseTheme = GLFW_BASE_THEME_LIGHT;
} }
else if (name == NSAppearanceNameDarkAqua) else if ([name isEqualToString:NSAppearanceNameDarkAqua])
{ {
theme->baseTheme = GLFW_BASE_THEME_DARK; theme->baseTheme = GLFW_BASE_THEME_DARK;
} }
else if (name == NSAppearanceNameVibrantLight) else if ([name isEqualToString:NSAppearanceNameVibrantLight])
{ {
theme->baseTheme = GLFW_BASE_THEME_LIGHT; theme->baseTheme = GLFW_BASE_THEME_LIGHT;
theme->flags |= GLFW_THEME_FLAG_VIBRANT; theme->flags |= GLFW_THEME_FLAG_VIBRANT;
} }
else if (name == NSAppearanceNameVibrantDark) else if ([name isEqualToString:NSAppearanceNameVibrantDark])
{ {
theme->baseTheme = GLFW_BASE_THEME_DARK; theme->baseTheme = GLFW_BASE_THEME_DARK;
theme->flags |= GLFW_THEME_FLAG_VIBRANT; theme->flags |= GLFW_THEME_FLAG_VIBRANT;
} }
if (name == NSAppearanceNameAccessibilityHighContrastAqua) if ([name isEqualToString:NSAppearanceNameAccessibilityHighContrastAqua])
{ {
theme->baseTheme = GLFW_BASE_THEME_LIGHT; theme->baseTheme = GLFW_BASE_THEME_LIGHT;
theme->flags |= GLFW_THEME_FLAG_HIGH_CONTRAST; theme->flags |= GLFW_THEME_FLAG_HIGH_CONTRAST;
} }
else if (name == NSAppearanceNameAccessibilityHighContrastDarkAqua) else if ([name isEqualToString:NSAppearanceNameAccessibilityHighContrastDarkAqua])
{ {
theme->baseTheme = GLFW_BASE_THEME_DARK; theme->baseTheme = GLFW_BASE_THEME_DARK;
theme->flags |= GLFW_THEME_FLAG_HIGH_CONTRAST; theme->flags |= GLFW_THEME_FLAG_HIGH_CONTRAST;
} }
else if (name == NSAppearanceNameAccessibilityHighContrastVibrantLight) else if ([name isEqualToString:NSAppearanceNameAccessibilityHighContrastVibrantLight])
{ {
theme->baseTheme = GLFW_BASE_THEME_LIGHT; theme->baseTheme = GLFW_BASE_THEME_LIGHT;
theme->flags |= GLFW_THEME_FLAG_VIBRANT | GLFW_THEME_FLAG_HIGH_CONTRAST; theme->flags |= GLFW_THEME_FLAG_VIBRANT | GLFW_THEME_FLAG_HIGH_CONTRAST;
} }
else if (name == NSAppearanceNameAccessibilityHighContrastVibrantDark) else if ([name isEqualToString:NSAppearanceNameAccessibilityHighContrastVibrantDark])
{ {
theme->baseTheme = GLFW_BASE_THEME_DARK; theme->baseTheme = GLFW_BASE_THEME_DARK;
theme->flags |= GLFW_THEME_FLAG_VIBRANT | GLFW_THEME_FLAG_HIGH_CONTRAST; theme->flags |= GLFW_THEME_FLAG_VIBRANT | GLFW_THEME_FLAG_HIGH_CONTRAST;
@ -456,6 +478,8 @@ static GLFWbool initializeTIS(void)
{ {
// This class is never subclassed, so it's safe to ignore the context parameter // This class is never subclassed, so it's safe to ignore the context parameter
// TODO: FIXME: this method is invoked twice when the high contrast setting is edited in the preferences.
GLFWtheme theme = { 0, 0 }; GLFWtheme theme = { 0, 0 };
nsAppearanceToGLFWTheme(NSApp.effectiveAppearance, &theme); nsAppearanceToGLFWTheme(NSApp.effectiveAppearance, &theme);

View File

@ -42,6 +42,9 @@ typedef void* id;
typedef void NSAppearance; typedef void NSAppearance;
#endif #endif
#if MAC_OS_X_VERSION_MIN_REQUIRED < 101300
typedef NSString *NSAppearanceName;
#endif
// NOTE: Many Cocoa enum values have been renamed and we need to build across // NOTE: Many Cocoa enum values have been renamed and we need to build across
// SDK versions where one is unavailable or deprecated. // SDK versions where one is unavailable or deprecated.

View File

@ -1884,22 +1884,17 @@ void _glfwSetThemeCocoa(_GLFWwindow* window, GLFWtheme* theme)
return; return;
} }
// TODO: support color // TODO: support color
// TODO: fix vibrancy
// As per the Cocoa documentation, passing the high contrast names to
// appearanceNamed: will result in nil, so these can not be used.
NSAppearanceName name; NSAppearanceName name;
if (theme->baseTheme == GLFW_BASE_THEME_LIGHT) if (theme->baseTheme == GLFW_BASE_THEME_LIGHT)
{ {
if ((theme->flags & GLFW_THEME_FLAG_VIBRANT) && (theme->flags & GLFW_THEME_FLAG_HIGH_CONTRAST)) if (theme->flags & GLFW_THEME_FLAG_VIBRANT) {
{
name = NSAppearanceNameAccessibilityHighContrastVibrantLight;
}
else if (theme->flags & GLFW_THEME_FLAG_VIBRANT) {
name = NSAppearanceNameVibrantLight; name = NSAppearanceNameVibrantLight;
} }
else if (theme->flags & GLFW_THEME_FLAG_HIGH_CONTRAST)
{
name = NSAppearanceNameAccessibilityHighContrastAqua;
}
else else
{ {
name = NSAppearanceNameAqua; name = NSAppearanceNameAqua;
@ -1907,17 +1902,9 @@ void _glfwSetThemeCocoa(_GLFWwindow* window, GLFWtheme* theme)
} }
else else
{ {
if ((theme->flags & GLFW_THEME_FLAG_VIBRANT) && (theme->flags & GLFW_THEME_FLAG_HIGH_CONTRAST)) if (theme->flags & GLFW_THEME_FLAG_VIBRANT) {
{
name = NSAppearanceNameAccessibilityHighContrastVibrantDark;
}
else if (theme->flags & GLFW_THEME_FLAG_VIBRANT) {
name = NSAppearanceNameVibrantDark; name = NSAppearanceNameVibrantDark;
} }
else if (theme->flags & GLFW_THEME_FLAG_HIGH_CONTRAST)
{
name = NSAppearanceNameAccessibilityHighContrastDarkAqua;
}
else else
{ {
name = NSAppearanceNameDarkAqua; name = NSAppearanceNameDarkAqua;