From 57991c1da899bea7fd553d9f976e015b0c72519f Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sat, 17 May 2025 11:35:40 +0200 Subject: [PATCH] Fix crash when clicking in the status view The click handler of MainViewController was registered as a global handler, so it was used when a side panel was focused that doesn't have a SwitchToFocusedMainViewController attached (e.g. Status, Worktrees, or Submodules). This handler would then push the main view context, but with the code that is meant only for toggling between the main view pair contexts, i.e. with taking over the parentContext from the otherContext, which doesn't have one at that point. This would later lead to a crash in onClick because the parentContext was nil. Fix this by splitting the click handler in two, one for when it already has the focus, and one for toggling from the other view, and make these focus specific. --- pkg/gui/controllers/main_view_controller.go | 41 +++++++++++---------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/pkg/gui/controllers/main_view_controller.go b/pkg/gui/controllers/main_view_controller.go index e6b209286..8ccbcc6af 100644 --- a/pkg/gui/controllers/main_view_controller.go +++ b/pkg/gui/controllers/main_view_controller.go @@ -56,21 +56,16 @@ func (self *MainViewController) GetKeybindings(opts types.KeybindingsOpts) []*ty func (self *MainViewController) GetMouseKeybindings(opts types.KeybindingsOpts) []*gocui.ViewMouseBinding { return []*gocui.ViewMouseBinding{ { - ViewName: self.context.GetViewName(), - Key: gocui.MouseLeft, - Handler: func(opts gocui.ViewMouseBindingOpts) error { - if self.isFocused() { - return self.onClick(opts) - } - - self.context.SetParentContext(self.otherContext.GetParentContext()) - self.c.Context().Push(self.context, types.OnFocusOpts{ - ClickedWindowName: self.context.GetWindowName(), - ClickedViewLineIdx: opts.Y, - }) - - return nil - }, + ViewName: self.context.GetViewName(), + Key: gocui.MouseLeft, + Handler: self.onClickInAlreadyFocusedView, + FocusedView: self.context.GetViewName(), + }, + { + ViewName: self.context.GetViewName(), + Key: gocui.MouseLeft, + Handler: self.onClickInOtherViewOfMainViewPair, + FocusedView: self.otherContext.GetViewName(), }, } } @@ -93,7 +88,7 @@ func (self *MainViewController) escape() error { return nil } -func (self *MainViewController) onClick(opts gocui.ViewMouseBindingOpts) error { +func (self *MainViewController) onClickInAlreadyFocusedView(opts gocui.ViewMouseBindingOpts) error { parentCtx := self.context.GetParentContext() if parentCtx.GetOnClickFocusedMainView() != nil { return parentCtx.GetOnClickFocusedMainView()(self.context.GetViewName(), opts.Y) @@ -101,6 +96,16 @@ func (self *MainViewController) onClick(opts gocui.ViewMouseBindingOpts) error { return nil } +func (self *MainViewController) onClickInOtherViewOfMainViewPair(opts gocui.ViewMouseBindingOpts) error { + self.context.SetParentContext(self.otherContext.GetParentContext()) + self.c.Context().Push(self.context, types.OnFocusOpts{ + ClickedWindowName: self.context.GetWindowName(), + ClickedViewLineIdx: opts.Y, + }) + + return nil +} + func (self *MainViewController) openSearch() error { if manager := self.c.GetViewBufferManagerForView(self.context.GetView()); manager != nil { manager.ReadToEnd(func() { @@ -112,7 +117,3 @@ func (self *MainViewController) openSearch() error { return nil } - -func (self *MainViewController) isFocused() bool { - return self.c.Context().Current().GetKey() == self.context.GetKey() -}