diff options
Diffstat (limited to '')
| -rw-r--r-- | TODO.org | 2 | ||||
| -rw-r--r-- | lua/null-browser/api.lua | 9 | ||||
| -rw-r--r-- | lua/null-browser/defaults/vi.lua | 10 | ||||
| -rw-r--r-- | src/LuaRuntime.hpp | 3 | ||||
| -rw-r--r-- | src/LuaRuntimeApi.hpp | 29 | ||||
| -rw-r--r-- | src/WindowActionRouter.cpp | 15 | ||||
| -rw-r--r-- | src/WindowMediator.hpp | 3 | ||||
| -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 |
11 files changed, 127 insertions, 2 deletions
@@ -6,7 +6,7 @@ - [X] Dev tools - [X] Configuration - [X] Searchengines -- [ ] Scroll api (for j/k/h/l/gg/G) +- [X] Scroll api (for j/k/h/l/gg/G) - [ ] Tests for api - [ ] table extend/merge - [ ] Fullscreen diff --git a/lua/null-browser/api.lua b/lua/null-browser/api.lua index ea7e0a7..170347f 100644 --- a/lua/null-browser/api.lua +++ b/lua/null-browser/api.lua @@ -274,4 +274,13 @@ function web.search.next(view_id) return __internals.search_next(view_id) end --- ``` function web.search.previous(view_id) return __internals.search_previous(view_id) end +--- TODO: Document +function web.view.scroll(deltax, deltay, view_id) return __internals.view_scroll(deltax, deltay, view_id) end + +--- TODO: Document +function web.view.scroll_to_top(view_id) return __internals.view_scroll_to_top(view_id) end + +--- TODO: Document +function web.view.scroll_to_bottom(view_id) return __internals.view_scroll_to_bottom(view_id) end + print("api loaded") diff --git a/lua/null-browser/defaults/vi.lua b/lua/null-browser/defaults/vi.lua index dee3bc5..d73ffb5 100644 --- a/lua/null-browser/defaults/vi.lua +++ b/lua/null-browser/defaults/vi.lua @@ -7,7 +7,7 @@ local M = {} local config = { menu = require 'null-browser.extras.dmenu', history = require 'null-browser.extras.history', - preprocess_url = function(url) return url end, + preprocess_url = trim, } @@ -23,6 +23,14 @@ function M.initialize() web.keymap.set('n', 'i', function() web.keymap.set_mode('i') end) web.keymap.set('i', '<esc>', function() web.keymap.set_mode('n') end) + -- Scrolling + web.keymap.set('n', 'j', function() web.view.scroll(0, 20) end) + web.keymap.set('n', 'k', function() web.view.scroll(0, -20) end) + web.keymap.set('n', '<s-j>', function() web.view.scroll(0, 80) end) + web.keymap.set('n', '<s-k>', function() web.view.scroll(0, -80) end) + web.keymap.set('n', 'gg', function() web.view.scroll_to_top() end) + web.keymap.set('n', '<s-g>', function() web.view.scroll_to_bottom() end) + -- Open in new view web.keymap.set('n', 'o', function() print(web.get('new_view_url'), web.get('user_agent')) 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); |
