aboutsummaryrefslogtreecommitdiff
path: root/src/widgets
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2025-03-29 11:45:18 +0530
committerAkshay Nair <phenax5@gmail.com>2025-03-29 11:45:18 +0530
commitbcecbced7e1af9d99443a7e823f18328c7943f09 (patch)
tree612fac477acfa38358e15916bd4a44f386dc9206 /src/widgets
parentf7392096d2d84be7143947d386528bf4f487ee83 (diff)
downloadnull-browser-bcecbced7e1af9d99443a7e823f18328c7943f09.tar.gz
null-browser-bcecbced7e1af9d99443a7e823f18328c7943f09.zip
Add window action router for multi-window
Diffstat (limited to 'src/widgets')
-rw-r--r--src/widgets/BrowserApp.cpp16
-rw-r--r--src/widgets/BrowserApp.hpp9
-rw-r--r--src/widgets/BrowserWindow.cpp (renamed from src/widgets/MainWindow.cpp)19
-rw-r--r--src/widgets/BrowserWindow.hpp20
-rw-r--r--src/widgets/MainWindow.hpp17
-rw-r--r--src/widgets/WebViewStack.cpp9
-rw-r--r--src/widgets/WebViewStack.hpp5
7 files changed, 50 insertions, 45 deletions
diff --git a/src/widgets/BrowserApp.cpp b/src/widgets/BrowserApp.cpp
index 0a82de0..3cd06b4 100644
--- a/src/widgets/BrowserApp.cpp
+++ b/src/widgets/BrowserApp.cpp
@@ -2,8 +2,9 @@
#include <QtCore>
#include "LuaRuntime.hpp"
+#include "WindowActionRouter.hpp"
#include "widgets/BrowserApp.hpp"
-#include "widgets/MainWindow.hpp"
+#include "widgets/BrowserWindow.hpp"
BrowserApp::BrowserApp() {
auto *lua = LuaRuntime::instance();
@@ -11,16 +12,13 @@ BrowserApp::BrowserApp() {
// Global event filter
qApp->installEventFilter(this);
-
- // NOTE: TMP
- lua->load_file("./config.lua");
};
-MainWindow *BrowserApp::create_window() {
- auto *win = new MainWindow((const Configuration &)configuration);
+BrowserWindow *BrowserApp::create_window() {
+ auto *win = new BrowserWindow((const Configuration &)configuration);
win->setWindowTitle("null-browser");
- windows.insert({last_id, win});
- last_id++;
+ auto *router = WindowActionRouter::instance();
+ router->add_window(win);
win->show();
return win;
}
@@ -29,7 +27,7 @@ bool BrowserApp::eventFilter(QObject *target, QEvent *event) {
if (event->type() != QEvent::KeyPress)
return false;
- for (auto &match : windows) {
+ for (const auto &match : WindowActionRouter::instance()->windows()) {
auto *win = match.second;
if (auto *target_widget = dynamic_cast<QWidget *>(target);
diff --git a/src/widgets/BrowserApp.hpp b/src/widgets/BrowserApp.hpp
index e4fcf99..f124a71 100644
--- a/src/widgets/BrowserApp.hpp
+++ b/src/widgets/BrowserApp.hpp
@@ -1,8 +1,6 @@
#pragma once
-#include <unordered_map>
-
-#include "widgets/MainWindow.hpp"
+#include "widgets/BrowserWindow.hpp"
class BrowserApp : public QObject {
Q_OBJECT
@@ -10,14 +8,11 @@ class BrowserApp : public QObject {
public:
BrowserApp();
- MainWindow *create_window();
+ BrowserWindow *create_window();
protected:
bool eventFilter(QObject *target, QEvent *event) override;
private:
- std::unordered_map<uint64_t, MainWindow *> windows;
- uint64_t last_id = 1;
-
Configuration configuration;
};
diff --git a/src/widgets/MainWindow.cpp b/src/widgets/BrowserWindow.cpp
index 2d17557..3ff4d3c 100644
--- a/src/widgets/MainWindow.cpp
+++ b/src/widgets/BrowserWindow.cpp
@@ -4,15 +4,14 @@
#include <QtCore>
#include "Configuration.hpp"
-#include "InputMediator.hpp"
+#include "WindowMediator.hpp"
#include "keymap/KeymapEvaluator.hpp"
-#include "widgets/MainWindow.hpp"
+#include "widgets/BrowserWindow.hpp"
#include "widgets/WebViewStack.hpp"
-MainWindow::MainWindow(const Configuration &configuration)
+BrowserWindow::BrowserWindow(const Configuration &configuration)
: configuration(configuration) {
- setStyleSheet("background-color: #000; color: #fff;");
- setCentralWidget(new QWidget()); // TODO: manage widget memory
+ setCentralWidget(new QWidget());
// Root stacked layout
auto *layout = new QStackedLayout();
@@ -26,10 +25,7 @@ MainWindow::MainWindow(const Configuration &configuration)
new WebViewStack(&configuration, new QWebEngineProfile("null-browser"));
layout->addWidget(web_view_stack);
- auto *keymap_evaluator = new KeymapEvaluator;
-
- input_mediator = new InputMediator(web_view_stack, LuaRuntime::instance(),
- keymap_evaluator);
+ auto *keymap_evaluator = KeymapEvaluator::instance();
// TODO: remoev
web_view_stack->open_url(QUrl("https://duckduckgo.com"), OpenType::OpenUrl);
@@ -47,9 +43,12 @@ MainWindow::MainWindow(const Configuration &configuration)
});
keymap_evaluator->add_keymap(KeyMode::Normal, "<c-t>a",
[]() { qDebug() << "Stuff"; });
+
+ input_mediator = new WindowMediator(web_view_stack, LuaRuntime::instance(),
+ keymap_evaluator);
}
-bool MainWindow::on_window_key_event(QKeyEvent *event) {
+bool BrowserWindow::on_window_key_event(QKeyEvent *event) {
const bool should_skip = input_mediator->evaluate_keymap(
event->modifiers(), (Qt::Key)event->key());
diff --git a/src/widgets/BrowserWindow.hpp b/src/widgets/BrowserWindow.hpp
new file mode 100644
index 0000000..6036ec1
--- /dev/null
+++ b/src/widgets/BrowserWindow.hpp
@@ -0,0 +1,20 @@
+#pragma once
+
+#include <QMainWindow>
+
+#include "Configuration.hpp"
+#include "WindowMediator.hpp"
+#include "utils.hpp"
+
+class BrowserWindow : public QMainWindow {
+public:
+ BrowserWindow(const Configuration &configuration);
+
+ DEFINE_GETTER(mediator, input_mediator)
+
+ bool on_window_key_event(QKeyEvent *event);
+
+private:
+ WindowMediator *input_mediator;
+ const Configuration &configuration;
+};
diff --git a/src/widgets/MainWindow.hpp b/src/widgets/MainWindow.hpp
deleted file mode 100644
index 7aaca2b..0000000
--- a/src/widgets/MainWindow.hpp
+++ /dev/null
@@ -1,17 +0,0 @@
-#pragma once
-
-#include <QMainWindow>
-
-#include "Configuration.hpp"
-#include "InputMediator.hpp"
-
-class MainWindow : public QMainWindow {
-public:
- MainWindow(const Configuration &configuration);
-
- bool on_window_key_event(QKeyEvent *event);
-
-private:
- InputMediator *input_mediator;
- const Configuration &configuration;
-};
diff --git a/src/widgets/WebViewStack.cpp b/src/widgets/WebViewStack.cpp
index aee4af9..f1c799f 100644
--- a/src/widgets/WebViewStack.cpp
+++ b/src/widgets/WebViewStack.cpp
@@ -36,7 +36,7 @@ void WebViewStack::open_url(const QUrl &url, OpenType open_type) {
}
WebView *WebViewStack::create_new_webview(const QUrl &url, bool focus) {
- auto *webview = new WebView(next_id++, profile);
+ auto *webview = new WebView(next_webview_id++, profile);
webview->setUrl(url);
layout->addWidget(webview);
webview_list.append(webview);
@@ -77,6 +77,9 @@ void WebViewStack::on_new_webview_request(
}
int32_t WebViewStack::get_webview_index(WebViewId webview_id) {
+ if (webview_id == 0 && window()->isActiveWindow())
+ webview_id = current_webview_id();
+
int index = 0;
for (auto &webview : webview_list) {
if (webview->get_id() == webview_id)
@@ -177,6 +180,10 @@ WebView *WebViewStack::get_webview(WebViewId webview_id) {
return webview_list.at(webview_index);
}
+bool WebViewStack::has_webview(WebViewId webview_id) {
+ return get_webview(webview_id) != nullptr;
+}
+
QUrl WebViewStack::current_url() {
auto *webview = current_webview();
if (webview == nullptr) {
diff --git a/src/widgets/WebViewStack.hpp b/src/widgets/WebViewStack.hpp
index 9df088e..f9ce397 100644
--- a/src/widgets/WebViewStack.hpp
+++ b/src/widgets/WebViewStack.hpp
@@ -23,6 +23,8 @@ struct WebViewData {
QString title;
};
+static WebViewId next_webview_id = 1;
+
class WebViewStack : public QWidget {
Q_OBJECT
@@ -37,6 +39,8 @@ public:
uint32_t count();
QUrl current_url();
+ bool has_webview(WebViewId webview_id);
+
/// @deprecated TODO: Remove
std::vector<QUrl> urls();
/// @deprecated TODO: Remove
@@ -63,5 +67,4 @@ private:
QWebEngineProfile *profile;
QStackedLayout *layout;
QList<WebView *> webview_list;
- WebViewId next_id = 1;
};