aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2025-03-27 13:08:20 +0530
committerAkshay Nair <phenax5@gmail.com>2025-03-28 19:39:52 +0530
commit46a7fee95a010f20490d7f66f0c2c82ed936ca5e (patch)
tree9afcbacfb0cdc22f44fb1c61f196347a391e5bd5 /src
parenta54f6ae81b54ca59bf913bba16f271a35ca08d9d (diff)
downloadnull-browser-46a7fee95a010f20490d7f66f0c2c82ed936ca5e.tar.gz
null-browser-46a7fee95a010f20490d7f66f0c2c82ed936ca5e.zip
Refactor and fix tab id use for focus/close
Diffstat (limited to 'src')
-rw-r--r--src/InputMediator.cpp7
-rw-r--r--src/InputMediator.hpp5
-rw-r--r--src/LuaRuntime.cpp122
-rw-r--r--src/LuaRuntime.hpp21
-rw-r--r--src/main.cpp1
-rw-r--r--src/widgets/MainWindow.cpp4
-rw-r--r--src/widgets/WebViewStack.cpp1
-rw-r--r--src/widgets/WebViewStack.hpp5
8 files changed, 85 insertions, 81 deletions
diff --git a/src/InputMediator.cpp b/src/InputMediator.cpp
index 91393c2..175a1b4 100644
--- a/src/InputMediator.cpp
+++ b/src/InputMediator.cpp
@@ -13,19 +13,22 @@ InputMediator::InputMediator(WebViewStack *webview_stack,
KeymapEvaluator *keymap_evaluator)
: webview_stack(webview_stack), lua_runtime(lua_runtime),
keymap_evaluator(keymap_evaluator) {
- connect(lua_runtime, &LuaRuntime::url_opened, webview_stack,
- &WebViewStack::open_url);
connect(lua_runtime, &LuaRuntime::keymap_added, this,
&InputMediator::add_keymap);
+
connect(lua_runtime, &LuaRuntime::history_back_requested, webview_stack,
&WebViewStack::webview_history_back);
connect(lua_runtime, &LuaRuntime::history_forward_requested, webview_stack,
&WebViewStack::webview_history_forward);
+
+ connect(lua_runtime, &LuaRuntime::url_opened, webview_stack,
+ &WebViewStack::open_url);
connect(lua_runtime, &LuaRuntime::webview_closed, webview_stack,
&WebViewStack::close);
connect(lua_runtime, &LuaRuntime::webview_selected, webview_stack,
&WebViewStack::focus_webview);
+ // TODO: Think of how to handle this for multi-window
lua_runtime->set_current_tab_id_fetcher(
[this]() { return this->webview_stack->current_webview_id(); });
lua_runtime->set_webview_data_list_fetcher(
diff --git a/src/InputMediator.hpp b/src/InputMediator.hpp
index d1dc122..63c7a76 100644
--- a/src/InputMediator.hpp
+++ b/src/InputMediator.hpp
@@ -16,12 +16,9 @@ public:
KeymapEvaluator *keymap_evaluator);
~InputMediator() override;
- // DELEGATE(webview_stack, open_url, open_url)
- // DELEGATE(webview_stack, current_url, current_url)
- // DELEGATE(webview_stack, close_current, close_current_webview)
DELEGATE(keymap_evaluator, evaluate, evaluate_keymap)
-protected:
+protected slots:
void add_keymap(const QString &mode_string, const QString &keyseq,
std::function<void()> action);
diff --git a/src/LuaRuntime.cpp b/src/LuaRuntime.cpp
index 0b5e698..edf3b74 100644
--- a/src/LuaRuntime.cpp
+++ b/src/LuaRuntime.cpp
@@ -60,6 +60,60 @@ QVariant LuaRuntime::evaluate_sync(const QString &code) {
return get_lua_value(-1); // TODO: error handling
}
+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::init_web_lib() {
+ // NOLINTBEGIN(modernize-avoid-c-arrays)
+
+ // web
+ luaL_Reg web[] = {
+ {"open", &LuaRuntime::lua_open_url},
+ {nullptr, nullptr},
+ };
+ luaL_newlib(state, web);
+ lua_setglobal(state, web_global_name);
+ lua_getglobal(state, web_global_name);
+
+ // Keymap api (web.keymap)
+ luaL_Reg webkeymap[] = {
+ {"set", &LuaRuntime::lua_keymap_set},
+ {nullptr, nullptr},
+ };
+ luaL_newlib(state, webkeymap);
+ lua_setfield(state, -2, "keymap");
+
+ // Tab actions (web.tabs)
+ luaL_Reg webtabs[] = {
+ {"close", &LuaRuntime::lua_tab_close},
+ {"new", &LuaRuntime::lua_tab_create},
+ {"current", &LuaRuntime::lua_tab_current},
+ {"list", &LuaRuntime::lua_tab_list},
+ {"select", &LuaRuntime::lua_tab_select},
+ {nullptr, nullptr},
+ };
+ luaL_newlib(state, webtabs);
+ lua_setfield(state, -2, "tabs");
+
+ // History navigation
+ luaL_Reg webhistory[] = {
+ {"back", &LuaRuntime::lua_history_back},
+ {"forward", &LuaRuntime::lua_history_forward},
+ {nullptr, nullptr},
+ };
+ luaL_newlib(state, webhistory);
+ lua_setfield(state, -2, "history");
+
+ // NOLINTEND(modernize-avoid-c-arrays)
+}
+
QVariant LuaRuntime::get_lua_value(int idx, QVariant default_value) {
if (lua_isnoneornil(state, idx))
return default_value;
@@ -76,21 +130,21 @@ QVariant LuaRuntime::get_lua_value(int idx, QVariant default_value) {
return lua_tostring(state, idx);
}
-int LuaRuntime::lua_on_url_open(lua_State *state) {
+int LuaRuntime::lua_open_url(lua_State *state) {
const char *url = luaL_optstring(state, 1, "");
auto *runtime = LuaRuntime::instance();
emit runtime->url_opened(url, OpenType::OpenUrl);
return 1;
}
-int LuaRuntime::lua_on_url_tab_open(lua_State *state) {
+int LuaRuntime::lua_tab_create(lua_State *state) {
const char *url = luaL_optstring(state, 1, "");
auto *runtime = LuaRuntime::instance();
emit runtime->url_opened(url, OpenType::OpenUrlInTab);
return 1;
}
-int LuaRuntime::lua_add_keymap(lua_State *state) {
+int LuaRuntime::lua_keymap_set(lua_State *state) {
const char *mode = lua_tostring(state, 1);
const char *keyseq = lua_tostring(state, 2);
@@ -113,68 +167,14 @@ int LuaRuntime::lua_add_keymap(lua_State *state) {
return 1;
}
-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::init_web_lib() {
- // NOLINTBEGIN(modernize-avoid-c-arrays)
-
- // web
- luaL_Reg web[] = {
- {"open", &LuaRuntime::lua_on_url_open},
- {nullptr, nullptr},
- };
- luaL_newlib(state, web);
- lua_setglobal(state, web_global_name);
- lua_getglobal(state, web_global_name);
-
- // Keymap api (web.keymap)
- luaL_Reg webkeymap[] = {
- {"set", &LuaRuntime::lua_add_keymap},
- {nullptr, nullptr},
- };
- luaL_newlib(state, webkeymap);
- lua_setfield(state, -2, "keymap");
-
- // Tab actions (web.tabs)
- luaL_Reg webtabs[] = {
- {"close", &LuaRuntime::lua_tab_closed},
- {"new", &LuaRuntime::lua_on_url_tab_open},
- {"current", &LuaRuntime::lua_get_current_tab_id},
- {"list", &LuaRuntime::lua_get_tab_list},
- {"select", &LuaRuntime::lua_tab_selected},
- {nullptr, nullptr},
- };
- luaL_newlib(state, webtabs);
- lua_setfield(state, -2, "tabs");
-
- // History navigation
- luaL_Reg webhistory[] = {
- {"back", &LuaRuntime::lua_history_back},
- {"forward", &LuaRuntime::lua_history_forward},
- {nullptr, nullptr},
- };
- luaL_newlib(state, webhistory);
- lua_setfield(state, -2, "history");
-
- // NOLINTEND(modernize-avoid-c-arrays)
-}
-
-int LuaRuntime::lua_get_current_tab_id(lua_State *state) {
+int LuaRuntime::lua_tab_current(lua_State *state) {
auto *runtime = LuaRuntime::instance();
auto tab_id = runtime->fetch_current_tab_id();
lua_pushinteger(state, tab_id);
return 1;
}
-int LuaRuntime::lua_get_tab_list(lua_State *state) {
+int LuaRuntime::lua_tab_list(lua_State *state) {
auto *runtime = LuaRuntime::instance();
auto tabs = runtime->fetch_webview_data_list();
lua_newtable(state);
@@ -235,7 +235,7 @@ int LuaRuntime::lua_history_forward(lua_State *state) {
return 1;
}
-int LuaRuntime::lua_tab_closed(lua_State *state) {
+int LuaRuntime::lua_tab_close(lua_State *state) {
auto *runtime = LuaRuntime::instance();
WebViewId tab_id;
@@ -249,7 +249,7 @@ int LuaRuntime::lua_tab_closed(lua_State *state) {
return 1;
}
-int LuaRuntime::lua_tab_selected(lua_State *state) {
+int LuaRuntime::lua_tab_select(lua_State *state) {
if (lua_isnoneornil(state, 1))
return 1; // TODO: return nil (for others too)
diff --git a/src/LuaRuntime.hpp b/src/LuaRuntime.hpp
index 209f8b7..e534d0e 100644
--- a/src/LuaRuntime.hpp
+++ b/src/LuaRuntime.hpp
@@ -40,29 +40,30 @@ public:
DEFINE_FETCHER(QList<WebViewData>(), webview_data_list)
signals:
- void url_opened(QString url, OpenType open_type);
void evaluation_completed(QVariant value);
void evaluation_failed(QString value);
- void keymap_added(QString mode, QString keyseq, std::function<void()>);
void history_back_requested(WebViewId webview_id, qsizetype history_index);
void history_forward_requested(WebViewId webview_id, qsizetype history_index);
+ void keymap_added(QString mode, QString keyseq, std::function<void()>);
+ void url_opened(QString url, OpenType open_type);
void webview_closed(WebViewId webview_id);
void webview_selected(WebViewId webview_id);
- // void output_produced(QVariant value);
protected:
LuaRuntime();
~LuaRuntime() override;
void init_web_lib();
- static int lua_on_url_open(lua_State *state);
- static int lua_on_url_tab_open(lua_State *state);
- static int lua_add_keymap(lua_State *state);
- static int lua_get_current_tab_id(lua_State *state);
+
+ // Lua api
static int lua_history_back(lua_State *state);
static int lua_history_forward(lua_State *state);
- static int lua_tab_closed(lua_State *state);
- static int lua_get_tab_list(lua_State *state);
- static int lua_tab_selected(lua_State *state);
+ static int lua_keymap_set(lua_State *state);
+ static int lua_open_url(lua_State *state);
+ static int lua_tab_close(lua_State *state);
+ static int lua_tab_create(lua_State *state);
+ static int lua_tab_current(lua_State *state);
+ static int lua_tab_list(lua_State *state);
+ static int lua_tab_select(lua_State *state);
private:
lua_State *state;
diff --git a/src/main.cpp b/src/main.cpp
index 4a30628..d676688 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,5 +1,4 @@
#include <QApplication>
-#include <QMainWindow>
#include "LuaRuntime.hpp"
#include "widgets/MainWindow.hpp"
diff --git a/src/widgets/MainWindow.cpp b/src/widgets/MainWindow.cpp
index c3c392e..d66611b 100644
--- a/src/widgets/MainWindow.cpp
+++ b/src/widgets/MainWindow.cpp
@@ -45,10 +45,10 @@ MainWindow::MainWindow() {
LuaRuntime::instance()->load_file("./config.lua");
// TODO: remove
- keymap_evaluator->add_keymap(KeyMode::Normal, "i", [keymap_evaluator]() {
+ keymap_evaluator->add_keymap(KeyMode::Normal, "i", [&keymap_evaluator]() {
keymap_evaluator->set_current_mode(KeyMode::Insert);
});
- keymap_evaluator->add_keymap(KeyMode::Insert, "<esc>", [keymap_evaluator]() {
+ keymap_evaluator->add_keymap(KeyMode::Insert, "<esc>", [&keymap_evaluator]() {
keymap_evaluator->set_current_mode(KeyMode::Normal);
});
keymap_evaluator->add_keymap(KeyMode::Normal, "<c-t>a",
diff --git a/src/widgets/WebViewStack.cpp b/src/widgets/WebViewStack.cpp
index fda4f28..aee4af9 100644
--- a/src/widgets/WebViewStack.cpp
+++ b/src/widgets/WebViewStack.cpp
@@ -103,6 +103,7 @@ void WebViewStack::close(WebViewId webview_id) {
// TODO: Focus on different webview
// focus_webview();
+ // TODO: Close window on empty
if (webview_list.isEmpty()) {
create_new_webview(configuration->new_tab_url, true);
}
diff --git a/src/widgets/WebViewStack.hpp b/src/widgets/WebViewStack.hpp
index b675af5..9df088e 100644
--- a/src/widgets/WebViewStack.hpp
+++ b/src/widgets/WebViewStack.hpp
@@ -31,12 +31,15 @@ public:
QWebEngineProfile *profile = new QWebEngineProfile,
QWidget *parent = nullptr);
- std::vector<QUrl> urls(); // TODO: Remove
QList<WebViewData> get_webview_list();
WebView *current_webview();
WebViewId current_webview_id();
uint32_t count();
QUrl current_url();
+
+ /// @deprecated TODO: Remove
+ std::vector<QUrl> urls();
+ /// @deprecated TODO: Remove
uint32_t current_webview_index();
protected: