diff options
Diffstat (limited to 'src/widgets')
| -rw-r--r-- | src/widgets/WebView.cpp | 22 | ||||
| -rw-r--r-- | src/widgets/WebView.hpp | 3 | ||||
| -rw-r--r-- | src/widgets/WebViewStack.cpp | 30 | ||||
| -rw-r--r-- | src/widgets/WebViewStack.hpp | 3 |
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); |
