diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-01-28 20:44:36 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-17 19:13:40 +1100 |
commit | a90b6efded49abcfa2516db794d7875b0396f558 (patch) | |
tree | ad9c3738830437064ada223f0978dab1d726b479 /pkg/gui/menu_panel.go | |
parent | fa8571e1f4c349e401542285ea238acdbd9d17ec (diff) |
start refactoring gui
Diffstat (limited to 'pkg/gui/menu_panel.go')
-rw-r--r-- | pkg/gui/menu_panel.go | 56 |
1 files changed, 17 insertions, 39 deletions
diff --git a/pkg/gui/menu_panel.go b/pkg/gui/menu_panel.go index b32b3bf44..f1bc558c4 100644 --- a/pkg/gui/menu_panel.go +++ b/pkg/gui/menu_panel.go @@ -3,31 +3,12 @@ package gui import ( "errors" "fmt" - "strings" + "github.com/jesseduffield/lazygit/pkg/gui/popup" "github.com/jesseduffield/lazygit/pkg/theme" "github.com/jesseduffield/lazygit/pkg/utils" ) -type menuItem struct { - displayString string - displayStrings []string - onPress func() error - // only applies when displayString is used - opensMenu bool -} - -// every item in a list context needs an ID -func (i *menuItem) ID() string { - if i.displayString != "" { - return i.displayString - } - - return strings.Join(i.displayStrings, "-") -} - -// specific functions - func (gui *Gui) getMenuOptions() map[string]string { keybindingConfig := gui.UserConfig.Keybinding @@ -42,37 +23,34 @@ func (gui *Gui) handleMenuClose() error { return gui.returnFromContext() } -type createMenuOptions struct { - showCancel bool -} - -func (gui *Gui) createMenu(title string, items []*menuItem, createMenuOptions createMenuOptions) error { - if createMenuOptions.showCancel { +// note: items option is mutated by this function +func (gui *Gui) createMenu(opts popup.CreateMenuOptions) error { + if !opts.HideCancel { // this is mutative but I'm okay with that for now - items = append(items, &menuItem{ - displayStrings: []string{gui.Tr.LcCancel}, - onPress: func() error { + opts.Items = append(opts.Items, &popup.MenuItem{ + DisplayStrings: []string{gui.Tr.LcCancel}, + OnPress: func() error { return nil }, }) } - gui.State.MenuItems = items + gui.State.MenuItems = opts.Items - stringArrays := make([][]string, len(items)) - for i, item := range items { - if item.opensMenu && item.displayStrings != nil { + stringArrays := make([][]string, len(opts.Items)) + for i, item := range opts.Items { + if item.OpensMenu && item.DisplayStrings != nil { return errors.New("Message for the developer of this app: you've set opensMenu with displaystrings on the menu panel. Bad developer!. Apologies, user") } - if item.displayStrings == nil { - styledStr := item.displayString - if item.opensMenu { + if item.DisplayStrings == nil { + styledStr := item.DisplayString + if item.OpensMenu { styledStr = opensMenuStyle(styledStr) } stringArrays[i] = []string{styledStr} } else { - stringArrays[i] = item.displayStrings + stringArrays[i] = item.DisplayStrings } } @@ -80,7 +58,7 @@ func (gui *Gui) createMenu(title string, items []*menuItem, createMenuOptions cr x0, y0, x1, y1 := gui.getConfirmationPanelDimensions(false, list) menuView, _ := gui.g.SetView("menu", x0, y0, x1, y1, 0) - menuView.Title = title + menuView.Title = opts.Title menuView.FgColor = theme.GocuiDefaultTextColor menuView.SetOnSelectItem(gui.onSelectItemWrapper(func(selectedLine int) error { return nil @@ -97,7 +75,7 @@ func (gui *Gui) onMenuPress() error { return err } - if err := gui.State.MenuItems[selectedLine].onPress(); err != nil { + if err := gui.State.MenuItems[selectedLine].OnPress(); err != nil { return err } |