1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-15 00:15:32 +02:00

allow explicitly managing focus

This commit is contained in:
Jesse Duffield
2020-08-23 13:09:36 +10:00
parent 94601b4dc9
commit 3eb3de3edc
3 changed files with 60 additions and 38 deletions

View File

@ -187,6 +187,7 @@ func (gui *Gui) enterCommitFile(selectedLineIdx int) error {
return gui.ask(askOpts{
title: gui.Tr.SLocalize("DiscardPatch"),
prompt: gui.Tr.SLocalize("DiscardPatchConfirm"),
handlersManageFocus: true,
handleConfirm: func() error {
gui.GitCommand.PatchManager.Reset()
return enterTheFile(selectedLineIdx)

View File

@ -17,21 +17,23 @@ import (
type createPopupPanelOpts struct {
hasLoader bool
returnFocusOnClose bool
editable bool
title string
prompt string
handleConfirm func() error
handleConfirmPrompt func(string) error
handleClose func() error
// when handlersManageFocus is true, do not return from the confirmation context automatically. It's expected that the handlers will manage focus, whether that means switching to another context, or manually returning the context.
handlersManageFocus bool
}
type askOpts struct {
returnFocusOnClose bool
title string
prompt string
handleConfirm func() error
handleClose func() error
handlersManageFocus bool
}
func (gui *Gui) createLoaderPanel(currentView *gocui.View, prompt string) error {
@ -45,9 +47,9 @@ func (gui *Gui) ask(opts askOpts) error {
return gui.createPopupPanel(createPopupPanelOpts{
title: opts.title,
prompt: opts.prompt,
handleConfirm: opts.handleConfirm,
handleClose: opts.handleClose,
handlersManageFocus: opts.handlersManageFocus,
})
}
@ -60,47 +62,60 @@ func (gui *Gui) prompt(title string, initialContent string, handleConfirm func(s
})
}
func (gui *Gui) wrappedConfirmationFunction(function func() error, returnFocusOnClose bool) func(*gocui.Gui, *gocui.View) error {
func (gui *Gui) wrappedConfirmationFunction(handlersManageFocus bool, function func() error) func(*gocui.Gui, *gocui.View) error {
return func(g *gocui.Gui, v *gocui.View) error {
if function != nil {
if err := function(); err != nil {
return err
}
}
return gui.closeConfirmationPrompt(returnFocusOnClose)
if err := gui.closeConfirmationPrompt(handlersManageFocus); err != nil {
return err
}
return nil
}
}
func (gui *Gui) wrappedPromptConfirmationFunction(function func(string) error, returnFocusOnClose bool) func(*gocui.Gui, *gocui.View) error {
func (gui *Gui) wrappedPromptConfirmationFunction(handlersManageFocus bool, function func(string) error) func(*gocui.Gui, *gocui.View) error {
return func(g *gocui.Gui, v *gocui.View) error {
if function != nil {
if err := function(v.Buffer()); err != nil {
return gui.surfaceError(err)
}
}
return gui.closeConfirmationPrompt(returnFocusOnClose)
}
}
func (gui *Gui) closeConfirmationPrompt(returnFocusOnClose bool) error {
view := gui.getConfirmationView()
if view == nil {
return nil // if it's already been closed we can just return
}
view.Editable = false
if err := gui.returnFromContext(); err != nil {
if err := gui.closeConfirmationPrompt(handlersManageFocus); err != nil {
return err
}
return nil
}
}
func (gui *Gui) deleteConfirmationView() {
gui.g.DeleteKeybinding("confirmation", gui.getKey("universal.confirm"), gocui.ModNone)
gui.g.DeleteKeybinding("confirmation", gui.getKey("universal.confirm-alt1"), gocui.ModNone)
gui.g.DeleteKeybinding("confirmation", gui.getKey("universal.return"), gocui.ModNone)
return gui.g.DeleteView("confirmation")
_ = gui.g.DeleteView("confirmation")
}
func (gui *Gui) closeConfirmationPrompt(handlersManageFocus bool) error {
view := gui.getConfirmationView()
if view == nil {
return nil // if it's already been closed we can just return
}
if !handlersManageFocus {
if err := gui.returnFromContext(); err != nil {
return err
}
}
gui.deleteConfirmationView()
return nil
}
func (gui *Gui) getMessageHeight(wrap bool, message string, width int) int {
@ -164,9 +179,7 @@ func (gui *Gui) createPopupPanel(opts createPopupPanelOpts) error {
gui.g.Update(func(g *gocui.Gui) error {
// delete the existing confirmation panel if it exists
if view, _ := g.View("confirmation"); view != nil {
if err := gui.closeConfirmationPrompt(true); err != nil {
gui.Log.Error(err)
}
gui.deleteConfirmationView()
}
confirmationView, err := gui.prepareConfirmationPanel(opts.title, opts.prompt, opts.hasLoader)
if err != nil {
@ -202,9 +215,9 @@ func (gui *Gui) setKeyBindings(opts createPopupPanelOpts) error {
gui.renderString("options", actions)
var onConfirm func(*gocui.Gui, *gocui.View) error
if opts.handleConfirmPrompt != nil {
onConfirm = gui.wrappedPromptConfirmationFunction(opts.handleConfirmPrompt, opts.returnFocusOnClose)
onConfirm = gui.wrappedPromptConfirmationFunction(opts.handlersManageFocus, opts.handleConfirmPrompt)
} else {
onConfirm = gui.wrappedConfirmationFunction(opts.handleConfirm, opts.returnFocusOnClose)
onConfirm = gui.wrappedConfirmationFunction(opts.handlersManageFocus, opts.handleConfirm)
}
if err := gui.g.SetKeybinding("confirmation", nil, gui.getKey("universal.confirm"), gocui.ModNone, onConfirm); err != nil {
@ -214,7 +227,7 @@ func (gui *Gui) setKeyBindings(opts createPopupPanelOpts) error {
return err
}
return gui.g.SetKeybinding("confirmation", nil, gui.getKey("universal.return"), gocui.ModNone, gui.wrappedConfirmationFunction(opts.handleClose, opts.returnFocusOnClose))
return gui.g.SetKeybinding("confirmation", nil, gui.getKey("universal.return"), gocui.ModNone, gui.wrappedConfirmationFunction(opts.handlersManageFocus, opts.handleClose))
}
func (gui *Gui) createErrorPanel(message string) error {

View File

@ -107,9 +107,17 @@ func (gui *Gui) handleResetSelection(g *gocui.Gui, v *gocui.View) error {
return gui.ask(askOpts{
title: gui.Tr.SLocalize("UnstageLinesTitle"),
prompt: gui.Tr.SLocalize("UnstageLinesPrompt"),
handlersManageFocus: true,
handleConfirm: func() error {
if err := gui.switchContext(gui.Contexts.Staging.Context); err != nil {
return err
}
return gui.applySelection(true)
},
handleClose: func() error {
return gui.switchContext(gui.Contexts.Staging.Context)
},
})
} else {
return gui.applySelection(true)