2022-01-16 05:46:53 +02:00
|
|
|
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-16 05:46:53 +02:00
|
|
|
|
2022-01-29 10:09:20 +02:00
|
|
|
getSelectedMenuItem func() *types.MenuItem
|
2022-01-16 05:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2022-01-16 05:46:53 +02:00
|
|
|
) *MenuController {
|
|
|
|
return &MenuController{
|
|
|
|
c: c,
|
2022-01-23 05:40:28 +02:00
|
|
|
getContext: getContext,
|
2022-01-16 05:46:53 +02:00
|
|
|
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-16 05:46:53 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-01-23 05:40:28 +02:00
|
|
|
return append(bindings, self.getContext().Keybindings(getKey, config, guards)...)
|
2022-01-16 05:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2022-01-16 05:46:53 +02:00
|
|
|
}
|