Extract a private helper function preserveSelection

The operation around which the selection should be preserved is passed
in so that it can be reused for different purposes.
This commit is contained in:
Stefan Haller
2026-08-15 14:58:10 +02:00
parent a1f1a4ee8c
commit b2b9519bcc
+22 -16
View File
@@ -167,6 +167,25 @@ func (self *FileTreeViewModel) SetStatusFilter(filter FileTreeDisplayFilter) {
self.IListCursor.SetSelection(0)
}
func (self *FileTreeViewModel) preserveSelection(f func()) {
selectedNode := self.GetSelected()
var selectedPath string
if selectedNode != nil {
selectedPath = selectedNode.GetInternalPath()
}
f()
if selectedPath != "" {
self.ExpandToPath(selectedPath)
if idx, found := self.GetIndexForPath(selectedPath); found {
self.SetSelection(idx)
return
}
}
self.ClampSelection()
}
// If we're going from flat to tree we want to select the same file.
// If we're going from tree to flat and we have a file selected we want to select that.
// If instead we've selected a directory we need to select the first file in that directory.
@@ -233,22 +252,9 @@ func (self *FileTreeViewModel) GetFilter() string {
}
func (self *FileTreeViewModel) ClearFilter() {
selectedNode := self.GetSelected()
var selectedPath string
if selectedNode != nil {
selectedPath = selectedNode.GetInternalPath()
}
self.IFileTree.SetTextFilter("", false)
if selectedPath != "" {
self.ExpandToPath(selectedPath)
if idx, found := self.GetIndexForPath(selectedPath); found {
self.SetSelection(idx)
return
}
}
self.ClampSelection()
self.preserveSelection(func() {
self.IFileTree.SetTextFilter("", false)
})
}
func (self *FileTreeViewModel) ReApplyFilter(useFuzzySearch bool) {