aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2025-07-26 00:39:23 +0530
committerAkshay Nair <phenax5@gmail.com>2025-07-26 00:39:23 +0530
commit3f56d76a2b0d528c05ab8d0a38059ca82ba90952 (patch)
treedaae7849815c122803f8ddd08cbfcb87552eab8a
parent031b22e0bb0f1d132731aea3776917731a8ab4ce (diff)
downloadnull-browser-3f56d76a2b0d528c05ab8d0a38059ca82ba90952.tar.gz
null-browser-3f56d76a2b0d528c05ab8d0a38059ca82ba90952.zip
Add set_html for views + example use with decorations
-rw-r--r--init.lua22
-rw-r--r--lua/null-browser/api.lua15
-rw-r--r--src/LuaRuntime.hpp1
-rw-r--r--src/LuaRuntimeApi.hpp9
-rw-r--r--src/WindowActionRouter.cpp4
-rw-r--r--src/widgets/BrowserWindow.hpp7
-rw-r--r--src/widgets/Decorations.hpp9
-rw-r--r--src/widgets/EdgeDecoration.cpp12
-rw-r--r--src/widgets/WebViewStack.cpp10
-rw-r--r--src/widgets/WebViewStack.hpp1
10 files changed, 75 insertions, 15 deletions
diff --git a/init.lua b/init.lua
index b16b08a..6e45040 100644
--- a/init.lua
+++ b/init.lua
@@ -61,21 +61,25 @@ web.event.add_listener('NotificationReceived', {
end,
})
+local start_clock = function(win_id)
+ local timer = web.uv.new_timer()
+ timer:start(500, 500, function()
+ local view = web.decorations.bottom.view({ win = win_id })
+ if view ~= nil then
+ local time = os.date("%X", os.time())
+ web.view.set_html('Time: ' .. time, { view = view })
+ end
+ end)
+end
+
-- Decorations config
web.event.add_listener('WinCreated', {
callback = function(event)
web.decorations.bottom.enable({ win = event.win_id })
- local timer = web.uv.new_timer()
- timer:start(100, 0, function()
- local view = web.decorations.bottom.view({ win = event.win_id })
- print('>>> view', view)
- if view ~= nil then
- web.view.set_url('https://google.com', view)
- end
- timer:close()
- end)
+ start_clock(event.win_id)
end,
})
+
web.keymap.set('n', '<space>gg', function()
if web.decorations.bottom.is_enabled({ win = 0 }) then
web.decorations.bottom.disable({ win = 0 })
diff --git a/lua/null-browser/api.lua b/lua/null-browser/api.lua
index a414522..62dfc76 100644
--- a/lua/null-browser/api.lua
+++ b/lua/null-browser/api.lua
@@ -111,6 +111,21 @@ function web.view.select(view_id) return __internals.view_select(view_id) end
--- ```
function web.view.set_url(url, view_id) return __internals.view_set_url(url, view_id) end
+--- @class SetHTMLOpts
+--- @field view? number View id
+
+--- Set html in a given view
+---
+--- @param html string HTML string
+--- @param opts? SetHTMLOpts Options
+---
+--- @example
+--- ```lua
+--- web.view.set_html('<h2>HJello</h2>')
+--- web.view.set_html('<h2>HJello</h2>', 3) -- Set html for view with id 3
+--- ```
+function web.view.set_html(html, opts) return __internals.view_set_html(html, (opts or {}).view) end
+
--- Open devtools window for the view
---
--- @param view_id? number Id of the view
diff --git a/src/LuaRuntime.hpp b/src/LuaRuntime.hpp
index 0f46eab..1132270 100644
--- a/src/LuaRuntime.hpp
+++ b/src/LuaRuntime.hpp
@@ -63,6 +63,7 @@ signals:
void webview_scroll_top_requested(WebViewId webview_id);
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);
protected:
LuaRuntime();
diff --git a/src/LuaRuntimeApi.hpp b/src/LuaRuntimeApi.hpp
index 9f6c631..17e0fe9 100644
--- a/src/LuaRuntimeApi.hpp
+++ b/src/LuaRuntimeApi.hpp
@@ -18,6 +18,14 @@ int lua_api_view_set_url(lua_State *state) {
return 1;
}
+int lua_api_view_set_html(lua_State *state) {
+ const char *html = lua_tostring(state, 1);
+ WebViewId view_id = lua_isnoneornil(state, 2) ? 0 : lua_tointeger(state, 2);
+ auto &runtime = LuaRuntime::instance();
+ emit runtime.set_view_html(html, view_id);
+ return 1;
+}
+
int lua_api_view_create(lua_State *state) {
const char *url = luaL_optstring(state, 1, "");
auto &runtime = LuaRuntime::instance();
@@ -315,6 +323,7 @@ static luaL_Reg internals_api[] = {
luaL_Reg{"view_list", &lua_view_list},
luaL_Reg{"view_select", &lua_view_select},
luaL_Reg{"view_set_url", &lua_api_view_set_url},
+ luaL_Reg{"view_set_html", &lua_api_view_set_html},
luaL_Reg{"view_open_devtools", &lua_api_view_open_devtools},
luaL_Reg{"view_scroll", &lua_api_view_scroll},
luaL_Reg{"view_scroll_to_top", &lua_api_view_scroll_top},
diff --git a/src/WindowActionRouter.cpp b/src/WindowActionRouter.cpp
index 6028c60..1278d48 100644
--- a/src/WindowActionRouter.cpp
+++ b/src/WindowActionRouter.cpp
@@ -59,6 +59,10 @@ void WindowActionRouter::initialize(Configuration *config) {
WITH_WEBVIEW_WINDOW(webview_id, window,
{ window->open_url(url, open_type, webview_id); });
});
+ connect(&runtime, &LuaRuntime::set_view_html, this,
+ [this](const QString &html, WebViewId webview_id) {
+ WITH_WEBVIEW_WINDOW(webview_id, window, { window->set_html(html, webview_id); });
+ });
connect(&runtime, &LuaRuntime::webview_closed, this, [this](WebViewId webview_id) {
WITH_WEBVIEW_WINDOW(webview_id, window, { window->close_webview(webview_id); });
});
diff --git a/src/widgets/BrowserWindow.hpp b/src/widgets/BrowserWindow.hpp
index 207524a..92eafa7 100644
--- a/src/widgets/BrowserWindow.hpp
+++ b/src/widgets/BrowserWindow.hpp
@@ -46,6 +46,13 @@ public:
webview_stack->open_url(url, open_type, webview_id);
}
+ void set_html(const QString &html, WebViewId webview_id) {
+ if (decorations->has_webview(webview_id))
+ decorations->set_html(html, webview_id);
+ else
+ webview_stack->set_html(html, webview_id);
+ }
+
bool on_window_key_event(QKeyEvent *event);
void closeEvent(QCloseEvent * /*event*/) override { emit closed(); };
diff --git a/src/widgets/Decorations.hpp b/src/widgets/Decorations.hpp
index 57fc526..8eac419 100644
--- a/src/widgets/Decorations.hpp
+++ b/src/widgets/Decorations.hpp
@@ -44,6 +44,15 @@ public:
}
}
+ void set_html(const QString &html, WebViewId view_id) {
+ for (auto *decoration : decorations()) {
+ if (decoration->get_view_id() == view_id) {
+ decoration->set_html(html);
+ return;
+ }
+ }
+ }
+
private:
EdgeDecoration *decoration_top;
EdgeDecoration *decoration_bottom;
diff --git a/src/widgets/EdgeDecoration.cpp b/src/widgets/EdgeDecoration.cpp
index 4cbb004..8880294 100644
--- a/src/widgets/EdgeDecoration.cpp
+++ b/src/widgets/EdgeDecoration.cpp
@@ -15,7 +15,7 @@ QString default_html_layout = R"HTML(
:where(html, body) {
margin: 0;
padding: 0;
- background: black;
+ background: #333;
color: white;
overflow: hidden;
}
@@ -30,8 +30,8 @@ EdgeDecoration::EdgeDecoration(bool vertical, QWebEngineProfile *profile, QWidge
setLayout(vbox);
}
-void EdgeDecoration::set_html(const QString &content) {
- html_content = content;
+void EdgeDecoration::set_enabled(bool enabled_value) {
+ enabled = enabled_value;
setup_webview();
}
@@ -40,8 +40,8 @@ void EdgeDecoration::set_size(u_int16_t size_value) {
setup_webview();
}
-void EdgeDecoration::set_enabled(bool enabled_value) {
- enabled = enabled_value;
+void EdgeDecoration::set_html(const QString &content) {
+ html_content = content;
setup_webview();
}
@@ -52,7 +52,7 @@ void EdgeDecoration::setup_webview() {
layout()->addWidget(webview.value());
}
- webview.value()->setHtml(default_html_layout.replace("{{body}}", html_content));
+ webview.value()->setHtml(QString(default_html_layout).replace("{{body}}", html_content));
if (vertical)
webview.value()->setFixedWidth(size);
else
diff --git a/src/widgets/WebViewStack.cpp b/src/widgets/WebViewStack.cpp
index 45d9c88..698cb88 100644
--- a/src/widgets/WebViewStack.cpp
+++ b/src/widgets/WebViewStack.cpp
@@ -318,3 +318,13 @@ void WebViewStack::scroll_to_bottom(WebViewId webview_id) {
webview->scroll_to_bottom();
}
+
+void WebViewStack::set_html(const QString &html, WebViewId webview_id) {
+ auto *webview = get_webview(webview_id);
+ if (webview == nullptr) {
+ qDebug() << "Webview does not exist";
+ return;
+ }
+
+ webview->setHtml(html);
+}
diff --git a/src/widgets/WebViewStack.hpp b/src/widgets/WebViewStack.hpp
index a5c4d4c..3da96aa 100644
--- a/src/widgets/WebViewStack.hpp
+++ b/src/widgets/WebViewStack.hpp
@@ -72,6 +72,7 @@ public slots:
void scroll(WebViewId webview_id, int deltax, int deltay);
void scroll_to_top(WebViewId webview_id);
void scroll_to_bottom(WebViewId webview_id);
+ void set_html(const QString &html, WebViewId webview_id = 0);
protected slots:
void on_new_webview_request(QWebEngineNewWindowRequest &request);