Skip to content

Commit

Permalink
Don't allow opening a menu while the search or filter prompt is open (#…
Browse files Browse the repository at this point in the history
…3878)

- **PR Description**

This solves several problems that arise from opening a menu while the
prompt is open. We might try to solve these in a different way, e.g. by
dismissing the search prompt before opening a menu, but restricting what
you can do while the prompt is open seems like the more robust fix.

To achieve this, we
- call resetKeyBindings both when opening and when closing the
search/filter prompt
- change the keybindings to only contain the ones for the search prompt
when that context is active.

Fixes #3875.
  • Loading branch information
stefanhaller authored Sep 2, 2024
2 parents 4ec9262 + 9ec77bb commit fa8cd47
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
23 changes: 16 additions & 7 deletions pkg/gui/controllers/helpers/search_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (self *SearchHelper) OpenFilterPrompt(context types.IFilterableContext) err
return err
}

return nil
return self.c.ResetKeybindings()
}

func (self *SearchHelper) OpenSearchPrompt(context types.ISearchableContext) error {
Expand All @@ -64,7 +64,7 @@ func (self *SearchHelper) OpenSearchPrompt(context types.ISearchableContext) err
return err
}

return nil
return self.c.ResetKeybindings()
}

func (self *SearchHelper) DisplayFilterStatus(context types.IFilterableContext) {
Expand Down Expand Up @@ -112,16 +112,21 @@ func (self *SearchHelper) Confirm() error {
return self.CancelPrompt()
}

var err error
switch state.SearchType() {
case types.SearchTypeFilter:
return self.ConfirmFilter()
err = self.ConfirmFilter()
case types.SearchTypeSearch:
return self.ConfirmSearch()
err = self.ConfirmSearch()
case types.SearchTypeNone:
return self.c.Context().Pop()
err = self.c.Context().Pop()
}

return nil
if err != nil {
return err
}

return self.c.ResetKeybindings()
}

func (self *SearchHelper) ConfirmFilter() error {
Expand Down Expand Up @@ -183,7 +188,11 @@ func modelSearchResults(context types.ISearchableContext) []gocui.SearchPosition
func (self *SearchHelper) CancelPrompt() error {
self.Cancel()

return self.c.Context().Pop()
if err := self.c.Context().Pop(); err != nil {
return err
}

return self.c.ResetKeybindings()
}

func (self *SearchHelper) ScrollHistory(scrollIncrement int) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/gui/gui_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ func (self *guiCommon) CallKeybindingHandler(binding *types.Binding) error {
return self.gui.callKeybindingHandler(binding)
}

func (self *guiCommon) ResetKeybindings() error {
return self.gui.resetKeybindings()
}

func (self *guiCommon) IsAnyModeActive() bool {
return self.gui.helpers.Mode.IsAnyModeActive()
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/gui/keybindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/context"
"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
"github.com/jesseduffield/lazygit/pkg/gui/types"
Expand Down Expand Up @@ -345,6 +346,18 @@ func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBi
}

func (self *Gui) GetInitialKeybindingsWithCustomCommands() ([]*types.Binding, []*gocui.ViewMouseBinding) {
// if the search or filter prompt is open, we only want the keybindings for
// that context. It shouldn't be possible, for example, to open a menu while
// the prompt is showing; you first need to confirm or cancel the search/filter.
if currentContext := self.State.ContextMgr.Current(); currentContext.GetKey() == context.SEARCH_CONTEXT_KEY {
bindings := currentContext.GetKeybindings(self.c.KeybindingsOpts())
viewName := currentContext.GetViewName()
for _, binding := range bindings {
binding.ViewName = viewName
}
return bindings, nil
}

bindings, mouseBindings := self.GetInitialKeybindings()
customBindings, err := self.CustomCommandsClient.GetCustomCommandKeybindings()
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/gui/types/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ type IGuiCommon interface {
KeybindingsOpts() KeybindingsOpts
CallKeybindingHandler(binding *Binding) error

ResetKeybindings() error

// hopefully we can remove this once we've moved all our keybinding stuff out of the gui god struct.
GetInitialKeybindingsWithCustomCommands() ([]*Binding, []*gocui.ViewMouseBinding)

Expand Down

0 comments on commit fa8cd47

Please sign in to comment.