diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-04-12 11:56:10 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-04-12 11:56:35 +0530 |
| commit | f3f109cd054888f857d51ca2b37b4123f3134069 (patch) | |
| tree | 81250db8a30f85d34d3c101830a37e3535e77054 /src | |
| parent | c0ee5203201d4ddf4ed560f856cb0da958935fc6 (diff) | |
| download | null-browser-f3f109cd054888f857d51ca2b37b4123f3134069.tar.gz null-browser-f3f109cd054888f857d51ca2b37b4123f3134069.zip | |
Add configuration api
Diffstat (limited to 'src')
| -rw-r--r-- | src/Configuration.hpp | 2 | ||||
| -rw-r--r-- | src/LuaRuntime.cpp | 40 | ||||
| -rw-r--r-- | src/LuaRuntime.hpp | 5 | ||||
| -rw-r--r-- | src/WindowActionRouter.cpp | 22 | ||||
| -rw-r--r-- | src/WindowActionRouter.hpp | 3 | ||||
| -rw-r--r-- | src/widgets/BrowserApp.cpp | 4 | ||||
| -rw-r--r-- | src/widgets/BrowserWindow.cpp | 2 | ||||
| -rw-r--r-- | src/widgets/WebViewStack.cpp | 2 |
8 files changed, 65 insertions, 15 deletions
diff --git a/src/Configuration.hpp b/src/Configuration.hpp index c1744e6..06335dc 100644 --- a/src/Configuration.hpp +++ b/src/Configuration.hpp @@ -8,7 +8,7 @@ class Configuration : public QObject { public: using QObject::QObject; - QUrl new_view_url = QUrl("https://lite.duckduckgo.com"); + QString new_view_url = "https://duckduckgo.com"; bool close_window_when_no_views = true; }; diff --git a/src/LuaRuntime.cpp b/src/LuaRuntime.cpp index 036f47f..07d8193 100644 --- a/src/LuaRuntime.cpp +++ b/src/LuaRuntime.cpp @@ -2,6 +2,7 @@ #include <cstdlib> #include <cstring> #include <lua.hpp> +#include <qvariant.h> extern "C" { #include <luv/luv.h> } @@ -73,13 +74,11 @@ void LuaRuntime::evaluate(const QString &code) { }); } -void LuaRuntime::load_file(const QString &path) { - queue_task([this, path]() { - preserve_top(state, { - if (luaL_dofile(state, path.toStdString().c_str()) != LUA_OK) { - qDebug() << "Load file error:" << lua_tostring(state, -1); - } - }) +void LuaRuntime::load_file_sync(const QString &path) { + preserve_top(state, { + if (luaL_dofile(state, path.toStdString().c_str()) != LUA_OK) { + qDebug() << "Load file error:" << lua_tostring(state, -1); + } }); } @@ -131,6 +130,8 @@ void LuaRuntime::init_web_lib() { luaL_Reg internals[] = { {"register_event", &LuaRuntime::lua_event_register}, + {"set_config", &LuaRuntime::lua_config_set}, + {"get_config", &LuaRuntime::lua_config_get}, {nullptr, nullptr}, }; luaL_newlib(state, internals); @@ -210,6 +211,31 @@ int LuaRuntime::lua_view_create(lua_State *state) { return 1; } +int LuaRuntime::lua_config_set(lua_State *state) { + const char *key = lua_tostring(state, 1); + auto &runtime = LuaRuntime::instance(); + QVariant value = runtime.get_lua_value(2, ""); + emit runtime.config_updated(key, value); + return 1; +} + +int LuaRuntime::lua_config_get(lua_State *state) { + const char *key = lua_tostring(state, 1); + auto &router = WindowActionRouter::instance(); + auto value = router.fetch_config_value(key); + + if (value.typeId() == QMetaType::QString) + lua_pushstring(state, value.toString().toStdString().c_str()); + else if (value.typeId() == QMetaType::Int) + lua_pushinteger(state, value.toInt()); + else if (value.typeId() == QMetaType::Double) + lua_pushnumber(state, value.toDouble()); + else + lua_pushnil(state); + + return 1; +} + int LuaRuntime::lua_keymap_set(lua_State *state) { const char *mode = lua_tostring(state, 1); const char *keyseq = lua_tostring(state, 2); diff --git a/src/LuaRuntime.hpp b/src/LuaRuntime.hpp index 64bb1f1..670c0ae 100644 --- a/src/LuaRuntime.hpp +++ b/src/LuaRuntime.hpp @@ -35,7 +35,7 @@ public: } void evaluate(const QString &code); - void load_file(const QString &path); + void load_file_sync(const QString &path); void stop_event_loop(); void start_event_loop(); @@ -54,6 +54,7 @@ signals: void url_opened(QString url, OpenType open_type, WebViewId webview_id); void webview_closed(WebViewId webview_id); void webview_selected(WebViewId webview_id); + void config_updated(const QString &key, const QVariant &value); protected: LuaRuntime(); @@ -71,6 +72,8 @@ protected: static int lua_view_current(lua_State *state); static int lua_view_list(lua_State *state); static int lua_view_select(lua_State *state); + static int lua_config_set(lua_State *state); + static int lua_config_get(lua_State *state); private: lua_State *state; diff --git a/src/WindowActionRouter.cpp b/src/WindowActionRouter.cpp index 51e24e9..4bac3ad 100644 --- a/src/WindowActionRouter.cpp +++ b/src/WindowActionRouter.cpp @@ -9,11 +9,31 @@ #include "widgets/BrowserWindow.hpp" #include "widgets/WebViewStack.hpp" -void WindowActionRouter::initialize() { +QVariant WindowActionRouter::fetch_config_value(const QString &key) { + if (key == "new_view_url") + return configuration->new_view_url; + if (key == "close_window_when_no_views") + return configuration->close_window_when_no_views; + return ""; +} + +// NOLINTNEXTLINE(readability-function-cognitive-complexity) +void WindowActionRouter::initialize(Configuration *config) { auto &runtime = LuaRuntime::instance(); + configuration = config; connect(&runtime, &LuaRuntime::keymap_added, this, &WindowActionRouter::add_keymap); + connect(&runtime, &LuaRuntime::config_updated, this, + [this](const QString &key, const QVariant &value) { + qDebug() << key << value; + if (key == "new_view_url") { + configuration->new_view_url = value.toString(); + } else if (key == "close_window_when_no_views") { + configuration->close_window_when_no_views = value.toBool(); + } + }); + connect(&runtime, &LuaRuntime::history_back_requested, this, [this](WebViewId webview_id, qsizetype history_index) { WITH_WEBVIEW_WINDOW(webview_id, window, { diff --git a/src/WindowActionRouter.hpp b/src/WindowActionRouter.hpp index cfb25da..17b935f 100644 --- a/src/WindowActionRouter.hpp +++ b/src/WindowActionRouter.hpp @@ -27,13 +27,14 @@ public: return router; } - void initialize(); + void initialize(Configuration *config); void add_window(BrowserWindow *window); const WindowMap &windows(); WebViewId fetch_current_view_id(WindowId win_id = 0); QList<WebViewData> fetch_webview_data_list(WindowId win_id = 0); + QVariant fetch_config_value(const QString &key); DELEGATE((&event_queue), dispatch_event, dispatch_event); DELEGATE((&event_queue), register_event, register_event) diff --git a/src/widgets/BrowserApp.cpp b/src/widgets/BrowserApp.cpp index 26124d6..4a43dfb 100644 --- a/src/widgets/BrowserApp.cpp +++ b/src/widgets/BrowserApp.cpp @@ -12,13 +12,13 @@ BrowserApp::BrowserApp() { // Router init auto &window_action_router = WindowActionRouter::instance(); - window_action_router.initialize(); + window_action_router.initialize(&configuration); // Global event filter qApp->installEventFilter(this); // NOTE: TMP - lua.load_file("./config.lua"); + lua.load_file_sync("./config.lua"); connect(&window_action_router, &WindowActionRouter::new_window_requested, this, [this](const QUrl &url) { create_window({url.toString()}); }); diff --git a/src/widgets/BrowserWindow.cpp b/src/widgets/BrowserWindow.cpp index cc3c6a8..2c4c5b4 100644 --- a/src/widgets/BrowserWindow.cpp +++ b/src/widgets/BrowserWindow.cpp @@ -27,7 +27,7 @@ BrowserWindow::BrowserWindow(const Configuration &configuration, const QStringLi // Open webviews for given urls if (urls.isEmpty()) { - webview_stack->open_url(configuration.new_view_url.toString()); + webview_stack->open_url(configuration.new_view_url); } else { for (const auto &url : urls) { webview_stack->open_url(url, OpenType::OpenUrlInView); diff --git a/src/widgets/WebViewStack.cpp b/src/widgets/WebViewStack.cpp index d43c9b5..f15ab64 100644 --- a/src/widgets/WebViewStack.cpp +++ b/src/widgets/WebViewStack.cpp @@ -204,7 +204,7 @@ QUrl WebViewStack::current_url() { auto *webview = current_webview(); if (webview == nullptr) { qDebug() << "No current webview"; - return configuration->new_view_url; + return QUrl{configuration->new_view_url}; } return webview->url(); |
