aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile1
-rw-r--r--TODO.org2
-rw-r--r--config.lua4
-rw-r--r--lua/null-browser/api.lua25
-rw-r--r--src/Configuration.hpp2
-rw-r--r--src/LuaRuntime.cpp40
-rw-r--r--src/LuaRuntime.hpp5
-rw-r--r--src/WindowActionRouter.cpp22
-rw-r--r--src/WindowActionRouter.hpp3
-rw-r--r--src/widgets/BrowserApp.cpp4
-rw-r--r--src/widgets/BrowserWindow.cpp2
-rw-r--r--src/widgets/WebViewStack.cpp2
12 files changed, 95 insertions, 17 deletions
diff --git a/Makefile b/Makefile
index 79f710b..eff7dca 100644
--- a/Makefile
+++ b/Makefile
@@ -32,7 +32,6 @@ clean:
rm -rf build/
rm -f compile_commands.json
-ARGS=""
run: build-dev
./build/null-browser $(ARGS)
diff --git a/TODO.org b/TODO.org
index 4c883ce..866c045 100644
--- a/TODO.org
+++ b/TODO.org
@@ -20,7 +20,7 @@
- [X] History completion
- [X] Close window if last view closed
- [X] Rename terms: tab to view? (also web.tabs)
-- [ ] Configuration lua api
+- [X] Configuration lua api
- [ ] Switch to __internals
- [ ] Respect cli args for main window
- [ ] Tests for api
diff --git a/config.lua b/config.lua
index a1f49c6..9466a20 100644
--- a/config.lua
+++ b/config.lua
@@ -14,6 +14,9 @@ local function get_current_view_index()
end
end
+web.set('new_view_url', 'https://lite.duckduckgo.com')
+-- web.set('close_window_when_no_views', false)
+
local dmenu = require 'null-browser.extras.dmenu'
local history = require 'null-browser.extras.history'
@@ -38,6 +41,7 @@ end
-- Open in new view
web.keymap.set('n', 'o', function()
+ print(web.get('new_view_url'))
dmenu.select(history.list(), { prompt = 'Open view:' }, function(err, result)
if err or not result then return end
web.view.new(to_url(result))
diff --git a/lua/null-browser/api.lua b/lua/null-browser/api.lua
index 3949fbc..7d61344 100644
--- a/lua/null-browser/api.lua
+++ b/lua/null-browser/api.lua
@@ -45,4 +45,29 @@ function web.event.add_listener(events, opts)
return __internals.register_event(opts)
end
+--- Set configuration options
+---
+--- @param key string The name of the configuration
+--- @param value string|boolean|number Configuration value
+---
+--- @example
+--- ```lua
+--- web.set('new_view_url', 'https://duckduckgo.com')
+--- ```
+function web.set(key, value)
+ __internals.set_config(key, value)
+end
+
+--- Get configuration value
+---
+--- @param key string The name of the configuration
+---
+--- @example
+--- ```lua
+--- local url = web.get('new_view_url')
+--- ```
+function web.get(key)
+ return __internals.get_config(key)
+end
+
print("api loaded")
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();