Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.
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
12 changes: 7 additions & 5 deletions DuckDuckGo/PinnedTabs/View/PinnedTabsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ struct PinnedTabsView: View {
}

extension PinnedTabsView {
func itemIndex(for point: CGPoint) -> Int? {
guard (0..<PinnedTabView.Const.dimension).contains(point.y) else {
return nil
}
let possibleItemIndex = Int(point.x / PinnedTabView.Const.dimension)

func index(forItemAt point: CGPoint) -> Int? {
guard !model.items.isEmpty,
(0..<PinnedTabView.Const.dimension).contains(point.y) else { return nil }

let possibleItemIndex = min(model.items.count - 1, Int(point.x / PinnedTabView.Const.dimension))
return model.items.index(model.items.startIndex, offsetBy: possibleItemIndex, limitedBy: model.items.endIndex)
}

}
27 changes: 13 additions & 14 deletions DuckDuckGo/TabBar/View/TabBarViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ final class TabBarViewController: NSViewController {

private func subscribeToPinnedTabsHostingView() {
pinnedTabsHostingView?.middleClickPublisher
.compactMap { [weak self] in self?.pinnedTabsView?.itemIndex(for: $0) }
.compactMap { [weak self] in self?.pinnedTabsView?.index(forItemAt: $0) }
.sink { [weak self] index in
self?.tabCollectionViewModel.remove(at: .pinned(index))
}
Expand Down Expand Up @@ -335,18 +335,17 @@ final class TabBarViewController: NSViewController {
}
}

private func selectTabWithPoint(_ point: NSPoint) {
guard let pointLocationOnPinnedTabsView = pinnedTabsHostingView?.convert(point, from: view) else {
return
}
private func selectTab(with event: NSEvent) {
let locationInWindow = event.locationInWindow

if let point = pinnedTabsHostingView?.mouseLocationInsideBounds(locationInWindow),
let index = pinnedTabsView?.index(forItemAt: point) {

if let index = pinnedTabsView?.itemIndex(for: pointLocationOnPinnedTabsView) {
tabCollectionViewModel.select(at: .pinned(index))
} else {
let pointLocationOnCollectionView = collectionView.convert(point, from: view)
if let indexPath = collectionView.indexPathForItem(at: pointLocationOnCollectionView) {
tabCollectionViewModel.select(at: .unpinned(indexPath.item))
}

} else if let point = collectionView.mouseLocationInsideBounds(locationInWindow),
let indexPath = collectionView.indexPathForItem(at: point) {
tabCollectionViewModel.select(at: .unpinned(indexPath.item))
}
}

Expand Down Expand Up @@ -413,9 +412,9 @@ final class TabBarViewController: NSViewController {

func mouseDown(with event: NSEvent) -> NSEvent? {
if event.window === view.window,
view.window?.isMainWindow == false,
let point = view.mouseLocationInsideBounds(event.locationInWindow) {
selectTabWithPoint(point)
view.window?.isMainWindow == false {

selectTab(with: event)
}

return event
Expand Down