diff options
Diffstat (limited to '')
| -rw-r--r-- | src/LuaRuntime.cpp | 34 | ||||
| -rw-r--r-- | src/widgets/BrowserManager.cpp | 27 | ||||
| -rw-r--r-- | src/widgets/MainWindow.cpp | 19 |
3 files changed, 77 insertions, 3 deletions
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(); } |
