diff --git a/DuckDuckGo/NavigationBar/View/AddressBarTextField.swift b/DuckDuckGo/NavigationBar/View/AddressBarTextField.swift index 57ae483378..8ee7d915f0 100644 --- a/DuckDuckGo/NavigationBar/View/AddressBarTextField.swift +++ b/DuckDuckGo/NavigationBar/View/AddressBarTextField.swift @@ -493,21 +493,7 @@ final class AddressBarTextField: NSTextField { } private func switchTo(_ tab: OpenTab) { - let selectedTabViewModel = tabCollectionViewModel.selectedTabViewModel - let selectionIndex = tabCollectionViewModel.selectionIndex - WindowControllersManager.shared.show(url: tab.url, source: .switchToOpenTab, newTab: true /* in case not found */) - - if let selectedTabViewModel, let selectionIndex, - case .newtab = selectedTabViewModel.tab.content { - // close tab with "new tab" page open - tabCollectionViewModel.remove(at: selectionIndex) - - // close the window if no more non-pinned tabs are open - if tabCollectionViewModel.tabs.isEmpty, let window, window.isVisible { - window.performClose(self) - } - } } private func makeUrl(suggestion: Suggestion?, stringValueWithoutSuffix: String, completion: @escaping (URL?, String, Bool) -> Void) { diff --git a/DuckDuckGo/Suggestions/Model/SuggestionContainer.swift b/DuckDuckGo/Suggestions/Model/SuggestionContainer.swift index 581498286b..7f1df51d70 100644 --- a/DuckDuckGo/Suggestions/Model/SuggestionContainer.swift +++ b/DuckDuckGo/Suggestions/Model/SuggestionContainer.swift @@ -55,11 +55,15 @@ final class SuggestionContainer { self.loading = suggestionLoading } - convenience init (burnerMode: BurnerMode) { + @MainActor + convenience init (burnerMode: BurnerMode, + windowControllersManager: WindowControllersManagerProtocol? = nil) { let urlFactory = { urlString in return URL.makeURL(fromSuggestionPhrase: urlString) } - self.init(openTabsProvider: Self.defaultOpenTabsProvider(burnerMode: burnerMode), + let windowControllersManager = windowControllersManager ?? WindowControllersManager.shared + self.init(openTabsProvider: Self.defaultOpenTabsProvider(burnerMode: burnerMode, + windowControllersManager: windowControllersManager), suggestionLoading: SuggestionLoader(urlFactory: urlFactory), historyCoordinating: HistoryCoordinator.shared, bookmarkManager: LocalBookmarkManager.shared) @@ -97,10 +101,10 @@ final class SuggestionContainer { latestQuery = nil } - private static func defaultOpenTabsProvider(burnerMode: BurnerMode) -> OpenTabsProvider { + private static func defaultOpenTabsProvider(burnerMode: BurnerMode, windowControllersManager: WindowControllersManagerProtocol) -> OpenTabsProvider { { @MainActor in - let selectedTab = WindowControllersManager.shared.selectedTab - let openTabViewModels = WindowControllersManager.shared.allTabViewModels(for: burnerMode) + let selectedTab = windowControllersManager.selectedTab + let openTabViewModels = windowControllersManager.allTabViewModels(for: burnerMode) var usedUrls = Set() // deduplicate return openTabViewModels.compactMap { model in guard model.tab !== selectedTab, diff --git a/DuckDuckGo/Windows/View/WindowControllersManager.swift b/DuckDuckGo/Windows/View/WindowControllersManager.swift index 53170a2820..c95e86316f 100644 --- a/DuckDuckGo/Windows/View/WindowControllersManager.swift +++ b/DuckDuckGo/Windows/View/WindowControllersManager.swift @@ -192,9 +192,24 @@ extension WindowControllersManager { // If there is any non-popup window available, open the URL in it ?? nonPopupMainWindowControllers.first { + let tabCollectionViewModel = windowController.mainViewController.tabCollectionViewModel + let selectedTabViewModel = tabCollectionViewModel.selectedTabViewModel + let selectionIndex = tabCollectionViewModel.selectionIndex + // Switch to already open tab if present if [.appOpenUrl, .switchToOpenTab].contains(source), let url, switchToOpenTab(with: url, preferring: windowController) == true { + + if let selectedTabViewModel, let selectionIndex, + case .newtab = selectedTabViewModel.tab.content { + // close tab with "new tab" page open + tabCollectionViewModel.remove(at: selectionIndex) + + // close the window if no more non-pinned tabs are open + if tabCollectionViewModel.tabs.isEmpty, let window = windowController.window, window.isVisible { + window.performClose(nil) + } + } return }