From bbd17abc43a7d0b8866b7129fd3fd1d650c70962 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Thu, 22 May 2025 08:19:13 +0200 Subject: [PATCH] Add ContextMgr.NextInStack and use it to access side panel of focused main view This way we don't have to abuse the parent context mechanism, which isn't meant for this purpose. --- pkg/gui/context.go | 16 ++++++++++++++++ pkg/gui/controllers/context_lines_controller.go | 4 +++- pkg/gui/controllers/main_view_controller.go | 8 +++----- .../rename_similarity_threshold_controller.go | 4 +++- .../switch_to_focused_main_view_controller.go | 1 - pkg/gui/types/context.go | 1 + pkg/gui/view_helpers.go | 6 +++--- 7 files changed, 29 insertions(+), 11 deletions(-) diff --git a/pkg/gui/context.go b/pkg/gui/context.go index ca5727d38..45b2bdf34 100644 --- a/pkg/gui/context.go +++ b/pkg/gui/context.go @@ -357,3 +357,19 @@ func (self *ContextMgr) CurrentPopup() []types.Context { return context.GetKind() == types.TEMPORARY_POPUP || context.GetKind() == types.PERSISTENT_POPUP }) } + +func (self *ContextMgr) NextInStack(c types.Context) types.Context { + self.RLock() + defer self.RUnlock() + + for i := range self.ContextStack { + if self.ContextStack[i].GetKey() == c.GetKey() { + if i == 0 { + return nil + } + return self.ContextStack[i-1] + } + } + + panic("context not in stack") +} diff --git a/pkg/gui/controllers/context_lines_controller.go b/pkg/gui/controllers/context_lines_controller.go index 8e35d5103..aec202aeb 100644 --- a/pkg/gui/controllers/context_lines_controller.go +++ b/pkg/gui/controllers/context_lines_controller.go @@ -131,7 +131,9 @@ func (self *ContextLinesController) currentSidePanel() types.Context { currentContext := self.c.Context().CurrentStatic() if currentContext.GetKey() == context.NORMAL_MAIN_CONTEXT_KEY || currentContext.GetKey() == context.NORMAL_SECONDARY_CONTEXT_KEY { - return currentContext.GetParentContext() + if sidePanelContext := self.c.Context().NextInStack(currentContext); sidePanelContext != nil { + return sidePanelContext + } } return currentContext diff --git a/pkg/gui/controllers/main_view_controller.go b/pkg/gui/controllers/main_view_controller.go index 8ccbcc6af..5dfdb3233 100644 --- a/pkg/gui/controllers/main_view_controller.go +++ b/pkg/gui/controllers/main_view_controller.go @@ -76,7 +76,6 @@ func (self *MainViewController) Context() types.Context { func (self *MainViewController) togglePanel() error { if self.otherContext.GetView().Visible { - self.otherContext.SetParentContext(self.context.GetParentContext()) self.c.Context().Push(self.otherContext, types.OnFocusOpts{}) } @@ -89,15 +88,14 @@ func (self *MainViewController) escape() 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) + sidePanelContext := self.c.Context().NextInStack(self.context) + if sidePanelContext != nil && sidePanelContext.GetOnClickFocusedMainView() != nil { + return sidePanelContext.GetOnClickFocusedMainView()(self.context.GetViewName(), opts.Y) } 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, diff --git a/pkg/gui/controllers/rename_similarity_threshold_controller.go b/pkg/gui/controllers/rename_similarity_threshold_controller.go index 88c611723..790c005a9 100644 --- a/pkg/gui/controllers/rename_similarity_threshold_controller.go +++ b/pkg/gui/controllers/rename_similarity_threshold_controller.go @@ -106,7 +106,9 @@ func (self *RenameSimilarityThresholdController) currentSidePanel() types.Contex currentContext := self.c.Context().CurrentStatic() if currentContext.GetKey() == context.NORMAL_MAIN_CONTEXT_KEY || currentContext.GetKey() == context.NORMAL_SECONDARY_CONTEXT_KEY { - return currentContext.GetParentContext() + if sidePanelContext := self.c.Context().NextInStack(currentContext); sidePanelContext != nil { + return sidePanelContext + } } return currentContext diff --git a/pkg/gui/controllers/switch_to_focused_main_view_controller.go b/pkg/gui/controllers/switch_to_focused_main_view_controller.go index 3c08388b9..1973e3d63 100644 --- a/pkg/gui/controllers/switch_to_focused_main_view_controller.go +++ b/pkg/gui/controllers/switch_to_focused_main_view_controller.go @@ -72,7 +72,6 @@ func (self *SwitchToFocusedMainViewController) handleFocusMainView() error { } func (self *SwitchToFocusedMainViewController) focusMainView(mainViewContext types.Context) error { - mainViewContext.SetParentContext(self.context) if context, ok := mainViewContext.(types.ISearchableContext); ok { context.ClearSearchString() } diff --git a/pkg/gui/types/context.go b/pkg/gui/types/context.go index b75d97fb6..1c9759486 100644 --- a/pkg/gui/types/context.go +++ b/pkg/gui/types/context.go @@ -300,6 +300,7 @@ type IContextMgr interface { CurrentStatic() Context CurrentSide() Context CurrentPopup() []Context + NextInStack(context Context) Context IsCurrent(c Context) bool IsCurrentOrParent(c Context) bool ForEach(func(Context)) diff --git a/pkg/gui/view_helpers.go b/pkg/gui/view_helpers.go index 383550d91..081b2a426 100644 --- a/pkg/gui/view_helpers.go +++ b/pkg/gui/view_helpers.go @@ -152,9 +152,9 @@ func (gui *Gui) postRefreshUpdate(c types.Context) { // just don't rerender the view while searching, on the assumption that users will probably // either search or change their data, but not both at the same time. if !currentCtx.GetView().IsSearching() { - parentCtx := currentCtx.GetParentContext() - if parentCtx.GetKey() == c.GetKey() { - parentCtx.HandleRenderToMain() + sidePanelContext := gui.State.ContextMgr.NextInStack(currentCtx) + if sidePanelContext != nil && sidePanelContext.GetKey() == c.GetKey() { + sidePanelContext.HandleRenderToMain() } } }