aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--init.lua7
-rw-r--r--lua/null-browser/api.lua3
-rw-r--r--src/LuaRuntime.hpp1
-rw-r--r--src/LuaRuntimeApi.hpp23
-rw-r--r--src/WindowActionRouter.cpp3
5 files changed, 37 insertions, 0 deletions
diff --git a/init.lua b/init.lua
index 6e45040..a91ff51 100644
--- a/init.lua
+++ b/init.lua
@@ -76,6 +76,13 @@ end
web.event.add_listener('WinCreated', {
callback = function(event)
web.decorations.bottom.enable({ win = event.win_id })
+
+ -- View id will only be available asynchronously after enable
+ web.schedule(function()
+ local view = web.decorations.bottom.view({ win = event.win_id })
+ print('>>>> view', view)
+ end)
+
start_clock(event.win_id)
end,
})
diff --git a/lua/null-browser/api.lua b/lua/null-browser/api.lua
index 62dfc76..8b5cebd 100644
--- a/lua/null-browser/api.lua
+++ b/lua/null-browser/api.lua
@@ -13,6 +13,9 @@ web.decorations = web.decorations or {}
require 'null-browser.utils'
+--- @param fn fun(): nil Schedule a function to be called on next tick (qt+libuv event loop)
+function web.schedule(fn) __internals.schedule(fn) end
+
--- Add a keymap
---
--- @param mode string Keymap mode ("i", "n", ...)
diff --git a/src/LuaRuntime.hpp b/src/LuaRuntime.hpp
index 1132270..5d8e7cc 100644
--- a/src/LuaRuntime.hpp
+++ b/src/LuaRuntime.hpp
@@ -64,6 +64,7 @@ signals:
void webview_scroll_bottom_requested(WebViewId webview_id);
void decoration_set_enabled(DecorationType type, bool enabled, std::optional<WindowId> win_id);
void set_view_html(const QString &html, WebViewId view_id);
+ void schedule_for_next_tick(const std::function<void()> &action);
protected:
LuaRuntime();
diff --git a/src/LuaRuntimeApi.hpp b/src/LuaRuntimeApi.hpp
index 17e0fe9..06323f0 100644
--- a/src/LuaRuntimeApi.hpp
+++ b/src/LuaRuntimeApi.hpp
@@ -10,6 +10,27 @@
#include "widgets/BrowserWindow.hpp"
#include "widgets/Decorations.hpp"
+int lua_api_schedule_fn(lua_State *state) {
+ lua_pushvalue(state, 1);
+ const int function_ref = luaL_ref(state, LUA_REGISTRYINDEX);
+ auto action = [state, function_ref]() {
+ preserve_top(state, {
+ lua_rawgeti(state, LUA_REGISTRYINDEX, function_ref);
+ if (lua_pcall(state, 0, 0, 0) != LUA_OK) {
+ const char *error = lua_tostring(state, -1);
+ qDebug() << "Error calling Lua function:" << error;
+ }
+ luaL_unref(state, LUA_REGISTRYINDEX, function_ref);
+ })
+ };
+
+ auto &runtime = LuaRuntime::instance();
+ emit runtime.schedule_for_next_tick(action);
+
+ lua_pushnil(state);
+ return 1;
+}
+
int lua_api_view_set_url(lua_State *state) {
const char *url = lua_tostring(state, 1);
WebViewId view_id = lua_isnoneornil(state, 2) ? 0 : lua_tointeger(state, 2);
@@ -78,6 +99,7 @@ int lua_api_keymap_set(lua_State *state) {
auto &runtime = LuaRuntime::instance();
emit runtime.keymap_add_requested(mode, keyseq, action);
+ lua_pushnil(state);
return 1;
}
@@ -337,5 +359,6 @@ static luaL_Reg internals_api[] = {
luaL_Reg{"decorations_set_enabled", &lua_api_decorations_set_enabled},
luaL_Reg{"decorations_get_enabled", &lua_api_decorations_get_enabled},
luaL_Reg{"decorations_get_view", &lua_api_decorations_get_view},
+ luaL_Reg{"schedule", &lua_api_schedule_fn},
luaL_Reg{nullptr, nullptr},
};
diff --git a/src/WindowActionRouter.cpp b/src/WindowActionRouter.cpp
index 1278d48..99d9931 100644
--- a/src/WindowActionRouter.cpp
+++ b/src/WindowActionRouter.cpp
@@ -106,6 +106,9 @@ void WindowActionRouter::initialize(Configuration *config) {
for (auto *win : get_relevant_windows(win_id))
win->set_decoration_enabled(type, enabled);
});
+
+ connect(&runtime, &LuaRuntime::schedule_for_next_tick, this,
+ [](const std::function<void()> &action) { LuaRuntime::instance().queue_task(action); });
}
void WindowActionRouter::find_current_search_text(WebViewId webview_id, bool forward) {