1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-27 12:32:37 +02:00

fix popup focus issue

This commit is contained in:
Jesse Duffield 2022-08-01 21:58:49 +10:00
parent fab2e14b55
commit debc58b6c5
6 changed files with 31 additions and 45 deletions

View File

@ -16,9 +16,9 @@ import (
// This file is for the rendering of confirmation panels along with setting and handling associated // This file is for the rendering of confirmation panels along with setting and handling associated
// keybindings. // keybindings.
func (gui *Gui) wrappedConfirmationFunction(handlersManageFocus bool, function func() error) func() error { func (gui *Gui) wrappedConfirmationFunction(function func() error) func() error {
return func() error { return func() error {
if err := gui.closeConfirmationPrompt(handlersManageFocus); err != nil { if err := gui.c.PopContext(); err != nil {
return err return err
} }
@ -32,9 +32,9 @@ func (gui *Gui) wrappedConfirmationFunction(handlersManageFocus bool, function f
} }
} }
func (gui *Gui) wrappedPromptConfirmationFunction(handlersManageFocus bool, function func(string) error, getResponse func() string) func() error { func (gui *Gui) wrappedPromptConfirmationFunction(function func(string) error, getResponse func() string) func() error {
return func() error { return func() error {
if err := gui.closeConfirmationPrompt(handlersManageFocus); err != nil { if err := gui.c.PopContext(); err != nil {
return err return err
} }
@ -48,27 +48,15 @@ func (gui *Gui) wrappedPromptConfirmationFunction(handlersManageFocus bool, func
} }
} }
func (gui *Gui) closeConfirmationPrompt(handlersManageFocus bool) error { func (gui *Gui) deactivateConfirmationPrompt() {
gui.Mutexes.PopupMutex.Lock() gui.Mutexes.PopupMutex.Lock()
gui.State.CurrentPopupOpts = nil gui.State.CurrentPopupOpts = nil
gui.Mutexes.PopupMutex.Unlock() gui.Mutexes.PopupMutex.Unlock()
// we've already closed it so we can just return
if !gui.Views.Confirmation.Visible {
return nil
}
if !handlersManageFocus {
if err := gui.c.PopContext(); err != nil {
return err
}
}
gui.clearConfirmationViewKeyBindings()
gui.Views.Confirmation.Visible = false gui.Views.Confirmation.Visible = false
gui.Views.Suggestions.Visible = false gui.Views.Suggestions.Visible = false
return nil gui.clearConfirmationViewKeyBindings()
} }
func (gui *Gui) getMessageHeight(wrap bool, message string, width int) int { func (gui *Gui) getMessageHeight(wrap bool, message string, width int) int {
@ -219,14 +207,13 @@ func (gui *Gui) setKeyBindings(opts types.CreatePopupPanelOpts) error {
_ = gui.renderString(gui.Views.Options, actions) _ = gui.renderString(gui.Views.Options, actions)
var onConfirm func() error var onConfirm func() error
if opts.HandleConfirmPrompt != nil { if opts.HandleConfirmPrompt != nil {
onConfirm = gui.wrappedPromptConfirmationFunction(opts.HandlersManageFocus, opts.HandleConfirmPrompt, func() string { return gui.Views.Confirmation.TextArea.GetContent() }) onConfirm = gui.wrappedPromptConfirmationFunction(opts.HandleConfirmPrompt, func() string { return gui.Views.Confirmation.TextArea.GetContent() })
} else { } else {
onConfirm = gui.wrappedConfirmationFunction(opts.HandlersManageFocus, opts.HandleConfirm) onConfirm = gui.wrappedConfirmationFunction(opts.HandleConfirm)
} }
keybindingConfig := gui.c.UserConfig.Keybinding keybindingConfig := gui.c.UserConfig.Keybinding
onSuggestionConfirm := gui.wrappedPromptConfirmationFunction( onSuggestionConfirm := gui.wrappedPromptConfirmationFunction(
opts.HandlersManageFocus,
opts.HandleConfirmPrompt, opts.HandleConfirmPrompt,
gui.getSelectedSuggestionValue, gui.getSelectedSuggestionValue,
) )
@ -248,7 +235,7 @@ func (gui *Gui) setKeyBindings(opts types.CreatePopupPanelOpts) error {
ViewName: "confirmation", ViewName: "confirmation",
Contexts: []string{string(context.CONFIRMATION_CONTEXT_KEY)}, Contexts: []string{string(context.CONFIRMATION_CONTEXT_KEY)},
Key: gui.getKey(keybindingConfig.Universal.Return), Key: gui.getKey(keybindingConfig.Universal.Return),
Handler: gui.wrappedConfirmationFunction(opts.HandlersManageFocus, opts.HandleClose), Handler: gui.wrappedConfirmationFunction(opts.HandleClose),
}, },
{ {
ViewName: "confirmation", ViewName: "confirmation",
@ -277,7 +264,7 @@ func (gui *Gui) setKeyBindings(opts types.CreatePopupPanelOpts) error {
ViewName: "suggestions", ViewName: "suggestions",
Contexts: []string{string(context.SUGGESTIONS_CONTEXT_KEY)}, Contexts: []string{string(context.SUGGESTIONS_CONTEXT_KEY)},
Key: gui.getKey(keybindingConfig.Universal.Return), Key: gui.getKey(keybindingConfig.Universal.Return),
Handler: gui.wrappedConfirmationFunction(opts.HandlersManageFocus, opts.HandleClose), Handler: gui.wrappedConfirmationFunction(opts.HandleClose),
}, },
{ {
ViewName: "suggestions", ViewName: "suggestions",

View File

@ -124,6 +124,10 @@ func (gui *Gui) contextTree() *context.ContextTree {
}), }),
context.ContextCallbackOpts{ context.ContextCallbackOpts{
OnFocus: OnFocusWrapper(gui.handleAskFocused), OnFocus: OnFocusWrapper(gui.handleAskFocused),
OnFocusLost: func() error {
gui.deactivateConfirmationPrompt()
return nil
},
}, },
), ),
CommitMessage: context.NewSimpleContext( CommitMessage: context.NewSimpleContext(

View File

@ -151,15 +151,10 @@ func (self *MergeAndRebaseHelper) CheckMergeOrRebase(result error) error {
return self.c.Confirm(types.ConfirmOpts{ return self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.FoundConflictsTitle, Title: self.c.Tr.FoundConflictsTitle,
Prompt: self.c.Tr.FoundConflicts, Prompt: self.c.Tr.FoundConflicts,
HandlersManageFocus: true,
HandleConfirm: func() error { HandleConfirm: func() error {
return self.c.PushContext(self.contexts.Files) return self.c.PushContext(self.contexts.Files)
}, },
HandleClose: func() error { HandleClose: func() error {
if err := self.c.PopContext(); err != nil {
return err
}
return self.genericMergeCommand(REBASE_OPTION_ABORT) return self.genericMergeCommand(REBASE_OPTION_ABORT)
}, },
}) })

View File

@ -424,7 +424,8 @@ func NewGui(
cmn, cmn,
gui.createPopupPanel, gui.createPopupPanel,
func() error { return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC}) }, func() error { return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC}) },
func() error { return gui.closeConfirmationPrompt(false) }, gui.popContext,
gui.currentContext,
gui.createMenu, gui.createMenu,
gui.withWaitingStatus, gui.withWaitingStatus,
gui.toast, gui.toast,

View File

@ -6,6 +6,7 @@ import (
"github.com/jesseduffield/gocui" "github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/common"
"github.com/jesseduffield/lazygit/pkg/gui/context"
"github.com/jesseduffield/lazygit/pkg/gui/style" "github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
@ -17,7 +18,8 @@ type PopupHandler struct {
sync.Mutex sync.Mutex
createPopupPanelFn func(types.CreatePopupPanelOpts) error createPopupPanelFn func(types.CreatePopupPanelOpts) error
onErrorFn func() error onErrorFn func() error
closePopupFn func() error popContextFn func() error
currentContextFn func() types.Context
createMenuFn func(types.CreateMenuOptions) error createMenuFn func(types.CreateMenuOptions) error
withWaitingStatusFn func(message string, f func() error) error withWaitingStatusFn func(message string, f func() error) error
toastFn func(message string) toastFn func(message string)
@ -30,7 +32,8 @@ func NewPopupHandler(
common *common.Common, common *common.Common,
createPopupPanelFn func(types.CreatePopupPanelOpts) error, createPopupPanelFn func(types.CreatePopupPanelOpts) error,
onErrorFn func() error, onErrorFn func() error,
closePopupFn func() error, popContextFn func() error,
currentContextFn func() types.Context,
createMenuFn func(types.CreateMenuOptions) error, createMenuFn func(types.CreateMenuOptions) error,
withWaitingStatusFn func(message string, f func() error) error, withWaitingStatusFn func(message string, f func() error) error,
toastFn func(message string), toastFn func(message string),
@ -41,7 +44,8 @@ func NewPopupHandler(
index: 0, index: 0,
createPopupPanelFn: createPopupPanelFn, createPopupPanelFn: createPopupPanelFn,
onErrorFn: onErrorFn, onErrorFn: onErrorFn,
closePopupFn: closePopupFn, popContextFn: popContextFn,
currentContextFn: currentContextFn,
createMenuFn: createMenuFn, createMenuFn: createMenuFn,
withWaitingStatusFn: withWaitingStatusFn, withWaitingStatusFn: withWaitingStatusFn,
toastFn: toastFn, toastFn: toastFn,
@ -97,7 +101,6 @@ func (self *PopupHandler) Confirm(opts types.ConfirmOpts) error {
Prompt: opts.Prompt, Prompt: opts.Prompt,
HandleConfirm: opts.HandleConfirm, HandleConfirm: opts.HandleConfirm,
HandleClose: opts.HandleClose, HandleClose: opts.HandleClose,
HandlersManageFocus: opts.HandlersManageFocus,
}) })
} }
@ -139,8 +142,8 @@ func (self *PopupHandler) WithLoaderPanel(message string, f func() error) error
} }
self.Lock() self.Lock()
if index == self.index { if index == self.index && self.currentContextFn().GetKey() == context.CONFIRMATION_CONTEXT_KEY {
_ = self.closePopupFn() _ = self.popContextFn()
} }
self.Unlock() self.Unlock()
}) })

View File

@ -85,9 +85,6 @@ type CreatePopupPanelOpts struct {
HandleConfirmPrompt func(string) error HandleConfirmPrompt func(string) error
HandleClose func() 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
FindSuggestionsFunc func(string) []*Suggestion FindSuggestionsFunc func(string) []*Suggestion
Mask bool Mask bool
} }
@ -97,7 +94,6 @@ type ConfirmOpts struct {
Prompt string Prompt string
HandleConfirm func() error HandleConfirm func() error
HandleClose func() error HandleClose func() error
HandlersManageFocus bool
HasLoader bool HasLoader bool
FindSuggestionsFunc func(string) []*Suggestion FindSuggestionsFunc func(string) []*Suggestion
Editable bool Editable bool