diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2018-09-18 21:07:25 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2018-09-18 21:07:25 +1000 |
commit | b384fcf6af4e5248616712414e0164887d7bbcbd (patch) | |
tree | c464c806ede33869764bd7476a48ef85ec310946 | |
parent | f89bc10af1f9fb4211badd2fee51e903bbc03f6c (diff) |
generalise popup menu panel
-rw-r--r-- | pkg/gui/gui.go | 1 | ||||
-rw-r--r-- | pkg/gui/keybindings.go | 7 | ||||
-rw-r--r-- | pkg/gui/menu_panel.go | 74 | ||||
-rw-r--r-- | pkg/gui/options_menu_panel.go | 51 |
4 files changed, 73 insertions, 60 deletions
diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index f527e8c5d..8a2aaea81 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -83,7 +83,6 @@ type guiState struct { EditHistory *stack.Stack Platform commands.Platform Updating bool - Keys []*Binding } // NewGui builds a new gui handler diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index e05caaec7..72a187472 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -93,7 +93,7 @@ func (gui *Gui) GetKeybindings() []*Binding { ViewName: "", Key: 'x', Modifier: gocui.ModNone, - Handler: gui.handleMenu, + Handler: gui.handleCreateOptionsMenu, }, { ViewName: "status", Key: 'e', @@ -369,11 +369,6 @@ func (gui *Gui) GetKeybindings() []*Binding { Key: 'q', Modifier: gocui.ModNone, Handler: gui.handleMenuClose, - }, { - ViewName: "menu", - Key: gocui.KeySpace, - Modifier: gocui.ModNone, - Handler: gui.handleMenuPress, }, } diff --git a/pkg/gui/menu_panel.go b/pkg/gui/menu_panel.go index 945d0020a..753e8f84d 100644 --- a/pkg/gui/menu_panel.go +++ b/pkg/gui/menu_panel.go @@ -8,21 +8,6 @@ import ( "github.com/jesseduffield/lazygit/pkg/utils" ) -func (gui *Gui) handleMenuPress(g *gocui.Gui, v *gocui.View) error { - lineNumber := gui.getItemPosition(v) - if gui.State.Keys[lineNumber].Key == nil { - return nil - } - if len(gui.State.Keys) > lineNumber { - err := gui.handleMenuClose(g, v) - if err != nil { - return err - } - return gui.State.Keys[lineNumber].Handler(g, v) - } - return nil -} - func (gui *Gui) handleMenuSelect(g *gocui.Gui, v *gocui.View) error { // doing nothing for now // but it is needed for switch in newLineFocused @@ -39,9 +24,9 @@ func (gui *Gui) renderMenuOptions(g *gocui.Gui) error { } func (gui *Gui) handleMenuClose(g *gocui.Gui, v *gocui.View) error { - // better to delete because for example after closing update confirmation panel, - // the focus isn't set back to any of panels and one is unable to even quit - //_, err := g.SetViewOnBottom(v.Name()) + if err := g.DeleteKeybinding("menu", gocui.KeySpace, gocui.ModNone); err != nil { + return err + } err := g.DeleteView("menu") if err != nil { return err @@ -49,55 +34,38 @@ func (gui *Gui) handleMenuClose(g *gocui.Gui, v *gocui.View) error { return gui.returnFocus(g, v) } -func (gui *Gui) getBindings(v *gocui.View) []*Binding { - var ( - bindingsGlobal, bindingsPanel []*Binding - ) - - bindings := gui.GetKeybindings() - - for _, binding := range bindings { - if binding.GetKey() != "" && binding.Description != "" { - switch binding.ViewName { - case "": - bindingsGlobal = append(bindingsGlobal, binding) - case v.Name(): - bindingsPanel = append(bindingsPanel, binding) - } - } - } - - // append dummy element to have a separator between - // panel and global keybindings - bindingsPanel = append(bindingsPanel, &Binding{}) - return append(bindingsPanel, bindingsGlobal...) -} - -func (gui *Gui) handleMenu(g *gocui.Gui, v *gocui.View) error { - gui.State.Keys = gui.getBindings(v) - - list, err := utils.RenderList(gui.State.Keys) +func (gui *Gui) createMenu(items interface{}, handlePress func(int) error) error { + list, err := utils.RenderList(items) if err != nil { return err } - x0, y0, x1, y1 := gui.getConfirmationPanelDimensions(g, list) - menuView, _ := g.SetView("menu", x0, y0, x1, y1, 0) + x0, y0, x1, y1 := gui.getConfirmationPanelDimensions(gui.g, list) + menuView, _ := gui.g.SetView("menu", x0, y0, x1, y1, 0) menuView.Title = strings.Title(gui.Tr.SLocalize("menu")) menuView.FgColor = gocui.ColorWhite menuView.Clear() fmt.Fprint(menuView, list) - if err := gui.renderMenuOptions(g); err != nil { + if err := gui.renderMenuOptions(gui.g); err != nil { + return err + } + + wrappedHandlePress := func(g *gocui.Gui, v *gocui.View) error { + lineNumber := gui.getItemPosition(v) + return handlePress(lineNumber) + } + + if err := gui.g.SetKeybinding("menu", gocui.KeySpace, gocui.ModNone, wrappedHandlePress); err != nil { return err } - g.Update(func(g *gocui.Gui) error { - _, err := g.SetViewOnTop("menu") - if err != nil { + gui.g.Update(func(g *gocui.Gui) error { + if _, err := g.SetViewOnTop("menu"); err != nil { return err } - return gui.switchFocus(g, v, menuView) + currentView := gui.g.CurrentView() + return gui.switchFocus(gui.g, currentView, menuView) }) return nil } diff --git a/pkg/gui/options_menu_panel.go b/pkg/gui/options_menu_panel.go new file mode 100644 index 000000000..2da002b43 --- /dev/null +++ b/pkg/gui/options_menu_panel.go @@ -0,0 +1,51 @@ +package gui + +import ( + "errors" + + "github.com/jesseduffield/gocui" +) + +func (gui *Gui) getBindings(v *gocui.View) []*Binding { + var ( + bindingsGlobal, bindingsPanel []*Binding + ) + + bindings := gui.GetKeybindings() + + for _, binding := range bindings { + if binding.GetKey() != "" && binding.Description != "" { + switch binding.ViewName { + case "": + bindingsGlobal = append(bindingsGlobal, binding) + case v.Name(): + bindingsPanel = append(bindingsPanel, binding) + } + } + } + + // append dummy element to have a separator between + // panel and global keybindings + bindingsPanel = append(bindingsPanel, &Binding{}) + return append(bindingsPanel, bindingsGlobal...) +} + +func (gui *Gui) handleCreateOptionsMenu(g *gocui.Gui, v *gocui.View) error { + bindings := gui.getBindings(v) + + handleOptionsMenuPress := func(index int) error { + if bindings[index].Key == nil { + return nil + } + if index <= len(bindings) { + return errors.New("Index is greater than size of bindings") + } + err := gui.handleMenuClose(g, v) + if err != nil { + return err + } + return bindings[index].Handler(g, v) + } + + return gui.createMenu(bindings, handleOptionsMenuPress) +} |