From f827945cab0ece00acae2fbb92ed77092f65e6dc Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Tue, 25 Mar 2025 15:01:16 +0100 Subject: [PATCH] Select line that is in the middle of the screen --- pkg/gui/controllers/main_view_controller.go | 8 ++++++++ .../switch_to_focused_main_view_controller.go | 19 ++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/pkg/gui/controllers/main_view_controller.go b/pkg/gui/controllers/main_view_controller.go index 48d9a5e1a..4720c9c4d 100644 --- a/pkg/gui/controllers/main_view_controller.go +++ b/pkg/gui/controllers/main_view_controller.go @@ -86,6 +86,14 @@ func (self *MainViewController) Context() types.Context { return self.context } +func (self *MainViewController) GetOnFocus() func(types.OnFocusOpts) { + return func(opts types.OnFocusOpts) { + if opts.ClickedWindowName != "" { + self.context.GetView().FocusPoint(0, opts.ClickedViewLineIdx) + } + } +} + func (self *MainViewController) togglePanel() error { if self.otherContext.GetView().Visible { self.c.Context().Push(self.otherContext, types.OnFocusOpts{}) 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 1973e3d63..a26da49b5 100644 --- a/pkg/gui/controllers/switch_to_focused_main_view_controller.go +++ b/pkg/gui/controllers/switch_to_focused_main_view_controller.go @@ -3,6 +3,7 @@ package controllers import ( "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/gui/types" + "github.com/samber/lo" ) // This controller is for all contexts that can focus their main view. @@ -60,21 +61,29 @@ func (self *SwitchToFocusedMainViewController) Context() types.Context { } func (self *SwitchToFocusedMainViewController) onClickMain(opts gocui.ViewMouseBindingOpts) error { - return self.focusMainView(self.c.Contexts().Normal) + return self.focusMainView(self.c.Contexts().Normal, opts.Y) } func (self *SwitchToFocusedMainViewController) onClickSecondary(opts gocui.ViewMouseBindingOpts) error { - return self.focusMainView(self.c.Contexts().NormalSecondary) + return self.focusMainView(self.c.Contexts().NormalSecondary, opts.Y) } func (self *SwitchToFocusedMainViewController) handleFocusMainView() error { - return self.focusMainView(self.c.Contexts().Normal) + return self.focusMainView(self.c.Contexts().Normal, -1) } -func (self *SwitchToFocusedMainViewController) focusMainView(mainViewContext types.Context) error { +func (self *SwitchToFocusedMainViewController) focusMainView(mainViewContext types.Context, clickedViewLineIdx int) error { if context, ok := mainViewContext.(types.ISearchableContext); ok { context.ClearSearchString() } - self.c.Context().Push(mainViewContext, types.OnFocusOpts{}) + onFocusOpts := types.OnFocusOpts{ClickedWindowName: mainViewContext.GetWindowName()} + if clickedViewLineIdx >= 0 { + onFocusOpts.ClickedViewLineIdx = clickedViewLineIdx + } else { + mainView := mainViewContext.GetView() + lineIdx := mainView.OriginY() + mainView.Height()/2 + onFocusOpts.ClickedViewLineIdx = lo.Clamp(lineIdx, 0, mainView.LinesHeight()-1) + } + self.c.Context().Push(mainViewContext, onFocusOpts) return nil }