diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-07-26 00:39:23 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-07-26 00:39:23 +0530 |
| commit | 3f56d76a2b0d528c05ab8d0a38059ca82ba90952 (patch) | |
| tree | daae7849815c122803f8ddd08cbfcb87552eab8a /src | |
| parent | 031b22e0bb0f1d132731aea3776917731a8ab4ce (diff) | |
| download | null-browser-3f56d76a2b0d528c05ab8d0a38059ca82ba90952.tar.gz null-browser-3f56d76a2b0d528c05ab8d0a38059ca82ba90952.zip | |
Add set_html for views + example use with decorations
Diffstat (limited to 'src')
| -rw-r--r-- | src/LuaRuntime.hpp | 1 | ||||
| -rw-r--r-- | src/LuaRuntimeApi.hpp | 9 | ||||
| -rw-r--r-- | src/WindowActionRouter.cpp | 4 | ||||
| -rw-r--r-- | src/widgets/BrowserWindow.hpp | 7 | ||||
| -rw-r--r-- | src/widgets/Decorations.hpp | 9 | ||||
| -rw-r--r-- | src/widgets/EdgeDecoration.cpp | 12 | ||||
| -rw-r--r-- | src/widgets/WebViewStack.cpp | 10 | ||||
| -rw-r--r-- | src/widgets/WebViewStack.hpp | 1 |
8 files changed, 47 insertions, 6 deletions
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); |
