diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-07-26 11:59:38 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-07-26 11:59:38 +0530 |
| commit | 2078c60477842d7cf6991148e23979565737a8b5 (patch) | |
| tree | 980e2aea150e85c6b87f2b521fce1006486244d8 /src | |
| parent | 68448538172663279919b29dd09b5faa51e0628a (diff) | |
| download | null-browser-2078c60477842d7cf6991148e23979565737a8b5.tar.gz null-browser-2078c60477842d7cf6991148e23979565737a8b5.zip | |
Refactor heavy + extras.tabline module (incomplete)
Diffstat (limited to 'src')
| -rw-r--r-- | src/WebViewData.hpp | 18 | ||||
| -rw-r--r-- | src/events/NotificationReceivedEvent.hpp | 3 | ||||
| -rw-r--r-- | src/events/PermissionRequestedEvent.hpp | 3 | ||||
| -rw-r--r-- | src/events/UrlChangedEvent.hpp | 2 | ||||
| -rw-r--r-- | src/keymap/KeySeqParser.cpp | 13 | ||||
| -rw-r--r-- | src/keymap/KeySeqParser.hpp | 12 | ||||
| -rw-r--r-- | src/widgets/BrowserWindow.cpp | 20 | ||||
| -rw-r--r-- | src/widgets/BrowserWindow.hpp | 24 | ||||
| -rw-r--r-- | src/widgets/Decorations.cpp | 35 | ||||
| -rw-r--r-- | src/widgets/Decorations.hpp | 42 | ||||
| -rw-r--r-- | src/widgets/EdgeDecoration.cpp | 10 | ||||
| -rw-r--r-- | src/widgets/EdgeDecoration.hpp | 13 | ||||
| -rw-r--r-- | src/widgets/IWebViewMediator.hpp | 13 | ||||
| -rw-r--r-- | src/widgets/WebViewStack.hpp | 31 |
14 files changed, 137 insertions, 102 deletions
diff --git a/src/WebViewData.hpp b/src/WebViewData.hpp new file mode 100644 index 0000000..6831d29 --- /dev/null +++ b/src/WebViewData.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include <QtCore> + +using WebViewId = qsizetype; + +enum OpenType : uint8_t { + OpenUrl, + OpenUrlInView, + OpenUrlInBgView, + OpenUrlInWindow, +}; + +struct WebViewData { + WebViewId id; + QString url; + QString title; +}; diff --git a/src/events/NotificationReceivedEvent.hpp b/src/events/NotificationReceivedEvent.hpp index ade2132..fd5a554 100644 --- a/src/events/NotificationReceivedEvent.hpp +++ b/src/events/NotificationReceivedEvent.hpp @@ -16,8 +16,7 @@ public: } void lua_push(lua_State *state) const override { - lua_newtable(state); - SET_FIELD("type", string, kind.toStdString().c_str()) + Event::lua_push(state); SET_FIELD("title", string, notification->title().toStdString().c_str()) SET_FIELD("message", string, notification->message().toStdString().c_str()) SET_FIELD("tag", string, notification->tag().toStdString().c_str()) diff --git a/src/events/PermissionRequestedEvent.hpp b/src/events/PermissionRequestedEvent.hpp index ec03ea5..f6c102a 100644 --- a/src/events/PermissionRequestedEvent.hpp +++ b/src/events/PermissionRequestedEvent.hpp @@ -40,8 +40,7 @@ public: } void lua_push(lua_State *state) const override { - lua_newtable(state); - SET_FIELD("type", string, kind.toStdString().c_str()) + Event::lua_push(state); SET_FIELD("permission_type", string, permission_type()) SET_FIELD("view_id", integer, webview_id) SET_FIELD("win_id", integer, win_id) diff --git a/src/events/UrlChangedEvent.hpp b/src/events/UrlChangedEvent.hpp index ec3980b..29547f2 100644 --- a/src/events/UrlChangedEvent.hpp +++ b/src/events/UrlChangedEvent.hpp @@ -3,9 +3,9 @@ #include <QtCore> #include <lua.hpp> +#include "WebViewData.hpp" #include "events/Event.hpp" #include "widgets/BrowserWindow.hpp" -#include "widgets/WebViewStack.hpp" class UrlChangedEvent : public Event { public: diff --git a/src/keymap/KeySeqParser.cpp b/src/keymap/KeySeqParser.cpp index 555e955..192f436 100644 --- a/src/keymap/KeySeqParser.cpp +++ b/src/keymap/KeySeqParser.cpp @@ -80,3 +80,16 @@ Qt::Key KeySeqParser::parse_key(const QString &key_name) { return Qt::Key_T; } + +KeyMatchType KeySeqParser::key_sequence_match(const KeySequence &target, + const KeySequence ¤t) { + for (int i = 0; i < target.length(); i++) { + if (current.length() <= i) + return KeyMatchType::Pending; + + if (target[i].key != current[i].key || !target[i].mod.testFlags(current[i].mod)) + return KeyMatchType::NoMatch; + } + + return KeyMatchType::Match; +} diff --git a/src/keymap/KeySeqParser.hpp b/src/keymap/KeySeqParser.hpp index 31680a9..3772f4e 100644 --- a/src/keymap/KeySeqParser.hpp +++ b/src/keymap/KeySeqParser.hpp @@ -20,17 +20,7 @@ enum KeyMatchType : uint8_t { class KeySeqParser { public: - static KeyMatchType key_sequence_match(const KeySequence &target, const KeySequence ¤t) { - for (int i = 0; i < target.length(); i++) { - if (current.length() <= i) - return KeyMatchType::Pending; - - if (target[i].key != current[i].key || !target[i].mod.testFlags(current[i].mod)) - return KeyMatchType::NoMatch; - } - - return KeyMatchType::Match; - } + static KeyMatchType key_sequence_match(const KeySequence &target, const KeySequence ¤t); KeySeqParser() = default; KeySequence parse(QString key_sequence); 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 <qwidget.h> #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<EdgeDecoration *> Decorations::get_decoration_widget(DecorationType type) { +std::optional<EdgeDecoration *> Decorations::get_decoration_widget_type(DecorationType type) { switch (type) { case DecorationType::DecorationTop: return decoration_top; @@ -59,3 +60,31 @@ std::optional<EdgeDecoration *> Decorations::get_decoration_widget(DecorationTyp } return nullptr; } + +std::optional<EdgeDecoration *> 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<WebViewId> 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 <QWidget> #include <QtCore> #include <optional> #include <qwebengineprofile.h> +/// @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<WebViewId> 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<WebViewId> 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<EdgeDecoration *> get_decoration_widget(DecorationType type); + std::optional<EdgeDecoration *> get_decoration_widget_type(DecorationType type); + std::optional<EdgeDecoration *> 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<WebViewId> 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 <qwebengineprofile.h> #include <qwebengineview.h> +#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<WebViewId> get_view_id(); DEFINE_GETTER(is_enabled, enabled) - std::optional<WebViewId> get_view_id() { - return webview.has_value() ? std::make_optional(webview.value()->get_id()) : std::nullopt; - } - private: bool vertical; std::optional<WebView *> 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 <vector> #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<WebViewData> 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<QUrl> 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); |
