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,16 +190,23 @@ static void createMenuBar(void)
void nsAppearanceToGLFWTheme(NSAppearance* appearance, GLFWtheme* theme) void nsAppearanceToGLFWTheme(NSAppearance* appearance, GLFWtheme* theme)
{ {
NSAppearanceName name = [appearance bestMatchFromAppearancesWithNames:@[ NSAppearanceName name;
NSAppearanceNameAqua,
NSAppearanceNameDarkAqua, if (@available(macOS 10.14, *))
NSAppearanceNameVibrantLight, {
NSAppearanceNameVibrantDark, name = [appearance bestMatchFromAppearancesWithNames:@[
NSAppearanceNameAccessibilityHighContrastAqua, NSAppearanceNameAqua,
NSAppearanceNameAccessibilityHighContrastDarkAqua, NSAppearanceNameDarkAqua,
NSAppearanceNameAccessibilityHighContrastVibrantLight, NSAppearanceNameVibrantLight,
NSAppearanceNameAccessibilityHighContrastVibrantDark NSAppearanceNameVibrantDark,
]]; NSAppearanceNameAccessibilityHighContrastAqua,
NSAppearanceNameAccessibilityHighContrastDarkAqua,
NSAppearanceNameAccessibilityHighContrastVibrantLight,
NSAppearanceNameAccessibilityHighContrastVibrantDark
]];
} else {
name = appearance.name;
}
if ([name isEqualToString:NSAppearanceNameAqua]) 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. // 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 };
nsAppearanceToGLFWTheme(NSApp.effectiveAppearance, &theme);
NSColor* color = [[NSColor controlAccentColor] colorUsingColorSpace:NSColorSpace.genericRGBColorSpace]; if (@available(macOS 10.14, *)) {
nsAppearanceToGLFWTheme(NSApp.effectiveAppearance, &theme);
theme.flags |= GLFW_THEME_FLAG_HAS_COLOR;
theme.color[0] = color.redComponent * 255; NSColor* color = [[NSColor controlAccentColor] colorUsingColorSpace:NSColorSpace.genericRGBColorSpace];
theme.color[1] = color.greenComponent * 255;
theme.color[2] = color.blueComponent * 255; theme.flags |= GLFW_THEME_FLAG_HAS_COLOR;
theme.color[3] = color.alphaComponent * 255; 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); _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,20 +1930,25 @@ 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;
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. if (@available(macOS 10.14, *)) {
NSColor* color = [[NSColor controlAccentColor] colorUsingColorSpace:NSColorSpace.genericRGBColorSpace]; // 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: Cannot use the accent color directly, for window themes, because the accent color is never 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->flags |= GLFW_THEME_FLAG_HAS_COLOR;
theme->color[1] = color.greenComponent * 255; theme->color[0] = color.redComponent * 255;
theme->color[2] = color.blueComponent * 255; theme->color[1] = color.greenComponent * 255;
theme->color[3] = color.alphaComponent * 255; theme->color[2] = color.blueComponent * 255;
theme->color[3] = color.alphaComponent * 255;
}
return theme; return theme;
} }