2018-08-28 20:13:01 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
2022-07-30 12:27:51 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
|
2022-01-29 10:09:20 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
2019-10-18 09:48:37 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/theme"
|
2022-07-30 12:27:51 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
2018-08-28 20:13:01 +02:00
|
|
|
)
|
|
|
|
|
2022-01-28 11:44:36 +02:00
|
|
|
// note: items option is mutated by this function
|
2022-01-29 10:09:20 +02:00
|
|
|
func (gui *Gui) createMenu(opts types.CreateMenuOptions) error {
|
2022-01-28 11:44:36 +02:00
|
|
|
if !opts.HideCancel {
|
2020-02-14 13:07:56 +02:00
|
|
|
// this is mutative but I'm okay with that for now
|
2022-01-29 10:09:20 +02:00
|
|
|
opts.Items = append(opts.Items, &types.MenuItem{
|
2022-05-08 06:23:32 +02:00
|
|
|
LabelColumns: []string{gui.c.Tr.LcCancel},
|
2022-01-28 11:44:36 +02:00
|
|
|
OnPress: func() error {
|
2020-02-14 13:07:56 +02:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-30 12:27:51 +02:00
|
|
|
maxColumnSize := 1
|
|
|
|
|
|
|
|
for _, item := range opts.Items {
|
|
|
|
if item.LabelColumns == nil {
|
|
|
|
item.LabelColumns = []string{item.Label}
|
|
|
|
}
|
|
|
|
|
|
|
|
if item.OpensMenu {
|
|
|
|
item.LabelColumns[0] = presentation.OpensMenuStyle(item.LabelColumns[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
maxColumnSize = utils.Max(maxColumnSize, len(item.LabelColumns))
|
|
|
|
}
|
|
|
|
|
2022-02-05 08:04:10 +02:00
|
|
|
for _, item := range opts.Items {
|
2022-07-30 12:27:51 +02:00
|
|
|
if len(item.LabelColumns) < maxColumnSize {
|
|
|
|
// we require that each item has the same number of columns so we're padding out with blank strings
|
|
|
|
// if this item has too few
|
|
|
|
item.LabelColumns = append(item.LabelColumns, make([]string, maxColumnSize-len(item.LabelColumns))...)
|
2021-11-02 12:16:00 +02:00
|
|
|
}
|
2020-02-14 13:07:56 +02:00
|
|
|
}
|
|
|
|
|
2022-05-08 04:46:48 +02:00
|
|
|
gui.State.Contexts.Menu.SetMenuItems(opts.Items)
|
|
|
|
gui.State.Contexts.Menu.SetSelectedLineIdx(0)
|
|
|
|
|
|
|
|
gui.Views.Menu.Title = opts.Title
|
|
|
|
gui.Views.Menu.FgColor = theme.GocuiDefaultTextColor
|
|
|
|
gui.Views.Menu.SetOnSelectItem(gui.onSelectItemWrapper(func(selectedLine int) error {
|
2020-04-21 11:25:05 +02:00
|
|
|
return nil
|
|
|
|
}))
|
2020-08-20 00:24:35 +02:00
|
|
|
|
2022-05-08 04:46:48 +02:00
|
|
|
gui.Views.Tooltip.Wrap = true
|
|
|
|
gui.Views.Tooltip.FgColor = theme.GocuiDefaultTextColor
|
|
|
|
gui.Views.Tooltip.Visible = true
|
2022-03-27 08:15:17 +02:00
|
|
|
|
|
|
|
// resetting keybindings so that the menu-specific keybindings are registered
|
|
|
|
if err := gui.resetKeybindings(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-05 08:04:10 +02:00
|
|
|
_ = gui.c.PostRefreshUpdate(gui.State.Contexts.Menu)
|
2020-08-20 00:24:35 +02:00
|
|
|
|
2022-02-05 08:04:10 +02:00
|
|
|
// TODO: ensure that if we're opened a menu from within a menu that it renders correctly
|
|
|
|
return gui.c.PushContext(gui.State.Contexts.Menu)
|
2020-08-20 00:24:35 +02:00
|
|
|
}
|