mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-02-09 13:47:11 +02:00
move another action into controller
This commit is contained in:
parent
f8c9ce33c2
commit
2cba98e3fe
@ -1,6 +1,7 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/jesseduffield/gocui"
|
||||||
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -52,6 +53,21 @@ func (self *GlobalController) GetKeybindings(opts types.KeybindingsOpts) []*type
|
|||||||
Handler: self.prevScreenMode,
|
Handler: self.prevScreenMode,
|
||||||
Description: self.c.Tr.LcPrevScreenMode,
|
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 {
|
func (self *GlobalController) prevScreenMode() error {
|
||||||
return (&ScreenModeActions{c: self.c}).Prev()
|
return (&ScreenModeActions{c: self.c}).Prev()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *GlobalController) createOptionsMenu() error {
|
||||||
|
return (&OptionsMenuAction{c: self.c}).Call()
|
||||||
|
}
|
||||||
|
@ -1,23 +1,52 @@
|
|||||||
package gui
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/jesseduffield/generics/slices"
|
"github.com/jesseduffield/generics/slices"
|
||||||
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
|
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
|
||||||
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
||||||
"github.com/samber/lo"
|
"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
|
var bindingsGlobal, bindingsPanel, bindingsNavigation []*types.Binding
|
||||||
|
|
||||||
bindings, _ := gui.GetInitialKeybindings()
|
bindings, _ := self.c.GetInitialKeybindingsWithCustomCommands()
|
||||||
customBindings, err := gui.CustomCommandsClient.GetCustomCommandKeybindings()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
bindings = append(customBindings, bindings...)
|
|
||||||
|
|
||||||
for _, binding := range bindings {
|
for _, binding := range bindings {
|
||||||
if keybindings.LabelFromKey(binding.Key) != "" && binding.Description != "" {
|
if keybindings.LabelFromKey(binding.Key) != "" && binding.Description != "" {
|
||||||
@ -48,35 +77,3 @@ func uniqueBindings(bindings []*types.Binding) []*types.Binding {
|
|||||||
return binding.Description
|
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,
|
|
||||||
})
|
|
||||||
}
|
|
@ -154,5 +154,9 @@ func (self *guiCommon) KeybindingsOpts() types.KeybindingsOpts {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (self *guiCommon) IsAnyModeActive() bool {
|
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()
|
||||||
}
|
}
|
||||||
|
@ -134,21 +134,6 @@ func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBi
|
|||||||
Modifier: gocui.ModNone,
|
Modifier: gocui.ModNone,
|
||||||
Handler: self.scrollDownMain,
|
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",
|
ViewName: "files",
|
||||||
Key: opts.GetKey(opts.Config.Universal.CopyToClipboard),
|
Key: opts.GetKey(opts.Config.Universal.CopyToClipboard),
|
||||||
@ -417,17 +402,21 @@ func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBi
|
|||||||
return bindings, mouseKeybindings
|
return bindings, mouseKeybindings
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gui *Gui) resetKeybindings() error {
|
func (self *Gui) GetInitialKeybindingsWithCustomCommands() ([]*types.Binding, []*gocui.ViewMouseBinding) {
|
||||||
gui.g.DeleteAllKeybindings()
|
bindings, mouseBindings := self.GetInitialKeybindings()
|
||||||
|
customBindings, err := self.CustomCommandsClient.GetCustomCommandKeybindings()
|
||||||
bindings, mouseBindings := gui.GetInitialKeybindings()
|
|
||||||
|
|
||||||
// prepending because we want to give our custom keybindings precedence over default keybindings
|
|
||||||
customBindings, err := gui.CustomCommandsClient.GetCustomCommandKeybindings()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
// prepending because we want to give our custom keybindings precedence over default keybindings
|
||||||
bindings = append(customBindings, bindings...)
|
bindings = append(customBindings, bindings...)
|
||||||
|
return bindings, mouseBindings
|
||||||
|
}
|
||||||
|
|
||||||
|
func (gui *Gui) resetKeybindings() error {
|
||||||
|
gui.g.DeleteAllKeybindings()
|
||||||
|
|
||||||
|
bindings, mouseBindings := gui.GetInitialKeybindingsWithCustomCommands()
|
||||||
|
|
||||||
for _, binding := range bindings {
|
for _, binding := range bindings {
|
||||||
if err := gui.SetKeybinding(binding); err != nil {
|
if err := gui.SetKeybinding(binding); err != nil {
|
||||||
|
@ -92,6 +92,9 @@ type IGuiCommon interface {
|
|||||||
State() IStateAccessor
|
State() IStateAccessor
|
||||||
|
|
||||||
KeybindingsOpts() KeybindingsOpts
|
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 {
|
type IModeMgr interface {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user