Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #7996: Switching Private Mode Tab Tray cause position loss in Nor…
Browse files Browse the repository at this point in the history
…mal Mode (#8015)
  • Loading branch information
soner-yuksel authored and iccub committed Sep 4, 2023
1 parent 8a835c8 commit 1df3158
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions Sources/Brave/Frontend/Browser/TabManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class TabManager: NSObject {
return _selectedIndex
}
var normalTabSelectedIndex: Int = 0
var privateTabSelectedIndex: Int = 0
var tempTabs: [Tab]?
private weak var rewards: BraveRewards?
private weak var tabGeneratorAPI: BraveTabGeneratorAPI?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,8 +621,10 @@ class TabTrayController: AuthenticationController {
return
}

// Record the slected index before private mode navigation
if !privateMode {
// Record the selected index before mode navigation
if privateMode {
tabManager.privateTabSelectedIndex = Preferences.Privacy.persistentPrivateBrowsing.value ? tabManager.selectedIndex : 0
} else {
tabManager.normalTabSelectedIndex = tabManager.selectedIndex
}

Expand All @@ -646,17 +648,32 @@ class TabTrayController: AuthenticationController {
tabManager.addTabAndSelect(isPrivate: true)
}

let privateModeTabSelected = tabManager.allTabs[safe: tabManager.privateTabSelectedIndex]

if Preferences.Privacy.persistentPrivateBrowsing.value {
tabManager.selectTab(privateModeTabSelected)
}
tabTrayView.hidePrivateModeInfo()
tabTrayView.collectionView.reloadData()

// When you go back from normal mode, current tab should be selected
// in case private tabs are persistent
if Preferences.Privacy.persistentPrivateBrowsing.value {
scrollToSelectedTab(privateModeTabSelected)
}
navigationController?.setNavigationBarHidden(false, animated: false)
}
} else {
tabTrayView.hidePrivateModeInfo()

// When you go back from private mode, a previous current tab is selected
// Reloding the collection view in order to mark the selecte the tab
tabManager.selectTab(tabManager.tabsForCurrentMode[safe: tabManager.normalTabSelectedIndex])
let normalModeTabSelected = tabManager.allTabs[safe: tabManager.normalTabSelectedIndex]

tabManager.selectTab(normalModeTabSelected)
tabTrayView.collectionView.reloadData()

scrollToSelectedTab(normalModeTabSelected)
navigationController?.setNavigationBarHidden(false, animated: false)
}

Expand Down Expand Up @@ -724,6 +741,15 @@ class TabTrayController: AuthenticationController {
present(settingsNavigationController, animated: true)
}

private func scrollToSelectedTab(_ tab: Tab?) {
if let selectedTab = tab,
let selectedIndexPath = dataSource.indexPath(for: selectedTab) {
DispatchQueue.main.async {
self.tabTrayView.collectionView.scrollToItem(at: selectedIndexPath, at: .centeredVertically, animated: false)
}
}
}

@objc private func tappedCollectionViewBackground() {
if traitCollection.horizontalSizeClass == .compact {
doneAction()
Expand Down

0 comments on commit 1df3158

Please sign in to comment.