From b2b9519bcc87ae4b9a75d72c679c7f25e97fcc69 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sat, 15 Aug 2026 14:52:53 +0200 Subject: [PATCH] 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. --- pkg/gui/filetree/file_tree_view_model.go | 38 ++++++++++++++---------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/pkg/gui/filetree/file_tree_view_model.go b/pkg/gui/filetree/file_tree_view_model.go index aabbbce7f..07dc10ec1 100644 --- a/pkg/gui/filetree/file_tree_view_model.go +++ b/pkg/gui/filetree/file_tree_view_model.go @@ -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) {