From 3c7b1c8297377393aa155714f3c7aacda7d2eb4d Mon Sep 17 00:00:00 2001 From: cg2121 Date: Mon, 10 Oct 2022 22:38:32 -0500 Subject: [PATCH] obs-frontend-api: Add theme functions This adds a frontend function to check if a theme is dark and also adds an event for when the theme is changed. --- UI/api-interface.cpp | 5 +++++ UI/obs-frontend-api/obs-frontend-api.cpp | 5 +++++ UI/obs-frontend-api/obs-frontend-api.h | 3 +++ UI/obs-frontend-api/obs-frontend-internal.hpp | 2 ++ UI/window-basic-main.cpp | 3 +++ docs/sphinx/reference-frontend-api.rst | 10 ++++++++++ 6 files changed, 28 insertions(+) diff --git a/UI/api-interface.cpp b/UI/api-interface.cpp index ebc200c73..853ad6a8d 100644 --- a/UI/api-interface.cpp +++ b/UI/api-interface.cpp @@ -642,6 +642,11 @@ struct OBSStudioAPI : obs_frontend_callbacks { return Str(string); } + bool obs_frontend_is_theme_dark(void) override + { + return App()->IsThemeDark(); + } + void on_load(obs_data_t *settings) override { for (size_t i = saveCallbacks.size(); i > 0; i--) { diff --git a/UI/obs-frontend-api/obs-frontend-api.cpp b/UI/obs-frontend-api/obs-frontend-api.cpp index 18ace62c4..51cb7693f 100644 --- a/UI/obs-frontend-api/obs-frontend-api.cpp +++ b/UI/obs-frontend-api/obs-frontend-api.cpp @@ -571,3 +571,8 @@ const char *obs_frontend_get_locale_string(const char *string) return !!callbacks_valid() ? c->obs_frontend_get_locale_string(string) : nullptr; } + +bool obs_frontend_is_theme_dark(void) +{ + return !!callbacks_valid() ? c->obs_frontend_is_theme_dark() : false; +} diff --git a/UI/obs-frontend-api/obs-frontend-api.h b/UI/obs-frontend-api/obs-frontend-api.h index 14c811279..864422982 100644 --- a/UI/obs-frontend-api/obs-frontend-api.h +++ b/UI/obs-frontend-api/obs-frontend-api.h @@ -60,6 +60,7 @@ enum obs_frontend_event { OBS_FRONTEND_EVENT_SCRIPTING_SHUTDOWN, OBS_FRONTEND_EVENT_PROFILE_RENAMED, OBS_FRONTEND_EVENT_SCENE_COLLECTION_RENAMED, + OBS_FRONTEND_EVENT_THEME_CHANGED, }; /* ------------------------------------------------------------------------- */ @@ -228,6 +229,8 @@ EXPORT void obs_frontend_open_source_interaction(obs_source_t *source); EXPORT char *obs_frontend_get_current_record_output_path(void); EXPORT const char *obs_frontend_get_locale_string(const char *string); +EXPORT bool obs_frontend_is_theme_dark(void); + /* ------------------------------------------------------------------------- */ #ifdef __cplusplus diff --git a/UI/obs-frontend-api/obs-frontend-internal.hpp b/UI/obs-frontend-api/obs-frontend-internal.hpp index 3197b4348..8a666cb85 100644 --- a/UI/obs-frontend-api/obs-frontend-internal.hpp +++ b/UI/obs-frontend-api/obs-frontend-internal.hpp @@ -145,6 +145,8 @@ struct obs_frontend_callbacks { virtual char *obs_frontend_get_current_record_output_path(void) = 0; virtual const char * obs_frontend_get_locale_string(const char *string) = 0; + + virtual bool obs_frontend_is_theme_dark(void) = 0; }; EXPORT void diff --git a/UI/window-basic-main.cpp b/UI/window-basic-main.cpp index c53f85b9f..fe92193f5 100644 --- a/UI/window-basic-main.cpp +++ b/UI/window-basic-main.cpp @@ -10316,4 +10316,7 @@ void OBSBasic::ResetProxyStyleSliders() ActivateAudioSource(source); UpdateContextBar(true); + + if (api) + api->on_event(OBS_FRONTEND_EVENT_THEME_CHANGED); } diff --git a/docs/sphinx/reference-frontend-api.rst b/docs/sphinx/reference-frontend-api.rst index 302d1be00..6beada0f2 100644 --- a/docs/sphinx/reference-frontend-api.rst +++ b/docs/sphinx/reference-frontend-api.rst @@ -178,6 +178,10 @@ Structures/Enumerations Triggered when the virtual camera is stopped. + - **OBS_FRONTEND_EVENT_THEME_CHANGED** + + Triggered when the theme is changed. + .. struct:: obs_frontend_source_list @@ -785,3 +789,9 @@ Functions .. function:: const char *obs_frontend_get_locale_string(const char *string) :return: Gets the frontend translation of a given string. + +--------------------------------------- + +.. function:: bool obs_frontend_is_theme_dark(void) + + :return: Checks if the current theme is dark or light.