diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-04-19 17:56:03 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-04-19 17:56:03 +0530 |
| commit | 0e95e99e72237058465959d55b77750a9d7c1bef (patch) | |
| tree | 5b99cd64f7c49b949a6381c4654ec253822934b5 /src | |
| parent | 7a458b424c2c1a6ed251211b25261acffef59b7f (diff) | |
| download | null-browser-0e95e99e72237058465959d55b77750a9d7c1bef.tar.gz null-browser-0e95e99e72237058465959d55b77750a9d7c1bef.zip | |
Add web.search and search text implemnetation
Diffstat (limited to '')
| -rw-r--r-- | src/AsyncEventLoop.cpp | 2 | ||||
| -rw-r--r-- | src/LuaRuntime.hpp | 3 | ||||
| -rw-r--r-- | src/LuaRuntimeApi.hpp | 29 | ||||
| -rw-r--r-- | src/WindowActionRouter.cpp | 17 | ||||
| -rw-r--r-- | src/WindowActionRouter.hpp | 1 | ||||
| -rw-r--r-- | src/WindowMediator.hpp | 1 | ||||
| -rw-r--r-- | src/widgets/WebView.hpp | 1 | ||||
| -rw-r--r-- | src/widgets/WebViewStack.cpp | 20 | ||||
| -rw-r--r-- | src/widgets/WebViewStack.hpp | 1 |
9 files changed, 72 insertions, 3 deletions
diff --git a/src/AsyncEventLoop.cpp b/src/AsyncEventLoop.cpp index f11babb..12e0351 100644 --- a/src/AsyncEventLoop.cpp +++ b/src/AsyncEventLoop.cpp @@ -74,7 +74,7 @@ AsyncEventLoop::~AsyncEventLoop() { uv_run(loop, UV_RUN_DEFAULT); if (uv_loop_alive(loop) > 0) - qDebug() << "WARNING: Loop still has active handles!"; + qWarning() << "WARNING: Loop still has active handles!"; free(loop); loop = nullptr; diff --git a/src/LuaRuntime.hpp b/src/LuaRuntime.hpp index f684409..0855570 100644 --- a/src/LuaRuntime.hpp +++ b/src/LuaRuntime.hpp @@ -52,6 +52,9 @@ signals: void webview_closed(WebViewId webview_id); void webview_selected(WebViewId webview_id); void config_updated(const QString &key, const QVariant &value); + void search_requested(const QString &text, WebViewId webview_id); + void search_next_requested(WebViewId webview_id); + void search_previous_requested(WebViewId webview_id); protected: LuaRuntime(); diff --git a/src/LuaRuntimeApi.hpp b/src/LuaRuntimeApi.hpp index 809b579..b6302ff 100644 --- a/src/LuaRuntimeApi.hpp +++ b/src/LuaRuntimeApi.hpp @@ -5,7 +5,7 @@ #include "LuaRuntime.hpp" #include "WindowActionRouter.hpp" -int lua_api_open_url(lua_State *state) { +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); auto &runtime = LuaRuntime::instance(); @@ -187,6 +187,28 @@ int lua_event_register(lua_State *state) { return 1; } +int lua_api_search_set_text(lua_State *state) { + const char *text = lua_tostring(state, 1); + WebViewId view_id = lua_isnoneornil(state, 1) ? 0 : lua_tointeger(state, 1); + auto &runtime = LuaRuntime::instance(); + emit runtime.search_requested(text, view_id); + return 1; +} + +int lua_api_search_next(lua_State *state) { + WebViewId view_id = lua_isnoneornil(state, 1) ? 0 : lua_tointeger(state, 1); + auto &runtime = LuaRuntime::instance(); + emit runtime.search_next_requested(view_id); + return 1; +} + +int lua_api_search_previous(lua_State *state) { + WebViewId view_id = lua_isnoneornil(state, 1) ? 0 : lua_tointeger(state, 1); + auto &runtime = LuaRuntime::instance(); + emit runtime.search_previous_requested(view_id); + return 1; +} + // NOLINTNEXTLINE static luaL_Reg internals_api[] = { luaL_Reg{"event_add_listener", &lua_event_register}, @@ -198,8 +220,11 @@ static luaL_Reg internals_api[] = { luaL_Reg{"view_current", &lua_api_view_current}, luaL_Reg{"view_list", &lua_view_list}, luaL_Reg{"view_select", &lua_view_select}, - luaL_Reg{"view_set_url", &lua_api_open_url}, + luaL_Reg{"view_set_url", &lua_api_view_set_url}, luaL_Reg{"history_back", &lua_history_back}, luaL_Reg{"history_forward", &lua_history_forward}, + luaL_Reg{"search_set_text", &lua_api_search_set_text}, + luaL_Reg{"search_previous", &lua_api_search_previous}, + luaL_Reg{"search_next", &lua_api_search_next}, luaL_Reg{nullptr, nullptr}, }; diff --git a/src/WindowActionRouter.cpp b/src/WindowActionRouter.cpp index d0b529b..5f787c0 100644 --- a/src/WindowActionRouter.cpp +++ b/src/WindowActionRouter.cpp @@ -58,6 +58,23 @@ void WindowActionRouter::initialize(Configuration *config) { WITH_WEBVIEW_WINDOW(webview_id, window, { emit window->mediator()->webview_selected(webview_id); }); }); + connect(&runtime, &LuaRuntime::search_requested, this, + [this](const QString &text, WebViewId webview_id) { + WITH_WEBVIEW_WINDOW(webview_id, window, { + this->current_search_text = text; + win_match.second->mediator()->set_search_text(text, webview_id, true); + }) + }); + connect(&runtime, &LuaRuntime::search_next_requested, this, [this](WebViewId webview_id) { + WITH_WEBVIEW_WINDOW(webview_id, window, { + win_match.second->mediator()->set_search_text(this->current_search_text, webview_id, true); + }) + }); + connect(&runtime, &LuaRuntime::search_previous_requested, this, [this](WebViewId webview_id) { + WITH_WEBVIEW_WINDOW(webview_id, window, { + win_match.second->mediator()->set_search_text(this->current_search_text, webview_id, false); + }) + }); } void WindowActionRouter::add_window(BrowserWindow *window) { diff --git a/src/WindowActionRouter.hpp b/src/WindowActionRouter.hpp index 17b935f..c1e6ecc 100644 --- a/src/WindowActionRouter.hpp +++ b/src/WindowActionRouter.hpp @@ -54,4 +54,5 @@ private: Configuration *configuration; EventQueue event_queue; + QString current_search_text; }; diff --git a/src/WindowMediator.hpp b/src/WindowMediator.hpp index b2ecb77..17190b3 100644 --- a/src/WindowMediator.hpp +++ b/src/WindowMediator.hpp @@ -16,6 +16,7 @@ public: DELEGATE(webview_stack, has_webview, has_webview) DELEGATE(webview_stack, current_webview_id, current_webview_id) DELEGATE(webview_stack, get_webview_list, get_webview_list) + DELEGATE(webview_stack, set_search_text, set_search_text) signals: void history_back_requested(WebViewId webview_id, qsizetype history_index); diff --git a/src/widgets/WebView.hpp b/src/widgets/WebView.hpp index 3f73316..98dceba 100644 --- a/src/widgets/WebView.hpp +++ b/src/widgets/WebView.hpp @@ -3,6 +3,7 @@ #include <QWebEngineView> #include <QtCore> #include <cstdint> +#include <qtypes.h> #include "utils.hpp" diff --git a/src/widgets/WebViewStack.cpp b/src/widgets/WebViewStack.cpp index 258fbc6..16fd3e9 100644 --- a/src/widgets/WebViewStack.cpp +++ b/src/widgets/WebViewStack.cpp @@ -1,4 +1,5 @@ #include <QStackedLayout> +#include <QWebEngineFindTextResult> #include <QWebEngineHistory> #include <QWebEngineNewWindowRequest> #include <QWebEngineProfile> @@ -198,6 +199,25 @@ void WebViewStack::focus_webview(WebViewId webview_id) { layout->setCurrentIndex((int)webview_index); } +void WebViewStack::set_search_text(const QString &text, WebViewId webview_id, bool forward) { + auto *webview = get_webview(webview_id); + if (webview == nullptr) { + qDebug() << "Webview does not exist"; + return; + } + + QString search_text = text.trimmed(); + + auto flags = + search_text.isLower() ? QWebEnginePage::FindFlags() : QWebEnginePage::FindCaseSensitively; + if (!forward) + flags |= QWebEnginePage::FindBackward; + + webview->page()->findText(search_text, flags, [](const QWebEngineFindTextResult &result) { + qDebug() << result.numberOfMatches() << result.activeMatch(); + }); +} + WebView *WebViewStack::get_webview(WebViewId webview_id) { auto webview_index = get_webview_index(webview_id); if (webview_index < 0) diff --git a/src/widgets/WebViewStack.hpp b/src/widgets/WebViewStack.hpp index 05362d6..e41af43 100644 --- a/src/widgets/WebViewStack.hpp +++ b/src/widgets/WebViewStack.hpp @@ -65,6 +65,7 @@ public slots: void webview_history_forward(WebViewId webview_id, qsizetype history_index); void close(WebViewId webview_id); void focus_webview(WebViewId webview_id); + void set_search_text(const QString &text, WebViewId webview_id, bool forward = true); protected slots: void on_new_webview_request(const QWebEngineNewWindowRequest &request); |
