diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-07-25 16:24:20 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-07-25 16:24:20 +0530 |
| commit | 7e3b6182c4581fcb39ab1d0ef2eb07fe9fa1d8b6 (patch) | |
| tree | 13808a26ad4e1f948d7fe0c042a2bdedb2658ee4 | |
| parent | 43f3261635e1667367cfd31a2f746832e94c3957 (diff) | |
| download | null-browser-7e3b6182c4581fcb39ab1d0ef2eb07fe9fa1d8b6.tar.gz null-browser-7e3b6182c4581fcb39ab1d0ef2eb07fe9fa1d8b6.zip | |
Remove window mediator class
Diffstat (limited to '')
| -rw-r--r-- | src/WindowActionRouter.cpp | 41 | ||||
| -rw-r--r-- | src/WindowActionRouter.hpp | 2 | ||||
| -rw-r--r-- | src/WindowMediator.cpp | 35 | ||||
| -rw-r--r-- | src/WindowMediator.hpp | 41 | ||||
| -rw-r--r-- | src/widgets/BrowserApp.cpp | 18 | ||||
| -rw-r--r-- | src/widgets/BrowserApp.hpp | 3 | ||||
| -rw-r--r-- | src/widgets/BrowserWindow.cpp | 28 | ||||
| -rw-r--r-- | src/widgets/BrowserWindow.hpp | 24 |
8 files changed, 77 insertions, 115 deletions
diff --git a/src/WindowActionRouter.cpp b/src/WindowActionRouter.cpp index 7270855..3e8d99e 100644 --- a/src/WindowActionRouter.cpp +++ b/src/WindowActionRouter.cpp @@ -4,7 +4,6 @@ #include "LuaRuntime.hpp" #include "WindowActionRouter.hpp" -#include "WindowMediator.hpp" #include "keymap/KeymapEvaluator.hpp" #include "widgets/BrowserWindow.hpp" #include "widgets/WebViewStack.hpp" @@ -27,43 +26,42 @@ void WindowActionRouter::initialize(Configuration *config) { connect(configuration, &Configuration::user_agent_updated, this, [this](const QString &user_agent) { for (auto &win_match : window_map) - win_match.second->mediator()->update_user_agent(user_agent); + win_match.second->update_user_agent(user_agent); }); connect(configuration, &Configuration::downloads_dir_updated, this, [this](const QString &downloads_dir) { for (auto &win_match : window_map) - win_match.second->mediator()->update_downloads_dir(downloads_dir); + win_match.second->update_downloads_dir(downloads_dir); }); connect(configuration, &Configuration::permissions_persistance_updated, this, [this](const QString &persistance) { for (auto &win_match : window_map) - win_match.second->mediator()->update_permissions_persistance(persistance); + win_match.second->update_permissions_persistance(persistance); }); // History connect(&runtime, &LuaRuntime::history_back_requested, this, [this](WebViewId webview_id, qsizetype history_index) { WITH_WEBVIEW_WINDOW(webview_id, window, - { window->mediator()->history_back(webview_id, history_index); }); + { window->history_back(webview_id, history_index); }); }); connect(&runtime, &LuaRuntime::history_forward_requested, this, [this](WebViewId webview_id, qsizetype history_index) { - WITH_WEBVIEW_WINDOW(webview_id, window, { - window->mediator()->history_forward(webview_id, history_index); - }); + WITH_WEBVIEW_WINDOW(webview_id, window, + { window->history_forward(webview_id, history_index); }); }); // Webview action connect(&runtime, &LuaRuntime::url_opened, this, [this](const QString &url, OpenType open_type, WebViewId webview_id) { WITH_WEBVIEW_WINDOW(webview_id, window, - { window->mediator()->open_url(url, open_type, webview_id); }); + { window->open_url(url, open_type, webview_id); }); }); connect(&runtime, &LuaRuntime::webview_closed, this, [this](WebViewId webview_id) { - WITH_WEBVIEW_WINDOW(webview_id, window, { window->mediator()->close_webview(webview_id); }); + WITH_WEBVIEW_WINDOW(webview_id, window, { window->close_webview(webview_id); }); }); connect(&runtime, &LuaRuntime::webview_selected, this, [this](WebViewId webview_id) { - WITH_WEBVIEW_WINDOW(webview_id, window, { window->mediator()->select_webview(webview_id); }); + WITH_WEBVIEW_WINDOW(webview_id, window, { window->select_webview(webview_id); }); }); // Search @@ -79,29 +77,27 @@ void WindowActionRouter::initialize(Configuration *config) { // Devtools connect(&runtime, &LuaRuntime::devtools_requested, this, [this](WebViewId webview_id) { - WITH_WEBVIEW_WINDOW(webview_id, window, - { win_match.second->mediator()->open_devtools(webview_id); }) + WITH_WEBVIEW_WINDOW(webview_id, window, { win_match.second->open_devtools(webview_id); }) }); // Scroll connect(&runtime, &LuaRuntime::webview_scroll_requested, this, [this](WebViewId webview_id, int deltax, int deltay) { WITH_WEBVIEW_WINDOW(webview_id, window, - { window->mediator()->scroll(webview_id, deltax, deltay); }); + { window->scroll(webview_id, deltax, deltay); }); }); connect(&runtime, &LuaRuntime::webview_scroll_top_requested, this, [this](WebViewId webview_id) { - WITH_WEBVIEW_WINDOW(webview_id, window, { window->mediator()->scroll_to_top(webview_id); }); + WITH_WEBVIEW_WINDOW(webview_id, window, { window->scroll_to_top(webview_id); }); }); connect(&runtime, &LuaRuntime::webview_scroll_bottom_requested, this, [this](WebViewId webview_id) { - WITH_WEBVIEW_WINDOW(webview_id, window, - { window->mediator()->scroll_to_bottom(webview_id); }); + WITH_WEBVIEW_WINDOW(webview_id, window, { window->scroll_to_bottom(webview_id); }); }); } void WindowActionRouter::find_current_search_text(WebViewId webview_id, bool forward) { WITH_WEBVIEW_WINDOW(webview_id, window, { - win_match.second->mediator()->set_search_text(current_search_text, webview_id, forward); + win_match.second->set_search_text(current_search_text, webview_id, forward); }) } @@ -122,9 +118,8 @@ void WindowActionRouter::add_window(BrowserWindow *window) { window_map.erase(window->get_id()); }); }); - connect(window->mediator(), &WindowMediator::close_window_requested, window, - [window]() { window->close(); }); - connect(window->mediator(), &WindowMediator::new_window_requested, this, + connect(window, &BrowserWindow::close_window_requested, window, [window]() { window->close(); }); + connect(window, &BrowserWindow::new_window_requested, this, &WindowActionRouter::new_window_requested); } @@ -142,7 +137,7 @@ WebViewId WindowActionRouter::fetch_current_view_id(WindowId win_id) { auto *win = pair.second; auto is_current_window = win_id == win->get_id() || (win_id == 0 && win->isActiveWindow()); if (is_current_window) { - return win->mediator()->current_webview_id(); + return win->current_webview_id(); } } return 0; @@ -154,7 +149,7 @@ QList<WebViewData> WindowActionRouter::fetch_webview_data_list(WindowId win_id) auto *win = pair.second; auto is_current_window = win_id == win->get_id() || (win_id == 0 && win->isActiveWindow()); if (is_current_window) { - return win->mediator()->get_webview_list(); + return win->get_webview_list(); } } return {}; diff --git a/src/WindowActionRouter.hpp b/src/WindowActionRouter.hpp index c43ee04..aa8737d 100644 --- a/src/WindowActionRouter.hpp +++ b/src/WindowActionRouter.hpp @@ -15,7 +15,7 @@ #define WITH_WEBVIEW_WINDOW(WEBVIEW_ID, IDENT, BLOCK) \ for (auto &win_match : window_map) { \ auto *IDENT = win_match.second; \ - if (IDENT->mediator()->has_webview(WEBVIEW_ID)) { \ + if (IDENT->has_webview(WEBVIEW_ID)) { \ BLOCK; \ } \ } diff --git a/src/WindowMediator.cpp b/src/WindowMediator.cpp deleted file mode 100644 index fa9a25e..0000000 --- a/src/WindowMediator.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include <QList> -#include <QWidget> -#include <QtCore> -#include <qwebengineprofile.h> - -#include "WindowMediator.hpp" -#include "widgets/WebViewStack.hpp" - -WindowMediator::WindowMediator(WebViewStack *webview_stack) : webview_stack(webview_stack) { - connect(webview_stack, &WebViewStack::new_window_requested, this, - &WindowMediator::new_window_requested); - connect(webview_stack, &WebViewStack::close_window_requested, this, - &WindowMediator::close_window_requested); -} - -void WindowMediator::update_user_agent(const QString &user_agent) { - auto *profile = webview_stack->get_profile(); - profile->setHttpUserAgent(user_agent); -} - -void WindowMediator::update_downloads_dir(const QString &downloads_dir) { - auto *profile = webview_stack->get_profile(); - profile->setDownloadPath(downloads_dir); -} - -void WindowMediator::update_permissions_persistance(const QString &persistance) { - auto *profile = webview_stack->get_profile(); - auto persistance_policy = Configuration::to_permission_persistance_policy(persistance); - profile->setPersistentPermissionsPolicy(persistance_policy); -} - -WindowMediator::~WindowMediator() { - disconnect(this); - delete webview_stack; -} diff --git a/src/WindowMediator.hpp b/src/WindowMediator.hpp deleted file mode 100644 index 163ab7a..0000000 --- a/src/WindowMediator.hpp +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once - -#include <QWidget> -#include <QtCore> - -#include "utils.hpp" -#include "widgets/WebViewStack.hpp" - -class WindowMediator : public QObject { - Q_OBJECT - -public: - WindowMediator(WebViewStack *webview_stack); - ~WindowMediator() override; - - DELEGATE(webview_stack, has_webview, has_webview) - DELEGATE(webview_stack, current_webview_id, current_webview_id) - DELEGATE(webview_stack, get_webview_list, get_webview_list) - DELEGATE(webview_stack, set_search_text, set_search_text) - DELEGATE(webview_stack, open_devtools, open_devtools) - DELEGATE(webview_stack, open_url, open_url) - DELEGATE(webview_stack, webview_history_back, history_back) - DELEGATE(webview_stack, webview_history_forward, history_forward) - DELEGATE(webview_stack, close, close_webview) - DELEGATE(webview_stack, focus_webview, select_webview) - DELEGATE(webview_stack, scroll, scroll) - DELEGATE(webview_stack, scroll_to_top, scroll_to_top) - DELEGATE(webview_stack, scroll_to_bottom, scroll_to_bottom) - -signals: - void new_window_requested(const QUrl &url); - void close_window_requested(); - -public slots: - void update_user_agent(const QString &user_agent); - void update_downloads_dir(const QString &downloads_dir); - void update_permissions_persistance(const QString &persistance); - -private: - WebViewStack *webview_stack; -}; diff --git a/src/widgets/BrowserApp.cpp b/src/widgets/BrowserApp.cpp index 54fd44a..9326d45 100644 --- a/src/widgets/BrowserApp.cpp +++ b/src/widgets/BrowserApp.cpp @@ -35,19 +35,23 @@ BrowserApp::BrowserApp(Configuration &configuration) : configuration(configurati // Initializes profile for (auto *profile : profiles) { - profile->setDownloadPath(configuration.downloads_dir()); - profile->setHttpUserAgent(configuration.user_agent()); - profile->setNotificationPresenter([](std::unique_ptr<QWebEngineNotification> notification) { - auto *event = new NotificationReceivedEvent(std::move(notification)); - WindowActionRouter::instance().dispatch_event(event); - }); - profile->setPersistentPermissionsPolicy(configuration.permission_persistance_policy()); + setup_profile(profile); } connect(&window_action_router, &WindowActionRouter::new_window_requested, this, [this](const QUrl &url) { create_window({url.toString()}); }); }; +void BrowserApp::setup_profile(QWebEngineProfile *profile) { + profile->setDownloadPath(configuration.downloads_dir()); + profile->setHttpUserAgent(configuration.user_agent()); + profile->setNotificationPresenter([](std::unique_ptr<QWebEngineNotification> notification) { + auto *event = new NotificationReceivedEvent(std::move(notification)); + WindowActionRouter::instance().dispatch_event(event); + }); + profile->setPersistentPermissionsPolicy(configuration.permission_persistance_policy()); +} + BrowserWindow *BrowserApp::create_window(const QStringList &urls) { auto *window = new BrowserWindow((const Configuration &)configuration, &default_profile, urls); window->setWindowTitle("null-browser"); diff --git a/src/widgets/BrowserApp.hpp b/src/widgets/BrowserApp.hpp index c659763..dcfe6f4 100644 --- a/src/widgets/BrowserApp.hpp +++ b/src/widgets/BrowserApp.hpp @@ -3,6 +3,7 @@ #include "Configuration.hpp" #include "widgets/BrowserWindow.hpp" #include <qlist.h> +#include <qwebengineprofile.h> class BrowserApp : public QObject { Q_OBJECT @@ -19,4 +20,6 @@ private: Configuration &configuration; QWebEngineProfile default_profile{"default"}; QList<QWebEngineProfile *> profiles{&default_profile}; + + void setup_profile(QWebEngineProfile *profile); }; diff --git a/src/widgets/BrowserWindow.cpp b/src/widgets/BrowserWindow.cpp index 8e1555f..c565cb9 100644 --- a/src/widgets/BrowserWindow.cpp +++ b/src/widgets/BrowserWindow.cpp @@ -6,7 +6,6 @@ #include <QtCore> #include "Configuration.hpp" -#include "WindowMediator.hpp" #include "keymap/KeymapEvaluator.hpp" #include "widgets/BrowserWindow.hpp" #include "widgets/WebViewStack.hpp" @@ -23,7 +22,7 @@ BrowserWindow::BrowserWindow(const Configuration &configuration, QWebEngineProfi centralWidget()->setLayout(layout); // Stack of web views - auto *webview_stack = new WebViewStack(&configuration, profile); + webview_stack = new WebViewStack(&configuration, profile, this); layout->addWidget(webview_stack); // Open webviews for given urls @@ -40,16 +39,19 @@ BrowserWindow::BrowserWindow(const Configuration &configuration, QWebEngineProfi keymap.define_mode("n", {.passthrough = false}); keymap.define_mode("i", {.passthrough = true}); - win_mediator = new WindowMediator(webview_stack); - // Update window title when webview changes connect(webview_stack, &WebViewStack::current_webview_title_changed, this, [this](int index) { - auto webviews = win_mediator->get_webview_list(); + auto webviews = webview_stack->get_webview_list(); if (index >= 0 && index < webviews.count()) { const auto &webview = webviews.at(index); setWindowTitle(webview.title); } }); + + connect(webview_stack, &WebViewStack::new_window_requested, this, + &BrowserWindow::new_window_requested); + connect(webview_stack, &WebViewStack::close_window_requested, this, + &BrowserWindow::close_window_requested); } bool BrowserWindow::on_window_key_event(QKeyEvent *event) { @@ -58,3 +60,19 @@ bool BrowserWindow::on_window_key_event(QKeyEvent *event) { return should_skip; } + +void BrowserWindow::update_user_agent(const QString &user_agent) { + auto *profile = webview_stack->get_profile(); + profile->setHttpUserAgent(user_agent); +} + +void BrowserWindow::update_downloads_dir(const QString &downloads_dir) { + auto *profile = webview_stack->get_profile(); + profile->setDownloadPath(downloads_dir); +} + +void BrowserWindow::update_permissions_persistance(const QString &persistance) { + auto *profile = webview_stack->get_profile(); + auto persistance_policy = Configuration::to_permission_persistance_policy(persistance); + profile->setPersistentPermissionsPolicy(persistance_policy); +} diff --git a/src/widgets/BrowserWindow.hpp b/src/widgets/BrowserWindow.hpp index ee59e5a..6c49006 100644 --- a/src/widgets/BrowserWindow.hpp +++ b/src/widgets/BrowserWindow.hpp @@ -3,8 +3,8 @@ #include <QMainWindow> #include "Configuration.hpp" -#include "WindowMediator.hpp" #include "utils.hpp" +#include "widgets/WebViewStack.hpp" using WindowId = qsizetype; @@ -15,20 +15,38 @@ public: BrowserWindow(const Configuration &configuration, QWebEngineProfile *profile = new QWebEngineProfile, const QStringList &urls = {}); - DEFINE_GETTER(mediator, win_mediator) DEFINE_GETTER(get_id, win_id) DEFINE_SETTER(set_id, win_id) + DELEGATE(webview_stack, has_webview, has_webview) + DELEGATE(webview_stack, current_webview_id, current_webview_id) + DELEGATE(webview_stack, get_webview_list, get_webview_list) + DELEGATE(webview_stack, set_search_text, set_search_text) + DELEGATE(webview_stack, open_devtools, open_devtools) + DELEGATE(webview_stack, open_url, open_url) + DELEGATE(webview_stack, webview_history_back, history_back) + DELEGATE(webview_stack, webview_history_forward, history_forward) + DELEGATE(webview_stack, close, close_webview) + DELEGATE(webview_stack, focus_webview, select_webview) + DELEGATE(webview_stack, scroll, scroll) + DELEGATE(webview_stack, scroll_to_top, scroll_to_top) + DELEGATE(webview_stack, scroll_to_bottom, scroll_to_bottom) bool on_window_key_event(QKeyEvent *event); void closeEvent(QCloseEvent * /*event*/) override { emit closed(); }; + void update_user_agent(const QString &user_agent); + void update_downloads_dir(const QString &downloads_dir); + void update_permissions_persistance(const QString &persistance); + signals: void closed(); + void new_window_requested(const QUrl &url); + void close_window_requested(); private: - WindowMediator *win_mediator; const Configuration &configuration; + WebViewStack *webview_stack; WindowId win_id = -1; }; |
