Implement _glfwGetSystemDefaultThemeCocoa.

Fix bug in _glfwGetThemeCocoa always reporting a light theme for system default themes.
This commit is contained in:
ws909 2023-01-30 03:09:39 +01:00
parent d87fc99503
commit afae2b0dfa
4 changed files with 46 additions and 27 deletions

View File

@ -252,6 +252,28 @@ void nsAppearanceToGLFWTheme(NSAppearance* appearance, _GLFWtheme* theme)
theme->variation = GLFW_THEME_LIGHT;
}
static void getSystemTheme(_GLFWtheme* theme)
{
theme->variation = GLFW_THEME_LIGHT;
theme->flags = 0;
if (@available(macOS 10.14, *))
{
// effectiveAppearance is actually not the system appearance, but the application appearance.
// As long as NSApplication.appearance is never set, using the effective appearance is fine
// to get and observe the system appearance.
nsAppearanceToGLFWTheme(NSApp.effectiveAppearance, theme);
NSColor* color = [[NSColor controlAccentColor] colorUsingColorSpace:NSColorSpace.genericRGBColorSpace];
theme->flags |= GLFW_THEME_ATTRIBUTE_HAS_COLOR;
theme->color[0] = color.redComponent;
theme->color[1] = color.greenComponent;
theme->color[2] = color.blueComponent;
theme->color[3] = color.alphaComponent;
}
}
// Create key code translation tables
//
static void createKeyTables(void)
@ -491,22 +513,8 @@ static GLFWbool initializeTIS(void)
// TODO: FIXME: this method is invoked twice when the high contrast setting is edited in the preferences.
_GLFWtheme theme = { GLFW_THEME_LIGHT, 0 };
if (@available(macOS 10.14, *)) {
// effectiveAppearance is actually not the system appearance, but the application appearance.
// As long as NSApplication.appearance is never set, using the effective appearance is fine
// to get and observe the system appearance.
nsAppearanceToGLFWTheme(NSApp.effectiveAppearance, &theme);
NSColor* color = [[NSColor controlAccentColor] colorUsingColorSpace:NSColorSpace.genericRGBColorSpace];
theme.flags |= GLFW_THEME_ATTRIBUTE_HAS_COLOR;
theme.color[0] = color.redComponent;
theme.color[1] = color.greenComponent;
theme.color[2] = color.blueComponent;
theme.color[3] = color.alphaComponent;
}
_GLFWtheme theme;
getSystemTheme(&theme);
_glfwInputSystemTheme(&theme);
@ -778,12 +786,13 @@ int _glfwInitCocoa(void)
context:nil];
*/
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 101400
[NSApp addObserver:_glfw.ns.helper
forKeyPath:@"effectiveAppearance"
options:0
context:nil];
#endif
if (@available(macOS 10.14, *))
{
[NSApp addObserver:_glfw.ns.helper
forKeyPath:@"effectiveAppearance"
options:0
context:nil];
}
if (![[NSRunningApplication currentApplication] isFinishedLaunching])
[NSApp run];
@ -847,8 +856,10 @@ void _glfwTerminateCocoa(void)
_GLFWtheme* _glfwGetSystemDefaultThemeCocoa(void)
{
_glfwInputError(GLFW_FEATURE_UNIMPLEMENTED, NULL);
return NULL; // TODO: implement
_GLFWtheme* theme = &_glfw.theme;
getSystemTheme(theme);
return theme;
}
#endif // _GLFW_COCOA

View File

@ -1935,7 +1935,12 @@ _GLFWtheme* _glfwGetThemeCocoa(_GLFWwindow* window)
if (@available(macOS 10.09, *))
{
nsAppearanceToGLFWTheme([(NSWindow*)window->ns.object appearance], theme);
NSAppearance* appearance = [(NSWindow*)window->ns.object appearance];
if (appearance == NULL)
appearance = [NSApp effectiveAppearance];
nsAppearanceToGLFWTheme(appearance, theme);
}
if (@available(macOS 10.14, *)) {

View File

@ -887,6 +887,9 @@ struct _GLFWlibrary
GLFWjoystickfun joystick;
GLFWthemefun theme;
} callbacks;
// Clients can mutate this theme data at any time
_GLFWtheme theme;
// These are defined in platform.h
GLFW_PLATFORM_LIBRARY_WINDOW_STATE

View File

@ -162,6 +162,8 @@ int main(int argc, char** argv)
fprintf(stderr, "Failed to initialize GLFW\n");
exit(EXIT_FAILURE);
}
print_theme(glfwGetSystemDefaultTheme(), "System theme");
window = glfwCreateWindow(200, 200, "Window Icon", NULL, NULL);
if (!window)
@ -178,8 +180,6 @@ int main(int argc, char** argv)
glfwSetKeyCallback(window, key_callback);
theme = glfwCreateTheme();
glfwThemeSetColor(theme, 0, 0, 0, 0);
set_theme(window, cur_theme_color);
glfwSetSystemThemeCallback(theme_callback);