Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix SettingsVC readding on pane change #2243

Merged
merged 5 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 40 additions & 28 deletions DuckDuckGo/Tab/Model/Tab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -943,43 +943,55 @@ protocol NewWindowPolicyDecisionMaker {
audioState = webView.audioState()
}

private func tabContentReloadInfo(for content: TabContent, shouldLoadInBackground: Bool) -> (url: URL, source: TabContent.URLSource, forceReload: Bool)? {
switch content {
case .url(let url, _, source: let source):
let forceReload = url.absoluteString == source.userEnteredValue ? shouldLoadInBackground : (source == .reload)
return (url, source, forceReload: forceReload)

case .subscription(let url):
return (url, .ui, forceReload: false)

case .newtab, .bookmarks, .onboarding, .dataBrokerProtection, .settings:
guard let contentUrl = content.urlForWebView, webView.url != contentUrl else { return nil }

return (contentUrl, .ui, forceReload: true) // always navigate built-in ui (duck://) urls

case .none:
return nil
}
}

@MainActor(unsafe)
@discardableResult
private func reloadIfNeeded(shouldLoadInBackground: Bool = false) -> ExpectedNavigation? {
let source: TabContent.URLSource
let url: URL
if case .url(let contentUrl, _, source: let urlSource) = content {
url = contentUrl
source = urlSource
} else if let contentUrl = content.urlForWebView {
url = contentUrl
source = .ui
} else {
guard let (url, source, forceReload) = tabContentReloadInfo(for: content, shouldLoadInBackground: shouldLoadInBackground),
forceReload || shouldReload(url, shouldLoadInBackground: shouldLoadInBackground) else { return nil }

if case .settings = content, case .settings = webView.url.flatMap({ TabContent.contentFromURL($0, source: .ui) }) {
// replace WebView URL without adding a new history item if switching settings panes
webView.evaluateJavaScript("location.replace('\(url.absoluteString.escapedJavaScriptString())')", in: nil, in: .defaultClient)
return nil
}

let forceReload = (url.absoluteString == content.userEnteredValue) ? shouldLoadInBackground : (source == .reload)
if forceReload || shouldReload(url, shouldLoadInBackground: shouldLoadInBackground) {
if webView.url == url, webView.backForwardList.currentItem?.url == url, !webView.isLoading {
return reload()
}
if restoreInteractionStateDataIfNeeded() { return nil /* session restored */ }
invalidateInteractionStateData()

if url.isFileURL {
return webView.navigator(distributedNavigationDelegate: navigationDelegate)
.loadFileURL(url, allowingReadAccessTo: URL(fileURLWithPath: "/"), withExpectedNavigationType: source.navigationType)
}

var request = URLRequest(url: url, cachePolicy: source.cachePolicy)
if #available(macOS 12.0, *), content.isUserEnteredUrl {
request.attribution = .user
}
if webView.url == url, webView.backForwardList.currentItem?.url == url, !webView.isLoading {
return reload()
}
if restoreInteractionStateDataIfNeeded() { return nil /* session restored */ }
invalidateInteractionStateData()

if url.isFileURL {
return webView.navigator(distributedNavigationDelegate: navigationDelegate)
.load(request, withExpectedNavigationType: source.navigationType)
.loadFileURL(url, allowingReadAccessTo: URL(fileURLWithPath: "/"), withExpectedNavigationType: source.navigationType)
}
return nil

var request = URLRequest(url: url, cachePolicy: source.cachePolicy)
if #available(macOS 12.0, *), content.isUserEnteredUrl {
request.attribution = .user
}

return webView.navigator(distributedNavigationDelegate: navigationDelegate)
.load(request, withExpectedNavigationType: source.navigationType)
}

@MainActor
Expand Down
17 changes: 10 additions & 7 deletions DuckDuckGo/Tab/View/BrowserTabViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -507,13 +507,16 @@ final class BrowserTabViewController: NSViewController {
addAndLayoutChild(bookmarksViewControllerCreatingIfNeeded())

case let .settings(pane):
removeAllTabContent()
let preferencesViewController = preferencesViewControllerCreatingIfNeeded()
if preferencesViewController.parent !== self {
removeAllTabContent()
}
if let pane = pane, preferencesViewController.model.selectedPane != pane {
preferencesViewController.model.selectPane(pane)
}
addAndLayoutChild(preferencesViewController)

if preferencesViewController.parent !== self {
addAndLayoutChild(preferencesViewController)
}
case .onboarding:
removeAllTabContent()
if !OnboardingViewModel.isOnboardingFinished {
Expand Down Expand Up @@ -971,12 +974,12 @@ extension BrowserTabViewController: TabDownloadsDelegate {
extension BrowserTabViewController: BrowserTabSelectionDelegate {

func selectedTabContent(_ content: Tab.TabContent) {
tabCollectionViewModel.selectedTabViewModel?.tab.setContent(content)
showTabContent(of: tabCollectionViewModel.selectedTabViewModel)
tabViewModel?.tab.setContent(content)
showTabContent(of: tabViewModel)
}

func selectedPreferencePane(_ identifier: PreferencePaneIdentifier) {
guard let selectedTab = tabCollectionViewModel.selectedTabViewModel?.tab else {
guard let selectedTab = tabViewModel?.tab else {
return
}

Expand Down Expand Up @@ -1097,7 +1100,7 @@ extension BrowserTabViewController {
if isWebViewFirstResponder {
self.setFirstResponderAfterAdding = true
}
self.showTabContent(of: self.tabCollectionViewModel.selectedTabViewModel)
self.showTabContent(of: self.tabViewModel)
self.webViewSnapshot?.removeFromSuperview()
}
}
Expand Down
Loading