From 7cdcd44e0ca10ebdc796638190ed1d9b45d99ef0 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Mon, 15 Jan 2024 12:31:20 +1100 Subject: Begin migration of remaining screens to Lua --- lib/luavgl/src/widgets/switch.c | 57 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 lib/luavgl/src/widgets/switch.c (limited to 'lib/luavgl/src/widgets/switch.c') diff --git a/lib/luavgl/src/widgets/switch.c b/lib/luavgl/src/widgets/switch.c new file mode 100644 index 00000000..e66f620e --- /dev/null +++ b/lib/luavgl/src/widgets/switch.c @@ -0,0 +1,57 @@ +#include "luavgl.h" +#include "private.h" +#include + +static int luavgl_switch_create(lua_State *L) { + return luavgl_obj_create_helper(L, lv_switch_create); +} + +LUALIB_API int luavgl_switch_set_property_kv(lua_State *L, void *data) { + lv_obj_t *obj = data; + /* switches only use base properties */ + int ret = luavgl_obj_set_property_kv(L, obj); + if (ret != 0) { + debug("unkown property for switch.\n"); + } + + return ret; +} + +static int luavgl_switch_set(lua_State *L) { + lv_obj_t *obj = luavgl_to_obj(L, 1); + + if (!lua_istable(L, -1)) { + luaL_error(L, "expect a table on 2nd para."); + return 0; + } + + luavgl_iterate(L, -1, luavgl_switch_set_property_kv, obj); + + return 0; +} + +static int luavgl_switch_enabled(lua_State *L) { + lv_obj_t *obj = luavgl_to_obj(L, 1); + lua_pushboolean(L, lv_obj_has_state(obj, LV_STATE_CHECKED)); + return 1; +} + +static int luavgl_switch_tostring(lua_State *L) { + lv_obj_t *obj = luavgl_to_obj(L, 1); + lua_pushfstring(L, "lv_switch:%p", obj); + return 1; +} + +static const luaL_Reg luavgl_switch_methods[] = { + {"set", luavgl_switch_set}, + {"enabled", luavgl_switch_enabled}, + {NULL, NULL}, +}; + +static void luavgl_switch_init(lua_State *L) { + luavgl_obj_newmetatable(L, &lv_switch_class, "lv_switch", + luavgl_switch_methods); + lua_pushcfunction(L, luavgl_switch_tostring); + lua_setfield(L, -2, "__tostring"); + lua_pop(L, 1); +} -- cgit v1.2.3