diff --git a/pkg/gui/context.go b/pkg/gui/context.go index 26cec4c23..7c669cbff 100644 --- a/pkg/gui/context.go +++ b/pkg/gui/context.go @@ -211,7 +211,7 @@ func (self *ContextMgr) deactivateContext(c types.Context, opts types.OnFocusLos } if filterableContext, ok := c.(types.IFilterableContext); ok { - if filterableContext.GetFilter() != "" { + if filterableContext.IsFiltering() { filterableContext.ClearFilter() self.gui.helpers.Search.Cancel() } @@ -247,12 +247,12 @@ func (self *ContextMgr) ActivateContext(c types.Context, opts types.OnFocusOpts) } if searchableContext, ok := c.(types.ISearchableContext); ok { - if searchableContext.GetSearchString() != "" { + if searchableContext.IsSearching() { self.gui.helpers.Search.DisplaySearchPrompt(searchableContext) } } if filterableContext, ok := c.(types.IFilterableContext); ok { - if filterableContext.GetFilter() != "" { + if filterableContext.IsFiltering() { self.gui.helpers.Search.DisplayFilterPrompt(filterableContext) } } diff --git a/pkg/gui/context/search_trait.go b/pkg/gui/context/search_trait.go index 5e745f995..fad68d794 100644 --- a/pkg/gui/context/search_trait.go +++ b/pkg/gui/context/search_trait.go @@ -68,3 +68,7 @@ func (self *SearchTrait) onSelectItemWrapper(innerFunc func(int) error) func(int return nil } } + +func (self *SearchTrait) IsSearching() bool { + return self.searchString != "" +} diff --git a/pkg/gui/controllers/helpers/search_helper.go b/pkg/gui/controllers/helpers/search_helper.go index 9c0e09db4..e825fba3e 100644 --- a/pkg/gui/controllers/helpers/search_helper.go +++ b/pkg/gui/controllers/helpers/search_helper.go @@ -138,10 +138,6 @@ func (self *SearchHelper) ConfirmFilter() error { func (self *SearchHelper) ConfirmSearch() error { state := self.searchState() - if err := self.c.PopContext(); err != nil { - return err - } - context, ok := state.Context.(types.ISearchableContext) if !ok { self.c.Log.Warnf("Context %s is searchable", state.Context.GetKey()) @@ -153,6 +149,10 @@ func (self *SearchHelper) ConfirmSearch() error { view := context.GetView() + if err := self.c.PopContext(); err != nil { + return err + } + if err := view.Search(searchString); err != nil { return err } @@ -171,9 +171,10 @@ func (self *SearchHelper) Cancel() { switch context := state.Context.(type) { case types.IFilterableContext: - context.SetFilter("") + context.ClearFilter() _ = self.c.PostRefreshUpdate(context) case types.ISearchableContext: + context.ClearSearchString() context.GetView().ClearSearch() default: // do nothing diff --git a/pkg/gui/controllers/quit_actions.go b/pkg/gui/controllers/quit_actions.go index 2487a62fe..a163f66c8 100644 --- a/pkg/gui/controllers/quit_actions.go +++ b/pkg/gui/controllers/quit_actions.go @@ -50,6 +50,19 @@ func (self *QuitActions) confirmQuitDuringUpdate() error { func (self *QuitActions) Escape() error { currentContext := self.c.CurrentContext() + switch ctx := currentContext.(type) { + case types.IFilterableContext: + if ctx.IsFiltering() { + self.c.Helpers().Search.Cancel() + return nil + } + case types.ISearchableContext: + if ctx.IsSearching() { + self.c.Helpers().Search.Cancel() + return nil + } + } + parentContext, hasParent := currentContext.GetParentContext() if hasParent && currentContext != nil && parentContext != nil { // TODO: think about whether this should be marked as a return rather than adding to the stack diff --git a/pkg/gui/types/context.go b/pkg/gui/types/context.go index 9c2ccd0d5..9da9ad2b1 100644 --- a/pkg/gui/types/context.go +++ b/pkg/gui/types/context.go @@ -93,6 +93,7 @@ type IFilterableContext interface { SetFilter(string) GetFilter() string ClearFilter() + IsFiltering() bool IsFilterableContext() } @@ -102,6 +103,7 @@ type ISearchableContext interface { SetSearchString(string) GetSearchString() string ClearSearchString() + IsSearching() bool IsSearchableContext() }