diff --git a/src/cocoa_init.m b/src/cocoa_init.m index 45708892..e92dcefe 100644 --- a/src/cocoa_init.m +++ b/src/cocoa_init.m @@ -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 diff --git a/src/cocoa_window.m b/src/cocoa_window.m index bb5c465a..a8fa387a 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -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, *)) { diff --git a/src/internal.h b/src/internal.h index 50f4674d..d6d76851 100644 --- a/src/internal.h +++ b/src/internal.h @@ -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 diff --git a/tests/theming.c b/tests/theming.c index 8d22e0b4..41bfdf35 100644 --- a/tests/theming.c +++ b/tests/theming.c @@ -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);