luat_lib_easylvgl_keyboard.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. @module easylvgl.keyboard
  3. @summary EasyLVGL Keyboard 组件 Lua 绑定
  4. @version 0.3.0
  5. @date 2025.12.15
  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. #include "luat_malloc.h"
  16. #define LUAT_LOG_TAG "easylvgl.keyboard"
  17. #include "luat_log.h"
  18. /**
  19. * 创建 Keyboard 组件
  20. * @api easylvgl.keyboard(config)
  21. * @table config 配置表
  22. * @int config.x X 坐标,默认 0
  23. * @int config.y Y 坐标,默认 ctx->height-160(或 0)
  24. * @int config.w 宽度,默认 ctx->width(或 480)
  25. * @int config.h 高度,默认 160
  26. * @string config.mode 键盘模式,如 "text"/"upper"/"special"/"numeric"
  27. * @boolean config.popovers 是否启用提示弹窗,默认 true
  28. * @userdata config.target 关联的 Textarea 对象,可选
  29. * @return userdata Keyboard 对象,失败返回 nil
  30. */
  31. static int l_easylvgl_keyboard(lua_State *L) {
  32. easylvgl_ctx_t *ctx = NULL;
  33. lua_getfield(L, LUA_REGISTRYINDEX, "easylvgl_ctx");
  34. if (lua_type(L, -1) == LUA_TLIGHTUSERDATA) {
  35. ctx = (easylvgl_ctx_t *)lua_touserdata(L, -1);
  36. }
  37. lua_pop(L, 1);
  38. if (ctx == NULL) {
  39. luaL_error(L, "easylvgl not initialized, call easylvgl.init() first");
  40. return 0;
  41. }
  42. luaL_checktype(L, 1, LUA_TTABLE);
  43. lv_obj_t *keyboard = easylvgl_keyboard_create_from_config(L, 1);
  44. if (keyboard == NULL) {
  45. lua_pushnil(L);
  46. return 1;
  47. }
  48. easylvgl_push_component_userdata(L, keyboard, EASYLVGL_KEYBOARD_MT);
  49. return 1;
  50. }
  51. /**
  52. * Keyboard:set_target(textarea)
  53. * @api keyboard:set_target(textarea)
  54. * @userdata textarea 目标 Textarea 对象
  55. * @return nil
  56. */
  57. static int l_keyboard_set_target(lua_State *L) {
  58. lv_obj_t *keyboard = easylvgl_check_component(L, 1, EASYLVGL_KEYBOARD_MT);
  59. lv_obj_t *textarea = easylvgl_check_component(L, 2, EASYLVGL_TEXTAREA_MT);
  60. easylvgl_keyboard_set_target(keyboard, textarea);
  61. return 0;
  62. }
  63. /**
  64. * Keyboard:show()
  65. * @api keyboard:show()
  66. * @return nil
  67. */
  68. static int l_keyboard_show(lua_State *L) {
  69. lv_obj_t *keyboard = easylvgl_check_component(L, 1, EASYLVGL_KEYBOARD_MT);
  70. easylvgl_keyboard_show(keyboard);
  71. return 0;
  72. }
  73. /**
  74. * Keyboard:hide()
  75. * @api keyboard:hide()
  76. * @return nil
  77. */
  78. static int l_keyboard_hide(lua_State *L) {
  79. lv_obj_t *keyboard = easylvgl_check_component(L, 1, EASYLVGL_KEYBOARD_MT);
  80. easylvgl_keyboard_hide(keyboard);
  81. return 0;
  82. }
  83. /**
  84. * Keyboard:set_on_commit(callback)
  85. * @api keyboard:set_on_commit(callback)
  86. * @function callback 提交事件回调
  87. * @return nil
  88. */
  89. static int l_keyboard_set_on_commit(lua_State *L) {
  90. lv_obj_t *keyboard = easylvgl_check_component(L, 1, EASYLVGL_KEYBOARD_MT);
  91. luaL_checktype(L, 2, LUA_TFUNCTION);
  92. lua_pushvalue(L, 2);
  93. int ref = luaL_ref(L, LUA_REGISTRYINDEX);
  94. easylvgl_keyboard_set_on_commit(keyboard, ref);
  95. return 0;
  96. }
  97. /**
  98. * Keyboard:set_layout(layout)
  99. * @api keyboard:set_layout(layout)
  100. * @string layout 布局名称
  101. * @return nil
  102. */
  103. static int l_keyboard_set_layout(lua_State *L) {
  104. lv_obj_t *keyboard = easylvgl_check_component(L, 1, EASYLVGL_KEYBOARD_MT);
  105. const char *layout = luaL_checkstring(L, 2);
  106. easylvgl_keyboard_set_layout(keyboard, layout);
  107. return 0;
  108. }
  109. /**
  110. * Keyboard:get_target()
  111. * @api keyboard:get_target()
  112. * @return userdata|null 当前关联 Textarea
  113. */
  114. static int l_keyboard_get_target(lua_State *L) {
  115. lv_obj_t *keyboard = easylvgl_check_component(L, 1, EASYLVGL_KEYBOARD_MT);
  116. easylvgl_component_meta_t *meta = easylvgl_component_meta_get(keyboard);
  117. if (meta == NULL || meta->user_data == NULL) {
  118. lua_pushnil(L);
  119. return 1;
  120. }
  121. easylvgl_keyboard_data_t *data = (easylvgl_keyboard_data_t *)meta->user_data;
  122. if (data == NULL || data->target == NULL) {
  123. lua_pushnil(L);
  124. return 1;
  125. }
  126. easylvgl_push_component_userdata(L, data->target, EASYLVGL_TEXTAREA_MT);
  127. return 1;
  128. }
  129. /**
  130. * Keyboard GC(释放资源)
  131. */
  132. static int l_keyboard_gc(lua_State *L) {
  133. easylvgl_component_ud_t *ud = (easylvgl_component_ud_t *)luaL_checkudata(L, 1, EASYLVGL_KEYBOARD_MT);
  134. if (ud != NULL && ud->obj != NULL) {
  135. easylvgl_component_meta_t *meta = easylvgl_component_meta_get(ud->obj);
  136. if (meta != NULL) {
  137. if (meta->user_data != NULL) {
  138. luat_heap_free(meta->user_data);
  139. meta->user_data = NULL;
  140. }
  141. easylvgl_component_meta_free(meta);
  142. }
  143. lv_obj_delete(ud->obj);
  144. ud->obj = NULL;
  145. }
  146. return 0;
  147. }
  148. /**
  149. * 注册 Keyboard 元表
  150. * @param L Lua 状态
  151. */
  152. void easylvgl_register_keyboard_meta(lua_State *L) {
  153. luaL_newmetatable(L, EASYLVGL_KEYBOARD_MT);
  154. lua_pushcfunction(L, l_keyboard_gc);
  155. lua_setfield(L, -2, "__gc");
  156. static const luaL_Reg methods[] = {
  157. {"set_target", l_keyboard_set_target},
  158. {"show", l_keyboard_show},
  159. {"hide", l_keyboard_hide},
  160. {"set_on_commit", l_keyboard_set_on_commit},
  161. {"set_layout", l_keyboard_set_layout},
  162. {"get_target", l_keyboard_get_target},
  163. {NULL, NULL}
  164. };
  165. luaL_newlib(L, methods);
  166. lua_setfield(L, -2, "__index");
  167. lua_pop(L, 1);
  168. }
  169. /**
  170. * Keyboard 创建函数(供主模块注册)
  171. */
  172. int easylvgl_keyboard_create(lua_State *L) {
  173. return l_easylvgl_keyboard(L);
  174. }