From 037cd9913881d5d39c5a9ed1ceb1c8368c5ccd0c Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 26 Mar 2023 15:38:18 +1100 Subject: [PATCH] move quit actions to controller --- pkg/gui/controllers/global_controller.go | 32 ++++++++++ pkg/gui/controllers/quit_actions.go | 75 ++++++++++++++++++++++++ pkg/gui/gui.go | 10 +++- pkg/gui/keybindings.go | 24 -------- pkg/gui/quitting.go | 71 ---------------------- pkg/gui/types/common.go | 2 + 6 files changed, 118 insertions(+), 96 deletions(-) create mode 100644 pkg/gui/controllers/quit_actions.go delete mode 100644 pkg/gui/quitting.go diff --git a/pkg/gui/controllers/global_controller.go b/pkg/gui/controllers/global_controller.go index 540318ac7..29843f00c 100644 --- a/pkg/gui/controllers/global_controller.go +++ b/pkg/gui/controllers/global_controller.go @@ -87,6 +87,26 @@ func (self *GlobalController) GetKeybindings(opts types.KeybindingsOpts) []*type Description: self.c.Tr.LcOpenDiffingMenu, OpensMenu: true, }, + { + Key: opts.GetKey(opts.Config.Universal.Quit), + Modifier: gocui.ModNone, + Handler: self.quit, + }, + { + Key: opts.GetKey(opts.Config.Universal.QuitAlt1), + Modifier: gocui.ModNone, + Handler: self.quit, + }, + { + Key: opts.GetKey(opts.Config.Universal.QuitWithoutChangingDirectory), + Modifier: gocui.ModNone, + Handler: self.quitWithoutChangingDirectory, + }, + { + Key: opts.GetKey(opts.Config.Universal.Return), + Modifier: gocui.ModNone, + Handler: self.escape, + }, } } @@ -125,3 +145,15 @@ func (self *GlobalController) createFilteringMenu() error { func (self *GlobalController) createDiffingMenu() error { return (&DiffingMenuAction{c: self.c}).Call() } + +func (self *GlobalController) quit() error { + return (&QuitActions{c: self.c}).Quit() +} + +func (self *GlobalController) quitWithoutChangingDirectory() error { + return (&QuitActions{c: self.c}).QuitWithoutChangingDirectory() +} + +func (self *GlobalController) escape() error { + return (&QuitActions{c: self.c}).Escape() +} diff --git a/pkg/gui/controllers/quit_actions.go b/pkg/gui/controllers/quit_actions.go new file mode 100644 index 000000000..2487a62fe --- /dev/null +++ b/pkg/gui/controllers/quit_actions.go @@ -0,0 +1,75 @@ +package controllers + +import ( + "github.com/jesseduffield/gocui" + "github.com/jesseduffield/lazygit/pkg/gui/types" +) + +type QuitActions struct { + c *ControllerCommon +} + +func (self *QuitActions) Quit() error { + self.c.State().SetRetainOriginalDir(false) + return self.quitAux() +} + +func (self *QuitActions) QuitWithoutChangingDirectory() error { + self.c.State().SetRetainOriginalDir(true) + return self.quitAux() +} + +func (self *QuitActions) quitAux() error { + if self.c.State().GetUpdating() { + return self.confirmQuitDuringUpdate() + } + + if self.c.UserConfig.ConfirmOnQuit { + return self.c.Confirm(types.ConfirmOpts{ + Title: "", + Prompt: self.c.Tr.ConfirmQuit, + HandleConfirm: func() error { + return gocui.ErrQuit + }, + }) + } + + return gocui.ErrQuit +} + +func (self *QuitActions) confirmQuitDuringUpdate() error { + return self.c.Confirm(types.ConfirmOpts{ + Title: self.c.Tr.ConfirmQuitDuringUpdateTitle, + Prompt: self.c.Tr.ConfirmQuitDuringUpdate, + HandleConfirm: func() error { + return gocui.ErrQuit + }, + }) +} + +func (self *QuitActions) Escape() error { + currentContext := self.c.CurrentContext() + + parentContext, hasParent := currentContext.GetParentContext() + if hasParent && currentContext != nil && parentContext != nil { + // TODO: think about whether this should be marked as a return rather than adding to the stack + return self.c.PushContext(parentContext) + } + + for _, mode := range self.c.Helpers().Mode.Statuses() { + if mode.IsActive() { + return mode.Reset() + } + } + + repoPathStack := self.c.State().GetRepoPathStack() + if !repoPathStack.IsEmpty() { + return self.c.Helpers().Repos.DispatchSwitchToRepo(repoPathStack.Pop(), true) + } + + if self.c.UserConfig.QuitOnTopLevelReturn { + return self.Quit() + } + + return nil +} diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 45c3ff44e..22f0c748a 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -178,6 +178,14 @@ func (self *StateAccessor) SetShowExtrasWindow(value bool) { self.gui.ShowExtrasWindow = value } +func (self *StateAccessor) GetRetainOriginalDir() bool { + return self.gui.RetainOriginalDir +} + +func (self *StateAccessor) SetRetainOriginalDir(value bool) { + self.gui.RetainOriginalDir = value +} + // we keep track of some stuff from one render to the next to see if certain // things have changed type PrevLayout struct { @@ -632,7 +640,7 @@ func (gui *Gui) RunAndHandleError(startArgs appTypes.StartArgs) error { switch err { case gocui.ErrQuit: - if gui.RetainOriginalDir { + if gui.c.State().GetRetainOriginalDir() { if err := gui.helpers.RecordDirectory.RecordDirectory(gui.InitialDir); err != nil { return err } diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go index 040d634b8..11917045f 100644 --- a/pkg/gui/keybindings.go +++ b/pkg/gui/keybindings.go @@ -79,30 +79,6 @@ func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBi opts := self.c.KeybindingsOpts() bindings := []*types.Binding{ - { - ViewName: "", - Key: opts.GetKey(opts.Config.Universal.Quit), - Modifier: gocui.ModNone, - Handler: self.handleQuit, - }, - { - ViewName: "", - Key: opts.GetKey(opts.Config.Universal.QuitWithoutChangingDirectory), - Modifier: gocui.ModNone, - Handler: self.handleQuitWithoutChangingDirectory, - }, - { - ViewName: "", - Key: opts.GetKey(opts.Config.Universal.QuitAlt1), - Modifier: gocui.ModNone, - Handler: self.handleQuit, - }, - { - ViewName: "", - Key: opts.GetKey(opts.Config.Universal.Return), - Modifier: gocui.ModNone, - Handler: self.handleTopLevelReturn, - }, { ViewName: "", Key: opts.GetKey(opts.Config.Universal.OpenRecentRepos), diff --git a/pkg/gui/quitting.go b/pkg/gui/quitting.go deleted file mode 100644 index 749866de0..000000000 --- a/pkg/gui/quitting.go +++ /dev/null @@ -1,71 +0,0 @@ -package gui - -import ( - "github.com/jesseduffield/gocui" - "github.com/jesseduffield/lazygit/pkg/gui/types" -) - -func (gui *Gui) handleQuitWithoutChangingDirectory() error { - gui.RetainOriginalDir = true - return gui.quit() -} - -func (gui *Gui) handleQuit() error { - gui.RetainOriginalDir = false - return gui.quit() -} - -func (gui *Gui) handleTopLevelReturn() error { - currentContext := gui.c.CurrentContext() - - parentContext, hasParent := currentContext.GetParentContext() - if hasParent && currentContext != nil && parentContext != nil { - // TODO: think about whether this should be marked as a return rather than adding to the stack - return gui.c.PushContext(parentContext) - } - - for _, mode := range gui.helpers.Mode.Statuses() { - if mode.IsActive() { - return mode.Reset() - } - } - - repoPathStack := gui.c.State().GetRepoPathStack() - if !repoPathStack.IsEmpty() { - return gui.helpers.Repos.DispatchSwitchToRepo(repoPathStack.Pop(), true) - } - - if gui.c.UserConfig.QuitOnTopLevelReturn { - return gui.handleQuit() - } - - return nil -} - -func (gui *Gui) quit() error { - if gui.c.State().GetUpdating() { - return gui.createUpdateQuitConfirmation() - } - - if gui.c.UserConfig.ConfirmOnQuit { - return gui.c.Confirm(types.ConfirmOpts{ - Title: "", - Prompt: gui.c.Tr.ConfirmQuit, - HandleConfirm: func() error { - return gocui.ErrQuit - }, - }) - } - - return gocui.ErrQuit -} - -func (gui *Gui) createUpdateQuitConfirmation() error { - return gui.c.Confirm(types.ConfirmOpts{ - Title: gui.Tr.ConfirmQuitDuringUpdateTitle, - Prompt: gui.Tr.ConfirmQuitDuringUpdate, - HandleConfirm: func() error { - return gocui.ErrQuit - }, - }) -} diff --git a/pkg/gui/types/common.go b/pkg/gui/types/common.go index 6fe0a8b6b..c42e3e299 100644 --- a/pkg/gui/types/common.go +++ b/pkg/gui/types/common.go @@ -233,6 +233,8 @@ type IStateAccessor interface { GetIsRefreshingFiles() bool GetShowExtrasWindow() bool SetShowExtrasWindow(bool) + GetRetainOriginalDir() bool + SetRetainOriginalDir(bool) } type IRepoStateAccessor interface {