fix document global for external Lua scripts in designer and Android
This commit is contained in:
@@ -16,6 +16,7 @@
|
|||||||
#include "testing/ui_inspector.h"
|
#include "testing/ui_inspector.h"
|
||||||
#include "testing/visual_capture.h"
|
#include "testing/visual_capture.h"
|
||||||
#include <RmlUi/Lua/Interpreter.h>
|
#include <RmlUi/Lua/Interpreter.h>
|
||||||
|
#include <RmlUi/Lua/LuaType.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
@@ -77,6 +78,18 @@ bool LoadDocument(const std::string& path);
|
|||||||
void ReloadDocument();
|
void ReloadDocument();
|
||||||
void PopulateSimulatorApps();
|
void PopulateSimulatorApps();
|
||||||
|
|
||||||
|
// Helper to set the 'document' global in Lua to the current 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");
|
||||||
|
std::cout << "Set Lua 'document' global to: " << doc->GetTitle() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
// GLFW callbacks
|
// GLFW callbacks
|
||||||
static void ErrorCallback(int error, const char* description) {
|
static void ErrorCallback(int error, const char* description) {
|
||||||
std::cerr << "GLFW Error " << error << ": " << description << std::endl;
|
std::cerr << "GLFW Error " << error << ": " << description << std::endl;
|
||||||
@@ -788,6 +801,7 @@ bool InitializeRmlUi(const std::string& assets_path, int fb_width, int fb_height
|
|||||||
auto* document = g_context->LoadDocument(path);
|
auto* document = g_context->LoadDocument(path);
|
||||||
if (document) {
|
if (document) {
|
||||||
document->Show();
|
document->Show();
|
||||||
|
SetLuaDocumentGlobal(document); // Set 'document' global for Lua scripts
|
||||||
g_current_document_path = path;
|
g_current_document_path = path;
|
||||||
g_current_screen_url = path; // Track current screen for hierarchy dump
|
g_current_screen_url = path; // Track current screen for hierarchy dump
|
||||||
// Log using RmlUi logging so it appears in log file
|
// Log using RmlUi logging so it appears in log file
|
||||||
@@ -840,6 +854,7 @@ bool InitializeRmlUi(const std::string& assets_path, int fb_width, int fb_height
|
|||||||
auto* document = g_context->LoadDocument(home_path);
|
auto* document = g_context->LoadDocument(home_path);
|
||||||
if (document) {
|
if (document) {
|
||||||
document->Show();
|
document->Show();
|
||||||
|
SetLuaDocumentGlobal(document); // Set 'document' global for Lua scripts
|
||||||
g_current_document_path = home_path;
|
g_current_document_path = home_path;
|
||||||
g_current_screen_url = home_path;
|
g_current_screen_url = home_path;
|
||||||
g_current_app_id = ""; // Clear current app
|
g_current_app_id = ""; // Clear current app
|
||||||
@@ -929,6 +944,7 @@ bool InitializeRmlUi(const std::string& assets_path, int fb_width, int fb_height
|
|||||||
auto* document = g_context->LoadDocument(entry);
|
auto* document = g_context->LoadDocument(entry);
|
||||||
if (document) {
|
if (document) {
|
||||||
document->Show();
|
document->Show();
|
||||||
|
SetLuaDocumentGlobal(document); // Set 'document' global for Lua scripts
|
||||||
g_current_document_path = entry;
|
g_current_document_path = entry;
|
||||||
g_current_screen_url = entry;
|
g_current_screen_url = entry;
|
||||||
std::cout << "Simulator: App launched successfully" << std::endl;
|
std::cout << "Simulator: App launched successfully" << std::endl;
|
||||||
@@ -973,6 +989,7 @@ bool InitializeRmlUi(const std::string& assets_path, int fb_width, int fb_height
|
|||||||
auto* document = g_context->LoadDocument(g_simulator_home_path);
|
auto* document = g_context->LoadDocument(g_simulator_home_path);
|
||||||
if (document) {
|
if (document) {
|
||||||
document->Show();
|
document->Show();
|
||||||
|
SetLuaDocumentGlobal(document); // Set 'document' global for Lua scripts
|
||||||
g_current_document_path = g_simulator_home_path;
|
g_current_document_path = g_simulator_home_path;
|
||||||
g_current_screen_url = g_simulator_home_path;
|
g_current_screen_url = g_simulator_home_path;
|
||||||
|
|
||||||
@@ -1207,8 +1224,9 @@ bool LoadDocument(const std::string& path) {
|
|||||||
std::cerr << "Failed to load: " << path << std::endl;
|
std::cerr << "Failed to load: " << path << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
document->Show();
|
document->Show();
|
||||||
|
SetLuaDocumentGlobal(document); // Set 'document' global for Lua scripts
|
||||||
g_current_document_path = path;
|
g_current_document_path = path;
|
||||||
g_current_screen_url = path; // Track current screen for hierarchy dump
|
g_current_screen_url = path; // Track current screen for hierarchy dump
|
||||||
std::cout << "Loaded: " << path << std::endl;
|
std::cout << "Loaded: " << path << std::endl;
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
#include <RmlUi/Core.h>
|
#include <RmlUi/Core.h>
|
||||||
#include <RmlUi/Lua.h>
|
#include <RmlUi/Lua.h>
|
||||||
#include <RmlUi/Lua/Interpreter.h>
|
#include <RmlUi/Lua/Interpreter.h>
|
||||||
|
#include <RmlUi/Lua/LuaType.h>
|
||||||
#include "RmlUi_Renderer_GL3.h"
|
#include "RmlUi_Renderer_GL3.h"
|
||||||
#include <span>
|
#include <span>
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
@@ -46,6 +47,19 @@ static std::string g_data_root;
|
|||||||
// Forward declarations
|
// Forward declarations
|
||||||
static void SwitchAppSandbox(const std::string& app_id, const std::string& app_data_path);
|
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::com::omixlab::mosis;
|
||||||
using namespace aidl::android::hardware;
|
using namespace aidl::android::hardware;
|
||||||
|
|
||||||
@@ -334,6 +348,7 @@ static int LuaLoadScreen(lua_State* L)
|
|||||||
if (g_document)
|
if (g_document)
|
||||||
{
|
{
|
||||||
g_document->Show();
|
g_document->Show();
|
||||||
|
SetLuaDocumentGlobal(g_document);
|
||||||
Logger::Log(std::format("Loaded screen: {}", path));
|
Logger::Log(std::format("Loaded screen: {}", path));
|
||||||
lua_pushboolean(L, true);
|
lua_pushboolean(L, true);
|
||||||
}
|
}
|
||||||
@@ -390,6 +405,7 @@ static int LuaGoHome(lua_State* L)
|
|||||||
if (g_document)
|
if (g_document)
|
||||||
{
|
{
|
||||||
g_document->Show();
|
g_document->Show();
|
||||||
|
SetLuaDocumentGlobal(g_document);
|
||||||
Logger::Log("Returned to home screen");
|
Logger::Log("Returned to home screen");
|
||||||
lua_pushboolean(L, true);
|
lua_pushboolean(L, true);
|
||||||
}
|
}
|
||||||
@@ -626,6 +642,7 @@ void Kernel::main_loop()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
g_document->Show();
|
g_document->Show();
|
||||||
|
SetLuaDocumentGlobal(g_document);
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -3,34 +3,11 @@
|
|||||||
|
|
||||||
local results = {}
|
local results = {}
|
||||||
local logCounter = 0
|
local logCounter = 0
|
||||||
local currentDocument = nil -- Cache the document reference
|
|
||||||
|
|
||||||
-- Helper to get document (try multiple methods)
|
-- Helper to get document (use global set by C++)
|
||||||
local function getDocument()
|
local function getDocument()
|
||||||
-- First try the cached document
|
-- The C++ code sets 'document' global after loading
|
||||||
if currentDocument then
|
return document
|
||||||
return currentDocument
|
|
||||||
end
|
|
||||||
-- Try the global document
|
|
||||||
if document then
|
|
||||||
currentDocument = document
|
|
||||||
return document
|
|
||||||
end
|
|
||||||
-- Try to get from RmlUi context (our context is named "default")
|
|
||||||
if rmlui and rmlui.contexts then
|
|
||||||
-- Try both "default" and "main" context names
|
|
||||||
local ctx = rmlui.contexts["default"] or rmlui.contexts.main
|
|
||||||
if ctx and ctx.documents then
|
|
||||||
-- documents is a proxy, iterate to get first doc
|
|
||||||
for i, doc in ipairs(ctx.documents) do
|
|
||||||
if doc then
|
|
||||||
currentDocument = doc
|
|
||||||
return currentDocument
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function log(msg)
|
local function log(msg)
|
||||||
@@ -241,10 +218,6 @@ function testStorage()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Initialize: cache the document when script loads
|
-- Initialize
|
||||||
if document then
|
|
||||||
currentDocument = document
|
|
||||||
print("[LUA] Document cached on load")
|
|
||||||
end
|
|
||||||
log("Sandbox Test App loaded")
|
log("Sandbox Test App loaded")
|
||||||
log("Lua version: " .. (_VERSION or "unknown"))
|
log("Lua version: " .. (_VERSION or "unknown"))
|
||||||
|
|||||||
Reference in New Issue
Block a user