1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-04 03:48:07 +02:00

move quit actions to controller

This commit is contained in:
Jesse Duffield 2023-03-26 15:38:18 +11:00
parent 6388885699
commit 037cd99138
6 changed files with 118 additions and 96 deletions

View File

@ -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()
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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),

View File

@ -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
},
})
}

View File

@ -233,6 +233,8 @@ type IStateAccessor interface {
GetIsRefreshingFiles() bool
GetShowExtrasWindow() bool
SetShowExtrasWindow(bool)
GetRetainOriginalDir() bool
SetRetainOriginalDir(bool)
}
type IRepoStateAccessor interface {