fix(lua): Store and update the 'zone' rectangle for Lua widgets. (#3060)

* Store 'zone' rectangle dat in Lua registry and update it when the screen layout changes or full screen mode changes. Allows Lua widgets to adjust to layout changes.

* Call widget 'update' function is layout has changed.
This commit is contained in:
philmoz
2023-02-20 11:46:27 +10:00
committed by GitHub
parent c3516b54c1
commit 1df8fe86a8
6 changed files with 73 additions and 9 deletions
+50 -1
View File
@@ -196,9 +196,10 @@ void LuaEventHandler::removeHandler(Window* w)
LuaWidget::LuaWidget(const WidgetFactory* factory, Window* parent,
const rect_t& rect, WidgetPersistentData* persistentData,
int luaWidgetDataRef) :
int luaWidgetDataRef,int zoneRectDataRef) :
Widget(factory, parent, rect, persistentData),
luaWidgetDataRef(luaWidgetDataRef),
zoneRectDataRef(zoneRectDataRef),
errorMessage(nullptr)
{
}
@@ -206,6 +207,7 @@ LuaWidget::LuaWidget(const WidgetFactory* factory, Window* parent,
LuaWidget::~LuaWidget()
{
luaL_unref(lsWidgets, LUA_REGISTRYINDEX, luaWidgetDataRef);
luaL_unref(lsWidgets, LUA_REGISTRYINDEX, zoneRectDataRef);
free(errorMessage);
}
@@ -288,6 +290,53 @@ void LuaWidget::update()
}
}
// Update table on top of Lua stack - set entry with name 'idx' to value 'val'
// Return true if value has changed
bool LuaWidget::updateTable(const char* idx, int val)
{
bool update = false;
// Check existing value (or invalid value)
lua_getfield(lsWidgets, -1, idx);
if (lua_isnumber(lsWidgets, -1)) {
int v = lua_tointeger(lsWidgets, -1);
update = (v != val);
} else {
// Force table update
update = true;
}
lua_pop(lsWidgets, 1);
if (update) {
lua_pushinteger(lsWidgets, val);
lua_setfield(lsWidgets, -2, idx);
}
return update;
}
void LuaWidget::updateZoneRect(rect_t rect)
{
if (lsWidgets)
{
// Update widget zone with current size and position
lua_rawgeti(lsWidgets, LUA_REGISTRYINDEX, zoneRectDataRef);
bool changed = false;
if (updateTable("w", rect.w)) changed = true;
if (updateTable("h", rect.h)) changed = true;
if (updateTable("xabs", rect.x)) changed = true;
if (updateTable("yabs", rect.y)) changed = true;
lua_pop(lsWidgets, 1);
if (changed)
update();
}
}
void LuaWidget::onFullscreen(bool enable)
{
if (enable) {