Skip to content

Commit 4dfa4e8

Browse files
committed
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.
1 parent 9c5c459 commit 4dfa4e8

File tree

4 files changed

+13
-11
lines changed

4 files changed

+13
-11
lines changed

pkg/gui/context/base_context.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ type BaseContext struct {
1818
onClickFn func() error
1919
onClickFocusedMainViewFn onClickFocusedMainViewFn
2020
onRenderToMainFn func()
21-
onFocusFn onFocusFn
22-
onFocusLostFn onFocusLostFn
21+
onFocusFns []onFocusFn
22+
onFocusLostFns []onFocusLostFn
2323

2424
focusable bool
2525
transient bool
@@ -135,9 +135,11 @@ func (self *BaseContext) AddMouseKeybindingsFn(fn types.MouseKeybindingsFn) {
135135
self.mouseKeybindingsFns = append(self.mouseKeybindingsFns, fn)
136136
}
137137

138-
func (self *BaseContext) ClearAllBindingsFn() {
138+
func (self *BaseContext) ClearAllAttachedControllerFunctions() {
139139
self.keybindingsFns = nil
140140
self.mouseKeybindingsFns = nil
141+
self.onFocusFns = nil
142+
self.onFocusLostFns = nil
141143
}
142144

143145
func (self *BaseContext) AddOnClickFn(fn func() error) {
@@ -168,13 +170,13 @@ func (self *BaseContext) AddOnRenderToMainFn(fn func()) {
168170

169171
func (self *BaseContext) AddOnFocusFn(fn onFocusFn) {
170172
if fn != nil {
171-
self.onFocusFn = fn
173+
self.onFocusFns = append(self.onFocusFns, fn)
172174
}
173175
}
174176

175177
func (self *BaseContext) AddOnFocusLostFn(fn onFocusLostFn) {
176178
if fn != nil {
177-
self.onFocusLostFn = fn
179+
self.onFocusLostFns = append(self.onFocusLostFns, fn)
178180
}
179181
}
180182

pkg/gui/context/simple_context.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ func (self *SimpleContext) HandleFocus(opts types.OnFocusOpts) {
3737
self.GetViewTrait().SetHighlight(true)
3838
}
3939

40-
if self.onFocusFn != nil {
41-
self.onFocusFn(opts)
40+
for _, fn := range self.onFocusFns {
41+
fn(opts)
4242
}
4343

4444
if self.onRenderToMainFn != nil {
@@ -49,8 +49,8 @@ func (self *SimpleContext) HandleFocus(opts types.OnFocusOpts) {
4949
func (self *SimpleContext) HandleFocusLost(opts types.OnFocusLostOpts) {
5050
self.GetViewTrait().SetHighlight(false)
5151
self.view.SetOriginX(0)
52-
if self.onFocusLostFn != nil {
53-
self.onFocusLostFn(opts)
52+
for _, fn := range self.onFocusLostFns {
53+
fn(opts)
5454
}
5555
}
5656

pkg/gui/controllers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (gui *Gui) Helpers() *helpers.Helpers {
2121
// the lower in the list the keybindings will appear.
2222
func (gui *Gui) resetHelpersAndControllers() {
2323
for _, context := range gui.Contexts().Flatten() {
24-
context.ClearAllBindingsFn()
24+
context.ClearAllAttachedControllerFunctions()
2525
}
2626

2727
helperCommon := gui.c

pkg/gui/types/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ type IBaseContext interface {
8888

8989
AddKeybindingsFn(KeybindingsFn)
9090
AddMouseKeybindingsFn(MouseKeybindingsFn)
91-
ClearAllBindingsFn()
91+
ClearAllAttachedControllerFunctions()
9292

9393
// This is a bit of a hack at the moment: we currently only set an onclick function so that
9494
// our list controller can come along and wrap it in a list-specific click handler.

0 commit comments

Comments
 (0)