Merge pull request #1257 from abdnh/sidebar-scroll-to-first-match

Scroll to first sidebar search match
This commit is contained in:
Damien Elmes 2021-06-29 11:44:28 +10:00 committed by GitHub
commit f8f6b828aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -215,23 +215,39 @@ class SidebarTreeView(QTreeView):
# start from a collapsed state, as it's faster # start from a collapsed state, as it's faster
self.collapseAll() self.collapseAll()
self.setColumnHidden(0, not self.model().search(text)) self.setColumnHidden(0, not self.model().search(text))
self._expand_where_necessary(self.model(), searching=True) self._expand_where_necessary(
self.model(), searching=True, scroll_to_first_match=True
)
def _expand_where_necessary( def _expand_where_necessary(
self, self,
model: SidebarModel, model: SidebarModel,
parent: Optional[QModelIndex] = None, parent: Optional[QModelIndex] = None,
searching: bool = False, searching: bool = False,
scroll_to_first_match: bool = False,
) -> None: ) -> None:
parent = parent or QModelIndex() def expand_node(parent: QModelIndex) -> None:
nonlocal scroll_to_first_match
for row in range(model.rowCount(parent)): for row in range(model.rowCount(parent)):
idx = model.index(row, 0, parent) idx = model.index(row, 0, parent)
if not idx.isValid(): if not idx.isValid():
continue continue
self._expand_where_necessary(model, idx, searching)
# descend into children first
expand_node(idx)
if item := model.item_for_index(idx): if item := model.item_for_index(idx):
if item.show_expanded(searching): if item.show_expanded(searching):
self.setExpanded(idx, True) self.setExpanded(idx, True)
if item.is_highlighted() and scroll_to_first_match:
self.selectionModel().setCurrentIndex(
idx, QItemSelectionModel.SelectCurrent
)
self.scrollTo(idx)
scroll_to_first_match = False
expand_node(parent or QModelIndex())
def update_search( def update_search(
self, self,