From b484b1e410894d344d6ef02a224f592e166630a3 Mon Sep 17 00:00:00 2001 From: Tom Strba <57389842+tomasstrba@users.noreply.github.com> Date: Wed, 3 Apr 2024 14:35:08 +0200 Subject: [PATCH] Closing empty tabs after download (#2510) Task/Issue URL: https://app.asana.com/0/1177771139624306/1206835038894671/f **Description**: Fix of empty tabs remaining in the tab bar after a download finishes. --- .../TabExtensions/DownloadsTabExtension.swift | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/DuckDuckGo/Tab/TabExtensions/DownloadsTabExtension.swift b/DuckDuckGo/Tab/TabExtensions/DownloadsTabExtension.swift index 65eb64e98a..2da642e537 100644 --- a/DuckDuckGo/Tab/TabExtensions/DownloadsTabExtension.swift +++ b/DuckDuckGo/Tab/TabExtensions/DownloadsTabExtension.swift @@ -213,16 +213,23 @@ extension DownloadsTabExtension: NavigationResponder { func enqueueDownload(_ download: WebKitDownload, withNavigationAction navigationAction: NavigationAction?) { let task = downloadManager.add(download, fromBurnerWindow: self.isBurner, delegate: self, destination: .auto) + var isMainFrameNavigationActionWithNoHistory: Bool { + guard let navigationAction, + navigationAction.isForMainFrame, + navigationAction.isTargetingNewWindow, + // webView has no navigation history (downloaded navigationAction has started from an empty state) + (navigationAction.redirectHistory?.first ?? navigationAction).fromHistoryItemIdentity == nil + else { return false } + return true + } + // If the download has started from a popup Tab - close it after starting the download // e.g. download button on this page: // https://en.wikipedia.org/wiki/Guitar#/media/File:GuitareClassique5.png - guard let navigationAction, - navigationAction.isForMainFrame, - navigationAction.isTargetingNewWindow, - let webView = download.webView, - // webView has no navigation history (downloaded navigationAction has started from an empty state) - (navigationAction.redirectHistory?.first ?? navigationAction).fromHistoryItemIdentity == nil - else { return } + guard let webView = download.webView, + isMainFrameNavigationActionWithNoHistory + // if converted from navigation response but no page was loaded + || navigationAction == nil && webView.backForwardList.currentItem == nil else { return } self.closeWebView(webView, afterDownloadTaskHasStarted: task) }