From 13c1103815e791966615647726000f2e3a7de828 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sat, 3 Jun 2023 13:15:41 +1000 Subject: [PATCH] Only cancel search if main or temporary context loses focus This is a pickle: initially I wanted it so that a filter would cancel automatically if the current context lost focus. But there are situations where you want to retain the focus, e.g. when a popup appears, or when you view the commits of a branch. The issue is that when you view the commits of a branch, the branches context is removed from the context stack. Even if this were not the case, you could imagine going branches -> sub-commits -> files -> sub-commits, where in that case branches would definitely be off the stack upon navigating to the files context. So because I'm too lazy to find a proper solution to this problem, I'm just making it so that filters in side contexts are retained unless explicitly cancelled. There's another edge case this commit handles which is that if I'm in the sub-commits context via the branches context and start a search, then navigate to the reflog context and hit enter to get to the sub-commits context again, I need to cancel the search before I switch. Likewise with the commit files context. --- pkg/gui/context.go | 5 ++++- pkg/gui/controllers/helpers/search_helper.go | 15 +++++++++++---- .../switch_to_diff_files_controller.go | 1 + .../switch_to_sub_commits_controller.go | 15 +++++++++------ 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/pkg/gui/context.go b/pkg/gui/context.go index 1c235a93f..e9a15870b 100644 --- a/pkg/gui/context.go +++ b/pkg/gui/context.go @@ -201,7 +201,10 @@ func (self *ContextMgr) deactivateContext(c types.Context, opts types.OnFocusLos view, _ := self.gui.c.GocuiGui().View(c.GetViewName()) if opts.NewContextKey != context.SEARCH_CONTEXT_KEY { - self.gui.helpers.Search.CancelSearchIfSearching(c) + self.gui.helpers.Search.HidePrompt() + if c.GetKind() == types.MAIN_CONTEXT || c.GetKind() == types.TEMPORARY_POPUP { + self.gui.helpers.Search.CancelSearchIfSearching(c) + } } // if we are the kind of context that is sent to back upon deactivation, we should do that diff --git a/pkg/gui/controllers/helpers/search_helper.go b/pkg/gui/controllers/helpers/search_helper.go index 68af20b35..294ce9dbf 100644 --- a/pkg/gui/controllers/helpers/search_helper.go +++ b/pkg/gui/controllers/helpers/search_helper.go @@ -41,15 +41,16 @@ func (self *SearchHelper) OpenFilterPrompt(context types.IFilterableContext) err return nil } -func (self *SearchHelper) OpenSearchPrompt(context types.Context) error { +func (self *SearchHelper) OpenSearchPrompt(context types.ISearchableContext) error { state := self.searchState() state.Context = context + searchString := context.GetSearchString() self.searchPrefixView().SetContent(self.c.Tr.SearchPrefix) promptView := self.promptView() - // TODO: should we show the currently searched thing here? Perhaps we can store that on the context promptView.ClearTextArea() + promptView.TextArea.TypeString(searchString) promptView.RenderTextArea() if err := self.c.PushContext(self.c.Contexts().Search); err != nil { @@ -78,7 +79,8 @@ func (self *SearchHelper) DisplaySearchPrompt(context types.ISearchableContext) state.Context = context searchString := context.GetSearchString() - self.searchPrefixView().SetContent(self.c.Tr.SearchPrefix) + _ = context.GetView().SelectCurrentSearchResult() + promptView := self.promptView() promptView.ClearTextArea() promptView.TextArea.TypeString(searchString) @@ -179,7 +181,7 @@ func (self *SearchHelper) Cancel() { // do nothing } - state.Context = nil + self.HidePrompt() } func (self *SearchHelper) OnPromptContentChanged(searchString string) { @@ -229,3 +231,8 @@ func (self *SearchHelper) CancelSearchIfSearching(c types.Context) { return } } + +func (self *SearchHelper) HidePrompt() { + state := self.searchState() + state.Context = nil +} diff --git a/pkg/gui/controllers/switch_to_diff_files_controller.go b/pkg/gui/controllers/switch_to_diff_files_controller.go index ffec6936e..767520d20 100644 --- a/pkg/gui/controllers/switch_to_diff_files_controller.go +++ b/pkg/gui/controllers/switch_to_diff_files_controller.go @@ -83,6 +83,7 @@ func (self *SwitchToDiffFilesController) viewFiles(opts SwitchToCommitFilesConte diffFilesContext.SetCanRebase(opts.CanRebase) diffFilesContext.SetParentContext(opts.Context) diffFilesContext.SetWindowName(opts.Context.GetWindowName()) + diffFilesContext.ClearFilter() if err := self.c.Refresh(types.RefreshOptions{ Scope: []types.RefreshableView{types.COMMIT_FILES}, diff --git a/pkg/gui/controllers/switch_to_sub_commits_controller.go b/pkg/gui/controllers/switch_to_sub_commits_controller.go index 5ae86f02b..8163181e5 100644 --- a/pkg/gui/controllers/switch_to_sub_commits_controller.go +++ b/pkg/gui/controllers/switch_to_sub_commits_controller.go @@ -71,12 +71,15 @@ func (self *SwitchToSubCommitsController) viewCommits() error { self.setSubCommits(commits) - self.c.Contexts().SubCommits.SetSelectedLineIdx(0) - self.c.Contexts().SubCommits.SetParentContext(self.context) - self.c.Contexts().SubCommits.SetWindowName(self.context.GetWindowName()) - self.c.Contexts().SubCommits.SetTitleRef(ref.Description()) - self.c.Contexts().SubCommits.SetRef(ref) - self.c.Contexts().SubCommits.SetLimitCommits(true) + subCommitsContext := self.c.Contexts().SubCommits + subCommitsContext.SetSelectedLineIdx(0) + subCommitsContext.SetParentContext(self.context) + subCommitsContext.SetWindowName(self.context.GetWindowName()) + subCommitsContext.SetTitleRef(ref.Description()) + subCommitsContext.SetRef(ref) + subCommitsContext.SetLimitCommits(true) + subCommitsContext.ClearSearchString() + subCommitsContext.GetView().ClearSearch() err = self.c.PostRefreshUpdate(self.c.Contexts().SubCommits) if err != nil {