From 2078c60477842d7cf6991148e23979565737a8b5 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sat, 26 Jul 2025 11:59:38 +0530 Subject: Refactor heavy + extras.tabline module (incomplete) --- src/widgets/BrowserWindow.cpp | 20 ++++++++++++++++++- src/widgets/BrowserWindow.hpp | 24 +++++------------------ src/widgets/Decorations.cpp | 35 ++++++++++++++++++++++++++++++--- src/widgets/Decorations.hpp | 42 ++++++++++------------------------------ src/widgets/EdgeDecoration.cpp | 10 ++++++++++ src/widgets/EdgeDecoration.hpp | 13 +++---------- src/widgets/IWebViewMediator.hpp | 13 +++++++++++++ src/widgets/WebViewStack.hpp | 31 ++++++++++------------------- 8 files changed, 102 insertions(+), 86 deletions(-) create mode 100644 src/widgets/IWebViewMediator.hpp (limited to 'src/widgets') diff --git a/src/widgets/BrowserWindow.cpp b/src/widgets/BrowserWindow.cpp index fe2285b..f97f397 100644 --- a/src/widgets/BrowserWindow.cpp +++ b/src/widgets/BrowserWindow.cpp @@ -23,7 +23,7 @@ BrowserWindow::BrowserWindow(const Configuration &configuration, QWebEngineProfi layout->setSpacing(0); centralWidget()->setLayout(layout); - // Stack of web views + // Stack of web views + decorations webview_stack = new WebViewStack(&configuration, profile, this); decorations = new Decorations(webview_stack, profile, this); layout->addWidget(decorations); @@ -79,3 +79,21 @@ void BrowserWindow::update_permissions_persistance(const QString &persistance) { auto persistance_policy = Configuration::to_permission_persistance_policy(persistance); profile->setPersistentPermissionsPolicy(persistance_policy); } + +IWebViewMediator *BrowserWindow::get_webview_mediator(WebViewId webview_id) { + if (decorations->has_webview(webview_id)) + return decorations; + return webview_stack; +} + +bool BrowserWindow::has_webview(WebViewId webview_id) { + return webview_stack->has_webview(webview_id) || decorations->has_webview(webview_id); +} + +void BrowserWindow::open_url(const QUrl &url, OpenType open_type, WebViewId webview_id) { + get_webview_mediator(webview_id)->open_url(url, open_type, webview_id); +} + +void BrowserWindow::set_html(const QString &html, WebViewId webview_id) { + get_webview_mediator(webview_id)->set_html(html, webview_id); +} diff --git a/src/widgets/BrowserWindow.hpp b/src/widgets/BrowserWindow.hpp index 92eafa7..e434521 100644 --- a/src/widgets/BrowserWindow.hpp +++ b/src/widgets/BrowserWindow.hpp @@ -5,6 +5,7 @@ #include "Configuration.hpp" #include "utils.hpp" #include "widgets/Decorations.hpp" +#include "widgets/IWebViewMediator.hpp" #include "widgets/WebViewStack.hpp" using WindowId = qsizetype; @@ -18,12 +19,10 @@ public: 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) @@ -35,23 +34,10 @@ public: DELEGATE(decorations, get_enabled, get_decoration_enabled) DELEGATE(decorations, get_view_id, get_decoration_view_id) - bool has_webview(WebViewId webview_id) { - return webview_stack->has_webview(webview_id) || decorations->has_webview(webview_id); - } - - void open_url(const QUrl &url, OpenType open_type, WebViewId webview_id) { - if (decorations->has_webview(webview_id)) - decorations->open_url(url, webview_id); - else - webview_stack->open_url(url, open_type, webview_id); - } - - void set_html(const QString &html, WebViewId webview_id) { - if (decorations->has_webview(webview_id)) - decorations->set_html(html, webview_id); - else - webview_stack->set_html(html, webview_id); - } + IWebViewMediator *get_webview_mediator(WebViewId webview_id); + bool has_webview(WebViewId webview_id); + void open_url(const QUrl &url, OpenType open_type, WebViewId webview_id); + void set_html(const QString &html, WebViewId webview_id); bool on_window_key_event(QKeyEvent *event); diff --git a/src/widgets/Decorations.cpp b/src/widgets/Decorations.cpp index 9df4c23..223e19a 100644 --- a/src/widgets/Decorations.cpp +++ b/src/widgets/Decorations.cpp @@ -6,6 +6,7 @@ #include #include "Decorations.hpp" +#include "WebViewData.hpp" #include "widgets/EdgeDecoration.hpp" Decorations::Decorations(QWidget *content_widget, QWebEngineProfile *profile, QWidget *parent) @@ -36,17 +37,17 @@ Decorations::Decorations(QWidget *content_widget, QWebEngineProfile *profile, QW } void Decorations::set_enabled(DecorationType type, bool enabled) { - auto decoration = get_decoration_widget(type); + auto decoration = get_decoration_widget_type(type); if (decoration.has_value()) decoration.value()->set_enabled(enabled); } bool Decorations::get_enabled(DecorationType type) { - auto decoration = get_decoration_widget(type); + auto decoration = get_decoration_widget_type(type); return decoration.has_value() && decoration.value()->is_enabled(); } -std::optional Decorations::get_decoration_widget(DecorationType type) { +std::optional Decorations::get_decoration_widget_type(DecorationType type) { switch (type) { case DecorationType::DecorationTop: return decoration_top; @@ -59,3 +60,31 @@ std::optional Decorations::get_decoration_widget(DecorationTyp } return nullptr; } + +std::optional Decorations::get_decoration_widget_by_view_id(WebViewId view_id) { + for (auto *decoration : decorations()) + if (decoration->get_view_id() == view_id) + return std::make_optional(decoration); + return std::nullopt; +} + +bool Decorations::has_webview(WebViewId view_id) { + return get_decoration_widget_by_view_id(view_id).has_value(); +} + +void Decorations::open_url(const QUrl &url, OpenType /*unused*/, WebViewId view_id) { + auto decoration = get_decoration_widget_by_view_id(view_id); + if (decoration.has_value()) + decoration.value()->set_url(url); +} + +void Decorations::set_html(const QString &html, WebViewId view_id) { + auto decoration = get_decoration_widget_by_view_id(view_id); + if (decoration.has_value()) + decoration.value()->set_html(html); +} + +std::optional Decorations::get_view_id(DecorationType type) { + auto decoration = get_decoration_widget_type(type); + return decoration.has_value() ? decoration.value()->get_view_id() : std::nullopt; +} diff --git a/src/widgets/Decorations.hpp b/src/widgets/Decorations.hpp index 8eac419..ef81c19 100644 --- a/src/widgets/Decorations.hpp +++ b/src/widgets/Decorations.hpp @@ -1,12 +1,14 @@ #pragma once +#include "WebViewData.hpp" #include "widgets/EdgeDecoration.hpp" -#include "widgets/WebViewStack.hpp" +#include "widgets/IWebViewMediator.hpp" #include #include #include #include +/// @see DecorationType in ./lua/null-browser/api.lua enum DecorationType : uint8_t { DecorationTop = 1, DecorationBottom = 2, @@ -14,7 +16,7 @@ enum DecorationType : uint8_t { DecorationRight = 4, }; -class Decorations : public QWidget { +class Decorations : public QWidget, public IWebViewMediator { Q_OBJECT public: @@ -23,35 +25,10 @@ public: void set_enabled(DecorationType type, bool enabled); bool get_enabled(DecorationType type); - std::optional get_view_id(DecorationType type) { - auto decoration = get_decoration_widget(type); - return decoration.has_value() ? decoration.value()->get_view_id() : std::nullopt; - } - - bool has_webview(WebViewId view_id) { - for (auto *decoration : decorations()) - if (decoration->get_view_id() == view_id) - return true; - return false; - } - - void open_url(const QUrl &url, WebViewId view_id) { - for (auto *decoration : decorations()) { - if (decoration->get_view_id() == view_id) { - decoration->set_url(url); - return; - } - } - } - - void set_html(const QString &html, WebViewId view_id) { - for (auto *decoration : decorations()) { - if (decoration->get_view_id() == view_id) { - decoration->set_html(html); - return; - } - } - } + std::optional get_view_id(DecorationType type); + bool has_webview(WebViewId view_id) override; + void open_url(const QUrl &url, OpenType /*unused*/, WebViewId view_id) override; + void set_html(const QString &html, WebViewId view_id) override; private: EdgeDecoration *decoration_top; @@ -67,5 +44,6 @@ private: decoration_right, }; } - std::optional get_decoration_widget(DecorationType type); + std::optional get_decoration_widget_type(DecorationType type); + std::optional get_decoration_widget_by_view_id(WebViewId view_id); }; diff --git a/src/widgets/EdgeDecoration.cpp b/src/widgets/EdgeDecoration.cpp index 8880294..7f5a59f 100644 --- a/src/widgets/EdgeDecoration.cpp +++ b/src/widgets/EdgeDecoration.cpp @@ -65,3 +65,13 @@ void EdgeDecoration::setup_webview() { } } } + +void EdgeDecoration::set_url(const QUrl &url) { + if (!webview.has_value()) + return; + webview.value()->setUrl(url); +} + +std::optional EdgeDecoration::get_view_id() { + return webview.has_value() ? std::make_optional(webview.value()->get_id()) : std::nullopt; +} diff --git a/src/widgets/EdgeDecoration.hpp b/src/widgets/EdgeDecoration.hpp index f627636..0a977df 100644 --- a/src/widgets/EdgeDecoration.hpp +++ b/src/widgets/EdgeDecoration.hpp @@ -7,9 +7,9 @@ #include #include +#include "WebViewData.hpp" #include "utils.hpp" #include "widgets/WebView.hpp" -#include "widgets/WebViewStack.hpp" class EdgeDecoration : public QWidget { Q_OBJECT @@ -20,18 +20,11 @@ public: void set_size(uint16_t size_value); void set_html(const QString &content); void set_enabled(bool enabled_value); - void set_url(const QUrl &url) { - if (!webview.has_value()) - return; - webview.value()->setUrl(url); - } + void set_url(const QUrl &url); + std::optional get_view_id(); DEFINE_GETTER(is_enabled, enabled) - std::optional get_view_id() { - return webview.has_value() ? std::make_optional(webview.value()->get_id()) : std::nullopt; - } - private: bool vertical; std::optional webview = std::nullopt; diff --git a/src/widgets/IWebViewMediator.hpp b/src/widgets/IWebViewMediator.hpp new file mode 100644 index 0000000..d20836a --- /dev/null +++ b/src/widgets/IWebViewMediator.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include "WebViewData.hpp" + +class IWebViewMediator { +public: + IWebViewMediator() = default; + + virtual bool has_webview(WebViewId webview_id) = 0; + + virtual void open_url(const QUrl &url, OpenType open_type, WebViewId webview_id) = 0; + virtual void set_html(const QString &html, WebViewId webview_id) = 0; +}; diff --git a/src/widgets/WebViewStack.hpp b/src/widgets/WebViewStack.hpp index 3da96aa..2096f25 100644 --- a/src/widgets/WebViewStack.hpp +++ b/src/widgets/WebViewStack.hpp @@ -7,24 +7,12 @@ #include #include "Configuration.hpp" +#include "WebViewData.hpp" +#include "utils.hpp" +#include "widgets/IWebViewMediator.hpp" #include "widgets/WebView.hpp" -using WebViewId = qsizetype; - -enum OpenType : uint8_t { - OpenUrl, - OpenUrlInView, - OpenUrlInBgView, - OpenUrlInWindow, -}; - -struct WebViewData { - WebViewId id; - QString url; - QString title; -}; - -class WebViewStack : public QWidget { +class WebViewStack : public QWidget, public IWebViewMediator { Q_OBJECT public: @@ -33,6 +21,8 @@ public: WebViewStack(const Configuration *configuration, QWebEngineProfile *profile = new QWebEngineProfile, QWidget *parent = nullptr); + DEFINE_GETTER(get_profile, profile) + QList get_webview_list(); WebView *current_webview(); WebViewId current_webview_id(); @@ -40,9 +30,7 @@ public: QUrl current_url(); void set_webview_url(const QUrl &url, WebViewId webview_id); - bool has_webview(WebViewId webview_id); - - QWebEngineProfile *get_profile() { return profile; } + bool has_webview(WebViewId webview_id) override; /// @deprecated TODO: Remove std::vector urls(); @@ -62,7 +50,8 @@ protected: void on_download_request(QWebEngineDownloadRequest *download); public slots: - void open_url(const QUrl &url, OpenType open_type = OpenType::OpenUrl, WebViewId webview_id = 0); + void open_url(const QUrl &url, OpenType open_type = OpenType::OpenUrl, + WebViewId webview_id = 0) override; void webview_history_back(WebViewId webview_id, qsizetype history_index); void webview_history_forward(WebViewId webview_id, qsizetype history_index); void close(WebViewId webview_id); @@ -72,7 +61,7 @@ public slots: void scroll(WebViewId webview_id, int deltax, int deltay); void scroll_to_top(WebViewId webview_id); void scroll_to_bottom(WebViewId webview_id); - void set_html(const QString &html, WebViewId webview_id = 0); + void set_html(const QString &html, WebViewId webview_id = 0) override; protected slots: void on_new_webview_request(QWebEngineNewWindowRequest &request); -- cgit v1.3.1