From 2cba98e3fe6c97b319e2a0a24baac93dccfad900 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Thu, 23 Mar 2023 22:21:42 +1100 Subject: [PATCH] move another action into controller --- pkg/gui/controllers/global_controller.go | 20 +++++ .../options_menu_action.go} | 81 +++++++++---------- pkg/gui/gui_common.go | 6 +- pkg/gui/keybindings.go | 33 +++----- pkg/gui/types/common.go | 3 + 5 files changed, 78 insertions(+), 65 deletions(-) rename pkg/gui/{options_menu_panel.go => controllers/options_menu_action.go} (80%) diff --git a/pkg/gui/controllers/global_controller.go b/pkg/gui/controllers/global_controller.go index 964172b92..cf3d0007d 100644 --- a/pkg/gui/controllers/global_controller.go +++ b/pkg/gui/controllers/global_controller.go @@ -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() +} diff --git a/pkg/gui/options_menu_panel.go b/pkg/gui/controllers/options_menu_action.go similarity index 80% rename from pkg/gui/options_menu_panel.go rename to pkg/gui/controllers/options_menu_action.go index 1635e9fbd..0e747a7ad 100644 --- a/pkg/gui/options_menu_panel.go +++ b/pkg/gui/controllers/options_menu_action.go @@ -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, - }) -} diff --git a/pkg/gui/gui_common.go b/pkg/gui/gui_common.go index 7c26c655d..3171cba78 100644 --- a/pkg/gui/gui_common.go +++ b/pkg/gui/gui_common.go @@ -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() } diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 57251e3bc..2260fcd3d 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -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 { diff --git a/pkg/gui/types/common.go b/pkg/gui/types/common.go index dcccc1747..6fe0a8b6b 100644 --- a/pkg/gui/types/common.go +++ b/pkg/gui/types/common.go @@ -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 {