luat_lib_easylvgl_switch.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. @module easylvgl.switch
  3. @summary EasyLVGL Switch 组件
  4. @version 0.2.0
  5. @date 2025.12.12
  6. @tag LUAT_USE_EASYLVGL
  7. */
  8. #include "luat_base.h"
  9. #include "luat_log.h"
  10. #include "lua.h"
  11. #include "lauxlib.h"
  12. #include "../inc/luat_easylvgl.h"
  13. #include "../inc/luat_easylvgl_component.h"
  14. #include "../inc/luat_easylvgl_binding.h"
  15. #define LUAT_LOG_TAG "easylvgl.switch"
  16. #include "luat_log.h"
  17. #define EASYLVGL_SWITCH_MT "easylvgl.switch"
  18. /**
  19. * 创建 Switch 组件
  20. * @api easylvgl.switch(config)
  21. * @table config 配置表
  22. * @int config.x X 坐标,默认 0
  23. * @int config.y Y 坐标,默认 0
  24. * @int config.w 宽度,默认 70
  25. * @int config.h 高度,默认 40
  26. * @boolean config.checked 初始状态,默认 false
  27. * @string config.style 预设样式,如 "danger"/"success"
  28. * @function config.on_change 状态变更回调
  29. * @userdata config.parent 父对象,可选
  30. * @return userdata Switch 对象,失败返回 nil
  31. */
  32. static int l_easylvgl_switch(lua_State *L)
  33. {
  34. easylvgl_ctx_t *ctx = NULL;
  35. lua_getfield(L, LUA_REGISTRYINDEX, "easylvgl_ctx");
  36. if (lua_type(L, -1) == LUA_TLIGHTUSERDATA) {
  37. ctx = (easylvgl_ctx_t *)lua_touserdata(L, -1);
  38. }
  39. lua_pop(L, 1);
  40. if (ctx == NULL) {
  41. luaL_error(L, "easylvgl not initialized, call easylvgl.init() first");
  42. return 0;
  43. }
  44. luaL_checktype(L, 1, LUA_TTABLE);
  45. lv_obj_t *sw = easylvgl_switch_create_from_config(L, 1);
  46. if (sw == NULL) {
  47. lua_pushnil(L);
  48. return 1;
  49. }
  50. easylvgl_push_component_userdata(L, sw, EASYLVGL_SWITCH_MT);
  51. return 1;
  52. }
  53. /**
  54. * Switch:set_state(state)
  55. * @api switch:set_state(state)
  56. * @boolean state 勾选状态
  57. * @return nil
  58. */
  59. static int l_switch_set_state(lua_State *L)
  60. {
  61. lv_obj_t *sw = easylvgl_check_component(L, 1, EASYLVGL_SWITCH_MT);
  62. bool checked = lua_toboolean(L, 2);
  63. easylvgl_switch_set_state(sw, checked);
  64. return 0;
  65. }
  66. /**
  67. * Switch:get_state()
  68. * @api switch:get_state()
  69. * @return boolean 当前状态
  70. */
  71. static int l_switch_get_state(lua_State *L)
  72. {
  73. lv_obj_t *sw = easylvgl_check_component(L, 1, EASYLVGL_SWITCH_MT);
  74. bool checked = easylvgl_switch_get_state(sw);
  75. lua_pushboolean(L, checked);
  76. return 1;
  77. }
  78. /**
  79. * Switch:set_on_change(callback)
  80. * @api switch:set_on_change(callback)
  81. * @function callback 状态变化回调
  82. * @return nil
  83. */
  84. static int l_switch_set_on_change(lua_State *L)
  85. {
  86. lv_obj_t *sw = easylvgl_check_component(L, 1, EASYLVGL_SWITCH_MT);
  87. luaL_checktype(L, 2, LUA_TFUNCTION);
  88. lua_pushvalue(L, 2);
  89. int ref = luaL_ref(L, LUA_REGISTRYINDEX);
  90. easylvgl_switch_set_on_change(sw, ref);
  91. return 0;
  92. }
  93. /**
  94. * Switch GC(释放 LVGL 对象)
  95. */
  96. static int l_switch_gc(lua_State *L)
  97. {
  98. easylvgl_component_ud_t *ud = (easylvgl_component_ud_t *)luaL_checkudata(L, 1, EASYLVGL_SWITCH_MT);
  99. if (ud != NULL && ud->obj != NULL) {
  100. easylvgl_component_meta_t *meta = easylvgl_component_meta_get(ud->obj);
  101. if (meta != NULL) {
  102. easylvgl_component_meta_free(meta);
  103. }
  104. lv_obj_delete(ud->obj);
  105. ud->obj = NULL;
  106. }
  107. return 0;
  108. }
  109. /**
  110. * 注册 Switch 元表
  111. * @param L Lua 状态
  112. */
  113. void easylvgl_register_switch_meta(lua_State *L)
  114. {
  115. luaL_newmetatable(L, EASYLVGL_SWITCH_MT);
  116. lua_pushcfunction(L, l_switch_gc);
  117. lua_setfield(L, -2, "__gc");
  118. static const luaL_Reg methods[] = {
  119. {"set_state", l_switch_set_state},
  120. {"get_state", l_switch_get_state},
  121. {"set_on_change", l_switch_set_on_change},
  122. {NULL, NULL}
  123. };
  124. luaL_newlib(L, methods);
  125. lua_setfield(L, -2, "__index");
  126. lua_pop(L, 1);
  127. }
  128. /**
  129. * Switch 创建函数(供主模块注册)
  130. */
  131. int easylvgl_switch_create(lua_State *L)
  132. {
  133. return l_easylvgl_switch(L);
  134. }