Conditionally execute theming based on OS version

This commit is contained in:
ws909 2023-01-26 06:38:27 +01:00
parent 2db9a368f7
commit fe1eddc2ad
2 changed files with 59 additions and 33 deletions

View File

@ -190,7 +190,11 @@ static void createMenuBar(void)
void nsAppearanceToGLFWTheme(NSAppearance* appearance, GLFWtheme* theme) void nsAppearanceToGLFWTheme(NSAppearance* appearance, GLFWtheme* theme)
{ {
NSAppearanceName name = [appearance bestMatchFromAppearancesWithNames:@[ NSAppearanceName name;
if (@available(macOS 10.14, *))
{
name = [appearance bestMatchFromAppearancesWithNames:@[
NSAppearanceNameAqua, NSAppearanceNameAqua,
NSAppearanceNameDarkAqua, NSAppearanceNameDarkAqua,
NSAppearanceNameVibrantLight, NSAppearanceNameVibrantLight,
@ -200,6 +204,9 @@ void nsAppearanceToGLFWTheme(NSAppearance* appearance, GLFWtheme* theme)
NSAppearanceNameAccessibilityHighContrastVibrantLight, NSAppearanceNameAccessibilityHighContrastVibrantLight,
NSAppearanceNameAccessibilityHighContrastVibrantDark NSAppearanceNameAccessibilityHighContrastVibrantDark
]]; ]];
} else {
name = appearance.name;
}
if ([name isEqualToString:NSAppearanceNameAqua]) if ([name isEqualToString:NSAppearanceNameAqua])
{ {
@ -480,7 +487,9 @@ static GLFWbool initializeTIS(void)
// TODO: FIXME: this method is invoked twice when the high contrast setting is edited in the preferences. // TODO: FIXME: this method is invoked twice when the high contrast setting is edited in the preferences.
GLFWtheme theme = { 0, 0 }; GLFWtheme theme = { GLFW_BASE_THEME_LIGHT, 0 };
if (@available(macOS 10.14, *)) {
nsAppearanceToGLFWTheme(NSApp.effectiveAppearance, &theme); nsAppearanceToGLFWTheme(NSApp.effectiveAppearance, &theme);
NSColor* color = [[NSColor controlAccentColor] colorUsingColorSpace:NSColorSpace.genericRGBColorSpace]; NSColor* color = [[NSColor controlAccentColor] colorUsingColorSpace:NSColorSpace.genericRGBColorSpace];
@ -490,6 +499,7 @@ static GLFWbool initializeTIS(void)
theme.color[1] = color.greenComponent * 255; theme.color[1] = color.greenComponent * 255;
theme.color[2] = color.blueComponent * 255; theme.color[2] = color.blueComponent * 255;
theme.color[3] = color.alphaComponent * 255; theme.color[3] = color.alphaComponent * 255;
}
_glfwInputSystemTheme(&theme); _glfwInputSystemTheme(&theme);

View File

@ -1883,6 +1883,12 @@ void _glfwSetThemeCocoa(_GLFWwindow* window, GLFWtheme* theme)
[window->ns.object setAppearance:nil]; [window->ns.object setAppearance:nil];
return; return;
} }
if (@available(macOS 10.10, *)) {} else
{
return;
}
// TODO: support color // TODO: support color
// TODO: fix vibrancy // TODO: fix vibrancy
@ -1892,7 +1898,8 @@ void _glfwSetThemeCocoa(_GLFWwindow* window, GLFWtheme* theme)
if (theme->baseTheme == GLFW_BASE_THEME_LIGHT) if (theme->baseTheme == GLFW_BASE_THEME_LIGHT)
{ {
if (theme->flags & GLFW_THEME_FLAG_VIBRANT) { if (theme->flags & GLFW_THEME_FLAG_VIBRANT)
{
name = NSAppearanceNameVibrantLight; name = NSAppearanceNameVibrantLight;
} }
else else
@ -1902,12 +1909,16 @@ void _glfwSetThemeCocoa(_GLFWwindow* window, GLFWtheme* theme)
} }
else else
{ {
if (theme->flags & GLFW_THEME_FLAG_VIBRANT) { if (theme->flags & GLFW_THEME_FLAG_VIBRANT)
{
name = NSAppearanceNameVibrantDark; name = NSAppearanceNameVibrantDark;
} }
else else if (@available(macOS 10.14, *))
{ {
name = NSAppearanceNameDarkAqua; name = NSAppearanceNameDarkAqua;
} else
{
name = NSAppearanceNameAqua;
} }
} }
@ -1919,11 +1930,15 @@ GLFWtheme* _glfwGetThemeCocoa(_GLFWwindow* window)
{ {
GLFWtheme* theme = &window->theme; GLFWtheme* theme = &window->theme;
theme->baseTheme = 0; theme->baseTheme = GLFW_BASE_THEME_LIGHT;
theme->flags = 0; theme->flags = 0;
if (@available(macOS 10.09, *))
{
nsAppearanceToGLFWTheme([window->ns.object appearance], theme); nsAppearanceToGLFWTheme([window->ns.object appearance], theme);
}
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. // 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]; NSColor* color = [[NSColor controlAccentColor] colorUsingColorSpace:NSColorSpace.genericRGBColorSpace];
// TODO: Cannot use the accent color directly, for window themes, because the accent color is never overridden. // TODO: Cannot use the accent color directly, for window themes, because the accent color is never overridden.
@ -1933,6 +1948,7 @@ GLFWtheme* _glfwGetThemeCocoa(_GLFWwindow* window)
theme->color[1] = color.greenComponent * 255; theme->color[1] = color.greenComponent * 255;
theme->color[2] = color.blueComponent * 255; theme->color[2] = color.blueComponent * 255;
theme->color[3] = color.alphaComponent * 255; theme->color[3] = color.alphaComponent * 255;
}
return theme; return theme;
} }