1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/gui/controllers/menu_controller.go

70 lines
1.6 KiB
Go
Raw Normal View History

package controllers
import (
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
type MenuController struct {
2022-01-29 10:09:20 +02:00
c *types.ControllerCommon
2022-01-23 05:40:28 +02:00
getContext func() types.IListContext
2022-01-29 10:09:20 +02:00
getSelectedMenuItem func() *types.MenuItem
}
var _ types.IController = &MenuController{}
func NewMenuController(
2022-01-29 10:09:20 +02:00
c *types.ControllerCommon,
2022-01-23 05:40:28 +02:00
getContext func() types.IListContext,
2022-01-29 10:09:20 +02:00
getSelectedMenuItem func() *types.MenuItem,
) *MenuController {
return &MenuController{
c: c,
2022-01-23 05:40:28 +02:00
getContext: getContext,
getSelectedMenuItem: getSelectedMenuItem,
}
}
func (self *MenuController) Keybindings(getKey func(key string) interface{}, config config.KeybindingConfig, guards types.KeybindingGuards) []*types.Binding {
bindings := []*types.Binding{
{
Key: getKey(config.Universal.Select),
Handler: self.press,
},
{
Key: getKey(config.Universal.Confirm),
Handler: self.press,
},
{
Key: getKey(config.Universal.ConfirmAlt1),
Handler: self.press,
},
{
Key: gocui.MouseLeft,
2022-01-23 05:40:28 +02:00
Handler: func() error { return self.getContext().HandleClick(self.press) },
},
}
2022-01-23 05:40:28 +02:00
return append(bindings, self.getContext().Keybindings(getKey, config, guards)...)
}
func (self *MenuController) press() error {
selectedItem := self.getSelectedMenuItem()
if err := self.c.PopContext(); err != nil {
return err
}
if err := selectedItem.OnPress(); err != nil {
return err
}
return nil
}
func (self *MenuController) Context() types.Context {
2022-01-23 05:40:28 +02:00
return self.getContext()
}