feat(lua): add 'edited' callback to 'numberEdit' control for LVGL Lua scripts (#7077)

This commit is contained in:
philmoz
2026-02-09 09:36:43 +10:00
committed by GitHub
parent 539f722e9b
commit dd931bc31b
4 changed files with 19 additions and 1 deletions
@@ -243,6 +243,7 @@ void NumberEdit::openEdit()
lv_obj_get_width(lvobj), lv_obj_get_height(lvobj)}); lv_obj_get_width(lvobj), lv_obj_get_height(lvobj)});
edit->setChangeHandler([=]() { edit->setChangeHandler([=]() {
update(); update();
if (onEdited) onEdited(currentValue);
if (edit->hasFocus()) if (edit->hasFocus())
lv_group_focus_obj(lvobj); lv_group_focus_obj(lvobj);
edit->hide(); edit->hide();
@@ -89,6 +89,11 @@ class NumberEdit : public TextButton
_getValue = std::move(handler); _getValue = std::move(handler);
} }
void setOnEditedHandler(std::function<void(int)> handler)
{
onEdited = std::move(handler);
}
int32_t getValue() const { return _getValue != nullptr ? _getValue() : 0; } int32_t getValue() const { return _getValue != nullptr ? _getValue() : 0; }
protected: protected:
@@ -97,6 +102,7 @@ class NumberEdit : public TextButton
NumberArea* edit = nullptr; NumberArea* edit = nullptr;
std::function<int()> _getValue; std::function<int()> _getValue;
std::function<void(int)> _setValue; std::function<void(int)> _setValue;
std::function<void(int)> onEdited;
int vdefault = 0; int vdefault = 0;
int vmin; int vmin;
int vmax; int vmax;
+11 -1
View File
@@ -165,6 +165,7 @@ bool LvglMinMaxParams::parseMinMaxParam(lua_State *L, const char *key)
} }
if (!strcmp(key, "max")) { if (!strcmp(key, "max")) {
max = luaL_checkinteger(L, -1); max = luaL_checkinteger(L, -1);
return true;
} }
return false; return false;
} }
@@ -2159,10 +2160,13 @@ void LvglWidgetTextEdit::build(lua_State *L)
void LvglWidgetNumberEdit::parseParam(lua_State *L, const char *key) void LvglWidgetNumberEdit::parseParam(lua_State *L, const char *key)
{ {
if (parseMinMaxParam(L, key)) return; if (parseMinMaxParam(L, key)) return;
if (parseGetSetParam(L, key)) return;
if (!strcmp(key, "display")) { if (!strcmp(key, "display")) {
dispFunction = luaL_ref(L, LUA_REGISTRYINDEX); dispFunction = luaL_ref(L, LUA_REGISTRYINDEX);
} else if (!parseGetSetParam(L, key)) { } else if (!strcmp(key, "edited")) {
editedFunction = luaL_ref(L, LUA_REGISTRYINDEX);
} else {
LvglWidgetObject::parseParam(L, key); LvglWidgetObject::parseParam(L, key);
} }
} }
@@ -2170,6 +2174,7 @@ void LvglWidgetNumberEdit::parseParam(lua_State *L, const char *key)
void LvglWidgetNumberEdit::clearRefs(lua_State *L) void LvglWidgetNumberEdit::clearRefs(lua_State *L)
{ {
clearRef(L, dispFunction); clearRef(L, dispFunction);
clearRef(L, editedFunction);
clearGetSetRefs(L); clearGetSetRefs(L);
LvglWidgetObject::clearRefs(L); LvglWidgetObject::clearRefs(L);
} }
@@ -2202,6 +2207,11 @@ void LvglWidgetNumberEdit::build(lua_State *L)
return s; return s;
}); });
} }
if (editedFunction != LUA_REFNIL) {
((NumberEdit*)window)->setOnEditedHandler([=]( int val) {
pcallSetIntVal(L, editedFunction, val);
});
}
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
+1
View File
@@ -802,6 +802,7 @@ class LvglWidgetNumberEdit : public LvglWidgetObject, public LvglGetSetParams, p
protected: protected:
int dispFunction = LUA_REFNIL; int dispFunction = LUA_REFNIL;
int editedFunction = LUA_REFNIL;
void build(lua_State *L) override; void build(lua_State *L) override;
void parseParam(lua_State *L, const char *key) override; void parseParam(lua_State *L, const char *key) override;