feat(color): allow Lua script to change the value of a textEdit control independently of user input (#7503)

This commit is contained in:
philmoz
2026-07-03 12:50:53 +10:00
committed by GitHub
parent 66d9e13199
commit e2d06085d5
3 changed files with 31 additions and 7 deletions
@@ -166,6 +166,7 @@ void TextEdit::openEdit()
edit->hide(); edit->hide();
}); });
} }
edit->update();
edit->show(); edit->show();
lv_group_focus_obj(edit->getLvObj()); lv_group_focus_obj(edit->getLvObj());
edit->openKeyboard(); edit->openKeyboard();
+23 -4
View File
@@ -2249,11 +2249,11 @@ void LvglWidgetToggleSwitch::build(lua_State *L)
void LvglWidgetTextEdit::parseParam(lua_State *L, const char *key) void LvglWidgetTextEdit::parseParam(lua_State *L, const char *key)
{ {
if (!strcmp(key, "value")) { if (!strcmp(key, "value")) {
txt = luaL_checkstring(L, -1); txt.parse(L);
} else if (!strcmp(key, "length")) { } else if (!strcmp(key, "length")) {
maxLen = luaL_checkinteger(L, -1); maxLen = luaL_checkinteger(L, -1);
if (maxLen > 128) maxLen = 128; if (maxLen > MAX_TEXT_EDIT_LEN) maxLen = MAX_TEXT_EDIT_LEN;
if (maxLen <= 0) maxLen = 32; if (maxLen <= 0) maxLen = DEFAULT_TEXT_EDIT_LEN;
} else if (!strcmp(key, "set")) { } else if (!strcmp(key, "set")) {
setFunction = ::getRef(L, LUA_REGISTRYINDEX); setFunction = ::getRef(L, LUA_REGISTRYINDEX);
} else { } else {
@@ -2261,15 +2261,33 @@ void LvglWidgetTextEdit::parseParam(lua_State *L, const char *key)
} }
} }
bool LvglWidgetTextEdit::callRefs(lua_State *L)
{
if (!LvglWidgetObject::callRefs(L)) return false;
if (isVisible()) {
if (!pcallUpdateStringVal(L, txt.function, [=](const char* s) {
if (txt.changedText(s)) {
strAppend(value, txt.chars(), MAX_TEXT_EDIT_LEN);
((TextEdit*)window)->update();
}
}))
return false;
}
return true;
}
void LvglWidgetTextEdit::clearRefs(lua_State *L) void LvglWidgetTextEdit::clearRefs(lua_State *L)
{ {
txt.clearRef(L);
clearRef(L, setFunction); clearRef(L, setFunction);
LvglWidgetObject::clearRefs(L); LvglWidgetObject::clearRefs(L);
} }
void LvglWidgetTextEdit::build(lua_State *L) void LvglWidgetTextEdit::build(lua_State *L)
{ {
strcpy(value, txt); strAppend(value, txt.chars(), MAX_TEXT_EDIT_LEN);
if (h == LV_SIZE_CONTENT) h = 0; if (h == LV_SIZE_CONTENT) h = 0;
window = new TextEdit(lvglManager->getCurrentParent(), {x, y, w, h}, value, window = new TextEdit(lvglManager->getCurrentParent(), {x, y, w, h}, value,
maxLen, [=]() { maxLen, [=]() {
@@ -2278,6 +2296,7 @@ void LvglWidgetTextEdit::build(lua_State *L)
PROTECT_LUA() PROTECT_LUA()
{ {
std::string s(value, maxLen); // Ensure string is terminated std::string s(value, maxLen); // Ensure string is terminated
txt.changedText(s.c_str());
if (!pcallFuncWithString(L, setFunction, 0, s.c_str())) { if (!pcallFuncWithString(L, setFunction, 0, s.c_str())) {
lvglManager->luaShowError(); lvglManager->luaShowError();
} }
+7 -3
View File
@@ -796,12 +796,16 @@ class LvglWidgetTextEdit : public LvglWidgetObject
public: public:
LvglWidgetTextEdit() : LvglWidgetObject(LVGL_SIMPLEMETATABLE) {} LvglWidgetTextEdit() : LvglWidgetObject(LVGL_SIMPLEMETATABLE) {}
bool callRefs(lua_State *L) override;
void clearRefs(lua_State *L) override; void clearRefs(lua_State *L) override;
static constexpr int DEFAULT_TEXT_EDIT_LEN = 32;
static constexpr int MAX_TEXT_EDIT_LEN = 128;
protected: protected:
const char *txt = ""; LvglParamFuncOrString txt = { .function = LUA_REFNIL, .txt = ""};
char value[129]; char value[MAX_TEXT_EDIT_LEN + 1];
int maxLen = 32; int maxLen = DEFAULT_TEXT_EDIT_LEN;
int setFunction = LUA_REFNIL; int setFunction = LUA_REFNIL;