Skip to content

Commit

Permalink
Clear keybinding functions in resetHelpersAndControllers
Browse files Browse the repository at this point in the history
When switching to a repo that was open before, the context tree is reused, so
before adding keybinding functions to those contexts again, we need to clear the
old ones.
  • Loading branch information
stefanhaller committed Jun 13, 2024
1 parent c828963 commit fd4d550
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
5 changes: 5 additions & 0 deletions pkg/gui/context/base_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ func (self *BaseContext) AddMouseKeybindingsFn(fn types.MouseKeybindingsFn) {
self.mouseKeybindingsFns = append(self.mouseKeybindingsFns, fn)
}

func (self *BaseContext) ClearAllBindingsFn() {
self.keybindingsFns = []types.KeybindingsFn{}
self.mouseKeybindingsFns = []types.MouseKeybindingsFn{}
}

func (self *BaseContext) AddOnClickFn(fn func() error) {
if fn != nil {
self.onClickFn = fn
Expand Down
4 changes: 4 additions & 0 deletions pkg/gui/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func (gui *Gui) Helpers() *helpers.Helpers {
// in the keybinding menu: the earlier that the controller is attached to a context,
// the lower in the list the keybindings will appear.
func (gui *Gui) resetHelpersAndControllers() {
for _, context := range gui.Contexts().Flatten() {
context.ClearAllBindingsFn()
}

helperCommon := gui.c
recordDirectoryHelper := helpers.NewRecordDirectoryHelper(helperCommon)
reposHelper := helpers.NewRecentReposHelper(helperCommon, recordDirectoryHelper, gui.onNewRepo)
Expand Down
1 change: 1 addition & 0 deletions pkg/gui/types/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ type IBaseContext interface {

AddKeybindingsFn(KeybindingsFn)
AddMouseKeybindingsFn(MouseKeybindingsFn)
ClearAllBindingsFn()

// This is a bit of a hack at the moment: we currently only set an onclick function so that
// our list controller can come along and wrap it in a list-specific click handler.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ var KeybindingSuggestionsWhenSwitchingRepos = NewIntegrationTest(NewIntegrationT
switchToRepo("repo")

t.Views().Options().Content(
/* EXPECTED:
Equals("Commit: c | Stash: s | Reset: D | Keybindings: ? | Cancel: <esc>"))
ACTUAL (all keybindings appear twice): */
Equals("Commit: c | Stash: s | Reset: D | Commit: c | Stash: s | Reset: D | Keybindings: ? | Cancel: <esc> | Keybindings: ? | Cancel: <esc>"))
},
})

0 comments on commit fd4d550

Please sign in to comment.