diff options
Diffstat (limited to 'pkg/gui/context')
-rw-r--r-- | pkg/gui/context/filtered_list.go | 14 | ||||
-rw-r--r-- | pkg/gui/context/filtered_list_view_model.go | 10 |
2 files changed, 24 insertions, 0 deletions
diff --git a/pkg/gui/context/filtered_list.go b/pkg/gui/context/filtered_list.go index b848b96d4..bffd0eddb 100644 --- a/pkg/gui/context/filtered_list.go +++ b/pkg/gui/context/filtered_list.go @@ -68,3 +68,17 @@ func (self *FilteredList[T]) applyFilter() { func (self *FilteredList[T]) match(haystack string, needle string) bool { return utils.CaseInsensitiveContains(haystack, needle) } + +func (self *FilteredList[T]) UnfilteredIndex(index int) int { + if self.filteredIndices == nil { + return index + } + + // we use -1 when there are no items + if index == -1 { + return -1 + } + + // TODO: mutex + return self.filteredIndices[index] +} diff --git a/pkg/gui/context/filtered_list_view_model.go b/pkg/gui/context/filtered_list_view_model.go index 6196ed180..77f6e1174 100644 --- a/pkg/gui/context/filtered_list_view_model.go +++ b/pkg/gui/context/filtered_list_view_model.go @@ -21,3 +21,13 @@ func NewFilteredListViewModel[T any](getList func() []T, getFilterFields func(T) // used for type switch func (self *FilteredListViewModel[T]) IsFilterableContext() {} + +func (self *FilteredListViewModel[T]) ClearFilter() { + // Set the selected line index to the unfiltered index of the currently selected line, + // so that the current item is still selected after the filter is cleared. + unfilteredIndex := self.FilteredList.UnfilteredIndex(self.GetSelectedLineIdx()) + + self.FilteredList.ClearFilter() + + self.SetSelectedLineIdx(unfilteredIndex) +} |