1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-03 13:21:56 +02:00

move another action into controller

This commit is contained in:
Jesse Duffield 2023-03-23 22:21:42 +11:00
parent f8c9ce33c2
commit 2cba98e3fe
5 changed files with 78 additions and 65 deletions

View File

@ -1,6 +1,7 @@
package controllers
import (
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
@ -52,6 +53,21 @@ func (self *GlobalController) GetKeybindings(opts types.KeybindingsOpts) []*type
Handler: self.prevScreenMode,
Description: self.c.Tr.LcPrevScreenMode,
},
{
ViewName: "",
Key: opts.GetKey(opts.Config.Universal.OptionMenu),
Handler: self.createOptionsMenu,
OpensMenu: true,
},
{
ViewName: "",
Key: opts.GetKey(opts.Config.Universal.OptionMenuAlt1),
Modifier: gocui.ModNone,
// we have the description on the alt key and not the main key for legacy reasons
// (the original main key was 'x' but we've reassigned that to other purposes)
Description: self.c.Tr.LcOpenMenu,
Handler: self.createOptionsMenu,
},
}
}
@ -78,3 +94,7 @@ func (self *GlobalController) nextScreenMode() error {
func (self *GlobalController) prevScreenMode() error {
return (&ScreenModeActions{c: self.c}).Prev()
}
func (self *GlobalController) createOptionsMenu() error {
return (&OptionsMenuAction{c: self.c}).Call()
}

View File

@ -1,23 +1,52 @@
package gui
package controllers
import (
"log"
"github.com/jesseduffield/generics/slices"
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/samber/lo"
)
func (gui *Gui) getBindings(context types.Context) []*types.Binding {
type OptionsMenuAction struct {
c *ControllerCommon
}
func (self *OptionsMenuAction) Call() error {
ctx := self.c.CurrentContext()
// Don't show menu while displaying popup.
if ctx.GetKind() == types.PERSISTENT_POPUP || ctx.GetKind() == types.TEMPORARY_POPUP {
return nil
}
bindings := self.getBindings(ctx)
menuItems := slices.Map(bindings, func(binding *types.Binding) *types.MenuItem {
return &types.MenuItem{
OpensMenu: binding.OpensMenu,
Label: binding.Description,
OnPress: func() error {
if binding.Handler == nil {
return nil
}
return binding.Handler()
},
Key: binding.Key,
Tooltip: binding.Tooltip,
}
})
return self.c.Menu(types.CreateMenuOptions{
Title: self.c.Tr.MenuTitle,
Items: menuItems,
HideCancel: true,
})
}
func (self *OptionsMenuAction) getBindings(context types.Context) []*types.Binding {
var bindingsGlobal, bindingsPanel, bindingsNavigation []*types.Binding
bindings, _ := gui.GetInitialKeybindings()
customBindings, err := gui.CustomCommandsClient.GetCustomCommandKeybindings()
if err != nil {
log.Fatal(err)
}
bindings = append(customBindings, bindings...)
bindings, _ := self.c.GetInitialKeybindingsWithCustomCommands()
for _, binding := range bindings {
if keybindings.LabelFromKey(binding.Key) != "" && binding.Description != "" {
@ -48,35 +77,3 @@ func uniqueBindings(bindings []*types.Binding) []*types.Binding {
return binding.Description
})
}
func (gui *Gui) handleCreateOptionsMenu() error {
ctx := gui.c.CurrentContext()
// Don't show menu while displaying popup.
if ctx.GetKind() == types.PERSISTENT_POPUP || ctx.GetKind() == types.TEMPORARY_POPUP {
return nil
}
bindings := gui.getBindings(ctx)
menuItems := slices.Map(bindings, func(binding *types.Binding) *types.MenuItem {
return &types.MenuItem{
OpensMenu: binding.OpensMenu,
Label: binding.Description,
OnPress: func() error {
if binding.Handler == nil {
return nil
}
return binding.Handler()
},
Key: binding.Key,
Tooltip: binding.Tooltip,
}
})
return gui.c.Menu(types.CreateMenuOptions{
Title: gui.c.Tr.MenuTitle,
Items: menuItems,
HideCancel: true,
})
}

View File

@ -154,5 +154,9 @@ func (self *guiCommon) KeybindingsOpts() types.KeybindingsOpts {
}
func (self *guiCommon) IsAnyModeActive() bool {
return self.IsAnyModeActive()
return self.gui.helpers.Mode.IsAnyModeActive()
}
func (self *guiCommon) GetInitialKeybindingsWithCustomCommands() ([]*types.Binding, []*gocui.ViewMouseBinding) {
return self.gui.GetInitialKeybindingsWithCustomCommands()
}

View File

@ -134,21 +134,6 @@ func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBi
Modifier: gocui.ModNone,
Handler: self.scrollDownMain,
},
{
ViewName: "",
Key: opts.GetKey(opts.Config.Universal.OptionMenu),
Handler: self.handleCreateOptionsMenu,
OpensMenu: true,
},
{
ViewName: "",
Key: opts.GetKey(opts.Config.Universal.OptionMenuAlt1),
Modifier: gocui.ModNone,
// we have the description on the alt key and not the main key for legacy reasons
// (the original main key was 'x' but we've reassigned that to other purposes)
Description: self.c.Tr.LcOpenMenu,
Handler: self.handleCreateOptionsMenu,
},
{
ViewName: "files",
Key: opts.GetKey(opts.Config.Universal.CopyToClipboard),
@ -417,17 +402,21 @@ func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBi
return bindings, mouseKeybindings
}
func (gui *Gui) resetKeybindings() error {
gui.g.DeleteAllKeybindings()
bindings, mouseBindings := gui.GetInitialKeybindings()
// prepending because we want to give our custom keybindings precedence over default keybindings
customBindings, err := gui.CustomCommandsClient.GetCustomCommandKeybindings()
func (self *Gui) GetInitialKeybindingsWithCustomCommands() ([]*types.Binding, []*gocui.ViewMouseBinding) {
bindings, mouseBindings := self.GetInitialKeybindings()
customBindings, err := self.CustomCommandsClient.GetCustomCommandKeybindings()
if err != nil {
log.Fatal(err)
}
// prepending because we want to give our custom keybindings precedence over default keybindings
bindings = append(customBindings, bindings...)
return bindings, mouseBindings
}
func (gui *Gui) resetKeybindings() error {
gui.g.DeleteAllKeybindings()
bindings, mouseBindings := gui.GetInitialKeybindingsWithCustomCommands()
for _, binding := range bindings {
if err := gui.SetKeybinding(binding); err != nil {

View File

@ -92,6 +92,9 @@ type IGuiCommon interface {
State() IStateAccessor
KeybindingsOpts() KeybindingsOpts
// hopefully we can remove this once we've moved all our keybinding stuff out of the gui god struct.
GetInitialKeybindingsWithCustomCommands() ([]*Binding, []*gocui.ViewMouseBinding)
}
type IModeMgr interface {