diff options
Diffstat (limited to '')
| -rw-r--r-- | src/widgets/WebView.cpp | 5 | ||||
| -rw-r--r-- | src/widgets/WebView.hpp | 10 | ||||
| -rw-r--r-- | src/widgets/WebViewStack.cpp | 97 | ||||
| -rw-r--r-- | src/widgets/WebViewStack.hpp | 22 |
4 files changed, 82 insertions, 52 deletions
diff --git a/src/widgets/WebView.cpp b/src/widgets/WebView.cpp index 9c542f8..2ba5c4c 100644 --- a/src/widgets/WebView.cpp +++ b/src/widgets/WebView.cpp @@ -3,5 +3,6 @@ #include "widgets/WebView.hpp" -WebView::WebView(QWebEngineProfile *profile, QWidget *parent_node) - : QWebEngineView(profile, parent_node) {} +WebView::WebView(uint32_t webview_id, QWebEngineProfile *profile, + QWidget *parent_node) + : QWebEngineView(profile, parent_node), id(webview_id) {} diff --git a/src/widgets/WebView.hpp b/src/widgets/WebView.hpp index f0457fd..0f2aae6 100644 --- a/src/widgets/WebView.hpp +++ b/src/widgets/WebView.hpp @@ -2,10 +2,18 @@ #include <QWebEngineView> #include <QtCore> +#include <cstdint> + +#include "utils.hpp" class WebView : public QWebEngineView { Q_OBJECT public: - WebView(QWebEngineProfile *profile, QWidget *parent_node = nullptr); + WebView(uint32_t webview_id, QWebEngineProfile *profile, + QWidget *parent_node = nullptr); + DEFINE_GETTER(get_id, id) + +private: + uint32_t id; }; diff --git a/src/widgets/WebViewStack.cpp b/src/widgets/WebViewStack.cpp index 2d3722a..1d0a35e 100644 --- a/src/widgets/WebViewStack.cpp +++ b/src/widgets/WebViewStack.cpp @@ -2,6 +2,7 @@ #include <QWebEngineHistory> #include <QWebEngineNewWindowRequest> #include <QWebEngineProfile> +#include <cstdint> #include <cstdlib> #include <vector> @@ -35,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(profile); + auto *webview = new WebView(next_id++, profile); webview->setUrl(url); layout->addWidget(webview); webview_list.append(webview); @@ -44,7 +45,7 @@ WebView *WebViewStack::create_new_webview(const QUrl &url, bool focus) { &WebViewStack::on_new_webview_request); if (focus) - focus_webview(webview_list.length() - 1); + focus_webview(webview->get_id()); return webview; } @@ -73,37 +74,32 @@ void WebViewStack::on_new_webview_request( } } -void WebViewStack::next() { - if (webview_list.isEmpty()) - return; - auto index = current_webview_index() + 1; - auto total = webview_list.length(); - index = index >= total ? index % total : index; - focus_webview(index); +int32_t WebViewStack::get_webview_index(WebViewId webview_id) { + int index = 0; + for (auto &webview : webview_list) { + if (webview->get_id() == webview_id) + return index; + index++; + } + return -1; } -void WebViewStack::previous() { - if (webview_list.isEmpty()) +void WebViewStack::close(WebViewId webview_id) { + auto *webview = get_webview(webview_id); + if (webview == nullptr) return; - auto index = current_webview_index() - 1; - auto total = webview_list.length(); - index = index < 0 ? total + index : index; - focus_webview(index); -} - -void WebViewStack::close_current() { close(current_webview_index()); } -void WebViewStack::close(WebViewId index) { - if (index < 0 || index >= webview_list.length()) + auto webview_index = get_webview_index(webview_id); + if (webview_index < 0) return; - auto *webview = webview_list.at(index); layout->removeWidget(webview); - webview_list.removeAt(index); + webview_list.removeAt(webview_index); disconnect(webview->page()); webview->deleteLater(); - focus_webview(current_webview_index()); + // TODO: Focus on different webview + // focus_webview(); if (webview_list.isEmpty()) { create_new_webview(configuration->new_tab_url, true); @@ -112,11 +108,13 @@ void WebViewStack::close(WebViewId index) { void WebViewStack::webview_history_back(WebViewId webview_id, qsizetype history_index) { - if (webview_id < 0 || webview_id >= webview_list.length()) + auto *webview = get_webview(webview_id); + if (webview == nullptr) { + qDebug() << "Invalid webview id" << webview_id; return; + } // TODO: Change this - auto *webview = webview_list.at(webview_id); auto *history = webview->history(); for (auto i = abs(history_index); i > 0; i--) if (history->canGoBack()) @@ -125,11 +123,13 @@ void WebViewStack::webview_history_back(WebViewId webview_id, void WebViewStack::webview_history_forward(WebViewId webview_id, qsizetype history_index) { - if (webview_id < 0 || webview_id >= webview_list.length()) + auto *webview = get_webview(webview_id); + if (webview == nullptr) { + qDebug() << "Invalid webview id" << webview_id; return; + } // TODO: Change this - auto *webview = webview_list.at(webview_id); auto *history = webview->history(); for (auto i = abs(history_index); i > 0; i--) if (history->canGoForward()) @@ -143,32 +143,53 @@ std::vector<QUrl> WebViewStack::urls() { return urls; } +WebViewId WebViewStack::current_webview_id() { + if (webview_list.empty()) + return -1; + return current_webview()->get_id(); +} + +WebView *WebViewStack::current_webview() { + if (webview_list.empty()) + return nullptr; + return webview_list.at(current_webview_index()); +} + uint32_t WebViewStack::current_webview_index() { - qDebug() << "CIRR" << layout->currentIndex(); return layout->currentIndex(); } uint32_t WebViewStack::count() { return webview_list.length(); } -void WebViewStack::focus_webview(WebViewId index) { - if (webview_list.isEmpty()) - return; +void WebViewStack::focus_webview(WebViewId webview_id) { + auto webview_index = get_webview_index(webview_id); + if (webview_index >= 0) + layout->setCurrentIndex((int)webview_index); +} - index = std::max((long long)0, - std::min((long long)index, webview_list.length() - 1)); - layout->setCurrentIndex((int)index); +WebView *WebViewStack::get_webview(WebViewId webview_id) { + auto webview_index = get_webview_index(webview_id); + if (webview_index < 0) + return nullptr; + return webview_list.at(webview_index); } QUrl WebViewStack::current_url() { - if (current_webview_index() >= webview_list.length()) + auto *webview = current_webview(); + if (webview == nullptr) { + qDebug() << "No current webview"; return QUrl{}; + } - return webview_list.at(current_webview_index())->url(); + return webview->url(); } void WebViewStack::set_current_url(const QUrl &url) { - if (current_webview_index() >= webview_list.length()) + auto *webview = current_webview(); + if (webview == nullptr) { + qDebug() << "No current webview"; return; + } - webview_list.at(current_webview_index())->setUrl(url); + webview->setUrl(url); } diff --git a/src/widgets/WebViewStack.hpp b/src/widgets/WebViewStack.hpp index c975d16..3c69888 100644 --- a/src/widgets/WebViewStack.hpp +++ b/src/widgets/WebViewStack.hpp @@ -30,23 +30,21 @@ public: QWebEngineProfile *profile = new QWebEngineProfile, QWidget *parent = nullptr); - void open_url(const QUrl &url, OpenType open_type = OpenType::OpenUrl); - - std::vector<QUrl> urls(); + std::vector<QUrl> urls(); // TODO: Remove QList<Tab> get_webview_list(); - uint32_t current_webview_index(); + WebView *current_webview(); + WebViewId current_webview_id(); uint32_t count(); QUrl current_url(); + WebView *get_webview(WebViewId webview_id); + uint32_t current_webview_index(); - void focus_webview(WebViewId index); - void next(); - void previous(); - - void close(WebViewId index); - void close_current(); - +public slots: // NOLINT(readability-redundant-access-specifiers) + void open_url(const QUrl &url, OpenType open_type = OpenType::OpenUrl); 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); + void focus_webview(WebViewId webview_id); private slots: void on_new_webview_request(const QWebEngineNewWindowRequest &request); @@ -54,10 +52,12 @@ private slots: protected: void set_current_url(const QUrl &url); WebView *create_new_webview(const QUrl &url, bool focus = false); + int32_t get_webview_index(WebViewId webview_id); private: const Configuration *configuration; QWebEngineProfile *profile; QStackedLayout *layout; QList<WebView *> webview_list; + WebViewId next_id = 1; }; |
