diff --git a/src/modules/event/sdl/Event.cpp b/src/modules/event/sdl/Event.cpp index fa66d3b78..2c4bb5beb 100644 --- a/src/modules/event/sdl/Event.cpp +++ b/src/modules/event/sdl/Event.cpp @@ -573,6 +573,9 @@ Message *Event::convert(const SDL_Event &e) case SDL_EVENT_LOCALE_CHANGED: msg = new Message("localechanged"); break; + case SDL_EVENT_SYSTEM_THEME_CHANGED: + msg = new Message("themechanged"); + break; case SDL_EVENT_SENSOR_UPDATE: sensorInstance = Module::getInstance(M_SENSOR); if (sensorInstance) diff --git a/src/modules/love/callbacks.lua b/src/modules/love/callbacks.lua index cfeca41c8..4ccc5824a 100644 --- a/src/modules/love/callbacks.lua +++ b/src/modules/love/callbacks.lua @@ -150,6 +150,9 @@ function love.createhandlers() sensorupdated = function (sensorType, x, y, z) if love.sensorupdated then return love.sensorupdated(sensorType, x, y, z) end end, + themechanged = function () + if love.themechanged then return love.themechanged() end + end, }, { __index = function(self, name) error("Unknown event: " .. name) diff --git a/src/modules/window/Window.cpp b/src/modules/window/Window.cpp index 15ca0ea08..57c36504c 100644 --- a/src/modules/window/Window.cpp +++ b/src/modules/window/Window.cpp @@ -112,5 +112,14 @@ STRINGMAP_CLASS_BEGIN(Window, Window::DisplayOrientation, Window::ORIENTATION_MA } STRINGMAP_CLASS_END(Window, Window::DisplayOrientation, Window::ORIENTATION_MAX_ENUM, orientation) +STRINGMAP_CLASS_BEGIN(Window, Window::SystemTheme, Window::THEME_MAX_ENUM, systemThemes) +{ + {"unknown", Window::THEME_UNKNOWN}, + {"light", Window::THEME_LIGHT}, + {"dark", Window::THEME_DARK} +}; +STRINGMAP_CLASS_END(Window, Window::SystemTheme, Window::THEME_MAX_ENUM, systemThemes) + + } // window } // love diff --git a/src/modules/window/Window.h b/src/modules/window/Window.h index 724986350..71f5e190d 100644 --- a/src/modules/window/Window.h +++ b/src/modules/window/Window.h @@ -114,6 +114,14 @@ public: ORIENTATION_MAX_ENUM }; + enum SystemTheme + { + THEME_UNKNOWN, + THEME_LIGHT, + THEME_DARK, + THEME_MAX_ENUM + }; + struct WindowSize { int width; @@ -252,11 +260,19 @@ public: virtual void requestAttention(bool continuous) = 0; + /** + * Get information about the system theme. + * + * @return System theme, which can be either light, dark, or unknown. + **/ + virtual SystemTheme getSystemTheme() const = 0; + STRINGMAP_CLASS_DECLARE(Setting); STRINGMAP_CLASS_DECLARE(FullscreenType); STRINGMAP_CLASS_DECLARE(MessageBoxType); STRINGMAP_CLASS_DECLARE(FileDialogType); STRINGMAP_CLASS_DECLARE(DisplayOrientation); + STRINGMAP_CLASS_DECLARE(SystemTheme); protected: diff --git a/src/modules/window/sdl/Window.cpp b/src/modules/window/sdl/Window.cpp index 07bbb80e3..36fb75f01 100644 --- a/src/modules/window/sdl/Window.cpp +++ b/src/modules/window/sdl/Window.cpp @@ -1655,6 +1655,25 @@ void Window::requestAttention(bool continuous) // TODO: Linux? } +Window::SystemTheme Window::getSystemTheme() const +{ + SDL_SystemTheme sdlstate = SDL_GetSystemTheme(); + SystemTheme state = THEME_UNKNOWN; + systemThemes.find(sdlstate, state); + + return state; +} + +EnumMap::Entry Window::systemThemeEntries[] = +{ + {Window::THEME_UNKNOWN, SDL_SYSTEM_THEME_UNKNOWN}, + {Window::THEME_LIGHT, SDL_SYSTEM_THEME_LIGHT}, + {Window::THEME_DARK, SDL_SYSTEM_THEME_DARK}, +}; + +EnumMap Window::systemThemes(Window::systemThemeEntries, sizeof(Window::systemThemeEntries)); + + } // sdl } // window } // love diff --git a/src/modules/window/sdl/Window.h b/src/modules/window/sdl/Window.h index 5eb9bd104..66378d92a 100644 --- a/src/modules/window/sdl/Window.h +++ b/src/modules/window/sdl/Window.h @@ -24,6 +24,7 @@ // LOVE #include "window/Window.h" #include "common/config.h" +#include "common/EnumMap.h" #include "graphics/Graphics.h" // SDL @@ -133,6 +134,8 @@ public: void requestAttention(bool continuous) override; + SystemTheme getSystemTheme() const override; + void handleSDLEvent(const SDL_Event &event); private: @@ -191,6 +194,9 @@ private: Uint32 dialogEventId; + static EnumMap::Entry systemThemeEntries[]; + static EnumMap systemThemes; + }; // Window } // sdl diff --git a/src/modules/window/wrap_Window.cpp b/src/modules/window/wrap_Window.cpp index 687cbdc73..17eefff14 100644 --- a/src/modules/window/wrap_Window.cpp +++ b/src/modules/window/wrap_Window.cpp @@ -765,6 +765,15 @@ int w_getPointer(lua_State *L) return 1; } +int w_getSystemTheme(lua_State *L) +{ + Window::SystemTheme theme = instance()->getSystemTheme(); + const char *str = "unknown"; + Window::getConstant(theme, str); + lua_pushstring(L, str); + return 1; +} + static const luaL_Reg functions[] = { { "getDisplayCount", w_getDisplayCount }, @@ -809,6 +818,7 @@ static const luaL_Reg functions[] = { "showFileDialog", w_showFileDialog }, { "requestAttention", w_requestAttention }, { "getPointer", w_getPointer }, + { "getSystemTheme", w_getSystemTheme }, { 0, 0 } };