Skip to content

Commit

Permalink
Fix layout creation. Allow button press function to return checked st…
Browse files Browse the repository at this point in the history
…ate for button.
  • Loading branch information
philmoz committed Jan 22, 2025
1 parent 8f861ec commit 9ef3bee
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
47 changes: 39 additions & 8 deletions radio/src/lua/lua_lvgl_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,12 @@ void LvglWidgetObjectBase::pcallSimpleFunc(lua_State *L, int funcRef)
if (funcRef != LUA_REFNIL) {
PROTECT_LUA()
{
auto save = luaLvglManager;
luaLvglManager = lvglManager;
if (!pcallFunc(L, funcRef, 0)) {
lvglManager->luaShowError();
}
luaLvglManager = nullptr;
luaLvglManager = save;
}
UNPROTECT_LUA();
}
Expand All @@ -141,6 +142,7 @@ bool LvglWidgetObjectBase::pcallUpdateBool(lua_State *L, int getFuncRef,
{
bool res = true;
if (getFuncRef != LUA_REFNIL) {
auto save = luaLvglManager;
luaLvglManager = lvglManager;
int t = lua_gettop(L);
if (pcallFunc(L, getFuncRef, 1)) {
Expand All @@ -150,7 +152,7 @@ bool LvglWidgetObjectBase::pcallUpdateBool(lua_State *L, int getFuncRef,
} else {
res = false;
}
lvglManager = nullptr;
lvglManager = save;
}
return res;
}
Expand All @@ -160,6 +162,7 @@ bool LvglWidgetObjectBase::pcallUpdate1Int(lua_State *L, int getFuncRef,
{
bool res = true;
if (getFuncRef != LUA_REFNIL) {
auto save = luaLvglManager;
luaLvglManager = lvglManager;
int t = lua_gettop(L);
if (pcallFunc(L, getFuncRef, 1)) {
Expand All @@ -169,7 +172,7 @@ bool LvglWidgetObjectBase::pcallUpdate1Int(lua_State *L, int getFuncRef,
} else {
res = false;
}
lvglManager = nullptr;
lvglManager = save;
}
return res;
}
Expand All @@ -179,6 +182,7 @@ bool LvglWidgetObjectBase::pcallUpdate2Int(lua_State *L, int getFuncRef,
{
bool res = true;
if (getFuncRef != LUA_REFNIL) {
auto save = luaLvglManager;
luaLvglManager = lvglManager;
int t = lua_gettop(L);
if (pcallFunc(L, getFuncRef, 2)) {
Expand All @@ -189,7 +193,7 @@ bool LvglWidgetObjectBase::pcallUpdate2Int(lua_State *L, int getFuncRef,
} else {
res = false;
}
lvglManager = nullptr;
lvglManager = save;
}
return res;
}
Expand All @@ -198,6 +202,7 @@ int LvglWidgetObjectBase::pcallGetIntVal(lua_State *L, int getFuncRef)
{
int val = 0;
if (getFuncRef != LUA_REFNIL) {
auto save = luaLvglManager;
luaLvglManager = lvglManager;
int t = lua_gettop(L);
PROTECT_LUA()
Expand All @@ -214,14 +219,41 @@ int LvglWidgetObjectBase::pcallGetIntVal(lua_State *L, int getFuncRef)
}
UNPROTECT_LUA();
lua_settop(L, t);
lvglManager = nullptr;
lvglManager = save;
}
return val;
}

int LvglWidgetObjectBase::pcallGetOptIntVal(lua_State *L, int getFuncRef, int defVal)
{
int val = 0;
if (getFuncRef != LUA_REFNIL) {
auto save = luaLvglManager;
luaLvglManager = lvglManager;
int t = lua_gettop(L);
PROTECT_LUA()
{
if (pcallFunc(L, getFuncRef, 1)) {
val = luaL_optinteger(L, -1, defVal);
} else {
lvglManager->luaShowError();
}
}
else
{
lvglManager->luaShowError();
}
UNPROTECT_LUA();
lua_settop(L, t);
lvglManager = save;
}
return val;
}

void LvglWidgetObjectBase::pcallSetIntVal(lua_State *L, int setFuncRef, int val)
{
if (setFuncRef != LUA_REFNIL) {
auto save = luaLvglManager;
luaLvglManager = lvglManager;
int t = lua_gettop(L);
PROTECT_LUA()
Expand All @@ -236,7 +268,7 @@ void LvglWidgetObjectBase::pcallSetIntVal(lua_State *L, int setFuncRef, int val)
}
UNPROTECT_LUA();
lua_settop(L, t);
lvglManager = nullptr;
lvglManager = save;
}
}

Expand Down Expand Up @@ -1313,8 +1345,7 @@ void LvglWidgetTextButton::build(lua_State *L)
if (h == LV_SIZE_CONTENT) h = 0;
window =
new TextButton(lvglManager->getCurrentParent(), {x, y, w, h}, txt, [=]() {
pcallSimpleFunc(L, pressFunction);
return 0;
return pcallGetOptIntVal(L, pressFunction, 0);
});
}

Expand Down
1 change: 1 addition & 0 deletions radio/src/lua/lua_lvgl_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class LvglWidgetObjectBase
bool pcallUpdate2Int(lua_State *L, int getFuncRef,
std::function<void(int, int)> update);
int pcallGetIntVal(lua_State *L, int getFuncRef);
int pcallGetOptIntVal(lua_State *L, int getFuncRef, int defVal);
void pcallSetIntVal(lua_State *L, int setFuncRef, int val);
};

Expand Down

0 comments on commit 9ef3bee

Please sign in to comment.