2022-02-05 08:04:10 +02:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
2022-03-19 10:12:58 +02:00
|
|
|
"github.com/jesseduffield/generics/slices"
|
2022-03-27 08:15:17 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
|
2022-03-27 08:22:31 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/style"
|
2022-02-05 08:04:10 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
2023-05-21 03:02:36 +02:00
|
|
|
"github.com/samber/lo"
|
2022-02-05 08:04:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type MenuContext struct {
|
2023-05-21 03:02:36 +02:00
|
|
|
c *ContextCommon
|
|
|
|
|
2022-02-05 08:04:10 +02:00
|
|
|
*MenuViewModel
|
|
|
|
*ListContextTrait
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ types.IListContext = (*MenuContext)(nil)
|
|
|
|
|
|
|
|
func NewMenuContext(
|
2023-03-23 03:35:07 +02:00
|
|
|
c *ContextCommon,
|
2022-02-05 08:04:10 +02:00
|
|
|
) *MenuContext {
|
2023-05-21 03:02:36 +02:00
|
|
|
viewModel := NewMenuViewModel(c)
|
2022-02-05 08:04:10 +02:00
|
|
|
|
|
|
|
return &MenuContext{
|
2023-05-21 03:02:36 +02:00
|
|
|
c: c,
|
2022-02-05 08:04:10 +02:00
|
|
|
MenuViewModel: viewModel,
|
|
|
|
ListContextTrait: &ListContextTrait{
|
|
|
|
Context: NewSimpleContext(NewBaseContext(NewBaseContextOpts{
|
2023-03-21 11:57:52 +02:00
|
|
|
View: c.Views().Menu,
|
2022-06-13 03:01:26 +02:00
|
|
|
WindowName: "menu",
|
|
|
|
Key: "menu",
|
|
|
|
Kind: types.TEMPORARY_POPUP,
|
|
|
|
Focusable: true,
|
|
|
|
HasUncontrolledBounds: true,
|
2023-03-21 12:01:58 +02:00
|
|
|
})),
|
2022-02-05 08:04:10 +02:00
|
|
|
getDisplayStrings: viewModel.GetDisplayStrings,
|
|
|
|
list: viewModel,
|
|
|
|
c: c,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: remove this thing.
|
|
|
|
func (self *MenuContext) GetSelectedItemId() string {
|
|
|
|
item := self.GetSelected()
|
|
|
|
if item == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2022-05-08 06:23:32 +02:00
|
|
|
return item.Label
|
2022-02-05 08:04:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type MenuViewModel struct {
|
2023-05-21 03:02:36 +02:00
|
|
|
c *ContextCommon
|
2022-02-05 08:04:10 +02:00
|
|
|
menuItems []*types.MenuItem
|
2022-03-19 00:31:52 +02:00
|
|
|
*BasicViewModel[*types.MenuItem]
|
2022-02-05 08:04:10 +02:00
|
|
|
}
|
|
|
|
|
2023-05-21 03:02:36 +02:00
|
|
|
func NewMenuViewModel(c *ContextCommon) *MenuViewModel {
|
2022-02-05 08:04:10 +02:00
|
|
|
self := &MenuViewModel{
|
|
|
|
menuItems: nil,
|
2023-05-21 03:02:36 +02:00
|
|
|
c: c,
|
2022-02-05 08:04:10 +02:00
|
|
|
}
|
|
|
|
|
2022-03-19 00:31:52 +02:00
|
|
|
self.BasicViewModel = NewBasicViewModel(func() []*types.MenuItem { return self.menuItems })
|
2022-02-05 08:04:10 +02:00
|
|
|
|
|
|
|
return self
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *MenuViewModel) SetMenuItems(items []*types.MenuItem) {
|
|
|
|
self.menuItems = items
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: move into presentation package
|
2022-03-19 10:12:58 +02:00
|
|
|
func (self *MenuViewModel) GetDisplayStrings(_startIdx int, _length int) [][]string {
|
2022-03-27 08:15:17 +02:00
|
|
|
showKeys := slices.Some(self.menuItems, func(item *types.MenuItem) bool {
|
|
|
|
return item.Key != nil
|
|
|
|
})
|
|
|
|
|
2022-03-19 10:12:58 +02:00
|
|
|
return slices.Map(self.menuItems, func(item *types.MenuItem) []string {
|
2022-07-30 12:27:51 +02:00
|
|
|
displayStrings := item.LabelColumns
|
2023-05-21 03:02:36 +02:00
|
|
|
|
|
|
|
if !showKeys {
|
|
|
|
return displayStrings
|
|
|
|
}
|
|
|
|
|
|
|
|
// These keys are used for general navigation so we'll strike them out to
|
|
|
|
// avoid confusion
|
|
|
|
reservedKeys := []string{
|
|
|
|
self.c.UserConfig.Keybinding.Universal.Confirm,
|
|
|
|
self.c.UserConfig.Keybinding.Universal.Select,
|
|
|
|
self.c.UserConfig.Keybinding.Universal.Return,
|
|
|
|
}
|
|
|
|
keyLabel := keybindings.LabelFromKey(item.Key)
|
|
|
|
keyStyle := style.FgCyan
|
|
|
|
if lo.Contains(reservedKeys, keyLabel) {
|
|
|
|
keyStyle = style.FgDefault.SetStrikethrough()
|
2022-02-05 08:04:10 +02:00
|
|
|
}
|
2023-05-21 03:02:36 +02:00
|
|
|
|
|
|
|
displayStrings = slices.Prepend(displayStrings, keyStyle.Sprint(keyLabel))
|
2022-03-27 08:15:17 +02:00
|
|
|
return displayStrings
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *MenuContext) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
|
|
|
|
basicBindings := self.ListContextTrait.GetKeybindings(opts)
|
|
|
|
menuItemsWithKeys := slices.Filter(self.menuItems, func(item *types.MenuItem) bool {
|
|
|
|
return item.Key != nil
|
|
|
|
})
|
|
|
|
|
|
|
|
menuItemBindings := slices.Map(menuItemsWithKeys, func(item *types.MenuItem) *types.Binding {
|
|
|
|
return &types.Binding{
|
|
|
|
Key: item.Key,
|
|
|
|
Handler: func() error { return self.OnMenuPress(item) },
|
2022-03-19 10:12:58 +02:00
|
|
|
}
|
|
|
|
})
|
2022-03-27 08:15:17 +02:00
|
|
|
|
|
|
|
// appending because that means the menu item bindings have lower precedence.
|
|
|
|
// So if a basic binding is to escape from the menu, we want that to still be
|
|
|
|
// what happens when you press escape. This matters when we're showing the menu
|
|
|
|
// for all keybindings of say the files context.
|
|
|
|
return append(basicBindings, menuItemBindings...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *MenuContext) OnMenuPress(selectedItem *types.MenuItem) error {
|
|
|
|
if err := self.c.PopContext(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := selectedItem.OnPress(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2022-02-05 08:04:10 +02:00
|
|
|
}
|