diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-03-09 22:09:09 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-03-09 23:41:36 +0530 |
| commit | 6b1d49e607da16853d1a0f460dbe756538e4790c (patch) | |
| tree | d5c784e6e568272afbb7530ea5dd752ce835b8fa | |
| parent | 61f99089288138989cf244d174882aa5088b738b (diff) | |
| download | null-browser-6b1d49e607da16853d1a0f460dbe756538e4790c.tar.gz null-browser-6b1d49e607da16853d1a0f460dbe756538e4790c.zip | |
Add lua integration for command input (web.open, web.tabopen)
Diffstat (limited to '')
| -rw-r--r-- | CMakeLists.txt | 8 | ||||
| -rw-r--r-- | include/LuaRuntime.hpp | 31 | ||||
| -rw-r--r-- | include/widgets/BrowserManager.hpp | 5 | ||||
| -rw-r--r-- | include/widgets/MainWindow.hpp | 2 | ||||
| -rw-r--r-- | src/LuaRuntime.cpp | 34 | ||||
| -rw-r--r-- | src/widgets/BrowserManager.cpp | 27 | ||||
| -rw-r--r-- | src/widgets/MainWindow.cpp | 19 |
7 files changed, 119 insertions, 7 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 94365c0..016f1ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,8 +32,7 @@ target_link_libraries(${PROJECT} # LuaJIT pkg_check_modules(LuaJIT REQUIRED luajit) -include_directories(${LuaJIT_INCLUDE_DIRS}) -link_directories(${LuaJIT_LIBRARY_DIRS}) +target_include_directories(${PROJECT} PRIVATE ${LuaJIT_INCLUDE_DIRS}) target_link_libraries(${PROJECT} ${LuaJIT_LINK_LIBRARIES}) # CEF (experiment) @@ -45,12 +44,13 @@ target_link_libraries(${PROJECT} ${LuaJIT_LINK_LIBRARIES}) enable_testing() add_executable(tests) target_sources(tests PRIVATE ${SOURCE_FILES} ${MOC_SRCS} ${SPEC_FILES}) -target_include_directories(tests PRIVATE include/ spec/) +target_include_directories(tests PRIVATE include/ spec/ ${LuaJIT_INCLUDE_DIRS}) target_link_libraries(tests PRIVATE Qt6::Core Qt6::Widgets Qt6::WebEngineWidgets - Qt6::Test) + Qt6::Test + ${LuaJIT_LINK_LIBRARIES}) add_test(NAME CommandInputSpec COMMAND tests) install(TARGETS ${PROJECT} RUNTIME DESTINATION bin) diff --git a/include/LuaRuntime.hpp b/include/LuaRuntime.hpp new file mode 100644 index 0000000..85bb1ae --- /dev/null +++ b/include/LuaRuntime.hpp @@ -0,0 +1,31 @@ +#pragma once +#include <QtCore> +#include <lua.hpp> + +#include "widgets/BrowserManager.hpp" + +class LuaRuntime : public QObject { + Q_OBJECT + +public: + static LuaRuntime *instance() { + static LuaRuntime inst; + return &inst; + } + + void evaluate(const char *code); + +protected: + LuaRuntime(); + ~LuaRuntime(); + +signals: + void urlOpenned(QString url, OpenType openType); + +protected: + static int lua_onUrlOpen(lua_State *state); + static int lua_onUrlTabOpen(lua_State *state); + +private: + lua_State *state; +}; diff --git a/include/widgets/BrowserManager.hpp b/include/widgets/BrowserManager.hpp index 84d6f6a..8cf265d 100644 --- a/include/widgets/BrowserManager.hpp +++ b/include/widgets/BrowserManager.hpp @@ -5,6 +5,8 @@ #include <QWebEngineView> #include <sys/types.h> +enum OpenType { OpenUrl, OpenUrlInTab, OpenUrlInBgTab, OpenUrlInWindow }; + class BrowserManager : public QWidget { Q_OBJECT @@ -20,6 +22,9 @@ public: QWebEngineView *createNewWebView(QUrl url = BrowserManager::NewtabURL, bool focus = false); + void openUrl(QUrl url = BrowserManager::NewtabURL, + OpenType openType = OpenType::OpenUrl); + std::vector<QUrl> webViewUrls(); u_int32_t currentWebViewIndex(); u_int32_t webViewCount(); diff --git a/include/widgets/MainWindow.hpp b/include/widgets/MainWindow.hpp index 8ba0ed9..15e2889 100644 --- a/include/widgets/MainWindow.hpp +++ b/include/widgets/MainWindow.hpp @@ -4,6 +4,7 @@ #include <QObject> #include <QWebEngineView> +#include "LuaRuntime.hpp" #include "widgets/BrowserManager.hpp" #include "widgets/CommandInput.hpp" @@ -21,4 +22,5 @@ protected: private: BrowserManager *browserManager; CommandInput *commandInput; + LuaRuntime *luaRuntime; }; diff --git a/src/LuaRuntime.cpp b/src/LuaRuntime.cpp new file mode 100644 index 0000000..37845d3 --- /dev/null +++ b/src/LuaRuntime.cpp @@ -0,0 +1,34 @@ +#include <lua.hpp> + +#include "LuaRuntime.hpp" + +LuaRuntime::LuaRuntime() { + state = luaL_newstate(); + luaL_openlibs(state); + + luaL_Reg weblib[] = { + {"open", &LuaRuntime::lua_onUrlOpen}, + {"tabopen", &LuaRuntime::lua_onUrlTabOpen}, + {NULL, NULL}, + }; + luaL_newlib(state, weblib); + lua_setglobal(state, "web"); +} + +void LuaRuntime::evaluate(const char *code) { luaL_dostring(state, code); } + +int LuaRuntime::lua_onUrlOpen(lua_State *state) { + const char *url = luaL_optstring(state, 1, ""); + auto runtime = LuaRuntime::instance(); + emit runtime->urlOpenned(url, OpenType::OpenUrl); + return 1; +} + +int LuaRuntime::lua_onUrlTabOpen(lua_State *state) { + const char *url = luaL_optstring(state, 1, ""); + auto runtime = LuaRuntime::instance(); + emit runtime->urlOpenned(url, OpenType::OpenUrlInTab); + return 1; +} + +LuaRuntime::~LuaRuntime() { lua_close(state); } diff --git a/src/widgets/BrowserManager.cpp b/src/widgets/BrowserManager.cpp index a2390fc..5ac6479 100644 --- a/src/widgets/BrowserManager.cpp +++ b/src/widgets/BrowserManager.cpp @@ -16,6 +16,23 @@ BrowserManager::BrowserManager(QWebEngineProfile *profile) : QWidget() { createNewWebView(BrowserManager::NewtabURL, true); } +void BrowserManager::openUrl(QUrl url, OpenType openType) { + switch (openType) { + case OpenType::OpenUrl: + setCurrentUrl(url); + break; + case OpenType::OpenUrlInTab: + createNewWebView(url, true); + break; + case OpenType::OpenUrlInBgTab: + createNewWebView(url, false); + break; + case OpenType::OpenUrlInWindow: + // TODO: impl + break; + } +} + QWebEngineView *BrowserManager::createNewWebView(QUrl url, bool focus) { auto webview = new QWebEngineView(profile); webview->setUrl(url); @@ -24,6 +41,16 @@ QWebEngineView *BrowserManager::createNewWebView(QUrl url, bool focus) { connect(webview->page(), &QWebEnginePage::newWindowRequested, this, &BrowserManager::onNewWebViewRequest); + // connect(webview->page(), &QWebEnginePage::windowCloseRequested, this, + // [this, webview]() { + // for (int i = 0; i <= this->webViewList.length(); i++) { + // auto w = this->webViewList.at(0); + // printf("::::: %d\n\n", w == webview); + // if (w == webview) { + // this->closeWebView(i); + // } + // } + // }); if (focus) focusWebView(webViewList.length() - 1); diff --git a/src/widgets/MainWindow.cpp b/src/widgets/MainWindow.cpp index e660e52..14f6aa7 100644 --- a/src/widgets/MainWindow.cpp +++ b/src/widgets/MainWindow.cpp @@ -23,7 +23,7 @@ MainWindow::MainWindow() { layout->addWidget(browserManager); // Command input - commandInput = new CommandInput(browserManager->currentUrl().toString()); + commandInput = new CommandInput; hideURLInput(); commandInput->move(0, 0); connect(commandInput, &CommandInput::submitted, this, @@ -31,6 +31,13 @@ MainWindow::MainWindow() { connect(commandInput, &CommandInput::cancelled, this, &MainWindow::hideURLInput); layout->addWidget(commandInput); + + // Lua runtime + luaRuntime = LuaRuntime::instance(); + connect(luaRuntime, &LuaRuntime::urlOpenned, this, + [this](QString url, OpenType openType) { + this->browserManager->openUrl(QUrl(url), openType); + }); } void MainWindow::hideURLInput() { @@ -46,8 +53,14 @@ void MainWindow::showURLInput() { } void MainWindow::evaluateCommand(QString command) { - if (!command.isEmpty()) - browserManager->setCurrentUrl(command); + if (!command.isEmpty()) { + // TODO: Temporary hack + if (command.startsWith("lua ")) { + luaRuntime->evaluate(command.slice(4).toStdString().c_str()); + } else { + browserManager->setCurrentUrl(command); + } + } hideURLInput(); } |
