Skip to content

Commit

Permalink
move switchToTab logics from AddressBarTextField to WindowControllers…
Browse files Browse the repository at this point in the history
…Manager
  • Loading branch information
mallexxx committed Dec 20, 2024
1 parent d3764b1 commit eeb2866
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
14 changes: 0 additions & 14 deletions DuckDuckGo/NavigationBar/View/AddressBarTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 9 additions & 5 deletions DuckDuckGo/Suggestions/Model/SuggestionContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Check failure on line 106 in DuckDuckGo/Suggestions/Model/SuggestionContainer.swift

View workflow job for this annotation

GitHub Actions / Make Release Build

value of type 'any WindowControllersManagerProtocol' has no member 'selectedTab'

Check failure on line 106 in DuckDuckGo/Suggestions/Model/SuggestionContainer.swift

View workflow job for this annotation

GitHub Actions / Make Release Build

value of type 'any WindowControllersManagerProtocol' has no member 'selectedTab'

Check failure on line 106 in DuckDuckGo/Suggestions/Model/SuggestionContainer.swift

View workflow job for this annotation

GitHub Actions / Test (Sandbox)

value of type 'any WindowControllersManagerProtocol' has no member 'selectedTab'

Check failure on line 106 in DuckDuckGo/Suggestions/Model/SuggestionContainer.swift

View workflow job for this annotation

GitHub Actions / Test (Non-Sandbox)

value of type 'any WindowControllersManagerProtocol' has no member 'selectedTab'
let openTabViewModels = windowControllersManager.allTabViewModels(for: burnerMode)
var usedUrls = Set<String>() // deduplicate
return openTabViewModels.compactMap { model in
guard model.tab !== selectedTab,
Expand Down
15 changes: 15 additions & 0 deletions DuckDuckGo/Windows/View/WindowControllersManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit eeb2866

Please sign in to comment.