diff --git a/src/cocoa_init.m b/src/cocoa_init.m index 059eb10f..9b52b9b4 100644 --- a/src/cocoa_init.m +++ b/src/cocoa_init.m @@ -35,6 +35,19 @@ // Needed for _NSGetProgname #include + +#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 // static void changeToResourcesDirectory(void) @@ -177,42 +190,51 @@ static void createMenuBar(void) 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; } - else if (name == NSAppearanceNameDarkAqua) + else if ([name isEqualToString:NSAppearanceNameDarkAqua]) { theme->baseTheme = GLFW_BASE_THEME_DARK; } - else if (name == NSAppearanceNameVibrantLight) + else if ([name isEqualToString:NSAppearanceNameVibrantLight]) { theme->baseTheme = GLFW_BASE_THEME_LIGHT; theme->flags |= GLFW_THEME_FLAG_VIBRANT; } - else if (name == NSAppearanceNameVibrantDark) + else if ([name isEqualToString:NSAppearanceNameVibrantDark]) { theme->baseTheme = GLFW_BASE_THEME_DARK; theme->flags |= GLFW_THEME_FLAG_VIBRANT; } - if (name == NSAppearanceNameAccessibilityHighContrastAqua) + if ([name isEqualToString:NSAppearanceNameAccessibilityHighContrastAqua]) { theme->baseTheme = GLFW_BASE_THEME_LIGHT; theme->flags |= GLFW_THEME_FLAG_HIGH_CONTRAST; } - else if (name == NSAppearanceNameAccessibilityHighContrastDarkAqua) + else if ([name isEqualToString:NSAppearanceNameAccessibilityHighContrastDarkAqua]) { theme->baseTheme = GLFW_BASE_THEME_DARK; theme->flags |= GLFW_THEME_FLAG_HIGH_CONTRAST; } - else if (name == NSAppearanceNameAccessibilityHighContrastVibrantLight) + else if ([name isEqualToString:NSAppearanceNameAccessibilityHighContrastVibrantLight]) { theme->baseTheme = GLFW_BASE_THEME_LIGHT; 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->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 + // TODO: FIXME: this method is invoked twice when the high contrast setting is edited in the preferences. + GLFWtheme theme = { 0, 0 }; nsAppearanceToGLFWTheme(NSApp.effectiveAppearance, &theme); diff --git a/src/cocoa_platform.h b/src/cocoa_platform.h index d22ca54b..9246b4b8 100644 --- a/src/cocoa_platform.h +++ b/src/cocoa_platform.h @@ -42,6 +42,9 @@ typedef void* id; typedef void NSAppearance; #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 // SDK versions where one is unavailable or deprecated. diff --git a/src/cocoa_window.m b/src/cocoa_window.m index b1416201..e4965d89 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -1884,22 +1884,17 @@ void _glfwSetThemeCocoa(_GLFWwindow* window, GLFWtheme* theme) return; } // 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; if (theme->baseTheme == GLFW_BASE_THEME_LIGHT) { - if ((theme->flags & GLFW_THEME_FLAG_VIBRANT) && (theme->flags & GLFW_THEME_FLAG_HIGH_CONTRAST)) - { - name = NSAppearanceNameAccessibilityHighContrastVibrantLight; - } - else if (theme->flags & GLFW_THEME_FLAG_VIBRANT) { + if (theme->flags & GLFW_THEME_FLAG_VIBRANT) { name = NSAppearanceNameVibrantLight; } - else if (theme->flags & GLFW_THEME_FLAG_HIGH_CONTRAST) - { - name = NSAppearanceNameAccessibilityHighContrastAqua; - } else { name = NSAppearanceNameAqua; @@ -1907,17 +1902,9 @@ void _glfwSetThemeCocoa(_GLFWwindow* window, GLFWtheme* theme) } else { - if ((theme->flags & GLFW_THEME_FLAG_VIBRANT) && (theme->flags & GLFW_THEME_FLAG_HIGH_CONTRAST)) - { - name = NSAppearanceNameAccessibilityHighContrastVibrantDark; - } - else if (theme->flags & GLFW_THEME_FLAG_VIBRANT) { + if (theme->flags & GLFW_THEME_FLAG_VIBRANT) { name = NSAppearanceNameVibrantDark; } - else if (theme->flags & GLFW_THEME_FLAG_HIGH_CONTRAST) - { - name = NSAppearanceNameAccessibilityHighContrastDarkAqua; - } else { name = NSAppearanceNameDarkAqua;