Skip to content

Commit

Permalink
Maintain scroll position, selection and expanded/collapsed when retur…
Browse files Browse the repository at this point in the history
…ning from search on bookmarks panel (#3269)

Task/Issue URL:
https://app.asana.com/0/1204006570077678/1208262956542585/f
Tech Design URL:
CC:

**Description**:
**Given** a user on the bookmarks panel, **when** the user starts a
search and comes back to the tree view, **then** the selected, collapsed
and expanded folders or bookmarks should be maintained.



https://github.com/user-attachments/assets/5ae781ab-e6c5-4e48-87e1-7eea9b267fda



**Steps to test this PR**:
1. Load a bunch of bookmarks
2. Go to the bookmarks panel, scroll to the last position and open some
folders
3. Start a search, type something, and then cancel the search
4. The opened folders should be expanded, and the scroll position should
be the same before the search.

**Definition of Done**:

* [x] Does this PR satisfy our [Definition of
Done](https://app.asana.com/0/1202500774821704/1207634633537039/f)?

—
###### 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)
[Pull Request
Documentation](https://app.asana.com/0/1202500774821704/1204012835277482/f)
  • Loading branch information
jotaemepereira authored Sep 17, 2024
1 parent 9cc7e9c commit 0a035ff
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions DuckDuckGo/Bookmarks/View/BookmarkListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ final class BookmarkListViewController: NSViewController {
}
return [BookmarkNode]()
}
private var lastOutlineScrollPosition: NSRect?

private(set) lazy var faviconsFetcherOnboarding: FaviconsFetcherOnboarding? = {
guard let syncService = NSApp.delegateTyped.syncService, let syncBookmarksAdapter = NSApp.delegateTyped.syncDataProviders?.bookmarksAdapter else {
Expand Down Expand Up @@ -511,6 +512,9 @@ final class BookmarkListViewController: NSViewController {
if !isSearchVisible {
outlineView.makeMeFirstResponder()
}

let selectedNodes = self.selectedNodes
expandAndRestore(selectedNodes: selectedNodes)
}

private func expandFoldersAndScrollUntil(_ folder: BookmarkFolder) {
Expand Down Expand Up @@ -759,6 +763,11 @@ extension BookmarkListViewController: NSSearchFieldDelegate {

if searchQuery.isBlank {
showTreeView()

/// Reset to the last scroll position if available
if let lastOutlineScrollPosition = self.lastOutlineScrollPosition {
outlineView.scrollToVisible(lastOutlineScrollPosition)
}
} else {
showSearch(forSearchQuery: searchQuery)
}
Expand All @@ -768,6 +777,12 @@ extension BookmarkListViewController: NSSearchFieldDelegate {
}

private func showSearch(forSearchQuery searchQuery: String) {
/// Before searching for the first letter we store the current outline scroll position.
/// This is needed because we want to maintain the scroll position in case the search is cancelled.
if searchQuery.count == 1 {
self.lastOutlineScrollPosition = outlineView.visibleRect
}

outlineView.highlightedRow = nil
dataSource.reloadData(forSearchQuery: searchQuery, sortMode: sortBookmarksViewModel.selectedSortMode)

Expand Down

0 comments on commit 0a035ff

Please sign in to comment.