mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-08-08 22:36:49 +02:00
Allow more than one controller to attach OnFocus/OnFocusLost functions
Trying to do this would previously have the second one silently overwrite the first one's. We don't currently have this in lazygit, but I ran into the situation once during development, and it can lead to bugs that are hard to diagnose. Instead of holding a list of functions, we could also have added a panic in case the function was set already; this would have been good enough for the current state, and enough to catch mistakes early in the future. However, I decided to allow multiple controllers to attach these functions, because I can't see a reason not to.
This commit is contained in:
@ -18,8 +18,8 @@ type BaseContext struct {
|
|||||||
onClickFn func() error
|
onClickFn func() error
|
||||||
onClickFocusedMainViewFn onClickFocusedMainViewFn
|
onClickFocusedMainViewFn onClickFocusedMainViewFn
|
||||||
onRenderToMainFn func()
|
onRenderToMainFn func()
|
||||||
onFocusFn onFocusFn
|
onFocusFns []onFocusFn
|
||||||
onFocusLostFn onFocusLostFn
|
onFocusLostFns []onFocusLostFn
|
||||||
|
|
||||||
focusable bool
|
focusable bool
|
||||||
transient bool
|
transient bool
|
||||||
@ -135,9 +135,11 @@ func (self *BaseContext) AddMouseKeybindingsFn(fn types.MouseKeybindingsFn) {
|
|||||||
self.mouseKeybindingsFns = append(self.mouseKeybindingsFns, fn)
|
self.mouseKeybindingsFns = append(self.mouseKeybindingsFns, fn)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *BaseContext) ClearAllBindingsFn() {
|
func (self *BaseContext) ClearAllAttachedControllerFunctions() {
|
||||||
self.keybindingsFns = nil
|
self.keybindingsFns = nil
|
||||||
self.mouseKeybindingsFns = nil
|
self.mouseKeybindingsFns = nil
|
||||||
|
self.onFocusFns = nil
|
||||||
|
self.onFocusLostFns = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *BaseContext) AddOnClickFn(fn func() error) {
|
func (self *BaseContext) AddOnClickFn(fn func() error) {
|
||||||
@ -168,13 +170,13 @@ func (self *BaseContext) AddOnRenderToMainFn(fn func()) {
|
|||||||
|
|
||||||
func (self *BaseContext) AddOnFocusFn(fn onFocusFn) {
|
func (self *BaseContext) AddOnFocusFn(fn onFocusFn) {
|
||||||
if fn != nil {
|
if fn != nil {
|
||||||
self.onFocusFn = fn
|
self.onFocusFns = append(self.onFocusFns, fn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *BaseContext) AddOnFocusLostFn(fn onFocusLostFn) {
|
func (self *BaseContext) AddOnFocusLostFn(fn onFocusLostFn) {
|
||||||
if fn != nil {
|
if fn != nil {
|
||||||
self.onFocusLostFn = fn
|
self.onFocusLostFns = append(self.onFocusLostFns, fn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,8 +37,8 @@ func (self *SimpleContext) HandleFocus(opts types.OnFocusOpts) {
|
|||||||
self.GetViewTrait().SetHighlight(true)
|
self.GetViewTrait().SetHighlight(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.onFocusFn != nil {
|
for _, fn := range self.onFocusFns {
|
||||||
self.onFocusFn(opts)
|
fn(opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.onRenderToMainFn != nil {
|
if self.onRenderToMainFn != nil {
|
||||||
@ -49,8 +49,8 @@ func (self *SimpleContext) HandleFocus(opts types.OnFocusOpts) {
|
|||||||
func (self *SimpleContext) HandleFocusLost(opts types.OnFocusLostOpts) {
|
func (self *SimpleContext) HandleFocusLost(opts types.OnFocusLostOpts) {
|
||||||
self.GetViewTrait().SetHighlight(false)
|
self.GetViewTrait().SetHighlight(false)
|
||||||
self.view.SetOriginX(0)
|
self.view.SetOriginX(0)
|
||||||
if self.onFocusLostFn != nil {
|
for _, fn := range self.onFocusLostFns {
|
||||||
self.onFocusLostFn(opts)
|
fn(opts)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ func (gui *Gui) Helpers() *helpers.Helpers {
|
|||||||
// the lower in the list the keybindings will appear.
|
// the lower in the list the keybindings will appear.
|
||||||
func (gui *Gui) resetHelpersAndControllers() {
|
func (gui *Gui) resetHelpersAndControllers() {
|
||||||
for _, context := range gui.Contexts().Flatten() {
|
for _, context := range gui.Contexts().Flatten() {
|
||||||
context.ClearAllBindingsFn()
|
context.ClearAllAttachedControllerFunctions()
|
||||||
}
|
}
|
||||||
|
|
||||||
helperCommon := gui.c
|
helperCommon := gui.c
|
||||||
|
@ -88,7 +88,7 @@ type IBaseContext interface {
|
|||||||
|
|
||||||
AddKeybindingsFn(KeybindingsFn)
|
AddKeybindingsFn(KeybindingsFn)
|
||||||
AddMouseKeybindingsFn(MouseKeybindingsFn)
|
AddMouseKeybindingsFn(MouseKeybindingsFn)
|
||||||
ClearAllBindingsFn()
|
ClearAllAttachedControllerFunctions()
|
||||||
|
|
||||||
// This is a bit of a hack at the moment: we currently only set an onclick function so that
|
// This is a bit of a hack at the moment: we currently only set an onclick function so that
|
||||||
// our list controller can come along and wrap it in a list-specific click handler.
|
// our list controller can come along and wrap it in a list-specific click handler.
|
||||||
|
Reference in New Issue
Block a user