summaryrefslogtreecommitdiff
path: root/src/lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua')
-rw-r--r--src/lua/CMakeLists.txt4
-rw-r--r--src/lua/bridge.cpp2
-rw-r--r--src/lua/include/lua_theme.hpp15
-rw-r--r--src/lua/lua_theme.cpp89
4 files changed, 108 insertions, 2 deletions
diff --git a/src/lua/CMakeLists.txt b/src/lua/CMakeLists.txt
index cee738bd..0240a50c 100644
--- a/src/lua/CMakeLists.txt
+++ b/src/lua/CMakeLists.txt
@@ -3,8 +3,8 @@
# SPDX-License-Identifier: GPL-3.0-only
idf_component_register(
- SRCS "lua_thread.cpp" "bridge.cpp" "property.cpp" "lua_database.cpp"
- "lua_queue.cpp" "lua_version.cpp" "lua_controls.cpp" "registry.cpp"
+ SRCS "lua_theme.cpp" "lua_thread.cpp" "bridge.cpp" "property.cpp" "lua_database.cpp"
+ "lua_queue.cpp" "lua_version.cpp" "lua_theme.cpp" "lua_controls.cpp" "registry.cpp"
"lua_screen.cpp"
INCLUDE_DIRS "include"
REQUIRES "drivers" "lvgl" "tinyfsm" "events" "system_fsm" "database"
diff --git a/src/lua/bridge.cpp b/src/lua/bridge.cpp
index 2bef1c30..cfa9d5f7 100644
--- a/src/lua/bridge.cpp
+++ b/src/lua/bridge.cpp
@@ -21,6 +21,7 @@
#include "lua_queue.hpp"
#include "lua_screen.hpp"
#include "lua_version.hpp"
+#include "lua_theme.hpp"
#include "lvgl.h"
#include "font/lv_font_loader.h"
@@ -85,6 +86,7 @@ auto Bridge::installBaseModules(lua_State* L) -> void {
RegisterDatabaseModule(L);
RegisterQueueModule(L);
RegisterVersionModule(L);
+ RegisterThemeModule(L);
RegisterScreenModule(L);
}
diff --git a/src/lua/include/lua_theme.hpp b/src/lua/include/lua_theme.hpp
new file mode 100644
index 00000000..fed710e0
--- /dev/null
+++ b/src/lua/include/lua_theme.hpp
@@ -0,0 +1,15 @@
+/*
+ * Copyright 2024 ailurux <ailuruxx@gmail.com>
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+#pragma once
+
+#include "lua.hpp"
+
+namespace lua {
+
+auto RegisterThemeModule(lua_State*) -> void;
+
+} // namespace lua
diff --git a/src/lua/lua_theme.cpp b/src/lua/lua_theme.cpp
new file mode 100644
index 00000000..72434d97
--- /dev/null
+++ b/src/lua/lua_theme.cpp
@@ -0,0 +1,89 @@
+
+/*
+ * Copyright 2023 ailurux <ailuruxx@gmail.com>
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+#include "lua_version.hpp"
+
+#include <string>
+
+#include "bridge.hpp"
+#include "lua.hpp"
+
+#include "esp_app_desc.h"
+#include "esp_log.h"
+#include "lauxlib.h"
+#include "lua.h"
+#include "lua_thread.hpp"
+#include "luavgl.h"
+#include "themes.hpp"
+
+namespace lua {
+
+static auto set_style(lua_State* L) -> int {
+ // Get the object and class name from the stack
+ std::string class_name = luaL_checkstring(L, -1);
+ lv_obj_t* obj = luavgl_to_obj(L, -2);
+ if (obj != NULL) {
+ ui::themes::Theme::instance()->ApplyStyle(obj, class_name);
+ }
+ return 0;
+}
+
+static auto set_theme(lua_State* L) -> int {
+ std::string class_name;
+ luaL_checktype(L, -1, LUA_TTABLE);
+ lua_pushnil(L); /* first key */
+ while (lua_next(L, -2) != 0) {
+ /* uses 'key' (at index -2) and 'value' (at index -1) */
+ if (lua_type(L, -2) == LUA_TSTRING) {
+ class_name = lua_tostring(L, -2);
+ }
+ if (lua_type(L, -1) == LUA_TTABLE) {
+ // Nesting
+ lua_pushnil(L); // First key
+ while (lua_next(L, -2) != 0) {
+ // Nesting the second
+ int selector = -1;
+ lua_pushnil(L); // First key
+ while (lua_next(L, -2) != 0) {
+ int idx = lua_tointeger(L, -2);
+ if (idx == 1) {
+ // Selector
+ selector = lua_tointeger(L, -1);
+ } else if (idx == 2) {
+ // Style
+ lv_style_t* style = luavgl_to_style(L, -1);
+ if (style == NULL) {
+ ESP_LOGI("lua_theme", "Style was null or malformed");
+ return 0;
+ } else {
+ ui::themes::Theme::instance()->AddStyle(class_name, selector, style);
+ }
+ }
+ lua_pop(L, 1);
+ }
+ lua_pop(L, 1);
+ }
+ }
+ /* removes 'value'; keeps 'key' for next iteration */
+ lua_pop(L, 1);
+ }
+ return 0;
+}
+
+static const struct luaL_Reg kThemeFuncs[] = {{"set", set_theme}, {"set_style", set_style}, {NULL, NULL}};
+
+static auto lua_theme(lua_State* L) -> int {
+ luaL_newlib(L, kThemeFuncs);
+ return 1;
+}
+
+auto RegisterThemeModule(lua_State* L) -> void {
+ luaL_requiref(L, "theme", lua_theme, true);
+ lua_pop(L, 1);
+}
+
+} // namespace lua