From fe1eddc2ad8a8dc38d2701b8b1c41eb30203e0d7 Mon Sep 17 00:00:00 2001 From: ws909 <37029098+ws909@users.noreply.github.com> Date: Thu, 26 Jan 2023 06:38:27 +0100 Subject: [PATCH] Conditionally execute theming based on OS version --- src/cocoa_init.m | 48 ++++++++++++++++++++++++++++------------------ src/cocoa_window.m | 44 ++++++++++++++++++++++++++++-------------- 2 files changed, 59 insertions(+), 33 deletions(-) diff --git a/src/cocoa_init.m b/src/cocoa_init.m index 9b52b9b4..213e41b1 100644 --- a/src/cocoa_init.m +++ b/src/cocoa_init.m @@ -190,16 +190,23 @@ static void createMenuBar(void) void nsAppearanceToGLFWTheme(NSAppearance* appearance, GLFWtheme* theme) { - NSAppearanceName name = [appearance bestMatchFromAppearancesWithNames:@[ - NSAppearanceNameAqua, - NSAppearanceNameDarkAqua, - NSAppearanceNameVibrantLight, - NSAppearanceNameVibrantDark, - NSAppearanceNameAccessibilityHighContrastAqua, - NSAppearanceNameAccessibilityHighContrastDarkAqua, - NSAppearanceNameAccessibilityHighContrastVibrantLight, - NSAppearanceNameAccessibilityHighContrastVibrantDark - ]]; + NSAppearanceName name; + + if (@available(macOS 10.14, *)) + { + name = [appearance bestMatchFromAppearancesWithNames:@[ + NSAppearanceNameAqua, + NSAppearanceNameDarkAqua, + NSAppearanceNameVibrantLight, + NSAppearanceNameVibrantDark, + NSAppearanceNameAccessibilityHighContrastAqua, + NSAppearanceNameAccessibilityHighContrastDarkAqua, + NSAppearanceNameAccessibilityHighContrastVibrantLight, + NSAppearanceNameAccessibilityHighContrastVibrantDark + ]]; + } else { + name = appearance.name; + } if ([name isEqualToString:NSAppearanceNameAqua]) { @@ -480,16 +487,19 @@ static GLFWbool initializeTIS(void) // 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); + GLFWtheme theme = { GLFW_BASE_THEME_LIGHT, 0 }; - NSColor* color = [[NSColor controlAccentColor] colorUsingColorSpace:NSColorSpace.genericRGBColorSpace]; - - theme.flags |= GLFW_THEME_FLAG_HAS_COLOR; - theme.color[0] = color.redComponent * 255; - theme.color[1] = color.greenComponent * 255; - theme.color[2] = color.blueComponent * 255; - theme.color[3] = color.alphaComponent * 255; + if (@available(macOS 10.14, *)) { + nsAppearanceToGLFWTheme(NSApp.effectiveAppearance, &theme); + + NSColor* color = [[NSColor controlAccentColor] colorUsingColorSpace:NSColorSpace.genericRGBColorSpace]; + + theme.flags |= GLFW_THEME_FLAG_HAS_COLOR; + theme.color[0] = color.redComponent * 255; + theme.color[1] = color.greenComponent * 255; + theme.color[2] = color.blueComponent * 255; + theme.color[3] = color.alphaComponent * 255; + } _glfwInputSystemTheme(&theme); diff --git a/src/cocoa_window.m b/src/cocoa_window.m index e4965d89..16bd5eb3 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -1883,6 +1883,12 @@ void _glfwSetThemeCocoa(_GLFWwindow* window, GLFWtheme* theme) [window->ns.object setAppearance:nil]; return; } + + if (@available(macOS 10.10, *)) {} else + { + return; + } + // TODO: support color // TODO: fix vibrancy @@ -1892,7 +1898,8 @@ void _glfwSetThemeCocoa(_GLFWwindow* window, GLFWtheme* theme) if (theme->baseTheme == GLFW_BASE_THEME_LIGHT) { - if (theme->flags & GLFW_THEME_FLAG_VIBRANT) { + if (theme->flags & GLFW_THEME_FLAG_VIBRANT) + { name = NSAppearanceNameVibrantLight; } else @@ -1902,12 +1909,16 @@ void _glfwSetThemeCocoa(_GLFWwindow* window, GLFWtheme* theme) } else { - if (theme->flags & GLFW_THEME_FLAG_VIBRANT) { + if (theme->flags & GLFW_THEME_FLAG_VIBRANT) + { name = NSAppearanceNameVibrantDark; } - else + else if (@available(macOS 10.14, *)) { name = NSAppearanceNameDarkAqua; + } else + { + name = NSAppearanceNameAqua; } } @@ -1919,20 +1930,25 @@ GLFWtheme* _glfwGetThemeCocoa(_GLFWwindow* window) { GLFWtheme* theme = &window->theme; - theme->baseTheme = 0; + theme->baseTheme = GLFW_BASE_THEME_LIGHT; theme->flags = 0; - nsAppearanceToGLFWTheme([window->ns.object appearance], theme); + if (@available(macOS 10.09, *)) + { + nsAppearanceToGLFWTheme([window->ns.object appearance], theme); + } - // TODO: this is not settable. Is there any reason in overriding a similar value? Does it apply to menu item highlights? If yes, then it must be overridden. - NSColor* color = [[NSColor controlAccentColor] colorUsingColorSpace:NSColorSpace.genericRGBColorSpace]; - // TODO: Cannot use the accent color directly, for window themes, because the accent color is never overridden. - - theme->flags |= GLFW_THEME_FLAG_HAS_COLOR; - theme->color[0] = color.redComponent * 255; - theme->color[1] = color.greenComponent * 255; - theme->color[2] = color.blueComponent * 255; - theme->color[3] = color.alphaComponent * 255; + if (@available(macOS 10.14, *)) { + // TODO: this is not settable. Is there any reason in overriding a similar value? Does it apply to menu item highlights? If yes, then it must be overridden. + NSColor* color = [[NSColor controlAccentColor] colorUsingColorSpace:NSColorSpace.genericRGBColorSpace]; + // TODO: Cannot use the accent color directly, for window themes, because the accent color is never overridden. + + theme->flags |= GLFW_THEME_FLAG_HAS_COLOR; + theme->color[0] = color.redComponent * 255; + theme->color[1] = color.greenComponent * 255; + theme->color[2] = color.blueComponent * 255; + theme->color[3] = color.alphaComponent * 255; + } return theme; }