1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-03 13:21:56 +02:00

Add Enabled func to Binding

This commit is contained in:
Stefan Haller 2023-09-05 22:27:28 +02:00
parent f2f50ccf75
commit e592d81b60
5 changed files with 36 additions and 5 deletions

View File

@ -25,6 +25,10 @@ func (self *OptionsMenuAction) Call() error {
appendBindings := func(bindings []*types.Binding, section *types.MenuSection) {
menuItems = append(menuItems,
lo.Map(bindings, func(binding *types.Binding, _ int) *types.MenuItem {
disabledReason := ""
if binding.GetDisabledReason != nil {
disabledReason = binding.GetDisabledReason()
}
return &types.MenuItem{
OpensMenu: binding.OpensMenu,
Label: binding.Description,
@ -33,11 +37,12 @@ func (self *OptionsMenuAction) Call() error {
return nil
}
return binding.Handler()
return self.c.IGuiCommon.CallKeybindingHandler(binding)
},
Key: binding.Key,
Tooltip: binding.Tooltip,
Section: section,
Key: binding.Key,
Tooltip: binding.Tooltip,
DisabledReason: disabledReason,
Section: section,
}
})...)
}

View File

@ -171,6 +171,10 @@ func (self *guiCommon) KeybindingsOpts() types.KeybindingsOpts {
return self.gui.keybindingOpts()
}
func (self *guiCommon) CallKeybindingHandler(binding *types.Binding) error {
return self.gui.callKeybindingHandler(binding)
}
func (self *guiCommon) IsAnyModeActive() bool {
return self.gui.helpers.Mode.IsAnyModeActive()
}

View File

@ -375,7 +375,10 @@ func (gui *Gui) wrappedHandler(f func() error) func(g *gocui.Gui, v *gocui.View)
}
func (gui *Gui) SetKeybinding(binding *types.Binding) error {
handler := binding.Handler
handler := func() error {
return gui.callKeybindingHandler(binding)
}
// TODO: move all mouse-ey stuff into new mouse approach
if gocui.IsMouseKey(binding.Key) {
handler = func() error {
@ -406,3 +409,14 @@ func (gui *Gui) SetMouseKeybinding(binding *gocui.ViewMouseBinding) error {
return gui.g.SetViewClickBinding(binding)
}
func (gui *Gui) callKeybindingHandler(binding *types.Binding) error {
disabledReason := ""
if binding.GetDisabledReason != nil {
disabledReason = binding.GetDisabledReason()
}
if disabledReason != "" {
return gui.c.ErrorMsg(disabledReason)
}
return binding.Handler()
}

View File

@ -104,6 +104,7 @@ type IGuiCommon interface {
State() IStateAccessor
KeybindingsOpts() KeybindingsOpts
CallKeybindingHandler(binding *Binding) error
// hopefully we can remove this once we've moved all our keybinding stuff out of the gui god struct.
GetInitialKeybindingsWithCustomCommands() ([]*Binding, []*gocui.ViewMouseBinding)

View File

@ -25,6 +25,13 @@ type Binding struct {
// to be displayed if the keybinding is highlighted from within a menu
Tooltip string
// Function to decide whether the command is enabled, and why. If this
// returns an empty string, it is; if it returns a non-empty string, it is
// disabled and we show the given text in an error message when trying to
// invoke it. When left nil, the command is always enabled. Note that this
// function must not do expensive calls.
GetDisabledReason func() string
}
// A guard is a decorator which checks something before executing a handler