Skip to content

Commit

Permalink
Closing empty tabs after download (#2510)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tomasstrba authored Apr 3, 2024
1 parent 7124c49 commit b484b1e
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions DuckDuckGo/Tab/TabExtensions/DownloadsTabExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit b484b1e

Please sign in to comment.