1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-08 04:04:22 +02:00
lazygit/pkg/gui/controllers/menu_controller.go
Stefan Haller 7f9818cfa2 Add DisabledReason field to MenuItem
This is useful to disable items that are not applicable right now because of
some condition (e.g. the "delete branch" menu item when the currently
checked-out branch is selected).

When a DisabledReason is set on a menu item, we
- show it in a tooltip (below the regular tooltip of the item, if it has one)
- strike through the item's key, if it has one
- show an error message with the DisabledReason if the user tries to invoke the
  command
2023-09-18 10:15:11 +02:00

83 lines
1.9 KiB
Go

package controllers
import (
"github.com/jesseduffield/lazygit/pkg/gui/context"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
type MenuController struct {
baseController
c *ControllerCommon
}
var _ types.IController = &MenuController{}
func NewMenuController(
common *ControllerCommon,
) *MenuController {
return &MenuController{
baseController: baseController{},
c: common,
}
}
// NOTE: if you add a new keybinding here, you'll also need to add it to
// `reservedKeys` in `pkg/gui/context/menu_context.go`
func (self *MenuController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{
{
Key: opts.GetKey(opts.Config.Universal.Select),
Handler: self.press,
},
{
Key: opts.GetKey(opts.Config.Universal.Confirm),
Handler: self.press,
Description: self.c.Tr.Execute,
Display: true,
},
{
Key: opts.GetKey(opts.Config.Universal.Return),
Handler: self.close,
Description: self.c.Tr.Close,
Display: true,
},
}
return bindings
}
func (self *MenuController) GetOnClick() func() error {
return self.press
}
func (self *MenuController) GetOnFocus() func(types.OnFocusOpts) error {
return func(types.OnFocusOpts) error {
selectedMenuItem := self.context().GetSelected()
if selectedMenuItem != nil {
self.c.Views().Tooltip.SetContent(self.c.Helpers().Confirmation.TooltipForMenuItem(selectedMenuItem))
}
return nil
}
}
func (self *MenuController) press() error {
return self.context().OnMenuPress(self.context().GetSelected())
}
func (self *MenuController) close() error {
if self.context().IsFiltering() {
self.c.Helpers().Search.Cancel()
return nil
}
return self.c.PopContext()
}
func (self *MenuController) Context() types.Context {
return self.context()
}
func (self *MenuController) context() *context.MenuContext {
return self.c.Contexts().Menu
}