diff options
Diffstat (limited to '')
| -rw-r--r-- | TODO.org | 3 | ||||
| -rw-r--r-- | config.lua | 2 | ||||
| -rw-r--r-- | spec/WebViewStackSpec.cpp | 2 | ||||
| -rw-r--r-- | src/Configuration.hpp | 23 | ||||
| -rw-r--r-- | src/LuaRuntime.cpp | 6 | ||||
| -rw-r--r-- | src/WindowActionRouter.cpp | 16 | ||||
| -rw-r--r-- | src/widgets/BrowserWindow.cpp | 2 | ||||
| -rw-r--r-- | src/widgets/WebViewStack.cpp | 6 |
8 files changed, 34 insertions, 26 deletions
@@ -23,9 +23,10 @@ - [X] Configuration lua api - [X] Switch to __internals - [X] Respect cli args for main window +- [ ] Downloading/download path config +- [ ] Use table for internals api options? - [ ] Tests for api - [ ] Log stdout, errors and results from lua somewhere -- [ ] Downloading/download path config - [ ] Run JS in page - [ ] INVESTIGATE: Segfault on close - [ ] INVESTIGATE: Errors in keymap segfaults @@ -15,7 +15,7 @@ local function get_current_view_index() end web.set('new_view_url', 'https://lite.duckduckgo.com') --- web.set('close_window_when_no_views', false) +web.set('close_window_when_no_views', true) local dmenu = require 'null-browser.extras.dmenu' local history = require 'null-browser.extras.history' diff --git a/spec/WebViewStackSpec.cpp b/spec/WebViewStackSpec.cpp index fb3add0..155218e 100644 --- a/spec/WebViewStackSpec.cpp +++ b/spec/WebViewStackSpec.cpp @@ -29,7 +29,7 @@ private slots: WebViewStack webview_stack(&configuration); QCOMPARE(webview_stack.count(), 0); - QCOMPARE(webview_stack.current_url(), configuration.new_view_url); + QCOMPARE(webview_stack.current_url(), configuration.new_view_url()); } } diff --git a/src/Configuration.hpp b/src/Configuration.hpp index 06335dc..a1eca51 100644 --- a/src/Configuration.hpp +++ b/src/Configuration.hpp @@ -1,14 +1,33 @@ #pragma once #include <QtCore> +#include <unordered_map> class Configuration : public QObject { Q_OBJECT +private: + std::unordered_map<QString, QVariant> config_map{ + {"new_view_url", "https://duckduckgo.com"}, + {"close_window_when_no_views", true}, + }; + public: using QObject::QObject; - QString new_view_url = "https://duckduckgo.com"; + void set_config(const QString &name, const QVariant &value) { + qDebug() << "config update" << name << value; + config_map[name] = value; + emit config_updated(name, value); + } + + QVariant get_config(const QString &name) const { return config_map.at(name); } + + QString new_view_url() const { return get_config("new_view_url").toString(); } + bool close_window_when_no_views() const { + return get_config("close_window_when_no_views").toBool(); + } - bool close_window_when_no_views = true; +signals: + void config_updated(const QString &name, const QVariant &value); }; diff --git a/src/LuaRuntime.cpp b/src/LuaRuntime.cpp index 5646d2b..127e014 100644 --- a/src/LuaRuntime.cpp +++ b/src/LuaRuntime.cpp @@ -156,12 +156,12 @@ QVariant LuaRuntime::get_lua_value(int idx, QVariant default_value) { if (lua_isstring(state, idx)) return lua_tostring(state, idx); - if (lua_isnumber(state, idx)) - return lua_tonumber(state, idx); - if (lua_isboolean(state, idx)) return lua_toboolean(state, idx); + if (lua_isnumber(state, idx)) + return lua_tonumber(state, idx); + return lua_tostring(state, idx); } diff --git a/src/WindowActionRouter.cpp b/src/WindowActionRouter.cpp index 4bac3ad..bba15d1 100644 --- a/src/WindowActionRouter.cpp +++ b/src/WindowActionRouter.cpp @@ -10,11 +10,7 @@ #include "widgets/WebViewStack.hpp" 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 ""; + return configuration->get_config(key); } // NOLINTNEXTLINE(readability-function-cognitive-complexity) @@ -24,15 +20,7 @@ void WindowActionRouter::initialize(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::config_updated, configuration, &Configuration::set_config); connect(&runtime, &LuaRuntime::history_back_requested, this, [this](WebViewId webview_id, qsizetype history_index) { diff --git a/src/widgets/BrowserWindow.cpp b/src/widgets/BrowserWindow.cpp index 2c4c5b4..dbefdd6 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); + 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 f15ab64..bdec508 100644 --- a/src/widgets/WebViewStack.cpp +++ b/src/widgets/WebViewStack.cpp @@ -126,10 +126,10 @@ void WebViewStack::close(WebViewId webview_id) { webview->deleteLater(); if (webview_list.isEmpty()) { - if (configuration->close_window_when_no_views) { + if (configuration->close_window_when_no_views()) { emit close_window_requested(); } else { - create_new_webview(configuration->new_view_url, true); + create_new_webview(configuration->new_view_url(), true); } } } @@ -204,7 +204,7 @@ QUrl WebViewStack::current_url() { auto *webview = current_webview(); if (webview == nullptr) { qDebug() << "No current webview"; - return QUrl{configuration->new_view_url}; + return QUrl{configuration->new_view_url()}; } return webview->url(); |
