aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2025-04-20 22:57:32 +0530
committerAkshay Nair <phenax5@gmail.com>2025-05-02 16:17:49 +0530
commit3f5325f59ba7f8552d093fdafd9674b713f3cad9 (patch)
tree481c26e17714fd673c024aba3c3514586f0fe3bb /src
parent1dd6332756b38bf4bbffeef9db6599320c03ca9a (diff)
downloadnull-browser-3f5325f59ba7f8552d093fdafd9674b713f3cad9.tar.gz
null-browser-3f5325f59ba7f8552d093fdafd9674b713f3cad9.zip
Add scrolling controls
Diffstat (limited to 'src')
-rw-r--r--src/LuaRuntime.hpp3
-rw-r--r--src/LuaRuntimeApi.hpp29
-rw-r--r--src/WindowActionRouter.cpp15
-rw-r--r--src/WindowMediator.hpp3
-rw-r--r--src/widgets/WebView.cpp22
-rw-r--r--src/widgets/WebView.hpp3
-rw-r--r--src/widgets/WebViewStack.cpp30
-rw-r--r--src/widgets/WebViewStack.hpp3
8 files changed, 108 insertions, 0 deletions
diff --git a/src/LuaRuntime.hpp b/src/LuaRuntime.hpp
index abefd86..bff200a 100644
--- a/src/LuaRuntime.hpp
+++ b/src/LuaRuntime.hpp
@@ -58,6 +58,9 @@ signals:
void search_next_requested(WebViewId webview_id);
void search_previous_requested(WebViewId webview_id);
void devtools_requested(WebViewId webview_id);
+ void webview_scroll_requested(WebViewId webview_id, int deltax, int deltay);
+ void webview_scroll_top_requested(WebViewId webview_id);
+ void webview_scroll_bottom_requested(WebViewId webview_id);
protected:
LuaRuntime();
diff --git a/src/LuaRuntimeApi.hpp b/src/LuaRuntimeApi.hpp
index 4767134..9994491 100644
--- a/src/LuaRuntimeApi.hpp
+++ b/src/LuaRuntimeApi.hpp
@@ -238,6 +238,32 @@ int lua_api_keymap_set_mode(lua_State *state) {
return 1;
}
+int lua_api_view_scroll(lua_State *state) {
+ long deltax = lua_tointeger(state, 1);
+ long deltay = lua_tointeger(state, 2);
+ WebViewId view_id = lua_isnoneornil(state, 1) ? 0 : lua_tointeger(state, 1);
+ auto &runtime = LuaRuntime::instance();
+ emit runtime.webview_scroll_requested(view_id, (int)deltax, (int)deltay);
+
+ lua_pushnil(state);
+ return 1;
+}
+
+int lua_api_view_scroll_top(lua_State *state) {
+ WebViewId view_id = lua_isnoneornil(state, 1) ? 0 : lua_tointeger(state, 1);
+ auto &runtime = LuaRuntime::instance();
+ emit runtime.webview_scroll_top_requested(view_id);
+ lua_pushnil(state);
+ return 1;
+}
+int lua_api_view_scroll_bottom(lua_State *state) {
+ WebViewId view_id = lua_isnoneornil(state, 1) ? 0 : lua_tointeger(state, 1);
+ auto &runtime = LuaRuntime::instance();
+ emit runtime.webview_scroll_bottom_requested(view_id);
+ lua_pushnil(state);
+ return 1;
+}
+
// NOLINTNEXTLINE
static luaL_Reg internals_api[] = {
luaL_Reg{"event_add_listener", &lua_event_register},
@@ -253,6 +279,9 @@ static luaL_Reg internals_api[] = {
luaL_Reg{"view_select", &lua_view_select},
luaL_Reg{"view_set_url", &lua_api_view_set_url},
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},
+ luaL_Reg{"view_scroll_to_bottom", &lua_api_view_scroll_bottom},
luaL_Reg{"history_back", &lua_history_back},
luaL_Reg{"history_forward", &lua_history_forward},
luaL_Reg{"search_get_text", &lua_api_search_get_text},
diff --git a/src/WindowActionRouter.cpp b/src/WindowActionRouter.cpp
index 2eabc94..721a3e7 100644
--- a/src/WindowActionRouter.cpp
+++ b/src/WindowActionRouter.cpp
@@ -77,6 +77,21 @@ void WindowActionRouter::initialize(Configuration *config) {
WITH_WEBVIEW_WINDOW(webview_id, window,
{ win_match.second->mediator()->open_devtools(webview_id); })
});
+
+ // Scroll
+ connect(&runtime, &LuaRuntime::webview_scroll_requested, this,
+ [this](WebViewId webview_id, int deltax, int deltay) {
+ WITH_WEBVIEW_WINDOW(webview_id, window,
+ { window->mediator()->scroll(webview_id, deltax, deltay); });
+ });
+ connect(&runtime, &LuaRuntime::webview_scroll_top_requested, this, [this](WebViewId webview_id) {
+ WITH_WEBVIEW_WINDOW(webview_id, window, { window->mediator()->scroll_to_top(webview_id); });
+ });
+ connect(&runtime, &LuaRuntime::webview_scroll_bottom_requested, this,
+ [this](WebViewId webview_id) {
+ WITH_WEBVIEW_WINDOW(webview_id, window,
+ { window->mediator()->scroll_to_bottom(webview_id); });
+ });
}
void WindowActionRouter::find_current_search_text(WebViewId webview_id, bool forward) {
diff --git a/src/WindowMediator.hpp b/src/WindowMediator.hpp
index 6046292..0a27f2e 100644
--- a/src/WindowMediator.hpp
+++ b/src/WindowMediator.hpp
@@ -23,6 +23,9 @@ public:
DELEGATE(webview_stack, webview_history_forward, history_forward)
DELEGATE(webview_stack, close, close_webview)
DELEGATE(webview_stack, focus_webview, select_webview)
+ DELEGATE(webview_stack, scroll, scroll)
+ DELEGATE(webview_stack, scroll_to_top, scroll_to_top)
+ DELEGATE(webview_stack, scroll_to_bottom, scroll_to_bottom)
signals:
void new_window_requested(const QUrl &url);
diff --git a/src/widgets/WebView.cpp b/src/widgets/WebView.cpp
index aead605..0ec2a74 100644
--- a/src/widgets/WebView.cpp
+++ b/src/widgets/WebView.cpp
@@ -21,3 +21,25 @@ void WebView::open_devtools() {
page()->setDevToolsPage(devtools_window->page());
}
+
+void WebView::scroll_increment(int deltax, int deltay) {
+ auto code = QString(R"((() => {
+ const $el = document.scrollingElement;
+ $el.scrollTo($el.scrollLeft + %1, $el.scrollTop + %2);
+ })())")
+ .arg(deltax)
+ .arg(deltay);
+
+ page()->runJavaScript(code);
+}
+
+void WebView::scroll_to_top() {
+ auto code = QString(R"(document.scrollingElement.scrollTo(0, 0))");
+ page()->runJavaScript(code);
+}
+
+void WebView::scroll_to_bottom() {
+ auto code =
+ QString(R"(document.scrollingElement.scrollTo(0, document.scrollingElement.scrollHeight))");
+ page()->runJavaScript(code);
+}
diff --git a/src/widgets/WebView.hpp b/src/widgets/WebView.hpp
index c861b6e..0900ea6 100644
--- a/src/widgets/WebView.hpp
+++ b/src/widgets/WebView.hpp
@@ -37,6 +37,9 @@ class WebView : public QWebEngineView {
public:
WebView(uint32_t webview_id, QWebEngineProfile *profile, QWidget *parent_node = nullptr);
void open_devtools();
+ void scroll_increment(int deltax, int deltay);
+ void scroll_to_top();
+ void scroll_to_bottom();
DEFINE_GETTER(get_id, id)
diff --git a/src/widgets/WebViewStack.cpp b/src/widgets/WebViewStack.cpp
index 9eef92a..53224c5 100644
--- a/src/widgets/WebViewStack.cpp
+++ b/src/widgets/WebViewStack.cpp
@@ -276,3 +276,33 @@ void WebViewStack::set_webview_url(const QUrl &url, WebViewId webview_id) {
webview->setUrl(url);
}
+
+void WebViewStack::scroll(WebViewId webview_id, int deltax, int deltay) {
+ auto *webview = get_webview(webview_id);
+ if (webview == nullptr) {
+ qDebug() << "Webview does not exist";
+ return;
+ }
+
+ webview->scroll_increment(deltax, deltay);
+}
+
+void WebViewStack::scroll_to_top(WebViewId webview_id) {
+ auto *webview = get_webview(webview_id);
+ if (webview == nullptr) {
+ qDebug() << "Webview does not exist";
+ return;
+ }
+
+ webview->scroll_to_top();
+}
+
+void WebViewStack::scroll_to_bottom(WebViewId webview_id) {
+ auto *webview = get_webview(webview_id);
+ if (webview == nullptr) {
+ qDebug() << "Webview does not exist";
+ return;
+ }
+
+ webview->scroll_to_bottom();
+}
diff --git a/src/widgets/WebViewStack.hpp b/src/widgets/WebViewStack.hpp
index fb78255..6e17753 100644
--- a/src/widgets/WebViewStack.hpp
+++ b/src/widgets/WebViewStack.hpp
@@ -67,6 +67,9 @@ public slots:
void focus_webview(WebViewId webview_id);
void set_search_text(const QString &text, WebViewId webview_id, bool forward = true);
void open_devtools(WebViewId webview_id);
+ void scroll(WebViewId webview_id, int deltax, int deltay);
+ void scroll_to_top(WebViewId webview_id);
+ void scroll_to_bottom(WebViewId webview_id);
protected slots:
void on_new_webview_request(const QWebEngineNewWindowRequest &request);