diff --git a/src/lib/downloads/downloadmanager.cpp b/src/lib/downloads/downloadmanager.cpp index ad23b3b1b..ed41e4034 100644 --- a/src/lib/downloads/downloadmanager.cpp +++ b/src/lib/downloads/downloadmanager.cpp @@ -28,6 +28,10 @@ #include "webview.h" #include "settings.h" #include "datapaths.h" +#include "tabwidget.h" +#include "tabbedwebview.h" +#include "tabbar.h" +#include "locationbar.h" #include #include @@ -113,6 +117,42 @@ void DownloadManager::keyPressEvent(QKeyEvent* e) QWidget::keyPressEvent(e); } +void DownloadManager::closeDownloadTab(const QUrl &url) const +{ + // Attempt to close empty tab that was opened only for loading the download url + auto testWebView = [](TabbedWebView *view, const QUrl &url) { + if (view->browserWindow()->tabWidget()->tabBar()->normalTabsCount() < 1) { + return false; + } + WebPage *page = view->page(); + if (page->history()->count() != 0) { + return false; + } + if (page->url() != QUrl() || page->requestedUrl() != QUrl()) { + return false; + } + const QUrl tabUrl(view->webTab()->locationBar()->text()); + return tabUrl.host() == url.host(); + }; + + if (testWebView(mApp->getWindow()->weView(), url)) { + mApp->getWindow()->weView()->closeView(); + return; + } + + const auto windows = mApp->windows(); + for (auto *window : windows) { + const auto tabs = window->tabWidget()->allTabs(); + for (auto *tab : tabs) { + auto *view = tab->webView(); + if (testWebView(view, url)) { + view->closeView(); + return; + } + } + } +} + void DownloadManager::startExternalManager(const QUrl &url) { QString arguments = m_externalArguments; @@ -205,6 +245,8 @@ void DownloadManager::clearList() void DownloadManager::download(QWebEngineDownloadItem *downloadItem) { + closeDownloadTab(downloadItem->url()); + QString downloadPath; bool openFile = false; diff --git a/src/lib/downloads/downloadmanager.h b/src/lib/downloads/downloadmanager.h index b7245f79c..e6b8797e9 100644 --- a/src/lib/downloads/downloadmanager.h +++ b/src/lib/downloads/downloadmanager.h @@ -91,6 +91,8 @@ private: void resizeEvent(QResizeEvent* e); void keyPressEvent(QKeyEvent* e); + void closeDownloadTab(const QUrl &url) const; + Ui::DownloadManager* ui; QBasicTimer m_timer;