Skip to content

Commit d55924a

Browse files
committed
Make menu keybindings take precedence over builtin ones, except for confirm/esc
This makes it possible to use 'j', 'k', 'H' or 'L' as menu item keybindings.
1 parent d8cb1d2 commit d55924a

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

pkg/gui/context/menu_context.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type MenuViewModel struct {
5656
promptLines []string
5757
columnAlignment []utils.Alignment
5858
allowFilteringKeybindings bool
59+
keybindingsTakePrecedence bool
5960
*FilteredListViewModel[*types.MenuItem]
6061
}
6162

@@ -117,6 +118,10 @@ func (self *MenuViewModel) SetAllowFilteringKeybindings(allow bool) {
117118
self.allowFilteringKeybindings = allow
118119
}
119120

121+
func (self *MenuViewModel) SetKeybindingsTakePrecedence(value bool) {
122+
self.keybindingsTakePrecedence = value
123+
}
124+
120125
// TODO: move into presentation package
121126
func (self *MenuViewModel) GetDisplayStrings(_ int, _ int) [][]string {
122127
menuItems := self.FilteredListViewModel.GetItems()
@@ -205,10 +210,19 @@ func (self *MenuContext) GetKeybindings(opts types.KeybindingsOpts) []*types.Bin
205210
}
206211
})
207212

208-
// appending because that means the menu item bindings have lower precedence.
209-
// So if a basic binding is to escape from the menu, we want that to still be
210-
// what happens when you press escape. This matters when we're showing the menu
211-
// for all keybindings of say the files context.
213+
if self.keybindingsTakePrecedence {
214+
// This is used for all normal menus except the keybindings menu. In this case we want the
215+
// bindings of the menu items to have higher precedence than the builtin bindings; this
216+
// allows assigning a keybinding to a menu item that overrides a non-essential binding such
217+
// as 'j', 'k', 'H', 'L', etc. This is safe to do because the essential bindings such as
218+
// confirm and return have already been removed from the menu items in this case.
219+
return append(menuItemBindings, basicBindings...)
220+
}
221+
222+
// For the keybindings menu we didn't remove the essential bindings from the menu items, because
223+
// it is important to see all bindings (as a cheat sheet for what the keys are when the menu is
224+
// not open). Therefore we want the essential bindings to have higher precedence than the menu
225+
// item bindings.
212226
return append(basicBindings, menuItemBindings...)
213227
}
214228

pkg/gui/menu_panel.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func (gui *Gui) createMenu(opts types.CreateMenuOptions) error {
5858
gui.State.Contexts.Menu.SetMenuItems(opts.Items, opts.ColumnAlignment)
5959
gui.State.Contexts.Menu.SetPrompt(opts.Prompt)
6060
gui.State.Contexts.Menu.SetAllowFilteringKeybindings(opts.AllowFilteringKeybindings)
61+
gui.State.Contexts.Menu.SetKeybindingsTakePrecedence(!opts.KeepConflictingKeybindings)
6162
gui.State.Contexts.Menu.SetSelection(0)
6263

6364
gui.Views.Menu.Title = opts.Title

pkg/integration/tests/custom_commands/custom_commands_submenu_with_special_keybindings.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,14 @@ var CustomCommandsSubmenuWithSpecialKeybindings = NewIntegrationTest(NewIntegrat
6060
Contains(" echo down"),
6161
)
6262
t.GlobalPress("j")
63-
/* EXPECTED:
6463
t.ExpectPopup().Alert().Title(Equals("echo j")).Content(Equals("j")).Confirm()
65-
ACTUAL: */
66-
// The menu stays open; 'j' didn't trigger the command; instead, it selected the
67-
// next item, which we can confirm by pressing enter:
68-
t.GlobalPress(keys.Universal.ConfirmMenu)
69-
t.ExpectPopup().Alert().Title(Equals("echo H")).Content(Equals("H")).Confirm()
7064
}).
7165
Press("x").
7266
Tap(func() {
7367
t.ExpectPopup().Menu().
7468
Title(Equals("My Custom Commands"))
7569
t.GlobalPress("H")
76-
/* EXPECTED:
7770
t.ExpectPopup().Alert().Title(Equals("echo H")).Content(Equals("H")).Confirm()
78-
ACTUAL: */
79-
// The menu stays open:
80-
t.ExpectPopup().Menu().
81-
Title(Equals("My Custom Commands"))
8271
})
8372
},
8473
})

0 commit comments

Comments
 (0)