Merge pull request #1 from ws909/dev-theming

dev-theming merge
This commit is contained in:
Andreas 2023-01-30 03:23:34 +01:00 committed by GitHub
commit e32cb2b2cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 312 additions and 96 deletions

View File

@ -1394,6 +1394,18 @@ typedef struct GLFWwindow GLFWwindow;
*/ */
typedef struct GLFWcursor GLFWcursor; typedef struct GLFWcursor GLFWcursor;
/*! @brief Opaque theme object.
*
* Opaque theme object.
*
* @see @ref theme_object
*
* @since Added in version 3.4.
*
* @ingroup theme
*/
typedef struct GLFWtheme GLFWtheme;
/*! @brief The function pointer type for memory allocation callbacks. /*! @brief The function pointer type for memory allocation callbacks.
* *
* This is the function pointer type for memory allocation callbacks. A memory * This is the function pointer type for memory allocation callbacks. A memory
@ -1964,6 +1976,28 @@ typedef void (* GLFWmonitorfun)(GLFWmonitor* monitor, int event);
*/ */
typedef void (* GLFWjoystickfun)(int jid, int event); typedef void (* GLFWjoystickfun)(int jid, int event);
/*! @brief The function pointer type for system theme callbacks.
*
* This is the function pointer type for system theme callbacks. A system
* theme callback function has the following signature:
* @code
* void function_name(GLFWtheme* theme)
* @endcode
*
* @param[in] theme The new system theme.
*
* @pointer_lifetime The theme is valid until the callback
* function returns.
*
* @sa @ref theme
* @sa @ref glfwSetSystemThemeCallback
*
* @since Added in version 3.4.
*
* @ingroup theme
*/
typedef void (* GLFWthemefun)(GLFWtheme* theme);
/*! @brief Video mode type. /*! @brief Video mode type.
* *
* This describes a single video mode. * This describes a single video mode.
@ -5845,15 +5879,6 @@ GLFWAPI void glfwSetClipboardString(GLFWwindow* window, const char* string);
*/ */
GLFWAPI const char* glfwGetClipboardString(GLFWwindow* window); GLFWAPI const char* glfwGetClipboardString(GLFWwindow* window);
typedef struct GLFWtheme GLFWtheme;
typedef struct _GLFWtheme
{
int variation; // light/dark
int flags;
unsigned char color[4]; // TODO: change to 128 bit (4 floats) to support wider gamuts.
} _GLFWtheme;
GLFWAPI GLFWtheme* glfwCreateTheme(void); GLFWAPI GLFWtheme* glfwCreateTheme(void);
GLFWAPI void glfwDestroyTheme(GLFWtheme* theme); GLFWAPI void glfwDestroyTheme(GLFWtheme* theme);
GLFWAPI void glfwCopyTheme(const GLFWtheme* source, GLFWtheme* target); GLFWAPI void glfwCopyTheme(const GLFWtheme* source, GLFWtheme* target);
@ -5861,34 +5886,167 @@ GLFWAPI void glfwCopyTheme(const GLFWtheme* source, GLFWtheme* target);
GLFWAPI int glfwThemeGetVariation(const GLFWtheme* theme); GLFWAPI int glfwThemeGetVariation(const GLFWtheme* theme);
GLFWAPI void glfwThemeSetVariation(GLFWtheme* theme, int value); GLFWAPI void glfwThemeSetVariation(GLFWtheme* theme, int value);
GLFWAPI int glfwThemeGetFlags(const GLFWtheme* theme); GLFWAPI int glfwThemeGetAttribute(const GLFWtheme* theme, int attribute);
GLFWAPI void glfwThemeSetFlags(GLFWtheme* theme, int value); GLFWAPI void glfwThemeSetAttribute(GLFWtheme* theme, int attribute, int value);
GLFWAPI void glfwThemeGetColor(const GLFWtheme* theme, float* red, float* green, float* blue, float* alpha); GLFWAPI void glfwThemeGetColor(const GLFWtheme* theme, float* red, float* green, float* blue, float* alpha);
GLFWAPI void glfwThemeSetColor(GLFWtheme* theme, float red, float green, float blue, float alpha); GLFWAPI void glfwThemeSetColor(GLFWtheme* theme, float red, float green, float blue, float alpha);
/*! @brief Theme variation type.
*
* Specifies that a theme is of a dark variation.
*
* @ingroup theme
*/
#define GLFW_THEME_DARK -1 #define GLFW_THEME_DARK -1
/*! @brief Theme variation type.
*
* Specifies that a theme has no specific variation.
* This is an invalid value for themes returned by GLFW, but this
* variation can be set to make GLFW use the default system theme when
* applying a user-specified theme.
*
* @ingroup theme
*/
#define GLFW_THEME_DEFAULT 0 #define GLFW_THEME_DEFAULT 0
/*! @brief Theme variation type.
*
* Specifies that a theme is of a light variation.
*
* @ingroup theme
*/
#define GLFW_THEME_LIGHT 1 #define GLFW_THEME_LIGHT 1
#define GLFW_THEME_FLAG_HAS_COLOR 1
#define GLFW_THEME_FLAG_HIGH_CONTRAST 2
#define GLFW_THEME_FLAG_VIBRANT 4
typedef void (* GLFWthemefun)(GLFWtheme* theme); /*! @brief Theme attribute.
/*! @brief Notifies the application when the system default theme changes.
* *
* Specifies that a theme provides a color. If this flag is set on a theme
* returned by GLFW, this theme contains a color. If this flag is set on a
* theme supplied to GLFW, GLFW applies this theme if the platform supports it.
* If the flag is unset, the theme's color is ignored.
*
* @ingroup theme
*/
#define GLFW_THEME_ATTRIBUTE_HAS_COLOR 1
/*! @brief Theme attribute.
*
* Specifies that a theme uses a high contrast mode.
*
* @ingroup theme
*/
#define GLFW_THEME_ATTRIBUTE_HIGH_CONTRAST 2
/*! @brief Theme attribute.
*
* Specifies that a theme is vibrant.
*
* @ingroup theme
*/
#define GLFW_THEME_ATTRIBUTE_VIBRANT 4
/*! @brief Sets the system theme callback.
*
* This function sets the system theme callback, or removes the
* currently set callback. This is called when the system theme changes,
* either because the user edited it, or the system automatically changed it.
*
* @param[in] callback The new callback, or `NULL` to remove the currently set
* callback.
* @return The previously set callback, or `NULL` if no callback was set or the
* library had not been [initialized](@ref intro_init).
*
* @callback_signature
* @code
* void function_name(GLFWtheme* theme)
* @endcode
* For more information about the callback parameters, see the
* [function pointer type](@ref GLFWthemefun).
*
* @errors Possible errors include @ref GLFW_NOT_INITIALIZED.
*
* @thread_safety This function must only be called from the main thread.
*
* @sa @ref theme_event
*
* @since Added in version 3.4.
*
* @ingroup theme
*/ */
GLFWAPI GLFWthemefun glfwSetSystemThemeCallback(GLFWthemefun callback); GLFWAPI GLFWthemefun glfwSetSystemThemeCallback(GLFWthemefun callback);
/*! @brief Sets the theme for a window. /*! @brief Sets the theme for a window.
* *
* @param[in] window The window to set the theme for. * @param[in] window The [window](@ref window) to set the theme for.
* @param[in] theme The theme to set. Pass `NULL` to set it to the system default. * @param[in] theme The [theme](@ref theme) to set. Pass `NULL` to set it to the system default.
*
* @errors Possible errors include @ref GLFW_NOT_INITIALIZED,
* and @ref GLFW_FEATURE_UNIMPLEMENTED.
*
* @pointer_lifetime The returned theme is allocated and freed by GLFW. You
* should not free it yourself. It is valid until this function or @ref glfwGetTheme
* is called again for the specified window, or the window is destroyed.
*
* @thread_safety This function must only be called from the main thread.
*
* @sa @ref theme
* @sa @ref glfwGetTheme
*
* @since Added in version 3.4.
*
* @ingroup theme
*/ */
GLFWAPI void glfwSetTheme(GLFWwindow* handle, const GLFWtheme* theme); GLFWAPI void glfwSetTheme(GLFWwindow* handle, const GLFWtheme* theme);
/*! @brief Returns the currently active system theme.
*
* @param[in] window The [window](@ref window) to retrieve the current theme for.
*
* @return A mutable [theme](@ref theme) object, or `NULL` if an
* [error](@ref error_handling) occurred.
*
* @errors Possible errors include @ref GLFW_NOT_INITIALIZED,
* and @ref GLFW_FEATURE_UNIMPLEMENTED.
*
* @pointer_lifetime The returned theme is allocated and freed by GLFW. You
* should not free it yourself. It is valid until this function or @ref glfwSetTheme
* is called again for the specified window, or the window is destroyed.
*
* @thread_safety This function must only be called from the main thread.
*
* @sa @ref theme
* @sa @ref glfwSetTheme
*
* @since Added in version 3.4.
*
* @ingroup theme
*/
GLFWAPI GLFWtheme* glfwGetTheme(GLFWwindow* handle); GLFWAPI GLFWtheme* glfwGetTheme(GLFWwindow* handle);
/*! @brief Returns the currently active system theme.
*
* @return A mutable [theme](@ref theme) object, or `NULL` if an
* [error](@ref error_handling) occurred.
*
* @errors Possible errors include @ref GLFW_NOT_INITIALIZED,
* and @ref GLFW_FEATURE_UNIMPLEMENTED.
*
* @pointer_lifetime The returned theme is allocated and freed by GLFW. You
* should not free it yourself. It is valid until this function is called
* again, or the library is terminated.
*
* @thread_safety This function must only be called from the main thread.
*
* @sa @ref theme
* @sa @ref glfwSetSystemThemeCallback
* @sa @ref glfwSetTheme
*
* @since Added in version 3.4.
*
* @ingroup theme
*/
GLFWAPI GLFWtheme* glfwGetSystemDefaultTheme(); GLFWAPI GLFWtheme* glfwGetSystemDefaultTheme();
/*! @brief Returns the GLFW time. /*! @brief Returns the GLFW time.

View File

@ -205,13 +205,13 @@ void nsAppearanceToGLFWTheme(NSAppearance* appearance, _GLFWtheme* theme)
if ([name isEqualToString:NSAppearanceNameVibrantLight]) if ([name isEqualToString:NSAppearanceNameVibrantLight])
{ {
theme->variation = GLFW_THEME_LIGHT; theme->variation = GLFW_THEME_LIGHT;
theme->flags |= GLFW_THEME_FLAG_VIBRANT; theme->flags |= GLFW_THEME_ATTRIBUTE_VIBRANT;
return; return;
} }
if ([name isEqualToString:NSAppearanceNameVibrantDark]) if ([name isEqualToString:NSAppearanceNameVibrantDark])
{ {
theme->variation = GLFW_THEME_DARK; theme->variation = GLFW_THEME_DARK;
theme->flags |= GLFW_THEME_FLAG_VIBRANT; theme->flags |= GLFW_THEME_ATTRIBUTE_VIBRANT;
return; return;
} }
} }
@ -226,25 +226,25 @@ void nsAppearanceToGLFWTheme(NSAppearance* appearance, _GLFWtheme* theme)
if ([name isEqualToString:NSAppearanceNameAccessibilityHighContrastAqua]) if ([name isEqualToString:NSAppearanceNameAccessibilityHighContrastAqua])
{ {
theme->variation = GLFW_THEME_LIGHT; theme->variation = GLFW_THEME_LIGHT;
theme->flags |= GLFW_THEME_FLAG_HIGH_CONTRAST; theme->flags |= GLFW_THEME_ATTRIBUTE_HIGH_CONTRAST;
return; return;
} }
if ([name isEqualToString:NSAppearanceNameAccessibilityHighContrastDarkAqua]) if ([name isEqualToString:NSAppearanceNameAccessibilityHighContrastDarkAqua])
{ {
theme->variation = GLFW_THEME_DARK; theme->variation = GLFW_THEME_DARK;
theme->flags |= GLFW_THEME_FLAG_HIGH_CONTRAST; theme->flags |= GLFW_THEME_ATTRIBUTE_HIGH_CONTRAST;
return; return;
} }
if ([name isEqualToString:NSAppearanceNameAccessibilityHighContrastVibrantLight]) if ([name isEqualToString:NSAppearanceNameAccessibilityHighContrastVibrantLight])
{ {
theme->variation = GLFW_THEME_LIGHT; theme->variation = GLFW_THEME_LIGHT;
theme->flags |= GLFW_THEME_FLAG_VIBRANT | GLFW_THEME_FLAG_HIGH_CONTRAST; theme->flags |= GLFW_THEME_ATTRIBUTE_VIBRANT | GLFW_THEME_ATTRIBUTE_HIGH_CONTRAST;
return; return;
} }
if ([name isEqualToString:NSAppearanceNameAccessibilityHighContrastVibrantDark]) if ([name isEqualToString:NSAppearanceNameAccessibilityHighContrastVibrantDark])
{ {
theme->variation = GLFW_THEME_DARK; theme->variation = GLFW_THEME_DARK;
theme->flags |= GLFW_THEME_FLAG_VIBRANT | GLFW_THEME_FLAG_HIGH_CONTRAST; theme->flags |= GLFW_THEME_ATTRIBUTE_VIBRANT | GLFW_THEME_ATTRIBUTE_HIGH_CONTRAST;
return; return;
} }
} }
@ -252,6 +252,28 @@ void nsAppearanceToGLFWTheme(NSAppearance* appearance, _GLFWtheme* theme)
theme->variation = GLFW_THEME_LIGHT; 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 // Create key code translation tables
// //
static void createKeyTables(void) 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. // TODO: FIXME: this method is invoked twice when the high contrast setting is edited in the preferences.
_GLFWtheme theme = { GLFW_THEME_LIGHT, 0 }; _GLFWtheme theme;
getSystemTheme(&theme);
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_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); _glfwInputSystemTheme(&theme);
@ -778,12 +786,13 @@ int _glfwInitCocoa(void)
context:nil]; context:nil];
*/ */
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 101400 if (@available(macOS 10.14, *))
[NSApp addObserver:_glfw.ns.helper {
forKeyPath:@"effectiveAppearance" [NSApp addObserver:_glfw.ns.helper
options:0 forKeyPath:@"effectiveAppearance"
context:nil]; options:0
#endif context:nil];
}
if (![[NSRunningApplication currentApplication] isFinishedLaunching]) if (![[NSRunningApplication currentApplication] isFinishedLaunching])
[NSApp run]; [NSApp run];
@ -847,8 +856,10 @@ void _glfwTerminateCocoa(void)
_GLFWtheme* _glfwGetSystemDefaultThemeCocoa(void) _GLFWtheme* _glfwGetSystemDefaultThemeCocoa(void)
{ {
_glfwInputError(GLFW_FEATURE_UNIMPLEMENTED, NULL); _GLFWtheme* theme = &_glfw.theme;
return NULL; // TODO: implement getSystemTheme(theme);
return theme;
} }
#endif // _GLFW_COCOA #endif // _GLFW_COCOA

View File

@ -1898,7 +1898,7 @@ void _glfwSetThemeCocoa(_GLFWwindow* window, _GLFWtheme* theme)
if (theme->variation == GLFW_THEME_LIGHT) if (theme->variation == GLFW_THEME_LIGHT)
{ {
if (theme->flags & GLFW_THEME_FLAG_VIBRANT) if (theme->flags & GLFW_THEME_ATTRIBUTE_VIBRANT)
{ {
name = NSAppearanceNameVibrantLight; name = NSAppearanceNameVibrantLight;
} }
@ -1909,7 +1909,7 @@ void _glfwSetThemeCocoa(_GLFWwindow* window, _GLFWtheme* theme)
} }
else else
{ {
if (theme->flags & GLFW_THEME_FLAG_VIBRANT) if (theme->flags & GLFW_THEME_ATTRIBUTE_VIBRANT)
{ {
name = NSAppearanceNameVibrantDark; name = NSAppearanceNameVibrantDark;
} }
@ -1935,7 +1935,12 @@ _GLFWtheme* _glfwGetThemeCocoa(_GLFWwindow* window)
if (@available(macOS 10.09, *)) 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, *)) { if (@available(macOS 10.14, *)) {
@ -1943,11 +1948,11 @@ _GLFWtheme* _glfwGetThemeCocoa(_GLFWwindow* window)
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.
theme->flags |= GLFW_THEME_FLAG_HAS_COLOR; theme->flags |= GLFW_THEME_ATTRIBUTE_HAS_COLOR;
theme->color[0] = color.redComponent * 255; theme->color[0] = color.redComponent;
theme->color[1] = color.greenComponent * 255; theme->color[1] = color.greenComponent;
theme->color[2] = color.blueComponent * 255; theme->color[2] = color.blueComponent;
theme->color[3] = color.alphaComponent * 255; theme->color[3] = color.alphaComponent;
} }
return theme; return theme;

View File

@ -72,6 +72,7 @@ typedef struct _GLFWplatform _GLFWplatform;
typedef struct _GLFWlibrary _GLFWlibrary; typedef struct _GLFWlibrary _GLFWlibrary;
typedef struct _GLFWmonitor _GLFWmonitor; typedef struct _GLFWmonitor _GLFWmonitor;
typedef struct _GLFWcursor _GLFWcursor; typedef struct _GLFWcursor _GLFWcursor;
typedef struct _GLFWtheme _GLFWtheme;
typedef struct _GLFWmapelement _GLFWmapelement; typedef struct _GLFWmapelement _GLFWmapelement;
typedef struct _GLFWmapping _GLFWmapping; typedef struct _GLFWmapping _GLFWmapping;
typedef struct _GLFWjoystick _GLFWjoystick; typedef struct _GLFWjoystick _GLFWjoystick;
@ -515,6 +516,20 @@ struct _GLFWcontext
GLFW_PLATFORM_CONTEXT_STATE GLFW_PLATFORM_CONTEXT_STATE
}; };
// Theme structure
//
struct _GLFWtheme
{
// The light/dark variation. If set to DEFAULT, use the system theme variation.
int variation;
int flags;
// When applying a theme, ignore this color if the HAS_COLOR
// flag is unset, and use the system theme color instead.
float color[4];
};
// Window and context structure // Window and context structure
// //
struct _GLFWwindow struct _GLFWwindow
@ -571,7 +586,8 @@ struct _GLFWwindow
GLFWdropfun drop; GLFWdropfun drop;
} callbacks; } callbacks;
_GLFWtheme theme; // TODO: This data is mutable by clients, so remove it. // Clients can mutate this theme data at any time
_GLFWtheme theme;
// This is defined in platform.h // This is defined in platform.h
GLFW_PLATFORM_WINDOW_STATE GLFW_PLATFORM_WINDOW_STATE
@ -872,6 +888,9 @@ struct _GLFWlibrary
GLFWthemefun theme; GLFWthemefun theme;
} callbacks; } callbacks;
// Clients can mutate this theme data at any time
_GLFWtheme theme;
// These are defined in platform.h // These are defined in platform.h
GLFW_PLATFORM_LIBRARY_WINDOW_STATE GLFW_PLATFORM_LIBRARY_WINDOW_STATE
GLFW_PLATFORM_LIBRARY_CONTEXT_STATE GLFW_PLATFORM_LIBRARY_CONTEXT_STATE

View File

@ -1,9 +1,26 @@
//========================================================================
// GLFW 3.4 - www.glfw.org
//------------------------------------------------------------------------
// Copyright (c) 2023 Andreas O. Jansen
// //
// theme.c // This software is provided 'as-is', without any express or implied
// glfw // warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
// //
// Created by Andreas Ormevik Jansen on 28/01/2023. // Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
// //
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
#include "internal.h" #include "internal.h"
@ -48,18 +65,30 @@ GLFWAPI void glfwThemeSetVariation(GLFWtheme* theme, int value)
((_GLFWtheme*) theme)->variation = value; ((_GLFWtheme*) theme)->variation = value;
} }
GLFWAPI int glfwThemeGetFlags(const GLFWtheme* theme) GLFWAPI int glfwThemeGetAttribute(const GLFWtheme* theme, int attribute)
{ {
assert(theme != NULL); assert(theme != NULL);
return ((_GLFWtheme*) theme)->flags; assert((attribute & ~(GLFW_THEME_ATTRIBUTE_HAS_COLOR |
GLFW_THEME_ATTRIBUTE_HIGH_CONTRAST |
GLFW_THEME_ATTRIBUTE_VIBRANT)) == 0);
return ((_GLFWtheme*) theme)->flags & attribute ? GLFW_TRUE : GLFW_FALSE;
} }
GLFWAPI void glfwThemeSetFlags(GLFWtheme* theme, int value) GLFWAPI void glfwThemeSetAttribute(GLFWtheme* theme, int attribute, int value)
{ {
assert(theme != NULL); assert(theme != NULL);
assert((value & ~(GLFW_THEME_FLAG_HAS_COLOR | GLFW_THEME_FLAG_HIGH_CONTRAST | GLFW_THEME_FLAG_VIBRANT)) == 0); assert(value == GLFW_TRUE || value == GLFW_FALSE);
assert((attribute & ~(GLFW_THEME_ATTRIBUTE_HAS_COLOR |
GLFW_THEME_ATTRIBUTE_HIGH_CONTRAST |
GLFW_THEME_ATTRIBUTE_VIBRANT)) == 0);
((_GLFWtheme*) theme)->flags = value; _GLFWtheme* _theme = (_GLFWtheme*) theme;
if (value == GLFW_TRUE)
_theme->flags |= attribute;
else
_theme->flags &= ~attribute;
} }
GLFWAPI void glfwThemeGetColor(const GLFWtheme* theme, float* red, float* green, float* blue, float* alpha) GLFWAPI void glfwThemeGetColor(const GLFWtheme* theme, float* red, float* green, float* blue, float* alpha)
@ -67,22 +96,22 @@ GLFWAPI void glfwThemeGetColor(const GLFWtheme* theme, float* red, float* green,
assert(theme != NULL); assert(theme != NULL);
assert(red != NULL && green != NULL && blue != NULL && alpha != NULL); assert(red != NULL && green != NULL && blue != NULL && alpha != NULL);
const _GLFWtheme* iTheme = ((_GLFWtheme*) theme); const float* color = ((_GLFWtheme*) theme)->color;
*red = iTheme->color[0] / 255.0f; *red = color[0];
*green = iTheme->color[1] / 255.0f; *green = color[1];
*blue = iTheme->color[2] / 255.0f; *blue = color[2];
*alpha = iTheme->color[3] / 255.0f; *alpha = color[3];
} }
GLFWAPI void glfwThemeSetColor(GLFWtheme* theme, float red, float green, float blue, float alpha) GLFWAPI void glfwThemeSetColor(GLFWtheme* theme, float red, float green, float blue, float alpha)
{ {
assert(theme != NULL); assert(theme != NULL);
_GLFWtheme* iTheme = ((_GLFWtheme*) theme); float* color = ((_GLFWtheme*) theme)->color;
iTheme->color[0] = red * 255.0f; color[0] = red;
iTheme->color[1] = green * 255.0f; color[1] = green;
iTheme->color[2] = blue * 255.0f; color[2] = blue;
iTheme->color[3] = alpha * 255.0f; color[3] = alpha;
} }

View File

@ -52,26 +52,25 @@ static GLFWtheme* theme;
static void print_theme(GLFWtheme* theme, const char* title) static void print_theme(GLFWtheme* theme, const char* title)
{ {
const int flags = glfwThemeGetFlags(theme);
int n = 0; int n = 0;
printf("%s: {\n", title); printf("%s: {\n", title);
printf(" Base: %s\n", glfwThemeGetVariation(theme) == GLFW_THEME_LIGHT ? "light" : "dark"); printf(" Base: %s\n", glfwThemeGetVariation(theme) == GLFW_THEME_LIGHT ? "light" : "dark");
printf(" Flags: ["); printf(" Flags: [");
if (flags & GLFW_THEME_FLAG_HAS_COLOR) if (glfwThemeGetAttribute(theme, GLFW_THEME_ATTRIBUTE_HAS_COLOR))
{ {
printf(n++ > 0 ? ", %s" : "%s", "HAS_COLOR"); printf(n++ > 0 ? ", %s" : "%s", "HAS_COLOR");
} }
if (flags & GLFW_THEME_FLAG_VIBRANT) if (glfwThemeGetAttribute(theme, GLFW_THEME_ATTRIBUTE_VIBRANT))
{ {
printf(n++ > 0 ? ", %s" : "%s", "VIBRANT"); printf(n++ > 0 ? ", %s" : "%s", "VIBRANT");
} }
if (flags & GLFW_THEME_FLAG_HIGH_CONTRAST) if (glfwThemeGetAttribute(theme, GLFW_THEME_ATTRIBUTE_HIGH_CONTRAST))
{ {
printf(n++ > 0 ? ", %s" : "%s", "HIGH_CONTRAST"); printf(n++ > 0 ? ", %s" : "%s", "HIGH_CONTRAST");
} }
printf("]\n"); printf("]\n");
if (flags & GLFW_THEME_FLAG_HAS_COLOR) if (glfwThemeGetAttribute(theme, GLFW_THEME_ATTRIBUTE_HAS_COLOR))
{ {
float r, g, b, a; float r, g, b, a;
glfwThemeGetColor(theme, &r, &g, &b, &a); glfwThemeGetColor(theme, &r, &g, &b, &a);
@ -91,12 +90,9 @@ static void set_theme(GLFWwindow* window, int theme_color)
); );
if (theme_color == 6) if (theme_color == 6)
{ glfwThemeSetAttribute(theme, GLFW_THEME_ATTRIBUTE_HAS_COLOR, GLFW_FALSE);
glfwThemeSetFlags(theme, glfwThemeGetFlags(theme) & ~GLFW_THEME_FLAG_HAS_COLOR); else
} else glfwThemeSetAttribute(theme, GLFW_THEME_ATTRIBUTE_HAS_COLOR, GLFW_TRUE);
{
glfwThemeSetFlags(theme, glfwThemeGetFlags(theme) | GLFW_THEME_FLAG_HAS_COLOR);
}
const char* title; const char* title;
@ -167,6 +163,8 @@ int main(int argc, char** argv)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
print_theme(glfwGetSystemDefaultTheme(), "System theme");
window = glfwCreateWindow(200, 200, "Window Icon", NULL, NULL); window = glfwCreateWindow(200, 200, "Window Icon", NULL, NULL);
if (!window) if (!window)
{ {
@ -182,10 +180,6 @@ int main(int argc, char** argv)
glfwSetKeyCallback(window, key_callback); glfwSetKeyCallback(window, key_callback);
theme = glfwCreateTheme(); theme = glfwCreateTheme();
glfwThemeSetVariation(theme, GLFW_THEME_DEFAULT);
glfwThemeSetFlags(theme, 0);
glfwThemeSetColor(theme, 0, 0, 0, 0);
set_theme(window, cur_theme_color); set_theme(window, cur_theme_color);
glfwSetSystemThemeCallback(theme_callback); glfwSetSystemThemeCallback(theme_callback);
@ -193,10 +187,10 @@ int main(int argc, char** argv)
while (!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window))
{ {
glClearColor( glClearColor(
theme_colors[cur_theme_color][0] / 255.0f, theme_colors[cur_theme_color][0],
theme_colors[cur_theme_color][1] / 255.0f, theme_colors[cur_theme_color][1],
theme_colors[cur_theme_color][2] / 255.0f, theme_colors[cur_theme_color][2],
theme_colors[cur_theme_color][3] / 255.0f theme_colors[cur_theme_color][3]
); );
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window); glfwSwapBuffers(window);