Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.

Commit 0bfef11

Browse files
authored
Fix TabBar scrolling (#1178)
Task/Issue URL: https://app.asana.com/0/1177771139624306/1204477216680148/f **Description**: - Fixes related to scrolling to Tab Bar selection **Steps to test this PR**: 1. Open many tabs to enter overflow mode 2. Reorder some tabs within the window 3. Validate tabs selection correctly adjusts tabs width 4. Drag very first tab to the far right of the tab bar - ensure TabBar is scrolled to show the tab completely 5. Open new window 6. Select another tab in the first window - ensure its width is correctly updated with animation <!-- Tagging instructions If this PR isn't ready to be merged for whatever reason it should be marked with the `DO NOT MERGE` label (particularly if it's a draft) If it's pending Product Review/PFR, please add the `Pending Product Review` label. If at any point it isn't actively being worked on/ready for review/otherwise moving forward (besides the above PR/PFR exception) strongly consider closing it (or not opening it in the first place). If you decide not to close it, make sure it's labelled to make it clear the PRs state and comment with more information. --> --- ###### Internal references: [Pull Request Review Checklist](https://app.asana.com/0/1202500774821704/1203764234894239/f) [Software Engineering Expectations](https://app.asana.com/0/59792373528535/199064865822552) [Technical Design Template](https://app.asana.com/0/59792373528535/184709971311943) **When ready for review, remember to post the PR in MM**
1 parent aca926d commit 0bfef11

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

DuckDuckGo/TabBar/View/TabBarCollectionView.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ final class TabBarCollectionView: NSCollectionView {
5656
os_log("TabBarCollectionView: More than 1 item or no item highlighted", type: .error)
5757
return
5858
}
59+
scroll(to: indexPath)
60+
}
5961

62+
func scroll(to indexPath: IndexPath) {
6063
let rect = frameForItem(at: indexPath.item)
6164
animator().performBatchUpdates({
6265
animator().scrollToVisible(rect)

DuckDuckGo/TabBar/View/TabBarViewController.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ final class TabBarViewController: NSViewController {
347347
collectionView.animator().selectItems(at: [newSelectionIndexPath], scrollPosition: .centeredHorizontally)
348348
} else {
349349
collectionView.selectItems(at: [newSelectionIndexPath], scrollPosition: .centeredHorizontally)
350+
collectionView.scrollToSelected()
350351
}
351352
}
352353

@@ -659,7 +660,7 @@ extension TabBarViewController: TabCollectionViewModelDelegate {
659660
updateEmptyTabArea()
660661
hideTabPreview()
661662
if tabMode == .overflow {
662-
scrollCollectionViewToEnd()
663+
collectionView.scroll(to: IndexPath(item: index))
663664
}
664665
}
665666

@@ -820,13 +821,7 @@ extension TabBarViewController: TabCollectionViewModelDelegate {
820821
extension TabBarViewController: NSCollectionViewDelegateFlowLayout {
821822

822823
func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
823-
var isItemSelected = tabCollectionViewModel.selectionIndex == .unpinned(indexPath.item)
824-
if TabDragAndDropManager.shared.sourceUnit?.tabCollectionViewModel === tabCollectionViewModel,
825-
let draggingOverIndex = TabDragAndDropManager.shared.sourceUnit?.index {
826-
// Drag&drop in progress - the empty space is equal to the selected tab width
827-
isItemSelected = draggingOverIndex == indexPath.item
828-
}
829-
824+
let isItemSelected = tabCollectionViewModel.selectionIndex == .unpinned(indexPath.item)
830825
return NSSize(width: self.currentTabWidth(selected: isItemSelected), height: TabBarViewItem.Height.standard.rawValue)
831826
}
832827

@@ -975,9 +970,15 @@ extension TabBarViewController: NSCollectionViewDelegate {
975970
// dropping a tab, dropping of url handled in collectionView:acceptDrop:
976971
guard session.draggingPasteboard.types == [TabBarViewItemPasteboardWriter.utiInternalType] else { return }
977972

973+
defer {
974+
TabDragAndDropManager.shared.clear()
975+
}
978976
if case .private = operation {
979977
// Perform the drag and drop between multiple windows
980978
TabDragAndDropManager.shared.performDragAndDropIfNeeded()
979+
DispatchQueue.main.async {
980+
self.collectionView.scrollToSelected()
981+
}
981982
return
982983
}
983984
// dropping not on a tab bar

DuckDuckGo/TabBar/View/TabDragAndDropManager.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,16 @@ final class TabDragAndDropManager {
5050

5151
@discardableResult
5252
func performDragAndDropIfNeeded() -> Bool {
53-
if let sourceUnit = sourceUnit, let destinationUnit = destinationUnit,
54-
sourceUnit.tabCollectionViewModel !== destinationUnit.tabCollectionViewModel &&
55-
sourceUnit.tabCollectionViewModel?.isBurner ==
56-
destinationUnit.tabCollectionViewModel?.isBurner {
53+
if let sourceUnit = sourceUnit,
54+
let destinationUnit = destinationUnit,
55+
sourceUnit.tabCollectionViewModel !== destinationUnit.tabCollectionViewModel,
56+
sourceUnit.tabCollectionViewModel?.isBurner == destinationUnit.tabCollectionViewModel?.isBurner {
5757

5858
performDragAndDrop(from: sourceUnit, to: destinationUnit)
59-
clear()
6059
return true
61-
} else {
62-
return false
6360
}
61+
62+
return false
6463
}
6564

6665
private func performDragAndDrop(from sourceUnit: Unit, to destinationUnit: Unit) {
@@ -75,7 +74,7 @@ final class TabDragAndDropManager {
7574
sourceTabCollectionViewModel.moveTab(at: sourceUnit.index, to: destinationTabCollectionViewModel, at: newIndex)
7675
}
7776

78-
private func clear() {
77+
func clear() {
7978
sourceUnit = nil
8079
destinationUnit = nil
8180
}

0 commit comments

Comments
 (0)