aboutsummaryrefslogtreecommitdiff
path: root/src/widgets
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/widgets
parent1dd6332756b38bf4bbffeef9db6599320c03ca9a (diff)
downloadnull-browser-3f5325f59ba7f8552d093fdafd9674b713f3cad9.tar.gz
null-browser-3f5325f59ba7f8552d093fdafd9674b713f3cad9.zip
Add scrolling controls
Diffstat (limited to 'src/widgets')
-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
4 files changed, 58 insertions, 0 deletions
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);