fix document global for external Lua scripts in designer and Android

This commit is contained in:
2026-01-19 22:51:19 +01:00
parent a3a15b0644
commit 5de087e8e0
3 changed files with 40 additions and 32 deletions

View File

@@ -13,6 +13,7 @@
#include <RmlUi/Core.h>
#include <RmlUi/Lua.h>
#include <RmlUi/Lua/Interpreter.h>
#include <RmlUi/Lua/LuaType.h>
#include "RmlUi_Renderer_GL3.h"
#include <span>
#include <ranges>
@@ -46,6 +47,19 @@ static std::string g_data_root;
// Forward declarations
static void SwitchAppSandbox(const std::string& app_id, const std::string& app_data_path);
// Helper to set the 'document' global in Lua to the current document
// This is needed for external script files (app.lua) that reference 'document'
static void SetLuaDocumentGlobal(Rml::ElementDocument* doc) {
if (!doc) return;
lua_State* L = Rml::Lua::Interpreter::GetLuaState();
if (!L) return;
// Push the document using RmlUi's Lua type system
Rml::Lua::LuaType<Rml::ElementDocument>::push(L, doc, false);
lua_setglobal(L, "document");
Logger::Log(std::format("Set Lua 'document' global to: {}", doc->GetTitle().empty() ? "(untitled)" : doc->GetTitle().c_str()));
}
using namespace aidl::com::omixlab::mosis;
using namespace aidl::android::hardware;
@@ -334,6 +348,7 @@ static int LuaLoadScreen(lua_State* L)
if (g_document)
{
g_document->Show();
SetLuaDocumentGlobal(g_document);
Logger::Log(std::format("Loaded screen: {}", path));
lua_pushboolean(L, true);
}
@@ -390,6 +405,7 @@ static int LuaGoHome(lua_State* L)
if (g_document)
{
g_document->Show();
SetLuaDocumentGlobal(g_document);
Logger::Log("Returned to home screen");
lua_pushboolean(L, true);
}
@@ -626,6 +642,7 @@ void Kernel::main_loop()
return;
}
g_document->Show();
SetLuaDocumentGlobal(g_document);
while (true)
{